diff --git a/Python_Exercises.py b/Python_Exercises.py index d7e6135..03e08cd 100644 --- a/Python_Exercises.py +++ b/Python_Exercises.py @@ -4,9 +4,13 @@ # Answer the questions or complete the tasks outlined in bold below. +from re import L + + def power(a,b): # ** What is 7 to the power of 4?** + return a**b return None @@ -14,13 +18,14 @@ def power(a,b): def split_str(s): - # ** Split this string:** +# ** Split this string:** # # s = "Hi there Sam!" # # **into a list. ** + l1=s.split() - return None + return l1 def format(planet,diameter): @@ -33,8 +38,9 @@ def format(planet,diameter): # ** Use .format() to print the following string: ** # # The diameter of Earth is 12742 kilometers. + l="The diameter of {} is {} kilometers.".format(planet,diameter) - return None + return l @@ -43,8 +49,9 @@ 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] + s= lst[3][1][2][0] - return None + return s def dictionary(d): @@ -52,9 +59,9 @@ 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']}]}]} + s=d['k1'][3]['tricky'][3]['target'][3] - - return None + return s def subjective(): @@ -62,7 +69,7 @@ def subjective(): # ** What is the main difference between a tuple and a list? ** # Tuple is _______ - return None + return "immutable" @@ -74,22 +81,28 @@ def domainGet(email): # user@domain.com # # **So for example, passing "user@domain.com" would return: domain.com** + l=len(email) + i=email.index('@') + s=email[i+1:l] - return None + return s 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=st.find('dog') + t=flag!=-1 - return None + return t def countDog(st): # ** Create a function that counts the number of times the word "dog" occurs in a string. Again ignore edge cases. ** + count=st.count('dog') - return None + return count @@ -102,8 +115,9 @@ def lambdafunc(seq): # **should be filtered down to:** # # ['soup','salad'] + out=filter(lambda var : var[0]=='s',seq) - return None + return list(out) def caught_speeding(speed, is_birthday): @@ -114,8 +128,21 @@ 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. ** - - return None + if(is_birthday==True): + if(speed<=65): + s= "No Ticket" + elif(speed<=85): + s= "Small Ticket" + else: + s= "Big Ticket" + else: + if(speed<=60): + s= "No Ticket" + elif(speed<=80): + s= "Small Ticket" + else: + s= "Big Ticket" + return s ## Numpy Exercises @@ -128,8 +155,10 @@ def create_arr_of_fives(): #### Create an array of 10 fives #### Convert your output into list #### e.g return list(arr) + array=np.ones(10)*5 + lis=list(array) - return None + return lis @@ -138,8 +167,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 + array=np.arange(10,51,2) + lis=list(array) + return lis @@ -148,8 +178,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,1).reshape(3,3) + lis=arr.tolist() + return lis @@ -158,8 +189,8 @@ 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) - - return None + arr=np.linspace(0,1,20) + return list(arr) @@ -168,8 +199,8 @@ 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.around(np.linspace(0.01,1.,100),decimals=2).reshape(10,10) + return arr.tolist() @@ -189,8 +220,10 @@ def slices_1(): # array([[12, 13, 14, 15], # [17, 18, 19, 20], # [22, 23, 24, 25]]) + slice=arr[2:5,1:5] + - return None + return slice.tolist() @@ -210,8 +243,9 @@ def slices_2(): # array([[ 2], # [ 7], # [12]]) + slice=arr[0:3,1:2] - return None + return slice.tolist() @@ -230,8 +264,9 @@ def slices_3(): ### e.g return (arr).tolist() # array([[16, 17, 18, 19, 20], # [21, 22, 23, 24, 25]]) + slice=arr[3:5] - return None + return slice.tolist() # Great job! diff --git a/__pycache__/Python_Exercises.cpython-38.pyc b/__pycache__/Python_Exercises.cpython-38.pyc new file mode 100644 index 0000000..7d034bd Binary files /dev/null and b/__pycache__/Python_Exercises.cpython-38.pyc differ