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

Saint Louis University

Computer Science 140
Introduction to Computer Science

Michael Goldwasser

Fall 2007

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 Thursday, 18 October 2007
Extended to 8pm Wednesday, 24 October 2007


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)

    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.

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

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

  2. (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 'answer is A'
      else:
        print 'answer is B'
    else:
       if y > 6 or x < 2:
         print 'answer is C'
       else:
         print 'answer is D'
    


    1. What value or sequence of values would be printed when the user enters 4 and then 4?

    2. What value or sequence of values would be printed when the user enters 9 and then 4?

    3. What value or sequence of values would be printed when the user enters 1 and then 9?

    4. What value or sequence of values would be printed when the user enters 6 and then 2?

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

  4. (8 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. ;-)


Extra Credit (2 points)

  1. (2 points)

    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, Fall 2007
Michael Goldwasser
goldwamh at our university domain

Last modified: Monday, 29 October 2007
Assignments | Course Home | Schedule & Lecture Notes | Submit | Tutoring Hours