Re: Project Programmer Wanter

new topic     » goto parent     » topic index » view thread      » older message » newer message

The following function for building variations with repetition is
a little clearer and faster than the one which I posted yesterday:
global function next_variation_r (sequence s, integer mini, integer maxi)
   -- Return the next variation with repetition on s
   -- (in lexicographic order);
   -- ** In order to get all possible variations, s must initially only
   --    contain 'mini' values. **
   integer i

   i = length(s)

   -- carry if necessary
   while s[i] >= maxi do
      s[i] = mini
      i -= 1
      if i = 0 then
         return -1          -- end
      end if
   end while

   s[i] += 1
   return s
end function


-- Demo
object x
integer mini, maxi, k

-- a) Ret's example:
mini = 'A'
maxi = 'D'
k = 3
x = repeat(mini, k)
while sequence(x) do
   puts(1, x & "\n")
   x = next_variation_r(x, mini, maxi)
end while

puts(1, "-------\n")

-- b) Get all possible combinations of two 6-sided dice:
mini = 1
maxi = 6
k = 2
x = repeat(mini, k)
while sequence(x) do
   ? x
   x = next_variation_r(x, mini, maxi)
end while


Regards,
   Juergen

-- 
http://luethje.eu/prog/

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu