Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Python_Exercises.py #152

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 45 additions & 29 deletions Python_Exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
def power(a,b):

# ** What is 7 to the power of 4?**

return None
return a**b



Expand All @@ -19,8 +18,7 @@ def split_str(s):
# s = "Hi there Sam!"
#
# **into a list. **

return None
return s.split()


def format(planet,diameter):
Expand All @@ -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



Expand All @@ -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):
Expand All @@ -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



Expand All @@ -75,21 +73,25 @@ def domainGet(email):
#
# **So for example, passing "[email protected]" 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




Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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)



Expand All @@ -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)



Expand All @@ -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()



Expand All @@ -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))



Expand All @@ -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()



Expand All @@ -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()



Expand All @@ -211,7 +227,7 @@ def slices_2():
# [ 7],
# [12]])

return None
return arr[0:3,1:2].tolist()



Expand All @@ -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!