We covered basic expressions (arithmetical, Boolean), variables, different types of values (integers, floats, strings, the math library (for floor and ceil), objects, input and output (input, print). You'll find this material in sections 2.1, 2.2, 2.4, 2.5, and 3.1. We then talked about strings and lists (2.2, 2.3, 3.4). We have also discussed decision patterns, functions and loops (Chapter 3). We will review programming patterns somewhat more systematically, including some more advaned loop patterns (Section 5.1-5.3) as well as file processing (Chapter 4).
We also mentioned
Submission: The homework is due by midnight (I will not accept late homeworks). You can submit your homework through d2l into the drop-box for this homework.
Please prepare your homework as a single file containing all answers (e.g. doc, docx, or pdf, not a zip file). When submitting programs, include the programs, together with screenshots of test-runs of your program (make sure screenshots are sized so the text is legible; resize and/or crop the images). For an example, see hwexample.docx. How to take screenshots? Check out screenshots for MAC, Windows, Linux. Unless I ask for it specifically, you do not have to submit separate files with program code or executables. If multiple files are necessary, please do not zip them up, just submit them separately.
1. (Reading Assignment) Read Chapters 2 and 3 of the textbook. If you want to read ahead, continue with Sections 4.1-4.3 of the textbook.
2. (Academic Integrity) Please read the academic integrity statement. Sign the document (electronically is fine), and submit it to the AI submission folder.
3. (Expressions, 10pt) Write (and run) expressions for the following tasks (the expressions here are more important than the results, so be sure to show your expressions, not just your results).
[5pt] The USB 2 standard, still used by many devices like web-cams, at its fastest manages a throughput of 480Mb/s (that is 480 *10^{6} bits per second). Is this sufficient to transfer uncompressed video for a resolution of 1920 x 1080 at 30 frames per second? Each pixel requires 24b (eight bits each for red, green and blue values). By what factor does image size has to be reduced to make streaming-video possible? Hint: You can look up the prefixes at metric prefixes .
[5pt] You have just finished writing your autobiography, and it is wildly popular. You are sitting at home, boxing up the copies of your book. The boxes all have the same size, and each box can fit 24 copies of the book. You had n = 1,000,000 copies of your book printed. How many boxes do you need to pack them all up? Hint: not all boxes will be full. Write an expression that solves this problem, and works for arbitrary values of n (try with n = 23, n = 24, n = 25, for starters. [+2EC] You will get extra credit if you can solve this problem without using floating point arithmetic (/) and rounding (int, ceil, floor, round).
4. (Chess, 10pt) Find a chessboard (physical or online). The lower left-hand square (1,1) of the board is black, and the colors alternate between black and white as you move horizontally or vertically. Write a program black(i,j) that given a row number i and a column number j of the board determines whether the square in position (i,j) is black (return True), or not (return False). We start counting rows and columns with 1. So the upper left-hand corner is (8,1), the lower-right hand-corner is (1,8), and the upper-right-hand corner is (8,8).
Hint: You do not need to check that i and j are at most 8. There are various ways of solving this, but anything that's more than four or five lines is probably too complicated.
5. (Benford's Law, 10pt) Write a function benford(n) that creates n random integers in the range between 0 and one billion (10**9) and returns what percentage of them start with the digit 1. (Do the test-runs below agree with what we would expect?)
Hint: How do you test whether a number starts with a specific digit? You may find the str() method useful.
6. (Guessing Game, 10pt) Start with the code we wrote for the guessing game (the version _without_ the eval). Modify the code, so it supports the following functionality:
a) [5pt] In subtraction questions, the answer is never negative (it may be zero). Hint: If you're inclined to use a loop, don't.
b) [5pt] If your answer is wrong, the program not only tells you that it is wrong, it tells you whether your answer is too small or too large. See screenshot below.
Note: you can hand this in together, but clearly mark (or comment) where in the code you made changes for a) and b). You can also just show the modified parts if the rest remains the same.