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

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

From: Ken Shmidheiser <kshmidheiser@...>
Date: Sun, 27 May 2012 21:31:31 -0400
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