Saint Louis University |
Computer Science 140
|
Dept. of Math & Computer Science |
Topic: High-Level Programming in Python
Related Reading: Ch. 8 of Dale/Lewis, as well as
lecture notes on Python
Due:
8pm Tuesday, 25 March 2008
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
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 "apple" else: print "banana" else: if y > 6 or x < 2: print "cherry" else: print "date"
Give an example of values that could be entered for x and y that would result in output "apple".
Give an example of values that could be entered for x and y that would result in output "banana".
Give an example of values that could be entered for x and y that would result in output "cherry".
Give an example of values that could be entered for x and y that would result in output "date".
For this problem, you can use python as an aide. On a test, you would be expected to do such a problem on your own, so please make sure that you fully understand the answer.
Consider the following Python program.
a = int(raw_input("Enter a number: "))
b = int(raw_input("Enter a number: "))
while a <= b:
a = a + 1
print a
On a particular execution, the observed output was the
following.
12
13
14
15
Based on these observations, what values for a and b were initially entered by the user?
For this problem, you can use python as an aide. On a test, you would be expected to do such a problem on your own, so please make sure that you fully understand the answer.
Consider the following Python program.
a = int(raw_input("Enter a number: "))
b = int(raw_input("Enter a number: "))
while a > b:
print a
a = a - 1
On a particular execution, the observed output was the
following.
27
26
25
24
Based on these observations, what values for a and b were initially entered by the user?
Consider the following conditional statement:
if X == 0: Z = Y + W else: Z = Y + Z
Translate this to the equivalent program expressed in the SSCPU assembly language (with W, X, Y, and Z as labels for data stored in memory).
Consider the following SSCPU program:
LDI 25 STO VAL FOO LOD J ADD K SUB VAL JNG BAR LOD J SUB K STO J JMP FOO BAR LOD J OUT STP VAL DAT J DAT K DAT
Give a Python program that naturally expresses the same underlying process (assuming that J and K are existing data variables).
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 equivalent 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.