Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CSCI 141 Computational Problem Solving Homework 2 1. The standard deviation Ο of an entire population of values tells us how closely grouped the values are. For a normal distribution, 68.2% of the values in the population are within 1 standard deviation of the population's mean value, and 95.4% of the values in the population are within 2 standard deviations of the population's mean value. We compute the width of 1 standard deviation as follows: π 1 π = β β(π₯π β π)2 π π=1 where π is the arithmetic mean (average) of all of the values. So we sum the squared differences of each value from the mean, take the average of that sum, and then take the square root of that average. A avg = (a + b + c)/3 mean = (a + b + c)/3 mean = [a + b + c] / 3 mean = avg(a, b, c) avg = math.avg(a, b, c) B def stdev(a, b, c): def stdev(): def pstdev(a b c): float stdev(a, b, c): def try(a, b, c): C Contained in the Python code below is a fully correct function that computes and returns the standard deviation of three integers provided as parameters. Each group contains at least one line from the correct function. Select exactly one line from each group and reorder those selections to restore the correct function. Note that indentation has been removed; restore the indentation to appropriate levels in your solution. Note the groups span more than one page. return variance ** 0.5 return math.sqrt(variance) print(variance ** 0.5) print(stdev) return stdev D total = (a-mean)**2 + (b-mean)**2 + (c-mean)**2 stdev = (a-mean)**2 + (b-mean)**2 + (c-mean)**2 variance = a-mean**2 + b-mean**2 + c-mean**2 stdev = ((a-mean)**2 + (b-mean)**2 + (c-mean)**2)**0.5 total = (a-mean + b-mean + c-mean)**2 E variance = total / 3 stdev = (total / 3) ** 0.5 variance = stdev ** 0.5 stdev = variance ** 0.5 variance = mean**2 def stdev(a, b, c): #B mean = (a + b + c)/3 #A total = (a-mean)**2 + (b - mean)**2 + (c - mean)**2 #D variance = total/3 #E return variance ** 0.5 #C 2. The following segment of Python code claims to print the sum of all multiples of three less than a certain user-input number. The algorithm is correct, but the implementation contains several syntax errors. Identify each error and show how to fix it. Also explain in English why it was an error. n = input('Enter a number: ') total = 0 while (n > 0): if n % 3 is 0: # is should be == total = total + i # i should be n n -= 1 return total In the first line, the string for the input prompt is missing quotation marks. Note the added quotes in green above. The second line is correct. The third line is correct In the fourth line, comparison between integers is done with the == operator. The is keyword in Python asks if the two operands are the same object, not whether the objects have the same value. (Consider the difference closely when using classes). The fifth line contains two errors. One is that we forgot to update total after adding i to it. Note the green assignment above. Also, there is no variable i in this code, so that should be n. The sixth line is correct. The seventh line is correct. 3. Provide two separate Python functions, each taking two parameters a and b. One function should compute a // b, and the other should compute a % b. Neither function can employ any direct form of multiplication, division, or exponentiation. Note that division is nothing more than repeated subtraction. To compute a // b, we simply count how many times we can subtract b from a and return that count. def div(a, b): quotient = 0 while b <= a: a = a - b quotient = quotient + 1 return quotient Likewise, a % b is simply whatever is left in a after we subtract b from it as many times as possible. def mod(a, b): while b <= a: a = a - b return a 4. You are asked to complete the following class definition: class Rocket (object): There are three class methods: ο· ο· ο· A class constructor, which takes the initial (x,y) position of the rocket and stores it. A method named move that takes two arguments dx and dy and increments the rocketβs (x,y) position by dx and dy, respectively. A method named distance_from that takes as its argument another rocket object and computes the Euclidean distance between the rockets and returns the result. Contained in the Python code below is a fully correct definition of the Rocket class. Each group contains at least one line from the correct code. Select exactly one line from each group and reorder those selections to restore the correct class definition. Note that indentation has been removed; restore the indentation to appropriate levels in your solution. Note the groups span more than one page. A self.__x = x x = self.x self.x = x __x = x B def def def def C return return return return D y = self.y + dy self.y = dy self.__y = self.__y + dy self.y = self.y + dy E self.__x = self.__x + dx x = self.x + dx self.x = self.x + dx self.x = dx F other.distance = ((self.x - other.x)**2 + (self.y - other.y)**2)**(1/2) distance = ((self.x - other.x)**2 + (self.y - other.y)**2)**(1/2) self.__distance = ((self.x - other.x) + (self.y - other.y))**(1/2) distance = ((self.x - dx)**2 + (self.y - dy)**2)**(1/2) G def def def def H self.__y = y self.y = y __y = y y = self.y self.move (dx, dy): move (self, __dx, __dy): move (dx, dy): move (self, dx, dy): self.x distance self.distance self.y __init__ (x, y): init (x, y): __init__ (self, x, y): init (self, x, y): I def def def def distance_from (other): distance_from (self, __other): self.distance_from (other): distance_from (self, other): class Rocket (object): #given def __init__(self, x, y): #G self.x = x #A self.y = y #H def move (self, dx, dy): #B self.x = self.x + dx #E self.y = self.y + dy #D def distance_from (self, other): #I distance = ((self.x - other.x)**2 + (self.y - other.y)**2)**(1/2) #F return distance #C 5. Without using a computer, determine what will be printed by the following program. class MeatAndThree (object): def __init__(self, meat, veg1, veg2, veg3): self.__meat = meat self.__veg1 = veg1 self.__veg2 = veg2 self.__veg3 = veg3 def __str__(self): s = 'Today''s lunch special is ' + \ self.__meat + ' with ' +\ self.__veg1 + ', ' +\ self.__veg2 + ', and ' +\ self.__veg3 + '.' return s def change_side(self, old_side, new_side): if (self.__veg1 == old_side): self.__veg1 == new_side if (self.__veg2 == old_side): self.__veg2 == new_side if (self.__veg3 == old_side): self.__veg3 == new_side menu = MeatAndThree('fried chicken', 'lima beans', 'mashed potatoes', 'collards') print(menu) menu.change_side('mashed potatoes', 'rice') print(menu) Todays lunch special is fried chicken with lima beans, mashed potatoes, and collards. Todays lunch special is fried chicken with lima beans, mashed potatoes, and collards. Note that 'Todays' does not contain an apostrophe. It looks like the __str__ function tries to include an apostrophe, but the double apostrophe only terminates and then resumes the string. Also, notice that the sides do not change. This is because in the change_side method, we do not actually perform any assignments. When we try to change from mashed potatoes to rice, the variable veg2 and old_side match. In the conditional, though, we do not update veg2 to be new_side. Instead, we ask if veg2 and new_side are equal (they are not). Python says False, but we ignore the return value of the comparison. No variables are modified. Also, this sounds delicious and I'm hungry now.