
CP104 - Exam Prep  

sum = 0

for i in range(0,4):
	print(i)
	sum = sum + 1

n = 4 
i = 0 
while i < n:
    print(i)
    i = i + 1


sum = 0 

for i in range(0,4):
    print(sum)
    sum = sum + 1 
    
  



time = int(input("Enter a time in 24 hour format: "))
length = int(input("Enter the number of minutes your call lasted: ))
base_cost = 0.08 * length

if 18 < time < 24:
	discount = base_cost * 0.25
elif 0 < time < 8:
	discount = base_cost * 0.50
else:
	discount = base_cost

if length > 30:
	final_price = discount * 0.1 
else: 	
	final_price = base_cost

print(final_price)


Number 5

import random 

def guess(door_guess, lucky_guess):

	door_random = randint(1,3)

	lucky_random = randint(1,10)

	if (door_guess and lucky_guess) == (door_random and lucky_random):
		print("Congrats you won a prize")
	else:
		None



Number 6 

def word_chain(list):

	empty = ""
	
	new_list = ""

	if list == empty:
		new_list = False 

	elif len(list) == 1:
		new_list = False 

	else:
		new_list = list.split(" ")

		for i in range(0,len(list)-1):
			if (list[i][-1].endswith(list[i+1][0])) == True:
				new_list = True 

	return new_list 


- Basically this is telling you that if the list has a word the last letter endswith the first letter of the next word
- Also this means that the whitespaces are still there thus you nust do i + 1 at 0


Number 7 

def min_diff(list):
	 
	empty = ""

	new_list = ""

	if list == empty:
		new_list = False 

	elif len(list) == 1:
		new_list = False 

	else: 
		
		for i in range(0,len(list)):
			new_list = list[i+1] - list[i]

	return new list 


from functions import min_diff 

list = int(input("Enter a list of numbers: "))

result = min_diff(list)

print(result)


result = []			
		
for i in range(len(my_list)):
	result.append(my_list[i+1]-my_list[i])
	return target
	
Number 8 

def bank(initial,value,type):


	if initial < 0:
		fee = 0 
		balance = -1 

	elif inital < 500:
		if type ==
Number 9

base_price = 25000
value_1 = 1000
value_2 = 1200
value_3 = 700 

option = int(input("Enter the options chosen: "))

if option == 1:
    base_price = base_price + value_1 
elif option == 2:
    base_price = base_price + value_2
elif option == 3:
    base_price = base_price + value_3
elif option == 12:
    base_price = base_price + value_1 + value_2
elif option == 23:
    base_price = base_price + value_2 + value_3
elif option == 123:
    base_price =(base_price + value_1 + value_2 + value_3)- ((base_price + value_1 + value_2 + value_3) * 0.1)
else:
    base_price = base_price 

print(base_price)



Number 10 

my_list = [‘luggage’,‘airplane’, ‘apples’, ‘phones’, ‘butter’, ‘butterscotch’]

new_list = []

for i in range(len(my_list)):

	if len(my_list[i]) == 6:

		new_list.append(my_list[i])

	print(new_list)

- remember to put in an argument for append 
- also return is only for a function 

Number 11 

strin = ("Code")

for i in range(0,len(strin)+1):
	for h in range(0,i):
		print(strin[h], end = "")
print() 

- must remember to add 1 to account for the last letter


Number 12

import random 

dice_1 = random.randint(1,6)
dice_2 = random.randint(1,6)

sum = dice_1 + dice_2

if sum == ( 7 or 11 ):
	print("You have won")
elif dice_1 == dice_2:
	print("Roll Again")
else:
	print("You have lost")

	

Number 13 

Number 14

Number 15

Number 16 

def period_length(period):

	period.find(".")
	
	len(period.find(".")
		
Number 17 

def find_genes(seq,gene):
	
	list_1 = []

	for i in range(0,len(seq)):
		
		if seq[i]==seq.find(gene):
			list_1.append(i)

	return list_1

from functions import find_genes

seq = input("Enter a sequence: ")
gene = input("Enter the gene to locate: ")

data = find_gene(seq,gene)

print(data)

Number 18 

def find_value(target,lista):

	new_list = []

	for i in range(0,len(lista)):
		
		if lista[i] == target:
			new_list.append(i)
	return new_list

Number 19 


def reverse_list(lista):

	new_list = []

	for i in range(1,len(lista)+1):
		
		new_list.append(lista[-i])

	return new_list 



def abs_convert(lista):

	

	for i in range(0,len(lista)):

		result = abs(lista[i])

	return result 

from functions import abs_covert 

lista = [38.4, -101.7, -2.1]

data = abs_convert(lista)

print("x        |x|")
print("{}       {}".format(lista,data))



def dot_product(source1, source2):
    """
    -------------------------------------------------------
    Calculates a dot product of two lists. Lists must be the
    same length.
    Use: target = dot_product(source1, source2)
    -------------------------------------------------------
    Parameters:
        source1 - list of numbers (list of float)
        source2 - list of numbers (list of float)
    Returns:
        target - source1 ⋅ source2 [source1 dot product source2] (float)
    -------------------------------------------------------
    """

	target = 0 

	for i in range(0,len(source1)):

		target = target + (source1[i] * source2[i])

	return target

from functions import dot_product

source1 = [10, 3, 10, 3, 1]

source2 = [8, 2, 7, 3, 6]

target = dot_product(source1,source2)

print(target)


def list_sums(source1, source2):
    """
    -------------------------------------------------------
    Calculates sums of elements of two lists. Lists must be the
    same length.
    Use: target = list_sum(source1, source2)
    -------------------------------------------------------
    Parameters:
        source1 - list of numbers (list of float)
        source2 - list of numbers (list of float)
    Returns:
        target - sums of elements of source1 and source2 (list of float)
    -------------------------------------------------------
    """

	target = []

	for i in range(0,len(source1)):

		result = source1[i] + source2[i]
		target.append(result)
	return target 

from functions import list_sums 

source1 = [10, 3, 10, 3, 1]

source2 = [8, 2, 7, 3, 6]

target = list_sums(source1,source2)

print(target)


def text_analyze(txt):
    """
    -------------------------------------------------------
    Analyzes txt and returns the number of uppercase letters,
    lowercase letters, digits, and number of whitespaces in txt.
    Use: uppr, lowr, dgts, whtspc = text_analyze(txt)
    -------------------------------------------------------
    Parameters:
        txt - the text to be analyzed (str)
    Returns:
        uppr - number of uppercase letters in txt (int >= 0)
        lowr - number of lowercase letters in txt (int >= 0)
        dgts - number of digits in txt (int >= 0)
        whtspc - number of white spaces in the text (spaces, tabs, linefeeds) (int >= 0)
    ------------------------------------------------------
    """

	uppr = 0 
	lowr = 0 
	dgts = 0 
	whtspc = 0 

	for i in txt:

		if txt.isupper() == i:
			uppr = uppr + 1 
		elif txt.islower() == i:
			lowr = lowr + 1 
		elif txt.isdigit() == i:
			dgts = dgts + 1 
		else:
			whtspc = len(txt) - len(txt.strip())

	return uppr, lowr, dgts, whtspc

from functions import text_analyze

txt =  Python uses whitespace indentation, rather than curly brackets or keywords, to delimit blocks.

uppr, lowr, dgts, whtspc = text_analyze(txt)

print(uppr)
print(lowr)
print(dgts)
print(whtspc)

def comma_period_replace(s):
    """
    -------------------------------------------------------
    Replaces all the commas with a period in s.
    Use: out = comma_period_replace(s)
    -------------------------------------------------------
    Parameters:
        s - a string (str)
    Returns:
        out - s with all commas replaced by a period (str)
    ------------------------------------------------------
    """

	new_string = ""
	
	for i in s:
		if i == ",":
			new_string.insert(i,".")

	return new_string


FILES 

	
def file_stats(fv):

	line = fv.readline()
	
	

	while line != "":

	
		







from functions import file_stats 

fv = open("numbers.txt","r")

result = file_stats(fv)

print(result)

fv.close()






line = fv.readline().strip()
    result = line.split(',')
    
    while line != "":
        if line.strip().split(',')[4] < result[4]:
            result = line.split(',')
        line = fv.readline().strip()
    
    return result 
            




string = input("Enter a string: ")

if string == reverse(string):
	result = True 
else:
	result = False 
















