Solution for Python Exam

Program Files


# COP 3223 Python Exam

# PID:       : _________________________________________

# First name : _________________________________________

# Last name  : _________________________________________


  __________          __  .__                    __________               __             ._.
  \______   \___.__._/  |_|  |__   ____   ____   \______   \ ____   ____ |  | __  ______ | |
   |     ___<   |  |\   __\  |  \ /  _ \ /    \   |       _//  _ \_/ ___\|  |/ / /  ___/ | |
   |    |    \___  | |  | |   Y  (  <_> )   |  \  |    |   (  <_> )  \___|    <  \___ \   \|
   |____|    / ____| |__| |___|  /\____/|___|  /  |____|_  /\____/ \___  >__|_ \/____  >  __
             \/                \/            \/          \/            \/     \/     \/   \/


# Problem # 0 ________

# Problem # 1 ________

# Problem # 2 ________

# Problem # 3 ________

# Problem # 4 ________

# Total       ________

#

#

#

Problem 0

# Problem 0        
#                                                                                   
# Full name: ______________________________
# 
# What does the following program output?                       

def main():
    
    str = "some random text"

    # 1
    print("This one is easy :)")

    # 2
    print(str[:])

    # 3
    print(str[8:])

    # 4
    print(str[0:4] + str[-8:-4])

    # 5
    i = str.find("m")

    print(str[i : i + len("whatever")])

main()

# Indicate the output

# Note that the added |'s at the beginning and the end of the output strings.  
# I've included them to indicate that there an invisible space in #4

# 1 |This one is easy :)|
# 2 |some random text|
# 3 |dom text|
# 4 |somedom |
# 5 |me rando|

Solution to Problem 0

# Problem 0        
#                                                                                   
# Full name: ______________________________
# 
# What does the following program output?                       

def main():
    
    str = "some random text"

    # 1
    print("This one is easy :)")

    # 2
    print(str[:])

    # 3
    print(str[8:])

    # 4
    print(str[0:4] + str[-8:-4])

    # 5
    i = str.find("m")

    print(str[i : i + len("whatever")])

main()

# Indicate the output

# Note that the added |'s at the beginning and the end of the output strings.  
# I've included them to indicate that there an invisible space in #4

# 1 |This one is easy :)|
# 2 |some random text|
# 3 |dom text|
# 4 |somedom |
# 5 |me rando|

Problem 1

# Problem 1        
#                                                                                       
# Full name: ______________________________
#
# Define a procedure, print_mult_table, that takes as input a positive whole number,
# and prints out the corresponding multiplication table.

# ==> You have to use for loops with range. !!!
# ==> You are not allowed to use while loops. !!!

# For instance, if the user enters 3, then print_mult_table(3) should print out:
# 1 * 1 = 1
# 1 * 2 = 2
# 1 * 3 = 3
# 2 * 1 = 2
# 2 * 2 = 4
# 2 * 3 = 6
# 3 * 1 = 3
# 3 * 2 = 6
# 3 * 3 = 9

def print_mult_table(n):
















def main():

    n = int(input("Enter a positive integer: "))

    print_mult_table(n)

main()

Solution to Problem 1

# Problem 1        
#                                                                                       
# Full name: ______________________________
#
# Define a procedure, print_mult_table, that takes as input a positive whole number,
# and prints out the corresponding multiplication table.

# ==> You have to use for loops with range. !!!
# ==> You are not allowed to use while loops. !!!

# For instance, if the user enters 3, then print_mult_table(3) should print out:
# 1 * 1 = 1
# 1 * 2 = 2
# 1 * 3 = 3
# 2 * 1 = 2
# 2 * 2 = 4
# 2 * 3 = 6
# 3 * 1 = 3
# 3 * 2 = 6
# 3 * 3 = 9

def print_mult_table(n):
    for i in range(1, n+1, 1):
        for j in range(1, n+1, 1):
            print(i, "*", j, "=", i*j)

def main():

    n = int(input("Enter a positive integer: "))

    print_mult_table(n)

main()

Problem 2

# Problem 2
#
# Full name: ______________________________
#
# What does the following program output?

def seq(n):

    a = 0, b = 1

    i = 0 

    while i <= n:

        print(a, ",", b);  

        a, b = b, b + 2 * a             # Recall this is a multi assignment 

        i = i + 1

def main():
    seq(3)

main()

Solution to Problem 2

# Problem 2
#
# Full name: ______________________________
#
# What does the following program output?

def seq(n):

    a = 0, b = 1

    i = 0 

    while i <= n:

        print(a, ",", b);  

        a, b = b, b + 2 * a             # Recall this is a multi assignment 

        i = i + 1

def main():
    seq(3)

main()

# Output
# 0 , 1
# 1 , 1
# 1 , 3
# 3 , 5

Problem 3

# Problem 3        
#                                                                                   
# Full name: ______________________________
#
# Complete the function find_min that takes a list of integers as input
# and returns the smallest element of the list
# Make sure that the list is not mutated.
# If the list is empty, return None

def find_min(p):

    if len(p) == 0:

        return ____________________

    min_elem = p[0]

    for i in range(len(p)):

        if p[i] < min_elem:

            min_elem = ____________________


    return min_elem

def main():
    print("The minimum of the list [1,2,3,4,-1,5,6] is " + find_min([1,2,3,4,-1,5,6]))
    
main()

Solution to Problem 3

# Problem 3        
#                                                                                   
# Full name: ______________________________
#
# Complete the function find_min that takes a list of integers as input
# and returns the smallest element of the list
# Make sure that the list is not mutated.
# If the list is empty, return None

def find_min(p):

    if len(p) == 0:

        return None

    min_elem = p[0]

    for i in range(len(p)):

        if p[i] < min_elem:

            min_elem = p[i]

    return min_elem

def main():
    print("The minimum of the list [1,2,3,4,-1,5,6] is", find_min([1,2,3,4,-1,5,6]))
    print("The minimum of the list [] is", find_min([]))
main()

Problem 4

# Problem 4         
#                                                                                         
# Full name: ______________________________
#
# Complete the function find_most_frequent_word that takes a list of strings (words)
# and the word that appears most often in the list
# If the list is empty, return None

def find_most_frequent_word(list):

    most_frequent_word = ____________________

    num_of_occurences = ____________________

    frequency_dict = {}
    for word in list:
        if word in frequency_dict:
            frequency_dict[word] = frequency_dict[word] + 1
        else:
            frequency_dict[word] = 1
    
    for ____________________

        if  num_of_occurences < frequency_dict[key]:
            num_of_occurences = frequency_dict[key]

            most_frequent_word = ____________________

    return most_frequent_word


# print(find_most_frequent_word(['Hi','Python','C','Python','fun']))

# should print out Python since this word occurs twice whereas all
# other words occur only once

Solution to Problem 4

# Problem 4         
#                                                                                         
# Full name: ______________________________
#
# Complete the function find_most_frequent_word that takes a list of strings (words)
# and the word that appears most often in the list
# If the list is empty, return None

def find_most_frequent_word(list):

    most_frequent_word = None

    num_of_occurences = 0

    frequency_dict = {}
    for word in list:
        if word in frequency_dict:
            frequency_dict[word] = frequency_dict[word] + 1
        else:
            frequency_dict[word] = 1
    
    for key in frequency_dict:

        if  num_of_occurences < frequency_dict[key]:
            num_of_occurences = frequency_dict[key]

            most_frequent_word = key

    return most_frequent_word


print(find_most_frequent_word(['Hi','Python','C','Python','fun']))

# should print out Python since this word occurs twice whereas all
# other words occur only once