Re: Re[4]: How to make routine like this
- Posted by rforno at tutopia.com Dec 16, 2001
- 544 views
John: There is no problem. The difference crops up because you are using puts() instead of ?. puts() prints the ASCII value instead of the numeric value. The result, in case the number of the permutation (I think that in this case it is called variation, but it was long ago when I left my statistics books...) is out of range, is -1. But if you use puts(), -1 prints as ASCII(255), which is what is called a "substitute blank" and, in fact, prints as a blank. 255 comes from the binary representation of -1, which is 11111111111...etc . puts() takes the 8 lower bits to show the ASCII value (please look at the Euphoria manual). If in the function you substitute -1 by '*', you will see an asterisk as the output. To make it more technical, the function returns a sequence with the correct permutation when the "which" parameter is in the right range, and an atom otherwise. Being L the length of the original sequence, and T the length of the intended result, the range is 1 to L * (L - 1) * ... * (L - T + 1) or factorial(L) / factorial(L - T). Out of curiosity: are you living in Portugal for work or just for pleasure? Regards. ----- Original Message ----- From: "John McAdam" <johnmcadam at clix.pt> To: "EUforum" <EUforum at topica.com> Sent: Wednesday, December 12, 2001 6:02 PM Subject: RE: Re[4]: How to make routine like this > > If the loop is too long (more than 100) it prints empty lines. > How do you know there are only 100 combinations? > I am using this only very slightly modified code, > replacing numbers with letters and increasing the > loop size to 125. > > > function var (sequence set, integer size, integer which) > integer c, len, a > > if size <= 0 then > return {} > elsif size = 1 then > return {set[which]} > end if > > len = length(set) > c = 1 > for i = len - size + 1 to len - 1 do > c *= i > end for > > a = floor((which - 1) / c) > which -= a * c > a += 1 > if a > len or a <= 0 then > return - 1 > end if > return set[a] & var(set[1..a - 1] & set[a + 1..len], size - 1, which) > end function > > for i = 1 to 125 do --125 is too many! > puts(1,var({'A','B','C','D','E'}, 5, i)&"\n") > end for > ? var({1, 2, 3, 4, 5}, 3, 0) > > > >