diff --git a/Python_Exercises.py b/Python_Exercises.py index d7e6135..acfdff5 100644 --- a/Python_Exercises.py +++ b/Python_Exercises.py @@ -7,8 +7,7 @@ def power(a,b): # ** What is 7 to the power of 4?** - - return None + return a**b @@ -19,8 +18,7 @@ def split_str(s): # s = "Hi there Sam!" # # **into a list. ** - - return None + return s.split() def format(planet,diameter): @@ -33,8 +31,8 @@ def format(planet,diameter): # ** Use .format() to print the following string: ** # # The diameter of Earth is 12742 kilometers. - - return None + s="The diameter of {} is {} kilometers.".format(planet,diameter) + return s @@ -44,7 +42,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,16 +52,16 @@ 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'][3] def subjective(): # ** What is the main difference between a tuple and a list? ** # Tuple is _______ + s="immutable" - return None - + return s @@ -75,21 +73,25 @@ def domainGet(email): # # **So for example, passing "user@domain.com" would return: domain.com** - return None + return email.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 + return 'dog' in st.lower().split() 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 word in st.lower().split(): + if word == 'dog': + count += 1 + return count + @@ -102,8 +104,9 @@ def lambdafunc(seq): # **should be filtered down to:** # # ['soup','salad'] - - return None + + l=list(filter(lambda word: word[0]=='s',seq)) + return l def caught_speeding(speed, is_birthday): @@ -115,7 +118,17 @@ def caught_speeding(speed, is_birthday): # 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: + speeding = speed - 5 + else: + speeding = speed + + if speeding > 80: + return 'Big Ticket' + elif speeding > 60: + return 'Small Ticket' + else: + return 'No Ticket' ## Numpy Exercises @@ -128,8 +141,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.ones(10)*5 + return list(arr) @@ -138,8 +151,10 @@ 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 + x = np.arange(10,51) + y = (x%2 == 0) + arr = x[y] + return list(arr) @@ -148,8 +163,8 @@ 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).reshape((3,3)) + return arr.tolist() @@ -159,7 +174,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 +184,8 @@ def decimal_mat(): ### Convert your output into list ### e.g return (arr).tolist() - return None + arr = np.arange(1,101).reshape(10,10)/100 + return arr.tolist() @@ -189,8 +205,8 @@ def slices_1(): # array([[12, 13, 14, 15], # [17, 18, 19, 20], # [22, 23, 24, 25]]) - - return None + + return arr[2:,1:].tolist() @@ -211,7 +227,7 @@ def slices_2(): # [ 7], # [12]]) - return None + return arr[0:3,1:2].tolist() @@ -231,7 +247,7 @@ def slices_3(): # array([[16, 17, 18, 19, 20], # [21, 22, 23, 24, 25]]) - return None + return arr[3:].tolist() # Great job!