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

Exercise solutions #147

Open
wants to merge 1 commit 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
96 changes: 65 additions & 31 deletions Python_Exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand All @@ -19,50 +19,52 @@ 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)



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"



Expand All @@ -74,36 +76,42 @@ def domainGet(email):
# [email protected]
#
# **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
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



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



Expand All @@ -190,7 +220,9 @@ def slices_1():
# [17, 18, 19, 20],
# [22, 23, 24, 25]])

return None
l=arr[2:,1:]

return l.tolist()



Expand All @@ -210,8 +242,9 @@ def slices_2():
# array([[ 2],
# [ 7],
# [12]])
l=arr[:3,1:2]

return None
return l.tolist()



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