HW1FINALBEST

py

School

Pennsylvania State University *

*We aren’t endorsed by this school

Course

132

Subject

Computer Science

Date

Feb 20, 2024

Type

py

Pages

5

Uploaded by MateWasp883

Report
# HW1 # REMINDER: The work in this assignment must be your own original work and must be completed alone. def rectangle(perimeter,area): """ >>> rectangle(14, 10) 5 >>> rectangle(12, 5) 5 >>> rectangle(25, 25) False >>> rectangle(50, 100) 20 >>> rectangle(11, 5) False >>> rectangle(11, 4) False """ #- YOUR CODE STARTS HERE x = 1 y = area semi = -perimeter/2 Dis = semi**2 - (4*x*y) if (Dis > 0): x1 = (-semi + Dis**0.5)/2 x2 = (-semi - Dis**0.5)/2 else: return False if (float.is_integer(x1) == True) and (float.is_integer(x2) == True): if x1<x2: return (int(x2)) else: return (int(x1)) else: return False def to_decimal(oct_num): """ >>> to_decimal(237) 159 >>> to_decimal(35) 29 >>> to_decimal(600) 384 >>> to_decimal(420) 272 """ #- YOUR CODE STARTS HERE x = 0 base = 1 while(oct_num): y = oct_num % 10 oct_num = int(oct_num / 10) x = x+ y * base base = base * 8 return x
pass def largeFactor(num): """ >>> largeFactor(15) # factors are 1, 3, 5 5 >>> largeFactor(80) # factors are 1, 2, 4, 5, 8, 10, 16, 20, 40 40 >>> largeFactor(13) # factor is 1 since 13 is prime 1 """ #- YOUR CODE STARTS HERE lst = [] for i in range(1, int(num/2)+1): if num%i == 0: lst.append(i) lst1 = 0 for i in range(len(lst)): if lst[i]>lst1: lst1 = lst[i] return lst1 def hailstone(num): """ >>> hailstone(10) [10, 5, 16, 8, 4, 2, 1] >>> hailstone(1) [1] >>> hailstone(27) [27, 82, 41, 124, 62, 31, 94, 47, 142, 71, 214, 107, 322, 161, 484, 242, 121, 364, 182, 91, 274, 137, 412, 206, 103, 310, 155, 466, 233, 700, 350, 175, 526, 263, 790, 395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167, 502, 251, 754, 377, 1132, 566, 283, 850, 425, 1276, 638, 319, 958, 479, 1438, 719, 2158, 1079, 3238, 1619, 4858, 2429, 7288, 3644, 1822, 911, 2734, 1367, 4102, 2051, 6154, 3077, 9232, 4616, 2308, 1154, 577, 1732, 866, 433, 1300, 650, 325, 976, 488, 244, 122, 61, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1] >>> hailstone(7) [7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] >>> hailstone(19) [19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] """ #- YOUR CODE STARTS HERE lst = [] while num!=1: if num%2 == 1: lst.append(int(num)) num = (3*num)+1 elif num%2 ==0: lst.append(int(num)) num = num/2 lst.append(1) return lst
def remove(txt): """ >>> remove("Dots ...................... many dots..X") ('Dots many dots X', {'.': 24}) >>> data = remove("I like chocolate cake!!(!! It's the best flavor..;.$ for real") >>> data[0] 'I like chocolate cake It s the best flavor for real' >>> data[1] {'!': 4, '(': 1, "'": 1, '.': 3, ';': 1, '$': 1} """ #- YOUR CODE STARTS HERE letters = ["A", "a", "B", "b", "C", 'c', 'D', 'd', 'E','e','F','f','G','g','H','h','I','i','J','j','K','k','L','l','M','m','N','n','O' ,'o','P','p','Q','q','R','r','S','s','T','t','U','u','V','v','W','w','X','x','Y','y ','Z','z',' '] dict1 = {} string = "" for i in txt: if i not in letters: string = string+" " if i not in dict1: dict1[i] = 1 else: dict1[i] = dict1[i]+1 else: string = string+i return (string, dict1) def translate(translation_file, msg): """ >>> translate('abbreviations.txt', 'c u in 5.') 'see you in 5.' >>> translate('abbreviations.txt', 'gr8, cu') 'great, see you' >>> translate('abbreviations.txt', 'b4 lunch, luv u!') 'before lunch, love you!' """ # Open file and read lines into one string all the way to the end of the file with open(translation_file) as file: contents = file.read() #- YOUR CODE STARTS HERE dict1={} lst=contents.split('\n') for i in lst: i=i.split('=') dict1[i[0]]=i[1] msg=msg.split() lst1=[] for i in msg: if i.strip(".?!,;:") not in dict1:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
lst1=lst1+[i] else: if i[len(i)-1] not in (".?!,;:"): lst1=lst1+[dict1[i.strip(".?!,;:")]] else: lst1=lst1+[dict1[i.strip(".?!,;:")]+i[len(i)-1]] return(' '.join(lst1)) def addToTrie(trie, word): """ >>> trie_dict = {'a' : {'word' : True, 'p' : {'p' : {'l' : {'e' : {'word' : True}}}}, 'i' : {'word' : True}}} >>> addToTrie(trie_dict, 'art') >>> trie_dict {'a': {'word': True, 'p': {'p': {'l': {'e': {'word': True}}}}, 'i': {'word': True}, 'r': {'t': {'word': True}}}} >>> addToTrie(trie_dict, 'moon') >>> trie_dict {'a': {'word': True, 'p': {'p': {'l': {'e': {'word': True}}}}, 'i': {'word': True}, 'r': {'t': {'word': True}}}, 'm': {'o': {'o': {'n': {'word': True}}}}} """ #- YOUR CODE STARTS HERE dict1 = trie #we are copying it into dict1, because we will be modifying it, so we dont want to alter the original trie for alpha in word: if alpha not in dict1: dict1[alpha] = {} dict1 = dict1[alpha] dict1['word'] = True pass def createDictionaryTrie(file_name): """ >>> trie = createDictionaryTrie("words.txt") >>> trie == {'b': {'a': {'l': {'l': {'word': True}}, 't': {'s': {'word': True}}}, 'i': {'r': {'d': {'word': True}},\ 'n': {'word': True}}, 'o': {'y': {'word': True}}}, 't': {'o': {'y': {'s': {'word': True}}},\ 'r': {'e': {'a': {'t': {'word': True}}, 'e': {'word': True}}}}} True """ # Open file and read lines into one string all the way to the end of the file with open(file_name) as file: contents = file.read() #- YOUR CODE STARTS HERE dict1={} #here we build it from scratch lst=contents.lower() lst=lst.split('\n')
for i in lst: addToTrie(dict1,i) return(dict1) def wordExists(trie, word): """ >>> trie_dict = {'a' : {'word' : True, 'p' : {'p' : {'l' : {'e' : {'word' : True}}}}, 'i' : {'word' : True}}} >>> wordExists(trie_dict, 'armor') False >>> wordExists(trie_dict, 'apple') True >>> wordExists(trie_dict, 'apples') False >>> wordExists(trie_dict, 'a') True >>> wordExists(trie_dict, 'as') False >>> wordExists(trie_dict, 'tt') False """ #- YOUR CODE STARTS HERE dict1 = trie #we are copying it into dict1, because we will be modifying it, so we dont want to alter the original trie for alpha in word: if alpha not in dict1: return False else: dict1 = dict1[alpha] #once we find a letter in the trie, we move forward to the values of the letter and find the next letters if 'word' not in dict1 and dict1['word'] != True: #once it has finished the for loop without returning false that is by finding all the letters we check if it is ending with 'word':True return False else: return True if __name__=='__main__': import doctest doctest.run_docstring_examples(remove, globals(), name='HW1',verbose=True) #doctest.testmod()