This action might not be possible to undo. Are you sure you want to continue?

**Show your work, and be precise.
**

print 3 ** (2 << 1) / 5 print ((3 | 4) / 2 * 1.0) / 2 print (64 >> ( 7 ^ 3) ) ** 3 print 1|2|4|8|16|32|64 # your answer should work for this series ending in any power of 2, not just 64 print 1+1|2|4|8|16|32|64 # and why when we add 1 do we get a smaller number? x = 53 y = x << 1 y |= x x ^= y print x, y print (1/2**4) or ((1/2)**4) or (float(1/2)**4) or ((float(1)/2)**4) or float(1/2**4)

2. Short Answers. Show your work, even for True/False, and be precise. 1. If you type the expression 2+2 into the python interpreter, it prints 4. But if you type 2+2 as the only text in the file foo.py, then run python foo.py, it seems like nothing happened (in particular, the 4 is not printed). What, precisely, is going on? 2. When you run a script in python, you can include the -i flag, like this: python -i foo.py Try it out and discuss the meaning of the -i flag, and why it might be useful. 3. TRUE or FALSE: Any binary number that ends in a 1 must be odd. Remember to show your work! 4. TRUE or FALSE: Any binary number that contains exactly one 1 must be a power of 2. 5. TRUE or FALSE: If x and y are positive integers where x < y, then (x / y) will always equal zero and (x % y) will always equal x. 6. Use algebra to show that (x % y) is equal to (x - (x/y)*y) as long as y is not zero. Hint: x/y is integer division, so it truncates. Hint: Express x as some multiple of y plus some remainder that is less than y. That is, x = ky + r, where 0 <= r < y. Question: does it matter whether or not x or y is positive? 7. In Java and many other programming languages, (-3 % 5) is -3. In Python, (-3 % 5) is 2. Explain why Python's semantics are more consistent. You may want to draw a graph of (x % 5) in each case. Be sure to use both positive and negative values for x. 8. At the end of lecture, I showed a "trick" using bitwise-xor (^): say you have a list of numbers (int's, to be more precise), where each number occurs an even number of times, except one number occurs an odd number of times. The numbers are not in any specific order. So you bitwise-xor all the numbers together. The result will be that special number that occurred an odd number of times. For example, consider this list:

5)))**2 == x) 6. 7. def g(x): return (type(x) == int) and (x/10 == x%10) 3. fabricYards Fabric must be purchased in whole yards. 5. which indeed is the right answer. 6. Review examples from class notes 1.0000001 d1 = abs(x)**2 d2 = abs(y)**0. write an expression equivalent to (x and (not y)) that does not use the and operator. ". Convert 143 from base 10 to base 2. prove that your answer is correct. 3.. Be precise. 3. onesDigit 2. Using truth tables.y): epsilon = 0. three 6's. 4. 1. def fabricYards(inches): return 42 def testFabricYards(): print "Testing fabricYards. 2.6. def h(x): return (type(x) == int) and (x >= 0) and ((int(round(x**0. Write a function that takes the number of inches of fabric desired. 3. two 5's. Write functions 1. Now.d2) < epsilon 2. Base conversions Show your work! 1. and two 7's. Logic and Truth Tables For booleans x and y. tensDigit 3. consider this expression: 6^3^5^6^3^7^3^7^6^3^5 This evaluates to 6.5 return abs(d1 . eggCartons 4. 6. def f(x. Convert 01001011 from base 2 to base 10. 5. what does each of the following functions do? Provide some explanation for your answer. 7. and returns the smallest number of whole yards of fabric that must be purchased. militaryTimeToStandardTime 7. 5 It contains four 3's. Explain why this "trick" with bitwise-xor works regardless of the order of the list. Mystery Functions In just a few words of plain English. assert(fabricYards(0) == 0) assert(fabricYards(1) == 1) assert(fabricYards(35) == 1) assert(fabricYards(36) == 1) . 3.. 3.

assert(fabricYards(37) == 2) assert(fabricYards(72) == 2) assert(fabricYards(73) == 3) assert(fabricYards(108) == 3) assert(fabricYards(109) == 4) assert(fabricYards(-12345) == 0) print "Passed all tests!" testFabricYards() 2. fabricExcess Write a function that takes the number of inches of fabric desired and returns the number of inches of excess fabric that must be purchased (as purchases must be in whole yards). Here is mathforum's description of the formula: "d is the number or the day of the month. The numbers of these months are 13 and 14 of the PREVIOUS YEAR. a VERY IMPORTANT RULE is the number to use for the months for January and February. Also. Hint: you may want to use fabricYards. and the year. While there are several ways to do this. the day (1-31).[y/100] + [y/400] + 2 Then the remainder when you divide N by 7 is the day-of-week. and y is the year.. where Sunday is 1. dayOfWeek Write a function that takes a date represented by three integers.org): N = d + 2m + [3(m+1)/5] + y + [y/4] . the month (1-12). This means that to find . m is the number of the month. ". which you just wrote! def fabricExcess(inches): return 42 def testFabricExcess(): print "Testing fabricExcess. and Saturday is 7. Monday is 2. assert(fabricExcess(0) == 0) assert(fabricExcess(1) == 35) assert(fabricExcess(35) == 1) assert(fabricExcess(36) == 0) assert(fabricExcess(37) == 35) assert(fabricExcess(72) == 0) assert(fabricExcess(73) == 35) assert(fabricExcess(108) == 0) assert(fabricExcess(109) == 35) assert(fabricExcess(-12345) == 0) print "Passed all tests!" testFabricExcess() 3. you must use this formula (from the most-excellent web site mathforum. and so on. where Saturday is 0 and Friday is 6. and returns an integer representing the day-of-week for that date. The brackets around the divisions mean to drop the remainder and just use the integer part that you get. Note that these values for the days are not quite the same as those returned by this method..

11. # On 2/5/2006. Isaac Asimov was born on a Friday! assert(dayOfWeek(1. So. 1998). you must use the date 13/1/97. 1961) == 4) # On 7/4/1776.. Yuri Gagarin became the first man in space. 1776) == 5) # on 1/2/1920. 1920) == 6) # on 10/11/1975. 1. on a Wednesday! assert(dayOfWeek(4. 1215) == 2) # On 3/11/1952. the date of New Year's Day for 1998 would be obtained in the natural way: dayOfWeek(1.." Note: you must make the adjustment to the month and year when appropriate. 15. 1975) == 7) print "Passed all tests!" . the author Douglas Adams was born on a Tuesday! assert(dayOfWeek(3. 4. the Magna Carta was signed on a Monday! assert(dayOfWeek(6. def testDayOfWeek(): print "Testing dayOfWeek. the Declaration of Independence was signed on a Thursday! assert(dayOfWeek(7. 1/1/98. for example. 2006) == 1) # On 6/15/1215. 2. 12. 1952) == 3) # on 4/12/1961. Saturday Night Live debuted on a Saturday (of course)! assert(dayOfWeek(10. You may ignore the cases where the month.the day of the week of New Year's Day [of 1998]. or year are out of bounds. ". day. the Steelers won Super Bowl XL on a Sunday! assert(dayOfWeek(2. 5. 11.

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue reading from where you left off, or restart the preview.

scribd