My transcript for Homework 1, Problem 5:
>>> a = 3 >>> b = 17 >>> c = -12 >>> b ** 2 - 4 * a * c 433 >>> sqrt(b ** 2 - 4 * a * c) Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> sqrt(b ** 2 - 4 * a * c) NameError: name 'sqrt' is not defined >>> import math >>> math.sqrt(b ** 2 - 4 * a * c) 20.808652046684813 >>> -b + math.sqrt(b ** 2 - 4 * a * c) 3.808652046684813 >>> (-b + math.sqrt(b ** 2 - 4 * a * c)) / (2 * a) 0.6347753411141355 >>> x = (-b - math.sqrt(b ** 2 - 4 * a * c)) / (2 * a) >>> a * x**2 + b * x + c 0.0 >>> x = (-b + math.sqrt(b ** 2 - 4 * a * c)) / (2 * a) >>> a * x**2 + b * x + c 3.552713678800501e-15
Is that a zero?
How do I know my answer is correct? Test it!
First, we build a 'q':
>>> ord('q') 113 >>> ord('e') 101 >>> ord('q') - ord('e') 12 >>> ord('e') + 12 113 >>> chr( ord('e') + 12 ) 'q'
Then we dive into binary and hexadecimal numbers... I expected this to be a refresher, but I think I expected too much. Sorry. (But cut me some slack; that's grade-school arithmetic.).
Converting to decimal was straightforward enough:
>>> 2**6 + 2**3 + 2**0 73 >>> e = 14 >>> f = 15 >>> f * (16 ** 1) + e* (16 ** 0) 254
Converting from was tougher. I should have given you an algorithm! In lab, most of you worked from big to small:
>>> 473 // 16 29 >>> 473 // 16 ** 2 1 >>> 473 % 16 ** 2 217 >>> 217 // 16 ** 1 13 >>> 217 % 16 ** 1 9
So, it's 1D9. We can also work from the smallest place value up...
The thing is, this is a low-level skill in computing. Most of us don't do this sort of thing very often. It does affect how we understand other ideas, though. And every once in a while it shows up, when you least expect it...
<body onLoad="init();" bgcolor="#fefefe" text="#000000" link="#0f0f0f" alink="#9b5fff" vlink="#ff0000" marginheight="0" marginwidth="0" leftmargin="0" topmargin="0">
Yes, that is hex.
The final problem in the lab asked you to create a Euro converter. It seemed as if this was more comfortable for most of you.
I always start in the shell:
>>> dollars = 100 >>> dollars_to_convert = dollars - 10 >>> euros = dollars_to_convert / 1.31 >>> euros 68.70229007633587 >>> print("Euros received:", euros) Euros received: 68.70229007633587
I have the makings of a correct program. So I copy the expressions that do the work into a new program file and, if I copied any remnants of the interaction, eliminate them:
dollars = 100 dollars_to_convert = dollars - 10 euros = dollars_to_convert / 1.31 print("Euros received:", euros)
The spec asks for us to take the number of dollars in as input from the user, so I change the first line (perhaps after first trying it out in the shell:
dollars = input("Dollars to trade:") dollars_to_convert = dollars - 10 euros = dollars_to_convert / 1.31 print("Euros received:", euros)
We run it... I'm missing a space in my prompt; that's easy to fix. But there is a bigger issue. I can't subtract 10 from '100'! Strings and numbers are different. But I know how to convert a string to an integer, using int(). With that fix, all is good.
Dollars to trade: 100 Euros received: 68.70229007633587
Issues: IPO pattern. Magic constants. Truncating the result. Reading in the exchange rate and dollar amount. Note: The last changes the spec! (How can that be bad?)
From here the notes are briefer and a bit rough. I'll update them in time.
Your textbook, Section 1.9. Try it (live) on PP 1, page 79. Some ambiguity, so let's start by assuming that we want to report the depth in inches. We may need to change to feet.
Understand the problem. Read. Draw picture.
Outline a set of steps to solve the problem. Break it down into things you know. Good start: IPO. Here, part of the "I" is given, and we have to find the rest.
Look up area: 8080464.3 km**2.
The "P" is usually complex enough that we need to break it down into smaller steps. Break it down. That is basically half of design: decompose the problem, and name the parts.
Depth = volume / area. But dimensions are in kilometers, and we need to report inches. So we need a second step to convert kilometers to inches. 0.621371 miles to a kilometer. 5280 feet to a mile. 12 inches to a foot.
Now we can convert to Python. Our early practice these two weeks has been toward helping us know how to convert basic arithmetic expressions into Python. We will learn more as we go along.
A few students have told me, I know how to do this in my head, (or on paper), but I don't know how to it in Python. ... That's programming!!
Practice. Use the shell for as much as you can. No pencil and paper? Or perhaps only to draw pictures. Practice.
[ Do it in Python. This is what we produced. ]