1. Euphorize THIS!

-- Here's code that creates a list of coordinates,
-- and then deletes each n-th element of that list
-- I would like some feedback on a more Euphorish
-- way to accomplish the task. Any takers?
-- Thanks, Irv

include get.e
constant n = 4, -- each n-th element
         scrn = 1

sequence dyn_list, new_list
         dyn_list = {} -- create an empty dynamic list
         new_list = {}

function GetCoords()
integer x,y
   x = prompt_number("Coord X:",{1,640})
   y = prompt_number("Coord Y:",{1,480})
   return({x,y})
end function

function delete(sequence list, integer item)
return list[1..item-1] & list[item+1..length(list)]
end function

dyn_list = {GetCoords()} -- get the first set of coords
while 1 do
    dyn_list = append(dyn_list,GetCoords()) -- get the next coords
    ? dyn_list
if match(dyn_list[1], dyn_list[length(dyn_list)]) then -- compare first and
     last list items
        exit -- quit when equal
     end if
 end while

printf(scrn,"You have entered %d items\n",length(dyn_list))
printf(scrn,"Now removing each %dth item\n",n)
puts(scrn,"Before: ") ? dyn_list
-----------------------------------
-- Here's where I need some help.--
-- Isn't there a more Euphorish  --
-- way to do this? Maybe without --
-- duplicating sequences and so  --
-- tying up more memory?   Tnx   --
-----------------------------------
for i = 1 to length(dyn_list) do
    if remainder(i,n) then
       new_list = append(new_list,dyn_list[i])
    end if
end for
puts(scrn,"After: ") ? new_list

new topic     » topic index » view message » categorize

2. Re: Euphorize THIS!

Erm, will this suffice?

-- Code starts here --
integer  i, max

i = 0   max = length(dyn_list)
for count = n to max by n do
dyn_list = dyn_list[1..(count - i) - 1] & dyn_list[(count - i) +
   1..length(dyn_list)]
   i += 1
end for
-- Code ends here --

I tested it, and it works..  I hope this is what you want.

   The AfterBeat

Irv Mullins wrote:

> -- Here's code that creates a list of coordinates,
> -- and then deletes each n-th element of that list
> -- I would like some feedback on a more Euphorish
> -- way to accomplish the task. Any takers?
> -- Thanks, Irv
>
> include get.e
> constant n = 4, -- each n-th element
>          scrn = 1
>
> sequence dyn_list, new_list
>          dyn_list = {} -- create an empty dynamic list
>          new_list = {}
>
> function GetCoords()
> integer x,y
>    x = prompt_number("Coord X:",{1,640})
>    y = prompt_number("Coord Y:",{1,480})
>    return({x,y})
> end function
>
> function delete(sequence list, integer item)
> return list[1..item-1] & list[item+1..length(list)]
> end function
>
> dyn_list = {GetCoords()} -- get the first set of coords
> while 1 do
>     dyn_list = append(dyn_list,GetCoords()) -- get the next coords
>     ? dyn_list
>      if match(dyn_list[1], dyn_list[length(dyn_list)]) then -- compare first
>      and last list items
>         exit -- quit when equal
>      end if
>  end while
>
> printf(scrn,"You have entered %d items\n",length(dyn_list))
> printf(scrn,"Now removing each %dth item\n",n)
> puts(scrn,"Before: ") ? dyn_list
> -----------------------------------
> -- Here's where I need some help.--
> -- Isn't there a more Euphorish  --
> -- way to do this? Maybe without --
> -- duplicating sequences and so  --
> -- tying up more memory?   Tnx   --
> -----------------------------------
> for i = 1 to length(dyn_list) do
>     if remainder(i,n) then
>        new_list = append(new_list,dyn_list[i])
>     end if
> end for
> puts(scrn,"After: ") ? new_list

new topic     » goto parent     » topic index » view message » categorize

3. Re: Euphorize THIS!

On Sat, 23 Jan 1999 11:40:20 -0800, The AfterBeat <afterbeat at GEOCITIES.COM>
wrote:

>Erm, will this suffice?
>
>-- Code starts here --
>integer  i, max
>
>i = 0   max = length(dyn_list)
>for count = n to max by n do
>   dyn_list = dyn_list[1..(count - i) - 1] & dyn_list[(count - i) +
>   1..length(dyn_list)]
>   i += 1
>end for
>-- Code ends here --
>
>I tested it, and it works..  I hope this is what you want.
>
>   The AfterBeat
>
Yup. That frees up memory that the second sequence was taking up.
I wonder if there is a way to do it with Euphoria's sequence
functions  - so as to avoid the for/loop?

Irv

new topic     » goto parent     » topic index » view message » categorize

4. Re: Euphorize THIS!

>>Erm, will this suffice?
>>
>>-- Code starts here --
>>integer  i, max
>>
>>i = 0   max = length(dyn_list)
>>for count = n to max by n do
>>   dyn_list = dyn_list[1..(count - i) - 1] & dyn_list[(count - i) +
1..length(dyn_list)]
>>   i += 1
>>end for
>>-- Code ends here --
>>
>>I tested it, and it works..  I hope this is what you want.

Excuse me, but isn't this code somewhat inefficient? The line within the
for-loop causes the *entire* list to contract at each iteration.Try the
following code. I tested it against the above and i figured that its quicker
(well at least for a 1-d sequence of integers 4000 elements long) by a
factor of about 100....
(I hope it gives the right answer!)

function nth(sequence s, integer n) -- remove each nth item from the list
 integer rem,len,c,d,p
 len=length(s)
 rem=remainder(len,n)
 c=n-1
 d=n-2
 p=n
 for i=n+1 to len - rem by n do
  s[p..p + d]=s[i..i + d]
  p+=c
 end for
 if rem then s[p..p + rem - 1] = s[len - rem + 1..len] end if
 s=s[1..p + rem - 1]
 return s
 end function

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu