Sie sind auf Seite 1von 2

Assignment 2

January 22, 2013


Due Date: Wednesday January 30, 11:55 pm 1. Sakshi likes to play tennis on weekends based on the weather condition. She makes her decision depending on the following three factors. (a) Outlook (which can be sunny, rainy or overcast) (b) Wind (which can be strong or weak) (c) Humidity (which can be high or normal) She decides to play tennis if a) outlook is overcast OR b) outlook is rainy and wind is weak OR XX  X c) outlook is sunny and  humidity is normal. wind We would like to automate her decision about playing tennis by creating a program which would take as input current weather condition i.e. outlook, wind and humidity, and output yes or no depending on whether Sakshi would like to play tennis in these weather conditions. (a) Write a Python program with at if-else structure (i.e. no nesting of if-else constructs) to achieve the above functionality. (2 points) (b) Write a Python program with nested if-else structure to achieve the above functionality. (2 points) Note: You can use == operator to compare two strings. For instance, if outlook=sunny then outlook==sunny will return True and outlook==overcast will return False. Similarly, to input a string on the command line, you should put the desired string in single quotes. 2. Recursive Evaluation of Polynomials: A polynomial P (x) = a0 + a1 x + a2 x2 + a3 x3 + + an xn can be stored as a sequence of its coecients i.e. a0 , a1 , a2 , a3 , an . Any operation on polynomials can be dened in terms of operation over their respective coecients. Consider the following alternate representation of a polynomial: P (x) = a0 + x(a1 + x(a2 + x(a3 + + x(an1 + xan )))) This alternate representation can be used to write a recursive program to evaluate a polynomial at any given value. Consider a polynomial where its coecients are given by the digits of a decimal number, the highest place value denoting the coecient of the highest order term and lower place values denoting the coecients of the lower order terms, respectively. E.g., 1235 represents x3 + 2x2 + 3x + 5. (a) Write a recursive Python program which takes as input a decimal number n(n 0) representing the coecients of a polynomial as described above, a value for x and returns the evaluation of the polynomial at the given value. You can assume that all the coecients are non-negative. Below is a sample input/output for your program. (4 points) 1

Input the number representing the coefficients: 1235 Input the value of x: 2 Value of the polynomial is 27 (b) (Extra Credit) Modify your program to print the actual polynomial represented by n in addition to its evaluation at the given value. (2 points) Note: For this part, you will need to gure out how to print all the terms of the polynomial on the same line. Note: You should follow the instructions on the website to submit your code.

Das könnte Ihnen auch gefallen