Download and view my resume (PDF)

Sunday, September 27, 2009

Well-Ordered Numbers In Python


Following up on my previous implementation in perl, here is a solution for printing well-ordered numbers in python. This shows some of the useful features of python such as the easy way to loop and the non-necessity of declaring variables in some situations.

def generateWellOrderedNumbers( count, beginning ):

if len( beginning ) == 0:
lastNumOfBeginning = 0
else:
lastNumOfBeginning = int( beginning[-1] )
count = count - 1

for i in range( lastNumOfBeginning + 1, 10 ):
printstring = beginning + str(i)
if count == 0:
print printstring
else:
generateWellOrderedNumbers( count, printstring )

generateWellOrderedNumbers( 8, "" );

No comments:

Post a Comment