From d28c9c1deb5c58d2dfbe75d9c24f6f59d2d89874 Mon Sep 17 00:00:00 2001 From: jchen033 <118309600+jchen033@users.noreply.github.com> Date: Fri, 9 Dec 2022 12:12:02 -0500 Subject: [PATCH 1/3] Update 12_09_practice.py --- 12_09_practice.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/12_09_practice.py b/12_09_practice.py index 7914563..080fb8f 100644 --- a/12_09_practice.py +++ b/12_09_practice.py @@ -1,5 +1,5 @@ #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]. - +Hi i am jing #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). #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 @@ -26,4 +26,4 @@ #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. From 649fba7ed3fce2e2a552feef3971863c46470736 Mon Sep 17 00:00:00 2001 From: Jing Chen Date: Sun, 11 Dec 2022 18:13:49 -0500 Subject: [PATCH 2/3] first commit --- 12_09_practice.py | 106 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 2 deletions(-) diff --git a/12_09_practice.py b/12_09_practice.py index 080fb8f..75747d7 100644 --- a/12_09_practice.py +++ b/12_09_practice.py @@ -1,29 +1,131 @@ +#!/usr/bin.env python3 + #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]. -Hi i am jing +def biggie_size(list): + for i in range(len(list)): + if list[i]>0: + list[i] = 'big' + return list + +#print(biggie_size([-1,3,4,-5])) + #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 count_positive(list): + count = 0 + for i in range(len(list)): + if list[i] > 0: + count += 1 + list[len(list)-1] = count + return list +#print(count_positive([-1,1,1,1])) #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(list): + total = 0 + for i in list: + total += i + return total +#print(SumTotal([1,2,3,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(list): + total = 0 + count = 0 + for i in list: + total += i + count += 1 + return total / count +#print(average([1,2,3,4])) #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(list): + length = 0 + for i in list: + length += 1 + return length +#print(length([1,2,3,4])) #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(list=False): + if len(list) > 0: + min = list[0] + for i in range(1,len(list)): + if min > list[i]: + min = list[i] + + return min + return False +#print(minimum([1,2,3,4])) +#print(minimum([-1,-2,-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. +def maxium(list = False): + if len(list) > 0: + max = list[0] + for i in range(1,len(list)): + if max < list[i]: + max = list[i] + return max + return False +#print(maxium([1,2,3,4])) #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. +def ultimatealyze(list): + return {'sumTotal':SumTotal(list),'average':average(list),'minimum':minimum(list),'maxium':maxium(list), 'length':length(list)} +#print(ultimatealyze([1,2,3,4])) #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(list): + + for i in range(len(list)//2): + temp = list[i] + list[i] = list[len(list)-1-i] + list[len(list)-1-i] = temp + return list + +#print(reverseList([1,2,3,4])) #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(string): + for i in range(len(string)//2): + if string[i] != string[len(string)-1-i]: + return False + return True +#print(ispalindrome('radar')) #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(): + for i in range(1,101): + if i%3==0 and i%5==0: + print('FizzBuzz') + elif i%3==0: + print('Fizz') + elif i%5==0: + print('Buzz') + else: + print(i) +#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, #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. + #Create a function that accepts any number and will create a sequence based on the fibonacci sequence +def fibonacci(n): + f0 = 0 + f1 = 1 + count = 0 + seq = [] + if n < 2: + return 1 + while count Date: Tue, 13 Dec 2022 09:24:30 -0500 Subject: [PATCH 3/3] Second commit --- 12_12_practice.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 12_12_practice.py diff --git a/12_12_practice.py b/12_12_practice.py new file mode 100644 index 0000000..009f0db --- /dev/null +++ b/12_12_practice.py @@ -0,0 +1,54 @@ +#! /usr/bin.env python3 +from functools import reduce +# Generator exercises +#1 +def primes_gen(): + n = 2 + while True: + for i in range(2,n): + if n % i ==0: + break + else: + yield n + n += 1 + +gen = primes_gen() +for _ in range(10): + print(next(gen), end=' ') + +#2 +def unique_letters(word): + unique = [] + for i in word: + if i not in unique: + unique.append(i) + return unique +for letter in unique_letters('hello'): + print(letter, end = ' ') + +# lambda Exercise +prog_lang = [('Python',3.8),('Java',13),('JavaScript',2019),('Scala', 2.13)] + +#1 +sort_lambda = sorted(prog_lang, key=lambda x: x[1], reverse=False) +print(sort_lambda) + +#2 +sort_length = sorted(prog_lang, key=lambda x:len(x[0]), reverse=True) +print(sort_length) + +#3 +filter_a = list(filter(lambda x: 'a' in x[0],prog_lang)) +print(filter_a) + +#4 +filter_int = list(filter(lambda x: isinstance(x[1],int), prog_lang)) +print(filter_int) + +#5 +map_pro = tuple(map(lambda x: (x[0].lower(),len(x[0])), prog_lang)) +print(map_pro) + +#6 +separated = tuple(reduce(lambda x,y: ((str(x[0])+',' +str(y[0])),(str(x[1])+','+str(y[1]))), prog_lang)) +print(separated)