Problem 1 --------- My answers assume that math, sys, and random modules have been imported prior to the execution of these expressions. 1. 3 < 10 or 5 True, bool 2. 3 < (10 or 2) True, bool 3. 3 < (2 or 10) False, bool 4. (10 < 100) or (50 < 100/0) True, bool 5. (50 < 100/0) or (10 < 100) Expression is well-formed but causes a "division by zero" error 6. len("hello") == 25/5 or 20/10 True, bool 7. -4*-2**3+-len(str(6)) 31, int 8. 2*(2*(2*len("01")) Expression is not well-formed; it contains a left parenthesis that does not have a matching right parenthesis. 9. (5 < 10) and (10 < 5) or (3 < 18) and not 8 < 18 False, bool 10. 5 < -20 False, bool 11. 5 < *20 Expression is not well-formed. 12. "expression"/10 + 15 Expression is well-formed, but causes a "cannot divide string by integer" error. 13. (len("expression")/10 + 15)/len(str(10+15)) 8, int 14. round(10.78)/3 < len("10.78) Expression is not well-formed; it contains a left quote with no matching right quote. 15. abs(5 - 25) < 15 False, bool 16. len(bin(25)) 7, int 17. bool(math.sqrt(-1)) Expression is well-formed, but it causes an error because sqrt is not defined for -1. 18. bool(math.sqrt((-1)**10)) True, bool 19. math.ceil(1.5)/math.floor(1.5)+200L 202.0, float 20. (sys.maxint + 10)*0 0L, long 21. 2/math.trunc(1.5)+200L 202L, long 22. sys.max+10 Expression is well-formed, but causes an error because there is nothing called "max" defined in the sys module. 23. 10**350 The number 1 followed by 350 zeros; type long. 24. 10.0**350 Expression is well-formed, but causes a floating point overflow, i.e., number of too large to have a floating point representation. 25. random.randint(0, 20) <= 100 True, bool Problem 2. ---------- For these answers I assume that the math and sys module have been imported and that the value of \texttt{sys.maxint} is \texttt{9223372036854775807}. (a) 100L + 200 300L, long (b) math.ceil(10.97) - math.floor(11.17) 0.0, float (c) len(str(10.97)) 5, int (d) bin(5) + bin(3) '0b1010b11', str (e) sys.maxint + 2 9223372036854775809L, long Problem 3. ---------- (a) bin(len(str(10<20))) '0b100', str (b) 4L*len("Problem1")/(len("Exam1")%3)+2.0 18.0, float (c) len('twelve'+"four")/(len('2L')+2*(-1)**3) Expression is well-formed, but causes a "division by zero" error. (d) round(math.pi, 2) < math.trunc(math.pi) False, bool (e) 20 < 30 and 50 < 25 and 4 < 100/0 False, bool (f) str(12 + 4.0/2**2) '13.0', str (g) 3.0 * input("Enter an integer: ") (Assume that user inputs the expression 10/2 + 1, when prompted) 18.0, float (h) not bool(0.1) or not (100 and not False) False, bool (i) 2*float("sys.maxint") Well-formed expression, but causes error because string cannot be converted in float. (j) len(str(random.randint(1, 4)*22)) 2, int Problem 4. ---------- (a) 10 50 10 30 40 80 40 60 70 110 70 90 100 140 100 120 (b) Line 1: 0 1 Line 1: 0 5 Line 1: 0 9 Line 2: 3 8 Line 2: 3 5 Line 2: 3 2 Line 1: 6 7 Line 2: 9 2 Problem 5. ---------- import sys # Variable used to read input numbers current = int(raw_input("Type a positive int (zero if done).")) # tracks the number just prior to the most recently read number previous = sys.maxint # counter to track the number of consecutive, increasing pairs numIncreasingPairs = 0 while current: if current > previous: numIncreasingPairs = numIncreasingPairs + 1 # Update previous (Blank 1) previous = current # Update current (Blank 2) current = int(raw_input("Type a positive int (zero if done).")) print numIncreasingPairs Problem 6. ---------- (a) def startsWithDigit(word): n = 0 # digit to be checked # check each digit while n < 10 : # if word starts with n, return True if startsWith(word, str(n)): return True n=n+1 # Word does not start with a digit, return False return False (b) password = raw_input("Please enter your password: ") if len(password) >= 6 and startsWithDigit(password) : print "Password accepted" else : print "Your password is not valid."