From 29509ecdc17790b18f020a80e5c430c5803e0a06 Mon Sep 17 00:00:00 2001 From: Jeremiah Morelock Date: Sun, 11 Dec 2022 11:44:21 -0500 Subject: [PATCH 1/5] Update 12_09_practice.py Here are my answers to the Per Scholas python practice problems from 12/9/22. --- 12_09_practice.py | 148 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 135 insertions(+), 13 deletions(-) diff --git a/12_09_practice.py b/12_09_practice.py index 7914563..48eeb36 100644 --- a/12_09_practice.py +++ b/12_09_practice.py @@ -1,29 +1,151 @@ -#Biggie Size - Given a list, write a function that changes all positive numbers in the list to "big". Example: make_it_big([-1, 3, 5, -5]) returns that same list, #changed to [-1, "big", "big", -5]. +# these lists come into play a lot below +numbers = [4, 0, 5, 10, 99, -4] +more_numbers = [0, -2, -5, 5, -99] -#Count Positives - Given a list of numbers, create a function to replace last value with number of positive values. Example, count_positives([-1,1,1,1]) changes list #to [-1,1,1,3] and returns it. (Note that zero is not considered to be a positive number). +#Biggie Size - Given a list, write a function that changes all positive numbers in the list to "big". +# Example: make_it_big([-1, 3, 5, -5]) returns that same list, #changed to [-1, "big", "big", -5]. +def make_it_big(assortment): + new_assortment = [] + for item in assortment: + if item <= 0: + new_assortment.append(item) + else: + new_assortment.append("big") + return new_assortment +print(make_it_big(numbers)) -#SumTotal - Create a function that takes a list as an argument and returns the sum of all the values in the list. For example sum_total([1,2,3,4]) should return 10 +#Count Positives - Given a list of numbers, create a function to replace last value with number of positive values. +# Example, count_positives([-1,1,1,1]) changes list #to [-1,1,1,3] and returns it. (Note that zero is not considered to be a positive number). +def end_count(assortment): + new_assortment = [] + count = 0 + for item in assortment: + if item <= 0: + pass + else: + count += 1 + assortment[-1] = count + return assortment +print(end_count(numbers)) -#Average - Create a function that takes a list as an argument and returns the average of all the values in the list. For example multiples([1,2,3,4]) should return #2.5 +#SumTotal - Create a function that takes a list as an argument and returns the sum of all the values in the list. +# For example sum_total([1,2,3,4]) should return 10 +def sumTotal(assortment): + total = 0 + for item in assortment: + total += item + return total +print(sumTotal(numbers)) -#Length - Create a function that takes a list as an argument and returns the length of the list. For example length([1,2,3,4]) should return 4 +#Average - Create a function that takes a list as an argument and returns the average of all the values in the list. +# For example multiples([1,2,3,4]) should return #2.5 +def average(assortment): + result = (sum(assortment))/(len(assortment)) + return result +print(average(numbers)) -#Minimum - Create a function that takes a list as an argument and returns the minimum value in the list. If the passed list is empty, have the function return false. #For example minimum([1,2,3,4]) should return 1; minimum([-1,-2,-3]) should return -3. -# -#Maximum - Create a function that takes a list as an argument and returns the maximum value in the list. If the passed list is empty, have the function return false. #For example maximum([1,2,3,4]) should return 4; maximum([-1,-2,-3]) should return -1. +#Length - Create a function that takes a list as an argument and returns the length of the list. +# For example length([1,2,3,4]) should return 4 +def length(assortment): + result = len(assortment) + return result +print(length(numbers)) -#Ultimateaalyze - Create a function that takes a list as an argument and returns a dictionary that has the sumTotal, average, minimum, maximum ad length of the list. +#Minimum - Create a function that takes a list as an argument and returns the minimum value in the list. +# If the passed list is empty, have the function return false. +# #For example minimum([1,2,3,4]) should return 1; minimum([-1,-2,-3]) should return -3. +def minimum(assortment): + if len(assortment) < 1: + return False + else: + assortment.sort() + minimum = assortment[0] + return minimum +print(minimum(more_numbers)) -#ReverseList - Create a function that takes a list as a argument and return a list in a reversed order. Do this without creating a empty temporary list. For example #reverse([1,2,3,4]) should return [4,3,2,1]. This challenge is known to appear during basic technical interviews. +#Maximum - Create a function that takes a list as an argument and returns the maximum value in the list. +# If the passed list is empty, have the function return false. +# #For example maximum([1,2,3,4]) should return 4; maximum([-1,-2,-3]) should return -1. +def maximum(assortment): + if len(assortment) < 1: + return False + else: + assortment.sort(reverse=True) + maximum = assortment[0] + return maximum +print(maximum(more_numbers)) -#Ispalindrome- Given a string, write a python function to check if it is palindrome or not. A string is said to be palindrome if the reverse of the string is the same as string. For example, “radar” is a palindrome, but “radix” is not a palindrome. +#Ultimateaalyze - Create a function that takes a list as an argument and returns a dictionary that has the +# sumTotal, average, minimum, maximum and length of the list. +def ultAn(assortment): + ultAn={} + if len(assortment) < 1: + return False + else: + assortment.sort() + ultAn["minimum"] = assortment[0] + assortment.sort(reverse=True) + ultAn["maximum"] = assortment[0] + ultAn["length"] = len(assortment) + ultAn["sumTotal"] = 0 + for item in assortment: + ultAn["sumTotal"] += item + ultAn["average"] = (sum(assortment))/(len(assortment)) + return ultAn +print(ultAn(numbers)) + +#ReverseList - Create a function that takes a list as a argument and return a list in a reversed order. +# Do this without creating a empty temporary list. For example #reverse([1,2,3,4]) should return [4,3,2,1]. +# This challenge is known to appear during basic technical interviews. +def ReverseList(assortment): + assortment2 = assortment[::-1] + return assortment2 +print(ReverseList([1,2,4,6,5])) + +#Ispalindrome- Given a string, write a python function to check if it is palindrome or not. +# A string is said to be palindrome if the reverse of the string is the same as string. +# For example, “radar” is a palindrome, but “radix” is not a palindrome. +def Ispalindrome(assortment): + if assortment[::-1] == assortment[::1]: + return True + else: + return False +print(Ispalindrome("radar")) +print(Ispalindrome("radix")) #Fizzbuzz- Create a function that will print numbers from 1 to 100, with certain exceptions: #If the number is a multiple of 3, print “Fizz” instead of the number. #If the number is a multiple of 5, print “Buzz” instead of the number. #If the number is a multiple of 3 and 5, print “FizzBuzz” instead of the number. +def fizzBuzz(): + numbers = [] + for i in range (1,101): + if i % 3 == 0: + if i % 5 == 0: + numbers.append("FizzBuzz") + else: + numbers.append("Fizz") + elif i % 5 == 0: + numbers.append("Buzz") + else: + numbers.append(i) + return numbers +print(fizzBuzz()) -#Fibonacci- The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, #starting from 0 and 1. That is, +#Fibonacci- The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, +# such that each number is the sum of the two preceding ones, #starting from 0 and 1. That is, #F(0) = 0, F(1) = 1 #F(n) = F(n - 1) + F(n - 2), for n > 1. - #Create a function that accepts any number and will create a sequence based on the fibonacci sequence. \ No newline at end of file + #Create a function that accepts any number and will create a sequence based on the fibonacci sequence. +def F(n): + result = 0 + if n == 0: + result = 0 + elif n == 1: + result += 1 + elif n >1: + result = result + n*2-3 + else: + return "That's not a positive number. Give me a positive number." + return result +print(F(10)) From 52b52a90364266c8e9c91b22a9bbddd84556cbb8 Mon Sep 17 00:00:00 2001 From: Jeremiah Morelock Date: Mon, 12 Dec 2022 18:42:07 -0500 Subject: [PATCH 2/5] Add files via upload --- JMO_practice_12_12.py | 58 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 JMO_practice_12_12.py diff --git a/JMO_practice_12_12.py b/JMO_practice_12_12.py new file mode 100644 index 0000000..dda20a5 --- /dev/null +++ b/JMO_practice_12_12.py @@ -0,0 +1,58 @@ +#Consider the list: +prog_lang = [('Python', 3.8), + ('Java', 13), + ('JavaScript', 2019), + ('Scala', 2.13)] +#1. Sort the list by each language's version in ascending order. +ascend = sorted(prog_lang, key=lambda x: x[1]) +print(ascend) + +#2. Sort the list by the length of the name of each language in descending order. +dlname = sorted(prog_lang, key=lambda x: len(x[0]), reverse = True) +print(dlname) + +#3. Filter the list so that it only contains languages with 'a' in it. +a = list(filter(lambda x: 'a' in x[0], prog_lang)) +print(a) + +#4. Filter the list so that it only contains languages whose version is in integer form. +integers = list(filter(lambda x: isinstance(x[1], int), prog_lang)) +print(integers) + +#5. Transform the list so that it contains the tuples in the form, +# ("language in all lower case", length of the language string) +def newlist(old): + new = [] + new2 = [] + new3 = [] +# "new" gets the names, new2 gets the lengths: + for x in old: + new.append(x[0].lower()) + new2.append(len(str(x[0]))) +# new3 is made of tuples combining new and new2 at the same index + for i in range (len(new)): + new3.append((new[i], new2[i])) + return new3 +print(newlist(prog_lang)) + +#6. Generate a tuple in the form, ("All languages separated by commas", +# "All versions separated by commas") +def newtuple(old): + new = [] + newstring = '' + new2 = [] + new2string = '' + for x in old: + new.append(x[0]) + new2.append(x[1]) + for y in new: + newstring += y + newstring += ',' + newstring = newstring[:-1] + for z in new2: + new2string += str(z) + new2string += ',' + new2string = new2string[:-1] + new3 = newstring, new2string + return new3 +print(newtuple(prog_lang)) \ No newline at end of file From d8513b887940b14331c073f1fe2ec1c2c8fba00e Mon Sep 17 00:00:00 2001 From: Jeremiah Morelock Date: Mon, 12 Dec 2022 18:51:58 -0500 Subject: [PATCH 3/5] Update JMO_practice_12_12.py --- JMO_practice_12_12.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/JMO_practice_12_12.py b/JMO_practice_12_12.py index dda20a5..168a313 100644 --- a/JMO_practice_12_12.py +++ b/JMO_practice_12_12.py @@ -45,14 +45,17 @@ def newtuple(old): for x in old: new.append(x[0]) new2.append(x[1]) +#new holds the names, new2 holds the values for y in new: newstring += y newstring += ',' newstring = newstring[:-1] +#newstring takes everything in new, followed by a comma, into a string - then the last character is removed for z in new2: new2string += str(z) new2string += ',' +#new2string does the same as above, for new2 new2string = new2string[:-1] new3 = newstring, new2string return new3 -print(newtuple(prog_lang)) \ No newline at end of file +print(newtuple(prog_lang)) From 01aecc242c897885c78a1e6511e7f2049e574204 Mon Sep 17 00:00:00 2001 From: Jeremiah Morelock Date: Mon, 12 Dec 2022 19:23:51 -0500 Subject: [PATCH 4/5] Update 12_09_practice.py --- 12_09_practice.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/12_09_practice.py b/12_09_practice.py index 48eeb36..7dc10d2 100644 --- a/12_09_practice.py +++ b/12_09_practice.py @@ -138,14 +138,12 @@ def fizzBuzz(): #F(n) = F(n - 1) + F(n - 2), for n > 1. #Create a function that accepts any number and will create a sequence based on the fibonacci sequence. def F(n): - result = 0 - if n == 0: - result = 0 - elif n == 1: - result += 1 - elif n >1: - result = result + n*2-3 + if n == 1: + return 0 + elif n == 2: + return 1 + elif n > 2: + return F(n - 1) + F(n - 2) else: return "That's not a positive number. Give me a positive number." - return result print(F(10)) From e535a246c3e5359a81333e05826ed108b4e433f4 Mon Sep 17 00:00:00 2001 From: Jeremiah Morelock Date: Wed, 14 Dec 2022 05:52:47 -0500 Subject: [PATCH 5/5] Add files via upload --- closures_decorators_12_13.py | 101 +++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 closures_decorators_12_13.py diff --git a/closures_decorators_12_13.py b/closures_decorators_12_13.py new file mode 100644 index 0000000..51079f3 --- /dev/null +++ b/closures_decorators_12_13.py @@ -0,0 +1,101 @@ +def multiples_of(n): + def limit(k): + for i in range(n,k): + if i % n == 0: + yield i + return limit + +m3 = multiples_of(3) +m3_under30 = m3(30) +m7_under30 = multiples_of(7)(30) +print(type(m3_under30)) +print(*m3_under30) +print(*m7_under30) + +#1. make_upper – make every letter of a string returned from the decorated function uppercase. +def make_upper(up): + def wrapper(): + return up().upper() + return wrapper + +@make_upper +def hello_world(): + return 'hello young, good day!!' + +uppity = make_upper(hello_world) # output: HELLO YOUNG, GOOD DAY!! +print(uppity()) + +#2. print_func_name – print the name of the decorated function before executing the function. +def print_func_name(machine): + def wrapper(): + func_name = machine.__name__ + print(f"{func_name} is running...") + print(machine()) + return wrapper + +@print_func_name +def my_func(): + return('Python is fun!!') + +my_func() # output: my_func is running... + #Python is fun + +#3. give_name(name) – concatenate the given name at the end of a string +# returned from the decorated function. +def give_name(name): + def wrapper(yar): + print(yar(), name) + return wrapper +@give_name('Theresa') +def greeting(): + return 'Hello' + +#print(greeting()) # output: Hello Theresa + +#4. print_input_type – print a data type of the input argument before executing the +# decorated function. +def print_input_type(q): + def wrapper(p): + x = type(p) + print(f"The input data type is {x}") + activate = q(p) + return activate + return wrapper +@print_input_type +def square(n): + return n ** 2 + +print(square(3.5)) # output: The input data type is + #12.25 + +#5. check_return_type(return_type) – check if the return type of the +# decorated function is return_type and print the result before executing +# the function. +def check_return_type(return_type): + def wrapper(p): + x = type(p) + activate = return_type() + returned = type(activate) + if returned == x: + print("bingo") + else: + print("=========Error!!") + print(f"=========The return type is not {return_type}") + print(type(activate), p, return_type, returned) + + return p + return wrapper +@check_return_type(str) +def square(n): + return n ** 2 + +print(square(6)) # output: =========Error!! + #=========The return type is NOT + #36 + +@check_return_type(float) +def square(n): + return n ** 2 + +print(square(2.9)) # output: The return type is + #8.41 \ No newline at end of file