CS 21, Spring 2017

Practice Questions, Week 2

  1. Write a program that intersperses dashes between each character in some string of text entered by the user, as in the interaction shown below. Typing done by the user is indicated in bold.

    $ python dashes.py
    Enter text: swarthmore
    -s-w-a-r-t-h-m-o-r-e-
    $ python dashes.py
    Enter text: CS 21
    -C-S- -2-1-
    
  2. Write a program that prints out a triangle made up of asterisks. The user should be able to enter an integer that indicates how many rows the triangle has:

    $ python triangle.py
    Number of rows: 5
    
    *
    **
    ***
    ****
    *****
    
    $ python triangle.py
    Number of rows: 3
    
    *
    **
    ***
    
    
  3. Write a program that computes the sum of the first n positive integers, where n is entered by the user:

    $ python sum.py
    n: 10
    55
    

    Extend the program to display the computation that was performed:

    $ python sum.py
    n: 10
    1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
    

    The formula for the sum of the first n numbers is (n*(n+1))/2. Extend your program again so that it compares the result of summing up the first n integers with the result of applying this formula.

    $ python sum.py
    n: 10
    1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
    (10 * (10 + 1)) / 2 = 55
    
  4. Write a program that computes the factorial of n, where n is provided by the user. Display the computation that was performed:

    $ python factorial.py
    n: 5
    5 * 4 * 3 * 2 * 1 = 120
    
  5. Write a program that takes some user-entered text and prints it out in a diamond pattern, as seen below:

    $ python diamond.py
    Enter text: swarthmore
    
    s
    sw
    swa
    swar
    swart
    swarth
    swarthm
    swarthmo
    swarthmor
    swarthmore
     warthmore
      arthmore
       rthmore
        thmore
         hmore
          more
           ore
            re
             e
    
    $ python diamond.py
    Enter text: slice
    
    s
    sl
    sli
    slic
    slice
     lice
      ice
       ce
        e
    
    
  6. Write a program that prints out user-entered text in an upside-down pyramid pattern as shown below:

    $ python pyramid.py
    Enter text: swarthmore
    
    swarthmore
     warthmor
      arthmo 
       rthm
        th
    
    $ python pyramid.py
    Enter text: ululate
    
    ululate
     lulat
      ula
       l
    
    
  7. For each of the following Python expressions, write down on a separate sheet of paper the value to which the expression evaluates as well as the type of that value. For instance with the expression 2 + 2 the value is 4 and the type is int. If the expression is invalid in Python you can just write "invalid" or "error". Note that after you've written down your answers you can check your work using the Python shell and the type function.

  8. Without using your computer, write down on a piece of paper what output the following program will produce:

    """
    Mystery program
    
    David Mauskop, CS 21
    """
    
    def main():
        s = "giraffes"
        t = ""
        for i in range(0, len(s), 2):
            t = t + s[i] 
        print(t)
    
    main()
  9. The program below has an error. Without using your computer, try to spot the error. Then rewrite the program on a separate piece of paper, so that it accomplishes the intended task without any errors.

    """
    This program prompts the user to enter 5 numbers, then
    prints the sum of all the entered numbers.
    
    David Mauskop, CS 21
    """
    
    def main():
        for i in range(5):
            number = float(raw_input("Enter number: "))
            total = total + number
    
        print("The total is: %f" % (total))
    
    main()
  10. On paper, without using your computer, write a program that converts temperatures from Celsius to Farenheit. To go from Celsius to Farenheit, multiply the Celsius temperature by 1.8 and then add 32. The program should prompt the user to enter a Celsius temperature and then print the corresponding Farenheit temperature. The output should show just one place after the decimal. Use string formatting to achieve this.

    If you typed in the program and ran it, here's what one interaction with the user might look like:

    $ python converttemp.py
    Enter Celsius temp: 12.4
    Farenheit temp: 54.3