Assignments | Class Photo | Course Home | Schedule & Lecture Notes | Submit | Tutoring Hours

Saint Louis University

Computer Science 140
Introduction to Computer Science

Michael Goldwasser

Spring 2008

Dept. of Math & Computer Science

Assignment 06

High-Level Programming in Python

Contents:


Overview

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


Internet Requirements

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.


Practice Problems

  1. 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)

  2. Review all of the code fragments in our Python lecture notes


Problems to be Submitted (20 points)

  1. (4 points)

    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"
    


    1. Give an example of values that could be entered for x and y that would result in output "apple".

    2. Give an example of values that could be entered for x and y that would result in output "banana".

    3. Give an example of values that could be entered for x and y that would result in output "cherry".

    4. Give an example of values that could be entered for x and y that would result in output "date".

  2. (4 points)

    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?

  3. (4 points)

    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?

  4. (4 points)

    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).

  5. (4 points)

    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).


Extra Credit (2 points)

  1. (2 points)

    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.

          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.
          
    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:
    valuequotientremainderpartial result
    591922
    196112
    620012
    2022012
    02012

    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:

          Enter Decimal Value: 59
          Enter Desired Base: 3
          The decimal value 59 is equivalent to the value 2012 in base 3
          
    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).

    Advice:

    Shhh!!! Once you succeed, please don't tell next year's students that they could have gotten the computer to do their early homeworks. ;-)

  2. (1 point)

    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.


CSCI 140, Spring 2008
Michael Goldwasser © 2008
goldwamh at our university domain

Last modified: Monday, 10 March 2008
Assignments | Class Photo | Course Home | Schedule & Lecture Notes | Submit | Tutoring Hours