From 6e0ec78d2e737e42186a1b625d8b6b487ce6f97a Mon Sep 17 00:00:00 2001 From: mridulr2003 <96919276+mridulr2003@users.noreply.github.com> Date: Mon, 7 Feb 2022 16:42:36 +0530 Subject: [PATCH 1/5] Update Python_Exercises.py --- Python_Exercises.py | 88 +++++++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 34 deletions(-) diff --git a/Python_Exercises.py b/Python_Exercises.py index d7e6135..5d550b3 100644 --- a/Python_Exercises.py +++ b/Python_Exercises.py @@ -7,32 +7,32 @@ def power(a,b): # ** What is 7 to the power of 4?** - - return None + return a**b def split_str(s): # ** Split this string:** -# -# s = "Hi there Sam!" +# + s = "Hi there Sam!" # # **into a list. ** - - return None + lst = s.spilt() + return lst def format(planet,diameter): # ** Given the variables:** # -# planet = "Earth" -# diameter = 12742 + planet = "Earth" + diameter = 12742 # # ** Use .format() to print the following string: ** # # The diameter of Earth is 12742 kilometers. + print("The diameter of {} is {} kilometers".format(planet,diameter)) return None @@ -42,7 +42,8 @@ def indexing(lst): # ** Given this nested list, use indexing to grab the word "hello" ** -#lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7] + lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7] + print(lst[3][1][1][2]) return None @@ -51,8 +52,8 @@ def dictionary(d): # ** Given this nested dictionary grab the word "hello". Be prepared, this will be annoying/tricky ** -# d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]} - + d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]} + print(d['k1'][3]['tricky'][3]['target'][3]) return None @@ -60,8 +61,8 @@ def dictionary(d): def subjective(): # ** What is the main difference between a tuple and a list? ** -# Tuple is _______ - +# Tuple is immutable whereas a list is mutable + print("Tuple is immutable whereas list is mutable") return None @@ -69,27 +70,38 @@ def subjective(): def domainGet(email): - # ** Create a function that grabs the email website domain from a string in the form: ** + # **Create a function that grabs the email website domain from a string in the form: ** # # user@domain.com # # **So for example, passing "user@domain.com" would return: domain.com** - + s = input() + i = s.find('@') + print(s[i:s.length()]) return None def findDog(st): # ** Create a basic function that returns True if the word 'dog' is contained in the input string. Don't worry about edge cases like a punctuation being attached to the word dog, but do account for capitalization. ** - - return None + flag = 0 + for i in range(0,s.length()-3): + if (s[i:i+3] == 'dog'): + flag = 1 + return True + + if flag == 0: + return None def countDog(st): # ** Create a function that counts the number of times the word "dog" occurs in a string. Again ignore edge cases. ** - - return None + count = 0 + for i in range(0,s.length()-3): + if (s[i:i+3] == 'dog'): + count = count + 1 + return count @@ -97,13 +109,17 @@ def lambdafunc(seq): # ** Use lambda expressions and the filter() function to filter out words from a list that don't start with the letter 's'. For example:** # -# seq = ['soup','dog','salad','cat','great'] + seq = ['soup','dog','salad','cat','great'] + a = [] + for i in range (0,seq.length()): + if i[0] == 's': + a.append(i) # # **should be filtered down to:** # # ['soup','salad'] - return None + return a def caught_speeding(speed, is_birthday): @@ -114,7 +130,13 @@ def caught_speeding(speed, is_birthday): # If your speed is 60 or less, the result is "No Ticket". If speed is between 61 # and 80 inclusive, the result is "Small Ticket". If speed is 81 or more, the result is "Big Ticket". Unless it is your birthday (encoded as a boolean value in the parameters of the function) -- on your birthday, your speed can be 5 higher in all # cases. ** - + speed = int(input()) + if speed <=60: + print("No ticket") + elif 61 <= speed <=80: + print("Small ticket") + else: + print("Big Ticket") return None @@ -128,8 +150,8 @@ def create_arr_of_fives(): #### Create an array of 10 fives #### Convert your output into list #### e.g return list(arr) - - return None + arr=np.full(10,5) + return list(arr) @@ -138,8 +160,8 @@ def even_num(): ### Create an array of all the even integers from 10 to 50 ### Convert your output into list ### e.g return list(arr) - - return None + arr=np.arange(10,51,2) + return list(arr) @@ -148,9 +170,7 @@ def create_matrix(): ### Create a 3x3 matrix with values ranging from 0 to 8 ### Convert your output into list ### e.g return (arr).tolist() - - return None - + return np.arange(9).reshape(3,3).tolist() def linear_space(): @@ -159,7 +179,7 @@ def linear_space(): ### Convert your output into list ### e.g return list(arr) - return None + return list(np.linspace(0,1,20)) @@ -169,7 +189,7 @@ def decimal_mat(): ### Convert your output into list ### e.g return (arr).tolist() - return None + return np.around(np.linspace(0.01,1.,100),decimals=2).reshape(10,10).tolist() @@ -190,7 +210,7 @@ def slices_1(): # [17, 18, 19, 20], # [22, 23, 24, 25]]) - return None + return arr[2: ,1: ].tolist() @@ -211,7 +231,7 @@ def slices_2(): # [ 7], # [12]]) - return None + return (arr[ :3,1:2].tolist()) @@ -231,7 +251,7 @@ def slices_3(): # array([[16, 17, 18, 19, 20], # [21, 22, 23, 24, 25]]) - return None + return arr[3: , ].tolist() # Great job! From c09d1ec147220a930a8f6cd64a1318fcced66d1f Mon Sep 17 00:00:00 2001 From: mridulr2003 <96919276+mridulr2003@users.noreply.github.com> Date: Mon, 7 Feb 2022 17:35:58 +0530 Subject: [PATCH 2/5] Update Python_Exercises.py Solutions --- Python_Exercises.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python_Exercises.py b/Python_Exercises.py index 5d550b3..06bf614 100644 --- a/Python_Exercises.py +++ b/Python_Exercises.py @@ -245,7 +245,7 @@ def slices_3(): # [16, 17, 18, 19, 20], # [21, 22, 23, 24, 25]]) - # Write a code to slice this given array + # Write a code to slice this given array ### Convert your output into list ### e.g return (arr).tolist() # array([[16, 17, 18, 19, 20], From 90140a8b3b8c938bb45623c112f7fb0d7c9e5ea8 Mon Sep 17 00:00:00 2001 From: mridulr2003 <96919276+mridulr2003@users.noreply.github.com> Date: Mon, 7 Feb 2022 20:29:29 +0530 Subject: [PATCH 3/5] Update Python_Exercises.py --- Python_Exercises.py | 65 +++++++++++++++++---------------------------- 1 file changed, 24 insertions(+), 41 deletions(-) diff --git a/Python_Exercises.py b/Python_Exercises.py index 06bf614..e171fb7 100644 --- a/Python_Exercises.py +++ b/Python_Exercises.py @@ -15,11 +15,9 @@ def split_str(s): # ** Split this string:** # - s = "Hi there Sam!" -# -# **into a list. ** - lst = s.spilt() - return lst + s = "Hi there Sam!" + my_list=list(s.split()) + return my_list def format(planet,diameter): @@ -32,9 +30,8 @@ def format(planet,diameter): # ** Use .format() to print the following string: ** # # The diameter of Earth is 12742 kilometers. - print("The diameter of {} is {} kilometers".format(planet,diameter)) + return "The diameter of {} is {} kilometers".format(planet,diameter) - return None @@ -43,9 +40,7 @@ def indexing(lst): # ** Given this nested list, use indexing to grab the word "hello" ** lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7] - print(lst[3][1][1][2]) - - return None + return lst[3][1][2][0] def dictionary(d): @@ -53,17 +48,15 @@ def dictionary(d): # ** Given this nested dictionary grab the word "hello". Be prepared, this will be annoying/tricky ** d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]} - print(d['k1'][3]['tricky'][3]['target'][3]) + return d['k1'][3]['tricky'][3]['target'][3] - return None def subjective(): # ** What is the main difference between a tuple and a list? ** # Tuple is immutable whereas a list is mutable - print("Tuple is immutable whereas list is mutable") - return None + return "immutable" @@ -75,33 +68,23 @@ def domainGet(email): # user@domain.com # # **So for example, passing "user@domain.com" would return: domain.com** - s = input() - i = s.find('@') - print(s[i:s.length()]) - return None + my_list=list(email.split("@")) + return str(my_list[1]) def findDog(st): # ** Create a basic function that returns True if the word 'dog' is contained in the input string. Don't worry about edge cases like a punctuation being attached to the word dog, but do account for capitalization. ** - flag = 0 - for i in range(0,s.length()-3): - if (s[i:i+3] == 'dog'): - flag = 1 - return True - - if flag == 0: - return None + return "dog" in st.lower() + def countDog(st): # ** Create a function that counts the number of times the word "dog" occurs in a string. Again ignore edge cases. ** count = 0 - for i in range(0,s.length()-3): - if (s[i:i+3] == 'dog'): - count = count + 1 - return count + return len(st.lower().split("dog"))-1 + @@ -111,15 +94,12 @@ def lambdafunc(seq): # seq = ['soup','dog','salad','cat','great'] a = [] - for i in range (0,seq.length()): - if i[0] == 's': - a.append(i) + return list(filter(lambda var:var[0]=='s',(seq))) # # **should be filtered down to:** # # ['soup','salad'] - return a def caught_speeding(speed, is_birthday): @@ -130,14 +110,17 @@ def caught_speeding(speed, is_birthday): # If your speed is 60 or less, the result is "No Ticket". If speed is between 61 # and 80 inclusive, the result is "Small Ticket". If speed is 81 or more, the result is "Big Ticket". Unless it is your birthday (encoded as a boolean value in the parameters of the function) -- on your birthday, your speed can be 5 higher in all # cases. ** - speed = int(input()) - if speed <=60: - print("No ticket") - elif 61 <= speed <=80: - print("Small ticket") + if (is_birthday): + limits =[65,85] + else: + limits =[60,80] + + if speed<=limits[0]: + return "No Ticket" + elif speed<=limits[1]: + return "Small Ticket" else: - print("Big Ticket") - return None + return "Big Ticket" ## Numpy Exercises From 228cd93bbba4ede75dfbd5ff6c79e0223b8114fe Mon Sep 17 00:00:00 2001 From: mridulr2003 <96919276+mridulr2003@users.noreply.github.com> Date: Mon, 7 Feb 2022 20:35:31 +0530 Subject: [PATCH 4/5] Updated solution Updated Solution --- Python_Exercises.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python_Exercises.py b/Python_Exercises.py index e171fb7..00d0ebc 100644 --- a/Python_Exercises.py +++ b/Python_Exercises.py @@ -96,7 +96,7 @@ def lambdafunc(seq): a = [] return list(filter(lambda var:var[0]=='s',(seq))) # -# **should be filtered down to:** +# **should be filtered down to: ** # # ['soup','salad'] From 48163e19a40508c14120e60eb4e3391198f348ee Mon Sep 17 00:00:00 2001 From: mridulr2003 <96919276+mridulr2003@users.noreply.github.com> Date: Mon, 7 Feb 2022 20:45:14 +0530 Subject: [PATCH 5/5] Update Python_Exercises.py --- Python_Exercises.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Python_Exercises.py b/Python_Exercises.py index 00d0ebc..8c74871 100644 --- a/Python_Exercises.py +++ b/Python_Exercises.py @@ -30,7 +30,7 @@ def format(planet,diameter): # ** Use .format() to print the following string: ** # # The diameter of Earth is 12742 kilometers. - return "The diameter of {} is {} kilometers".format(planet,diameter) + return "The diameter of {} is {} kilometers.".format(planet,diameter) @@ -96,7 +96,7 @@ def lambdafunc(seq): a = [] return list(filter(lambda var:var[0]=='s',(seq))) # -# **should be filtered down to: ** +# **should be filtered down to:** # # ['soup','salad']