diff --git a/Python_Exercises.py b/Python_Exercises.py index d7e6135..028a128 100644 --- a/Python_Exercises.py +++ b/Python_Exercises.py @@ -6,9 +6,9 @@ def power(a,b): - # ** What is 7 to the power of 4?** + # ** What is 7 to the power of 4? - return None + return a**b @@ -19,22 +19,22 @@ def split_str(s): # s = "Hi there Sam!" # # **into a list. ** - - return None + + return 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. +# print("The diameter of {} is {} kilometers.".format(planet,diameter)) - return None + return "The diameter of {} is {} kilometers.".format(planet,diameter) @@ -42,27 +42,29 @@ 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][2][0]) - return None + return lst[3][1][2][0] 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 + return d['k1'][3]['tricky'][3]['target'][3] def subjective(): # ** What is the main difference between a tuple and a list? ** -# Tuple is _______ +# print("Tuple is immutable") - return None + return "immutable" @@ -74,22 +76,26 @@ def domainGet(email): # user@domain.com # # **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 + t=st.split() + return 'dog' in t 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 + t=st.split() + count=0 + for item in t: + if item=='dog': + count=count+1 + return count @@ -97,13 +103,15 @@ 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'] # # **should be filtered down to:** # # ['soup','salad'] - return None + t=list(filter(lambda item: 's' in item, seq)) + + return t def caught_speeding(speed, is_birthday): @@ -114,8 +122,23 @@ 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. ** + if is_birthday==True: + if speed<=65: + return "No Ticket" + elif speed<=85: + return "Small Ticket" + else: + return "Big Ticket" + else: + if speed<=60: + return "No Ticket" + elif speed<=80: + return "Small Ticket" + else: + return "Big Ticket" - return None + + ## Numpy Exercises @@ -126,50 +149,57 @@ def caught_speeding(speed, is_birthday): def create_arr_of_fives(): #### Create an array of 10 fives + l=np.full((10,1),5) #### Convert your output into list #### e.g return list(arr) - return None + return list(l) def even_num(): ### Create an array of all the even integers from 10 to 50 + l=np.arange(10,51,2) ### Convert your output into list ### e.g return list(arr) - return None + return list(l) def create_matrix(): ### Create a 3x3 matrix with values ranging from 0 to 8 + t=np.arange(0,8+1) + l=t.reshape(3,3) ### Convert your output into list ### e.g return (arr).tolist() - return None + return l.tolist() def linear_space(): ### Create an array of 20 linearly spaced points between 0 and 1 + l=np.linspace(0,1,20) ### Convert your output into list ### e.g return list(arr) - return None + return list(l) def decimal_mat(): ### Create an array of size 10*10 consisting of numbers from 0.01 to 1 + l=np.linspace(0.01,1,100).reshape(10,10) + x=np.round(l,decimals=2) ### Convert your output into list ### e.g return (arr).tolist() - return None + return x.tolist() @@ -190,7 +220,9 @@ def slices_1(): # [17, 18, 19, 20], # [22, 23, 24, 25]]) - return None + l=arr[2:,1:] + + return l.tolist() @@ -210,8 +242,9 @@ def slices_2(): # array([[ 2], # [ 7], # [12]]) + l=arr[:3,1:2] - return None + return l.tolist() @@ -230,8 +263,9 @@ def slices_3(): ### e.g return (arr).tolist() # array([[16, 17, 18, 19, 20], # [21, 22, 23, 24, 25]]) + t=arr[3:] - return None + return t.tolist() # Great job!