Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
CSCI-UA.0002-010: Introduction to Computer Programming Fall 2013 Homework #7 - SOLUTIONS The following programs are due at the beginning of class on Tuesday, November 5. You can submit your programs online via NYU Classes. Please submit a separate .py file for each program, and put your name and the problem/assignment number in a comment at the top of the program. 1. Modify your guessing game program from homework #5 (problem #3) so that it randomly picks a whole number between 1 and 100 and then asks the user to guess this number (with hints like ”Higher!” and ”Lower!”) # SOLUTIONS : Homework 7 − Problem 1 import random s e c r e t n u m b e r = random . r a n d i n t ( 1 , 1 0 0 ) count = 1 g u e s s = i n t ( i n p u t ( ” I ’m t h i n k i n g o f a number between 1 and 1 0 0 . Can you g u e s s what i t i s ? ” ) ) w h i l e g u e s s != s e c r e t n u m b e r : count += 1 i f guess < secret number : p r i n t ( ” Higher ! ” ) g u e s s = i n t ( i n p u t ( ”What number am I t h i n k i n g o f ? ” ) ) e l i f guess > secret number : p r i n t ( ” Lower ! ” ) g u e s s = i n t ( i n p u t ( ”What number am I t h i n k i n g o f ? ” ) ) i f count > 1 : p r i n t ( ” You g u e s s e d i t ! And i t o n l y took you ” , count , ” t r i e s .”) else : p r i n t ( ” You g u e s s e d i t ! And i t o n l y took you ” , count , ” t r y . ” ) 2. The average value in the following data set is 5. 3, 6, 7, 5, 4 When statisticians want to measure how much variation there is from the average, they use a statistic called the standard deviation. To calculate the standard deviation, first compute the difference of each data point from the average and square each result. (3 − 5)2 (6 − 5)2 (7 − 5)2 (5 − 5)2 (4 − 5)2 = (−2)2 = 4 = (−1)2 = 1 = 22 = 4 = 02 = 0 = (−1)2 = 1 Next, add up each of these numbers, divide by the total number of data points minus one, and take the square root of the result: r r 4+1+4+0+1 10 = ≈ 1.58 5−1 4 The standard deviation is this number, 1.58. This is a tedious calculation even when you only have five data points! Write a function calculate standard deviation that takes a list of numbers as an argument and returns the standard deviation. Use your function to write a program that calculates the standard deviation for the data set 3, 6, 7, 5, 4, 2, 2, 1, 10, 9, 4, 6, 4, 5, 2, 7, 8, 3, 9, 3 # SOLUTIONS : Homework 7 − Problem 2 from math import s q r t def calculate mean ( list of numbers ) : total = 0 count = 0 f o r num i n l i s t o f n u m b e r s : t o t a l += num count += 1 r e t u r n t o t a l / count def calculate standard deviation ( list of numbers ) : total = 0 count = 0 mean = c a l c u l a t e m e a n ( l i s t o f n u m b e r s ) f o r num i n l i s t o f n u m b e r s : t o t a l += (num − mean ) ∗∗2 count += 1 v a r i a n c e = t o t a l / ( count − 1 ) return sqrt ( variance ) sample = [ 3 , 6 , 7 , 5 , 4 , 2 , 2 , 1 , 1 0 , 9 , 4 , 6 , 4 , 5 , 2 , 7 , 8 , 3 , 9 , 3] s t d e v = c a l c u l a t e s t a n d a r d d e v i a t i o n ( sample ) s t d e v = format ( stdev , ’ , . 4 f ’ ) p r i n t ( ” The s t a n d a r d d e v i a t i o n i s ” , stdev , ” . ” , sep = ’ ’) # ALTERNATE SOLUTION : Homework 7 − Problem 2 # This s o l u t i o n i s more p o l i s h e d ( but more c o m p l i c a t e d . ) # I t g e t s t h e l i s t o f numbers from t h e u s e r , and a v o i d s # t h e s p e c i a l c a s e where you would a c c i d e n t a l l y d i v i d e by 0 . from math import s q r t def get sample () : list of numbers = [ ] num = ’ ’ p r i n t ( ” This program c a l c u l a t e s t h e s t a n d a r d d e v i a t i o n o f a sample . ” ) p r i n t ( ” P l e a s e e n t e r t h e data i n your sample , one number a t a time , and e n t e r ’ done ’ when you ’ r e done . ” ) w h i l e num != ’ done ’ : num = i n p u t ( ” The next number i s ? ” ) i f num != ’ done ’ : num = i n t (num) l i s t o f n u m b e r s += [ num ] return list of numbers def calculate mean ( list of numbers ) : total = 0 count = 0 f o r num i n l i s t o f n u m b e r s : t o t a l += num count += 1 i f count != 0 : #This i f s t a t e m e n t i s t o a v o i d d i v i d i n g by 0 . r e t u r n t o t a l / count def calculate standard deviation ( list of numbers ) : total = 0 count = 0 mean = c a l c u l a t e m e a n ( l i s t o f n u m b e r s ) f o r num i n l i s t o f n u m b e r s : t o t a l += (num − mean ) ∗∗2 count += 1 i f count != 1 : #This i f s t a t e m e n t i s t o a v o i d d i v i d i n g by 0 . v a r i a n c e = t o t a l / ( count − 1 ) return sqrt ( variance ) else : return 0 sample = g e t s a m p l e ( ) s t d e v = c a l c u l a t e s t a n d a r d d e v i a t i o n ( sample ) s t d e v = format ( stdev , ’ , . 4 f ’ ) p r i n t ( ” The s t a n d a r d d e v i a t i o n i s ” , stdev , ” . ” , sep = ’ ’) 3. Assume that a file containing a sequence of names (as strings) is titled names.txt and exists on the computer’s hard disk. Write a program that displays each name in the file and the total number of names stored in the file. # SOLUTIONS : Homework 7 − Problem 3 i n f i l e = open ( ’ names . txt ’ , ’ r ’ ) count = 0 line = i n f i l e . readline () w h i l e l i n e != ’ ’ : p r i n t ( l i n e , end = ’ ’) count += 1 line = i n f i l e . readline () i n f i l e . close () p r i n t ( ” There were a t o t a l o f ” , count , ”name ( s ) . ” ) 4. Write a program that writes a sequence of random numbers to a file. Each number should be between 1 and 100, and the program should let the user specify how many random numbers the file will hold. # SOLUTIONS : Homework 7 − Problem 4 from random import r a n d i n t o u t f i l e = open ( ’ random numbers . txt ’ , ’w’ ) p r i n t ( ” This program w r i t e s numbers between 1 and 100 t o a f i l e random numbers . t x t ” ) num = i n t ( i n p u t ( ”How many random numbers do you want w r i t t e n t o the f i l e ? ”) ) f o r x i n r an ge (num) : random number = r a n d i n t ( 1 , 1 0 0 ) random number = s t r ( random number ) + ’ \ n ’ o u t f i l e . w r i t e ( random number ) outfile . close ()