Instructions


Test One - Thursday, Oct 15th, 2015

  1. Write numeric expressions in Python for these values:
    1.      5 + 3 * 2n-2
           ------------
              10 * p     
      
      
    2. the sum of the cube of integer x and the square of integer y
      
      
      
    3. the number of seconds in 3 days, 14 hours, 15 minutes, and 9 seconds
      (There are sixty seconds in a minute, and sixty minutes in an hour, 24 hours in a day.)
    
    
    

  2. Write boolean expressions in Python for these conditions:

    1. 2 is NOT a factor of y (that is, y is an odd number, y is odd)
    2. 
      
         
      
    3. age is at least 21 and gender equals 'female'
    4.    
      
      
      
    5. the string in variable ingredient contains an 'A'

  3.    
    
  4. Consider this code:
        if x % 2 == 1:
            if x**3 != 27:
                x = x + 49
            else:
                x = x / 1.5
        else:
            if x <= 10:
                x = x * 2
            else:
                x = x - 2
        print(x)
    
    

    What does this code print if x == 3?

    What does this code print if x == 12?

    What does this code print if x == 4?


  5. The user enters a name in format last name, first name. Write each of the three expressions for first, last and name2. Write Python code that uses name as the input.

    Another example: Baggins, Frodo as input would output 'Frodo' for first, 'Baggins' for last, and output 'Frodo Baggins' for name2.

    >>> name = input("Enter name: last, first -\n           example Smith, John--: ")
    Enter name: last, first -
               example Smith, John--: Jacobson, Mark
    >>> name
    'Jacobson, Mark'
    
    
    >>> first = 
    >>> first
    'Mark'
    
    
    
    >>> last = 
    >>> last
    'Jacobson'
    
    
    
    >>> 
    >>> name2 = 
    >>> name2
    'Mark Jacobson'
    
    
    
    

  6. The user enters the birthdays of, a and b, two people who were born in the same year. Write Python code that prints ( b is younger ) if a's birthday comes before b's, and ( a is younger ) if b's birthday comes before a's and ( SAME BIRTHDAY for a and b!! ) if a's birthday is the same as b's.
        a_month = int( input('Enter the month: ') )
        a_day   = int( input('Enter the day  : ') )
        b_month = int( input('Enter the month: ') )
        b_day   = int( input('Enter the day  : ') )
                                                                        # fill in the blank
    
    
    
    
    
    
    
    
    
    

  7. Answer briefly each of these questions about algorithms and programs:

    1. What is the most important programming proverb of all proverbs about programming? Just state it please. (Hint: It is five words, so this is a fill in the five blanks question).
    2.    
         
      
    3. What are the first three steps or phases of the problem solving/programming process according to the lectures?
  8.       Step/phase #1:
    
    
    
          Step/phase #2:
    
    
    
          Step/phase #3:
    
    

  9. Consider this code:
        x = input('Enter a string: ')
        y = 0
        for ch in x:
            print(ch, end=' ')
            y += 1
            if y % 4 == 0:
                print( x[0:y] )
    
    
    
    
    

    What does this code print if the user enters 'PANTHERS'?
    Note: Show what the output would look like too.


  10. 
    
    
  11. Convert the following numbers from one base to another base:
    VIP Note: You MUST show your work and process of arriving at the answer.
                   Calculators are not allowed on the exam. Another reason to show your work!

    1. Decimal base ten number 89 is what in binary (base two).
    2. 
         
         
      
         
      
    3. Base ten 135 in binary is:
    4.    
      
         
      
         
      
    5. The binary number 1011110 is what value in base ten decimal?
    6.    
         
      
         
      

  12. Write a Python loop to get the sum of all the odd numbers between 1 to n, for a given value of n. For example, the sum of the odds between 1 and 7 is 1 + 3 + 5 + 7 = 16.

    ... or if n = 11 or n = 12, the output would be 36, since 1 + 3 + 5 + 7 + 9 + 11 = 36.


  13.    
       
    
       
    
  14. Consider this code:
    for i in range(10,100,5):
        print(i)
    

    Write a while loop that does exactly the same thing as the for loop.


  15.    
       
       
    
    
    
    

  16. Suppose we want to censor a string in this way:
        Enter the source     : Ghostbusters
        Enter char to replace: s
        Enter char to insert : *
    
        Result: Gho*tbu*ter*
    

    Write Python code that prints out the characters in string str, only with character old_ch replaced with character new_ch.

        str    = input('Enter the source     : ')
        old_ch = input('Enter char to replace: ')
        new_ch = input('Enter char to insert : ')
    
    
    
    
    
    
                                                                         # fill in the blank
    
    
    
    
    
    

                                                        **** Another example: 
                                                        Enter the source     : MISSISSIPPI
                                                        Enter char to replace: I
                                                        Enter char to insert : #
        print('\nResult:', result)
                                                        Result: M#SS#SS#PP#
    
  17. Suppose we want to know the sum of the digits in an integer:
        Enter an integer     : 357                    Enter an integer     : 123456
    
        Result: 357 digit sum is 15                   Result: 123456 digit sum is 21
    

    Write the Python code that obtains the sum of the digits in number.
    The first statement and the last statement of the program are given for you.

        number  = input('Enter an integer     : ')
    
     
    
    
    
    
    
                                                                           # fill in the blank
    
    
    
    
    
    
        print('\n', number, "digit sum is", digitSum)