To Lecture Notes

IT 211 -- Feb 20, 2019

Review Exercises

  1. What does this PPM image display?
    P3 3 3 255
    192 192 192  0 0 255  192 192 192
      0   0 255  0 0 255    0   0 255
    192 192 192  0 0 255  192 192 192
    
    a. A black X            b. A blue cross
    c. A yellow box       d. A teal L
    Ans: b, a blue cross. 0 0 255 are the color components for blue.
  2. What is a list? We already know about lists from this statement:
    fields = line.split(",")
    
    Ans: A list is a sequence of items like a = [3, 2, 5, 4] or b = ["dog", "cat", "mouse"]. The items are accessed by zero-basec index: a[0] is 3; b[1] is "cat".
  3. What is the output?
    1. a = [4, 1, 7, 5, 8]
      print(a[3])
      # Output: 5
      
    2. a = [4, 1, 7, 5, 8]
      print(a[4] - a[1], a[4 - 1], a[-2])
      
      a[-2] is the second item from the end of the list.
    3. a = [4, 1, 7, 5, 8]
      total = 0
      for n in a:
          total += n
      print(total)
      
      # Output: 25
      
    4. t = 1
      s = "2 1 4 6"
      a = s.split(" ")
      for val in a:
          t *= int(val)
      print(t)
      
      # Output: 48
      
  4. Write a script that inputs a string, converts it to a list, and prints the list. Ans:
    s = input("Input a string: ")
    lst = list(s)
    print(s)
    
    We can combine these three lines into one like this:
    print(list(input("Input a string: ")))
    
  5. Select four list methods from the Builtin Methods Document and test them.
  6. Test the str method join. Ans:
    s = ["d", "o", "g"]
    t = ":".join(s)
    u = "".join(s)
    print(t)
    # Output:
    d:o:g
    dog
    

Practice Problems

  1. Write and test user defined standalone methods using the following specs:

    Method Name Input Parameter(s) Return Value Side Effects*
    rm_vowels input_string Input string with vowels removed None
    factors number List of factors None

    *Because neither method has any side effects, this means that each of these methods can only return a value, it can't do anything else like print to the screen or write to a file.

    Here are two scripts that you can convert into the methods specified in the preceding table.
    1. # Remove vowels
      input_str = print("Enter input string: ")
      input_str = gets.chomp
      output_str = ""
      for letter in input_str:
          letter = letter.upper( )
          if letter != 'A' and letter != 'E' and letter != 'I' \ 
             letter != 'O' and letter != 'U':
      
              output_str += letter
      
      print("String without vowels", output_str)
      
      # Ans:
      def rm_vowels(input_string):
          output_str = ""
          for letter in input_str:
              letter = letter.upper( )
              if letter != 'A' and letter != 'E' and letter != 'I' \ 
                 letter != 'O' and letter != 'U':
      
                  output_str += letter
      
          return output_str
          
      string = print("Enter input string: ")
      processed_string = rm_vowels(string)
      print("String without vowels", processed_string)
      
    2. # Obtain list all factors of input integer.
      n = int(input("Enter a positive integer: "))
      factors = [ ]
      for f in range(2, n):
          if n % f == 0
              factors.append(f)
              
      print(factors)
      
      # Ans:
      def factors(number):
          # 1 and number are always factors so we can
          # add them to the list without checking.
          factors = [1]
          for f in range(2, number):
          if number % f == 0
              factors.append(f)
          factors.append(number)
          return factors
          
      n = int(input("Enter a positive integer: "))
      print(factors(n))
      

Project 5