[futurebasic] Re: [FB] A little programming challenge/puzzle...

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : May 2012 : Group Archive : Group : All Groups

From: Jay Reeve <jayreeve@...>
Date: Sun, 27 May 2012 21:21:16 -0500
Okay, Ken, you can have the extra credit. But here's my code altered to do 1000 UNIQUE numbers (no 2 the same) in 42 lines. :)

Jay
'-----------
_listSize = 1000
include "ConsoleWindow"
clear local fn SixDigitNumber as str255
'~'1
dim as long        j, digit//, theNumber// 0 by default
dim as Boolean     flag(9)// all _false by default
dim as str255      theString : theString[0] = 6             
for j = 1 to 6
do
digit = rnd( 10 ) - 1
until flag( digit ) == _false 
flag( digit ) = _ztrue
theString[j] = _"0" + digit //theNumber = theNumber * 10 + digit
next
end fn = theString

local fn addToOrderedList( @theList(_listSize) as long, theString as str255, n as long ) as boolean
'~'1
dim as long theNum : theNum = val&( theString )
while theList(n-1) > theNum
n--
theList(n+1) = theList(n)
wend
if theList(n) == theNum then n = 0 : exit fn
theList(n) = theNum
end fn = n

randomize
window _FBConsoleWndNum,"6 digit Numbers"
dim as long        i, list(_listSize)
dim as str255      temp
for i = 1 to _listSize 
temp = fn SixDigitNumber
if fn addToOrderedList( list(0), temp, i ) == 0 then i ++
print  temp,//print numbers as generated
next
print : print : print "Sorted list"
for i = 1 to _listSize
print list(i),//print sorted list
next
'-----------




'-----------
_listSize = 1000
include "ConsoleWindow"
clear local fn SixDigitNumber as str255
'~'1
dim as long        j, digit//, theNumber// 0 by default
dim as Boolean     flag(9)// all _false by default
dim as str255      theString : theString[0] = 6             
for j = 1 to 6
do
digit = rnd( 10 ) - 1
until flag( digit ) == _false 
flag( digit ) = _ztrue
theString[j] = _"0" + digit //theNumber = theNumber * 10 + digit
next
end fn = theString

local fn addToOrderedList( @theList(_listSize) as long, theString as str255, n as long ) as boolean
'~'1
dim as long theNum : theNum = val&( theString )
while theList(n-1) > theNum
n--
theList(n+1) = theList(n)
wend
if theList(n) == theNum then n = 0 : exit fn
theList(n) = theNum
end fn = n

randomize
window _FBConsoleWndNum,"6 digit Numbers"
dim as long        i, list(_listSize)
dim as str255      temp
for i = 1 to _listSize 
temp = fn SixDigitNumber
if fn addToOrderedList( list(0), temp, i ) == 0 then i ++
print  temp,//print numbers as generated
next
print : print : print "Sorted list"
for i = 1 to _listSize
print list(i),//print sorted list
next
'-----------



On May 27, 2012, at 8:31 PM, Ken Shmidheiser wrote:

> Great to see Jay coding!
> 
> Do I get extra credit for taking it to a thousand sorted random numbers each with no duplicate digits?
> 
> (49 lines. I'm too used to writing in Objective-C.)
> 
> Ken
> 
> include "ConsoleWindow"
> randomize : _elements = 1000
> 
> local fn DifferentDigits( n as long ) as boolean
> '~'1
> dim as CFStringRef cfStr
> dim as UniChar     charOne, charTwo
> dim as CFindex     i, j, count, hit
> dim as boolean     result : result = _true
> 
> cfStr = fn CFStringCreateWithFormat( _kCFAllocatorDefault, NULL, @"%d", n )
> count = fn CFStringGetLength( cfStr )
> for i = 0 to count
> charOne = fn CFStringGetCharacterAtIndex( cfStr, i )
> hit = 0
> for j = 0 to count
> charTwo = fn CFStringGetCharacterAtIndex( cfStr, j )
> if Chr$( charOne ) == Chr$( charTwo ) then hit++
> next
> if hit > 1 then result = _false : exit fn
> next
> end fn = result
> 
> dim as long i, j, n, v, incr, gotIt, arr( _elements )
> while ( gotIt < _elements )
> i = rnd ( 999999 - 100000 + 1 ) + 100000 
> if ( fn DifferentDigits( i ) ==  _true ) then gotIt++ : arr( gotIt ) = i
> wend 
> 
> n = _elements : incr = 1
> do
> incr = 3 * incr + 1
> until incr > n
> do
> incr = incr / 3
> for i = incr + 1 to n
> v = arr( i ) : j = i
> while arr( j-incr ) > v
> arr( j ) = arr( j-incr ) : j = j-incr
> if j <= incr then exit while
> wend
> arr( j ) = v
> next i
> until incr <= 1
> 
> for i = 1 to _elements
> print i; ". "; arr(i)
> next
> --
> To unsubscribe, send ANY message to: futurebasic-unsubscribe@...
> To access the list archives, go to:  http://freegroups.net/groups/futurebasic/
> 
>