To Lecture Notes

IT 211 -- Feb 25, 2019

Review Exercises

  1. How many permutations are there of the letters of the string "elephant"?
    Ans: There are 8 factorial permutations: 8! = 40,320 permulations.
  2. How do you change a string into a list of characters? Ans:
    input_string = input("Enter a Jumble string: ")
    input_list = list(input_string)
    
  3. How do you obtain all the permutations of a list? Ans:
    perms = list(itertools.permutations(input_list)
    
    Don't forget import itertools at the top of your script.
  4. How to you change the list of of tuples from Exercise 3 into a list of strings? Ans:
    output_list = [ ]
    for p in perms:
       output_list.append("".join(p))
    
  5. Choose six list methods from the Builtin Methods document and test them. Ans:
    a = list[45, 21, 87, 45, 1]
    
    a.insert(1, 99)
    print(a)
    
    a.reverse( )
    print(a)
    
    print(a.count(45))
    
    a.append(999)
    print(a)
    
    a.sort( )
    print(a)
    
    # Output
    45 21 87 45 1
    45 99 87 45 1
    1 45 87 21 99 45 999
    1 21 45 45 87 99 999
    
  6. Extract the specified method from these scripts. Do not include the input and print statements in the extracted method.
    a. # Method name: miles_to_km
       # Input parameter: miles
       # Return value: kilometers
       # Original Script:
           miles = input("Enter the distance in miles: ")
           kilometers = miles * 5280 * 12 * 2.54 / 100000.0
           print(f"Distance in km: {kilometers}.")
           
       # Script with method extracted:
       def miles_to_km(miles):
           kilometers = miles * 5280 * 12 * 2.54 / 100000.0
           return kilometers
      
       m = input("Enter the distance in miles: ")
       km = miles_to_km(m)
       print("Distance in km:", km)
         
    b. # Method name: collapse_list
       # Input parameter: input_list
       # Return value: output_string
       # Original script:
           input_list = eval(input("Enter a list of strings: ")
           output_string = "".join(input_list)
           print(output_string)
           
       # Script with method extracted
       def collapse_list(the_list):
           output_string = "".join(the_list)
           return output_string
           
       input_list = eval(input("Enter a list of strings: ")
       s = collapse_list(input_list)
       print("Collapsed list:", s)
           
    c. # Method name: vowel_count
       # Input parameter: input_string
       # Return value: number of vowels in input string
           input_string = input("Enter a string: ")
           v_count = 0
           for char in input_string:
               if char.upper( ) == "A" or \
                  char.upper( ) == "E" or \
                  char.upper( ) == "I" or \
                  char.upper( ) == "O" or \
                  char.upper( ) == "U":
    
                   v_count += 1
    
           print("Vowel count: " + str(vowel_count))
           
       # Script with extracted method
       def vowel_count(input_string):
           v_count = 0
           for char in input_string:
               if char.upper( ) == "A" or \
                  char.upper( ) == "E" or \
                  char.upper( ) == "I" or \
                  char.upper( ) == "O" or \
                  char.upper( ) == "U":
    
                   v_count += 1
           return v_count
       
       input_string = input("Enter a string: ")
       count = vowel_count
       print("Vowel count is", count)     
    
  7. What is the difference between this list and tuple?
    a1 = [5, 2, 4]
    a2 = (5, 2, 4) 
    
    Check the datatypes of the objects a1 and a2 like this:
    print(type(a1))
    print(type(a2))
    
    Ans: a1 is a list, which is mutable (can be changed), a2 is a tuple, which is immutable (cannot be changed).
  8. Here is the outp

Lists, Tuples, and Dictionaries