Saint Louis University |
Computer Science 140
|
Dept. of Math & Computer Science |
Topic: High-Level Programming in Python
Related Reading: Ch. 5.1-5.4 and Ch. 6 of Brookshear, as well as
lecture notes on Python
Due:
Bud1
For this assignment, we will rely heavily on the Python environment demonstrated in class. You will need to have an Internet connection to run this software.
In the lecture notes, we demonstrate a program to print all odd numbers from 1 to 20. That approach was based on a loop which counts from 1 to 20, and then a conditional statement nested in the loop body to filter odd vs. even numbers.
Another approach to accomplish the same result is by using a loop which counts from 1 to 10, with the body of the loop printing out the kth odd number, which has the form 2*k-1.
Use this new approach to write Python code for printing out the first ten odd numbers. (solution available online)
Review all of the code fragments in our Python lecture notes
For this problem, you can use python to do the work for you if you'd like. Though on a test, you would certainly be expected to do such a problem on your own, so please make sure that you fully understand the answer.
What value or sequence of values would be printed when
running the following segment of code?
val = 3
while val<6:
print val
val = val + 1
What value or sequence of values would be printed when
running the following segment of code?
val = 11
while val>=7:
val = val - 1
print val
Carefully consider the following program:
x = int(raw_input('Enter a value for x: ')) y = int(raw_input('Enter a value for y: ')) if x > 5: if y <= 3 and x > 8: print 'answer is A' else: print 'answer is B' else: if y > 6 or x < 2: print 'answer is C' else: print 'answer is D'
What value or sequence of values would be printed when the user enters 4 and then 4?
What value or sequence of values would be printed when the user enters 9 and then 4?
What value or sequence of values would be printed when the user enters 1 and then 9?
What value or sequence of values would be printed when the user enters 6 and then 2?
Consider the following conditional statement:
if X == 0: Z = Y + W else: Z = Y + Z
Translate this to the equivalent machine language code from Appendix C, assuming that the variables W, X, Y, and Z correspond to integers stored in memory cells 20, 21, 22, and 23, respectively.
Earlier in the course, we used an algorithm (by hand) for taking
a value expressed in the decimal system, and converting it to a
value expressed in some other base.
For review, we will rephrase that high-level algorithm in
English as follows.
As an example, we earlier considered the conversion of decimal
value 59 into base 3. We could trace this algorithm on
that example as follows:
Start with the original value and an initially empty result.
While the value is greater than zero, do the following:
Compute both the quotient and remainder of that value divided by the desired base.
The digit representing that remainder should be added to the left-side of the result.
The value should then be reassigned to the computed quotient.
The final result is the representation of the original number in the desired base.
value | quotient | remainder | partial result |
---|---|---|---|
59 | 19 | 2 | 2 |
19 | 6 | 1 | 12 |
6 | 2 | 0 | 012 |
2 | 0 | 2 | 2012 |
0 | 2012 |
Your goal is to implement this algorithm in Python. The
end result should be a program which prompts the user to enter a
decimal value and a desired base, and which then produces output
which demonstrates the converted result (as a string of digits).
A sample session might look as follows:
For convenience, you may assume that the other base will be less
than 10 (so that you don't have to worry about using digits like
"A", "B", and so on -- see extra credit for that).
Enter Decimal Value: 59
Enter Desired Base: 3
The decimal value 59 is equivlaent to the value 2012 in base 3
Advice:
The quotient of an integer division, a dividied by b, can be computed using the syntax a/b
The remainder of an integer division, a dividied by b, can be computed using the syntax a%b
You will need to be careful in keeping straight when data is represented as a string of characters and when it is represented as a true integer, and converting between these data types. You can convert from a string to the corresponding integer using a syntax such as int("59") which gets you the integer value 59. In contrast you can convert from an integer to the string representing that integer using a syntax such as str(59) which gets you the string "59".
This will be significant for this program. For example, though you may use raw_input to ask the user to enter a value, what that returns is technically the string of characters entered by the user. If you want to interpret it as an integer, you must explicitly convert it. For example, the result of a call to raw_input is technically a string of characters. If you want to convert it to the associated decimal value, you must use the function int( raw_input() ) as demonstrated in the lecture notes.
In similar fashion, you do not want to confuse the base 3 result "2012" with the decimal value 2012 and so you should make sure to use the string data type for represeting the (partial) result.
Shhh!!! Once you succeed, please don't tell next year's students that they could have gotten the computer to do their early homeworks. ;-)
Write the base conversion program so that it can handle bases greater than 10 as well. As we do with hex, use digit A to represent the value 10, B to represent 11 and so on (you may presume that you do not need to handle nay bases larger than base 35, thus Z will be the largest digit you need).
Though you might accomplish this in by writing a combination of conditional statements essentially saying that if the remainder is 10 use 'A', otherwise if 11 use 'B', otherwise if 12 use 'C', and so on. But this is quite tedious.
This can be more easily expressed by using our knowledge of the ascii representation of the various characters. It turns out that the letter 'A' is represented in ascii using the (decimal) value 65. 'B' is represented in ascii using the value 66, and so on. Therefore, when you have a digit which represents a value of ten or more, you can use math to figure out what ascii value you wish to use for the actual character displayed in the result. In Python, you can convert from an ascii value to the actual character using a syntax such as chr(65), which results in the character 'A'.
With this knowledge in hand, you should be able to adapt your earlier program to properly handle bases higher than 10.