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

python.py #149

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
94 changes: 64 additions & 30 deletions Python_Exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

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

import numpy as np


def power(a,b):

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

return None
# a = 7 and b = 4
return a**b



Expand All @@ -20,21 +21,23 @@ def split_str(s):
#
# **into a list. **

return None
return list(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.
s = "The diameter of {} is {} kilometers."

return None

return s.format(planet, diameter)



Expand All @@ -44,7 +47,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,15 +57,15 @@ 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']


def subjective():

# ** What is the main difference between a tuple and a list? **
# Tuple is _______

return None
return "Immutable"



Expand All @@ -74,22 +77,26 @@ def domainGet(email):
# [email protected]
#
# **So for example, passing "[email protected]" would return: domain.com**
s = "[email protected]"

return None
return s.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
dog = st.lower().split()
if "dog" in dog:
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. **

return None
return st.lower().split().count("dog")



Expand All @@ -103,7 +110,7 @@ def lambdafunc(seq):
#
# ['soup','salad']

return None
return list(filter(lamba i:i[0]=='s',seq)


def caught_speeding(speed, is_birthday):
Expand All @@ -112,10 +119,25 @@ 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
# 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 > 85:
return "Big Ticket"
elif speed > 65 and speed <= 85:
return "Small ticket"
else
return "No ticket"

else:
if speed > 80:
return "Big Ticket"
elif speed > 60 and speed <= 80:
return "Small ticket"
else
return "No ticket"



## Numpy Exercises
Expand All @@ -128,8 +150,10 @@ def create_arr_of_fives():
#### Create an array of 10 fives
#### Convert your output into list
#### e.g return list(arr)

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

return None
return list(arr)



Expand All @@ -138,8 +162,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
arr = np.arange(10,51,2)
l = list(arr)
return l



Expand All @@ -148,8 +173,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)
arr = arr.reshape(3,3)
return arr.tolist()



Expand All @@ -158,8 +184,9 @@ 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)
arr = np.linspace(0,1,20)

return None
return list(arr)



Expand All @@ -168,8 +195,11 @@ 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.zeros((10,10))
for a in range(0,10):
for b in range(0,10):
arr[a][b]=(a*10 + b + 1)/100
return arr.tolist()



Expand All @@ -189,8 +219,8 @@ def slices_1():
# array([[12, 13, 14, 15],
# [17, 18, 19, 20],
# [22, 23, 24, 25]])

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



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

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




Expand All @@ -231,7 +263,9 @@ def slices_3():
# array([[16, 17, 18, 19, 20],
# [21, 22, 23, 24, 25]])

return None
arr = arr[3:]
return arr.tolist()



# Great job!