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

Made changes in Python_Exercises.py #150

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
194 changes: 119 additions & 75 deletions Python_Exercises.py
Original file line number Diff line number Diff line change
@@ -1,121 +1,145 @@

# ## Exercises

# Answer the questions or complete the tasks outlined in bold below.


def power(a,b):

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

return None
x = a**b

#7 to power 4 will be returned value of power(7, 4)
return x



def split_str(s):

# ** Split this string:**
#
# s = "Hi there Sam!"
#
#
s = "Hi there Sam!"

#
# **into a list. **
split = s.split()

return None
return 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.

return None
str = "The diameter of {} is {} kilometers.".format(planet, diameter)
return str



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]
hello = lst[3][1][2][0]

return None
return hello


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']}]}]}
# ** 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']}]}]}

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 _______

return None
return "immutable"




def domainGet(email):

# ** Create a function that grabs the email website domain from a string in the form: **
#
#
# [email protected]
#
#
# **So for example, passing "[email protected]" would return: domain.com**
for i in range(0, len(email) - 1):
if (email[i] == "@"):
str = email[i+1:]
break


return None
return str


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
# ** 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. **
if (('dog'.upper() in st) or ('dog'.lower() in st)):
return True
else:
return False


def countDog(st):

# ** Create a function that counts the number of times the word "dog" occurs in a string. Again ignore edge cases. **
x = st.split()

return None
return x.count('dog')



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']
#
#
# **should be filtered down to:**
#
#
# ['soup','salad']
check = lambda x: x[0] == 's'
filtered = filter(check, seq)

return None
return list(filtered)


def caught_speeding(speed, is_birthday):


# **You are driving a little too fast, and a police officer stops you. Write a function
# to return one of 3 possible results: "No ticket", "Small ticket", or "Big Ticket".
# 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
# to return one of 3 possible results: "No ticket", "Small ticket", or "Big Ticket".
# 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):
speed -= 5
else:
pass

if (speed <= 60 and speed >= 0):
return "No Ticket"
elif (speed >= 61 and speed <= 80):
return "Small Ticket"
elif (speed >= 81):
return "Big Ticket"


## Numpy Exercises
Expand All @@ -124,114 +148,134 @@ def caught_speeding(speed, is_birthday):


def create_arr_of_fives():

#### Create an array of 10 fives
#### Convert your output into list
#### e.g return list(arr)
#### Convert your output into list
#### e.g return list(arr)

return None
arr = np.array([5, 5, 5, 5, 5, 5, 5, 5, 5, 5])

return list(arr)


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)
### Convert your output into list
### e.g return list(arr)

x = np.array([x for x in range(10, 51) if x%2 == 0])

return None
return list(x)



def create_matrix():

### Create a 3x3 matrix with values ranging from 0 to 8
### Convert your output into list
### Convert your output into list
### e.g return (arr).tolist()

return None
x = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])

return x.tolist()



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)
### Convert your output into list
### e.g return list(arr)

x = np.linspace(0, 1, 20)

return None
return list(x)



def decimal_mat():

### Create an array of size 10*10 consisting of numbers from 0.01 to 1
### Convert your output into list
### Convert your output into list
### e.g return (arr).tolist()

return None
l = []
for i in range(0, 10):
l.append([j/100 for j in range(i*10 + 1, i*10 + 11)])

x = np.array(l)

return x.tolist()



def slices_1():

# This is a given array
arr = np.arange(1,26).reshape(5,5)
arr = np.arange(1,26).reshape(5,5)
# array([[ 1, 2, 3, 4, 5],
# [ 6, 7, 8, 9, 10],
# [11, 12, 13, 14, 15],
# [16, 17, 18, 19, 20],
# [21, 22, 23, 24, 25]])

# Write a code to slice this given array
### Convert your output into list
### Convert your output into list
### e.g return (arr).tolist()
# array([[12, 13, 14, 15],
# [17, 18, 19, 20],
# [22, 23, 24, 25]])

return None

sliced = arr[2:5, 1:5]

return sliced.tolist()



def slices_2():

# This is a given array
arr = np.arange(1,26).reshape(5,5)
arr = np.arange(1,26).reshape(5,5)
# array([[ 1, 2, 3, 4, 5],
# [ 6, 7, 8, 9, 10],
# [11, 12, 13, 14, 15],
# [16, 17, 18, 19, 20],
# [21, 22, 23, 24, 25]])

# Write a code to slice this given array
### Convert your output into list
### Convert your output into list
### e.g return (arr).tolist()
# array([[ 2],
# [ 7],
# [12]])

return None
sliced = arr[0:3, 1:2]

return sliced.tolist()



def slices_3():

# This is a given array
arr = np.arange(1,26).reshape(5,5)
arr = np.arange(1,26).reshape(5,5)
# array([[ 1, 2, 3, 4, 5],
# [ 6, 7, 8, 9, 10],
# [11, 12, 13, 14, 15],
# [16, 17, 18, 19, 20],
# [21, 22, 23, 24, 25]])

# Write a code to slice this given array
### Convert your output into list
### Convert your output into list
### e.g return (arr).tolist()
# array([[16, 17, 18, 19, 20],
# [21, 22, 23, 24, 25]])

return None

sliced = arr[3:5]

return sliced.tolist()


# Great job!