### week3/day1

parent 66c90500
 def count_repetition(array): result = {} for writer in array: if writer in result: result[writer] += 1 else: result[writer] = 1 return result """ #WIP with nested loops #method 1 def count_repetition(array): result = {} for x in range(len(array)): for y in range(x + 1, len(array)): # print(array[x], array[y]) #--the loop is working count=2 if array[x] == array[y]: print(array[x], array[y]) # it is getting the right amount of pairs count +=1 # the COUNT is not right... print(count) result[array[x]]= count #print(count) else: result[array[x]] = 1 return result #method 2 def count_repetition(array): result = {} for x in array: for y in array[1::]: # print(x, y) #--the loop is working count = 1 if x == y : print(x, y) count += 1 # the COUNT is not right... print(count) result[x] = count #print(count) else: result[x] = count return result """ names = ['kerouac', 'fante', 'fante', 'buk', 'hemingway', 'hornby', 'kerouac', 'buk', 'fante'] print(count_repetition(names)) # {'kerouac': 2, 'fante': 3, 'buk': 2, 'hemingway': 1, 'hornby': 1}
 def exp_recursive(base, exp): if exp ==0: return 1 else: return base*exp_recursive(base, exp-1) print(exp_recursive(5, 3)) # 125 print(exp_recursive(2, 4)) # 16 print(exp_recursive(5, 1)) # 5 print(exp_recursive(6, 0)) # 1 \ No newline at end of file
 def is_array_or_tuple(arg): if isinstance(arg,list) or isinstance(arg, tuple): return True else: return False print(is_array_or_tuple('hello')) # False print(is_array_or_tuple(['hello'])) # True print(is_array_or_tuple([2, {}, 10])) # True print(is_array_or_tuple({'a': 2})) # False print(is_array_or_tuple((1, 2))) # True print(is_array_or_tuple(set())) # False \ No newline at end of file
 def are_same_type(array): for x in array: for y in array: if type(x) != type(y): return False else: return True print(are_same_type(['hello', 'world', 'long sentence'])) # True print(are_same_type([1, 2, 9, 10])) # True print(are_same_type([['hello'], 'hello', ['bye']])) # False print(are_same_type([['hello'], [1, 2, 3], [{'a': 2}]])) # True print(are_same_type([['hello'], set('hello')])) # False \ No newline at end of file
 def is_only_string(string): if type(string) == str: #isinstance(string, str): for element in string: if element == " " or element.isdigit(): return False return True else: return False print(is_only_string('11')) # False print(is_only_string('hello')) #True print(is_only_string(['hello'])) # ? Please handle this case!! Should return False print(is_only_string('this is a long sentence')) # False print(is_only_string({'a': 2})) # ? Please handle this case!! Should return False \ No newline at end of file
 def bank(D,W): return net_amount print (bank([200, 200, 300], )) print (bank([400,300], )) #print(sum([[1,2]])) """ def bank(D,W): deposit_array = [] deposit_total = deposit_array.extend(D) #print(deposit_array) deposit= sum(deposit_total) print(deposit) withdraw_array =[] withdraw_total=withdraw_array.extend(W) withdraw=sum(withdraw_total) net_amount = deposit-withdraw return net_amount print (bank([400,300], )) """
 def new_dict(array): result={} for x in array: result[x] #for key in result.keys(): return result print(new_dict([1, 2, 3, 4, 5])) # {1: {2: {3: {4: {5: {}}}}}} \ No newline at end of file
 def nt_dictionary(num): import random dictionary={} for key in range(num): #bc exercise states that the number of keys is equal to the input num while True: new_key = random.randint(1, 1000) # setting the key if new_key in dictionary.keys(): #check this logic continue #check this logic break #check this logic dictionary[new_key]= new_key**2 #setting the value return dictionary print(nt_dictionary(20)) print('Length', len(nt_dictionary(20))) \ No newline at end of file
 #TO DO the manual way #the package inflect can do this def write_number(num): import inflect p = inflect.engine() result = p.write_number() return result write_number(11) # "eleven" write_number(2) # "two" write_number(32) # "thirty-two" \ No newline at end of file
 def is_caught(arg): if arg.index("C") >= arg.index("m")-3: return True else: return False print(is_caught('C.....m')) # False print(is_caught('C..m')) # True print(is_caught('..C..m')) # True print(is_caught('...C...m')) # False print(is_caught('C.m')) # True \ No newline at end of file
 def convert(args): result=[] #convert to an iterable str args_iterable=str(args) for element in args_iterable: #convert each string a=element to integer result.append(int(element)) a = sorted(result) b= a[::-1] return b print(convert(429563)) # [9, 6, 5, 4, 3, 2] print(convert(324)) # [4, 3, 2] \ No newline at end of file
 def letter_counter(string): count_upper=0 count_lower=0 for x in string: if x.isupper(): count_upper+=1 elif x.islower(): count_lower+=1 print ("UPPER CASE" + str(count_upper) + "/ LOWER CASE" + str(count_lower)) letter_counter("Hello World!") # Hello World! # UPPER CASE 2 # LOWER CASE 8
 def is_alphanumeric(str): for element in str: if type(element) == str or element.isdigit(): return True elif element == " ": return False else: return False print(is_alphanumeric('11')) # True print(is_alphanumeric(['hello'])) # False print(is_alphanumeric('this is a long sentence')) # False print(is_alphanumeric({'a': 2})) # False print(is_alphanumeric("this is string....!!!")) # False
 def intersection(a,b): new_string =a + b sorted_string=sorted(new_string) result="" for x in sorted_string: if x not in result: result +=x return result #to check if the inputs are only letters (ie no spaces or digits) AND if they are not equal a = 'xyaabbbccccdefww' b = 'xxxxyyyyabklmopq' x = 'abcdefghijklmnopqrstuvwxyz' print(intersection(a, b)) # abcdefklmopqwxy print(intersection(a, x)) # abcdefghijklmnopqrstuvwxyz \ No newline at end of file
 def split_the_bill(arg): sum=0 result={} for value in arg.values(): #iterate through values sum+=value average = sum/len(arg) print(average) #itarate through whole object and replace the values of the keys: for key, value in arg.items(): result[key] =value- average return result group = { 'Amy': 20, 'Bill': 15, 'Chris': 10 } print(split_the_bill(group)) # { 'Amy': -5, 'Bill': 0, 'Chris': 5 }
 def is_string(string): if isinstance(string, str): print (True) else: print (False) is_string("hello") is_string(13)
 def zero_sum(array): result=[] for x in array: for y in array[1::]: #print(array[x],array[y]) if x== -y : output=[array.index(x),array.index(y)] reverse_output= output[::-1] #bc of NOTE below ahd to create an interim array and see if those modules existed already in the nested module results object if output not in result and reverse_output not in result: result.append(output) #result.append([array.index(x),array.index(y)]) return result print(zero_sum([1, 5, 0, -5, 3, -1])) # [[0, 5], [1, 3], [2, 2]] print(zero_sum([1, -1])) # [[0, 1]] print(zero_sum([0, 4, 3, 5])) # [[0, 0]] """ #NOTE: in a nested array, or nested tutle, not in only recognized exact modules and does not see numbers inside nested modules #tests to demonstrate the above def test(array): if (array) not in [[2, 3], (5, 6), 9, 1]: return False else: return True print (test([2,3])) print (test([3,2])) print (test(2)) print (test(9)) """
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!