diff --git a/Python_Exercises.py b/Python_Exercises.py index d7e6135..e17402e 100644 --- a/Python_Exercises.py +++ b/Python_Exercises.py @@ -3,12 +3,13 @@ # Answer the questions or complete the tasks outlined in bold below. +import numpy as np + def power(a,b): - # ** What is 7 to the power of 4?** - - return None + # a = 7 and b = 4 + return a**b @@ -20,21 +21,23 @@ def split_str(s): # # **into a list. ** - return None + return list(s.split()) 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. +s = "The diameter of {} is {} kilometers." - return None + + return s.format(planet, diameter) @@ -44,7 +47,7 @@ def indexing(lst): #lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7] - return None + return lst[3][1][2][0] def dictionary(d): @@ -54,7 +57,7 @@ def dictionary(d): # d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]} - return None + return d['k1'][3]['tricky'][3]['target'] def subjective(): @@ -62,7 +65,7 @@ def subjective(): # ** What is the main difference between a tuple and a list? ** # Tuple is _______ - return None + return "Immutable" @@ -74,22 +77,26 @@ def domainGet(email): # user@domain.com # # **So for example, passing "user@domain.com" would return: domain.com** +s = "user@domain.com" - return None + return s.split('@')[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. ** - - return None +dog = st.lower().split() +if "dog" in dog: + return True +else: + return False 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 + return st.lower().split().count("dog") @@ -103,7 +110,7 @@ def lambdafunc(seq): # # ['soup','salad'] - return None + return list(filter(lamba i:i[0]=='s',seq) def caught_speeding(speed, is_birthday): @@ -112,10 +119,25 @@ def caught_speeding(speed, is_birthday): # **You are driving a little too fast, and a police officer stops you. Write a function # to return one of 3 possible results: "No ticket", "Small ticket", or "Big Ticket". # 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 +# 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. ** - - return None + if is_birthday == True: + if speed > 85: + return "Big Ticket" + elif speed > 65 and speed <= 85: + return "Small ticket" + else + return "No ticket" + + else: + if speed > 80: + return "Big Ticket" + elif speed > 60 and speed <= 80: + return "Small ticket" + else + return "No ticket" + ## Numpy Exercises @@ -128,8 +150,10 @@ def create_arr_of_fives(): #### Create an array of 10 fives #### Convert your output into list #### e.g return list(arr) + + arr = np.array([5,5,5,5,5,5,5,5,5,5]) - return None + return list(arr) @@ -138,8 +162,9 @@ 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) +l = list(arr) + return l @@ -148,8 +173,9 @@ 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 + arr = np.arange(0,9) + arr = arr.reshape(3,3) + return arr.tolist() @@ -158,8 +184,9 @@ def linear_space(): ### Create an array of 20 linearly spaced points between 0 and 1 ### Convert your output into list ### e.g return list(arr) + arr = np.linspace(0,1,20) - return None + return list(arr) @@ -168,8 +195,11 @@ def decimal_mat(): ### Create an array of size 10*10 consisting of numbers from 0.01 to 1 ### Convert your output into list ### e.g return (arr).tolist() - - return None + arr = np.zeros((10,10)) + for a in range(0,10): + for b in range(0,10): + arr[a][b]=(a*10 + b + 1)/100 + return arr.tolist() @@ -189,8 +219,8 @@ def slices_1(): # array([[12, 13, 14, 15], # [17, 18, 19, 20], # [22, 23, 24, 25]]) - - return None + arr = arr[2:, 1:] + return arr.tolist() @@ -211,7 +241,9 @@ def slices_2(): # [ 7], # [12]]) - return None + arr = arr[:3, 1:] + return arr.tolist() + @@ -231,7 +263,9 @@ def slices_3(): # array([[16, 17, 18, 19, 20], # [21, 22, 23, 24, 25]]) - return None + arr = arr[3:] + return arr.tolist() + # Great job!