These are the notes from Mon Jun 1, 2009. Notes for Monday June 1, 2009. We want to work out two problems: a) Write a program that reads a string from the user and then writes back the same string with all the characters inside in random order. The plan then is: read word from user result = "" while word != "": choose random letter from word move it from word to another variable (result) print result Example: word result ------------------------ "python" "" choose word[1] ------------------------ "pthon" "y" choose word[1] ------------------------ "phon" "yt" choose word[3] ------------------------ "pho" "ytn" ... and so on. So the big issue is moving the letter. >>> word = "python" >>> index = 1 >>> word[index] 'y' >>> word 'python' >>> letter = word[index] >>> word 'python' >>> letter 'y' >>> index 1 >>> word[0:index] + word[index+1:] 'pthon' >>> word 'python' >>> word = word[0:index] + word[index+1:] >>> word 'pthon' >>> index 1 >>> letter 'y' >>> result = "" >>> result = result + letter >>> result 'y' >>> And now we can put everything together: import random word = raw_input("Type: ") result = "" while word != "": index = random.randrange(len(word)) letter = word[index] result = result + letter word = word[0:index] + word[index+1:] print result >>> ================================ RESTART =================================== >>> Type: watermelon tewnelarmo >>> ================================ RESTART =================================== >>> Type: lemur rulem >>> ================================ RESTART =================================== >>> Type: lemur uemrl >>> ================================ RESTART =================================== >>> Type: lemur elumr >>> b) The second problem is listed on What's New? and it goes like this: Here's a program we need to discuss in class today. Write a program that reads a string and then reports a string profile as follows: a) all letters in the word are reported once b) for each letter the number of occurrences is indicated c) the letters are listed by number of occurrences, in descending order For example if we enter "banana" the report is: a appears 3 times n appears 2 times b appears 1 times But Leo asked something about the random number generator so we decided to work on that problem. We'll postpone the string profile problem for tomorrow. We can get the same from Leo's question. So let's do Leo's problem: Let's see how good the random number generator is. Let's tabulate the frequencies of occurrence for the numbers in some range when selected with random.randrange(...) import random size = int(raw_input("Number: ")) a = [0,0,0,0,0,0,0,0,0,0] for count in range(size): number = random.randrange(10) print number a[number] = a[number] + 1 print a >>> ================================ RESTART ================================ >>> Number: 10 7 6 7 5 3 4 4 3 9 4 [0, 0, 0, 2, 3, 1, 1, 2, 0, 1] >>> Now this takes us to the following. This is how we determine volunteers: import random size = int(raw_input("How many: ")) words = ( "yi", "korey", "chaitanya", "asaf", "linwood", "daniel", "velocity", "rick", "dgerman", "mike", "sung ho", "robin", "joe", "echo", "leo" ) for count in range(size): number = random.randrange(len(words)) a = " " + str(count + 1) print a[-3:] + ".", words[number] print words[number], "should come and write code now..." It runs like this: >>> How many: 10 1. robin 2. linwood 3. joe 4. yi 5. echo 6. joe 7. velocity 8. chaitanya 9. leo 10. chaitanya chaitanya should come and write code now... Instead can we report as follows: 1. robin: selected 1 time 2. joe: selected 2 times 3. yi: selected 1 time 4. asaf: selected 0 times 5. chaitanya: selected 2 times 6. leo: selected 1 time 7. velocity: selected 1 time 8. echo: selected 1 time 9. linwood: selected 1 time 10. dgerman: selected 0 times Then maybe we can determine the volunteer as the person with the greatest number of selections. Think about it for tomorrow.