Skip to content
Open
Show file tree
Hide file tree
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
146 changes: 133 additions & 13 deletions 12_09_practice.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,149 @@
#Biggie Size - Given a list, write a function that changes all positive numbers in the list to "big". Example: make_it_big([-1, 3, 5, -5]) returns that same list, #changed to [-1, "big", "big", -5].
# these lists come into play a lot below
numbers = [4, 0, 5, 10, 99, -4]
more_numbers = [0, -2, -5, 5, -99]

#Count Positives - Given a list of numbers, create a function to replace last value with number of positive values. Example, count_positives([-1,1,1,1]) changes list #to [-1,1,1,3] and returns it. (Note that zero is not considered to be a positive number).
#Biggie Size - Given a list, write a function that changes all positive numbers in the list to "big".
# Example: make_it_big([-1, 3, 5, -5]) returns that same list, #changed to [-1, "big", "big", -5].
def make_it_big(assortment):
new_assortment = []
for item in assortment:
if item <= 0:
new_assortment.append(item)
else:
new_assortment.append("big")
return new_assortment
print(make_it_big(numbers))

#SumTotal - Create a function that takes a list as an argument and returns the sum of all the values in the list. For example sum_total([1,2,3,4]) should return 10
#Count Positives - Given a list of numbers, create a function to replace last value with number of positive values.
# Example, count_positives([-1,1,1,1]) changes list #to [-1,1,1,3] and returns it. (Note that zero is not considered to be a positive number).
def end_count(assortment):
new_assortment = []
count = 0
for item in assortment:
if item <= 0:
pass
else:
count += 1
assortment[-1] = count
return assortment
print(end_count(numbers))

#Average - Create a function that takes a list as an argument and returns the average of all the values in the list. For example multiples([1,2,3,4]) should return #2.5
#SumTotal - Create a function that takes a list as an argument and returns the sum of all the values in the list.
# For example sum_total([1,2,3,4]) should return 10
def sumTotal(assortment):
total = 0
for item in assortment:
total += item
return total
print(sumTotal(numbers))

#Length - Create a function that takes a list as an argument and returns the length of the list. For example length([1,2,3,4]) should return 4
#Average - Create a function that takes a list as an argument and returns the average of all the values in the list.
# For example multiples([1,2,3,4]) should return #2.5
def average(assortment):
result = (sum(assortment))/(len(assortment))
return result
print(average(numbers))

#Minimum - Create a function that takes a list as an argument and returns the minimum value in the list. If the passed list is empty, have the function return false. #For example minimum([1,2,3,4]) should return 1; minimum([-1,-2,-3]) should return -3.
#
#Maximum - Create a function that takes a list as an argument and returns the maximum value in the list. If the passed list is empty, have the function return false. #For example maximum([1,2,3,4]) should return 4; maximum([-1,-2,-3]) should return -1.
#Length - Create a function that takes a list as an argument and returns the length of the list.
# For example length([1,2,3,4]) should return 4
def length(assortment):
result = len(assortment)
return result
print(length(numbers))

#Ultimateaalyze - Create a function that takes a list as an argument and returns a dictionary that has the sumTotal, average, minimum, maximum ad length of the list.
#Minimum - Create a function that takes a list as an argument and returns the minimum value in the list.
# If the passed list is empty, have the function return false.
# #For example minimum([1,2,3,4]) should return 1; minimum([-1,-2,-3]) should return -3.
def minimum(assortment):
if len(assortment) < 1:
return False
else:
assortment.sort()
minimum = assortment[0]
return minimum
print(minimum(more_numbers))

#ReverseList - Create a function that takes a list as a argument and return a list in a reversed order. Do this without creating a empty temporary list. For example #reverse([1,2,3,4]) should return [4,3,2,1]. This challenge is known to appear during basic technical interviews.
#Maximum - Create a function that takes a list as an argument and returns the maximum value in the list.
# If the passed list is empty, have the function return false.
# #For example maximum([1,2,3,4]) should return 4; maximum([-1,-2,-3]) should return -1.
def maximum(assortment):
if len(assortment) < 1:
return False
else:
assortment.sort(reverse=True)
maximum = assortment[0]
return maximum
print(maximum(more_numbers))

#Ispalindrome- Given a string, write a python function to check if it is palindrome or not. A string is said to be palindrome if the reverse of the string is the same as string. For example, “radar” is a palindrome, but “radix” is not a palindrome.
#Ultimateaalyze - Create a function that takes a list as an argument and returns a dictionary that has the
# sumTotal, average, minimum, maximum and length of the list.
def ultAn(assortment):
ultAn={}
if len(assortment) < 1:
return False
else:
assortment.sort()
ultAn["minimum"] = assortment[0]
assortment.sort(reverse=True)
ultAn["maximum"] = assortment[0]
ultAn["length"] = len(assortment)
ultAn["sumTotal"] = 0
for item in assortment:
ultAn["sumTotal"] += item
ultAn["average"] = (sum(assortment))/(len(assortment))
return ultAn
print(ultAn(numbers))

#ReverseList - Create a function that takes a list as a argument and return a list in a reversed order.
# Do this without creating a empty temporary list. For example #reverse([1,2,3,4]) should return [4,3,2,1].
# This challenge is known to appear during basic technical interviews.
def ReverseList(assortment):
assortment2 = assortment[::-1]
return assortment2
print(ReverseList([1,2,4,6,5]))

#Ispalindrome- Given a string, write a python function to check if it is palindrome or not.
# A string is said to be palindrome if the reverse of the string is the same as string.
# For example, “radar” is a palindrome, but “radix” is not a palindrome.
def Ispalindrome(assortment):
if assortment[::-1] == assortment[::1]:
return True
else:
return False
print(Ispalindrome("radar"))
print(Ispalindrome("radix"))

#Fizzbuzz- Create a function that will print numbers from 1 to 100, with certain exceptions:
#If the number is a multiple of 3, print “Fizz” instead of the number.
#If the number is a multiple of 5, print “Buzz” instead of the number.
#If the number is a multiple of 3 and 5, print “FizzBuzz” instead of the number.
def fizzBuzz():
numbers = []
for i in range (1,101):
if i % 3 == 0:
if i % 5 == 0:
numbers.append("FizzBuzz")
else:
numbers.append("Fizz")
elif i % 5 == 0:
numbers.append("Buzz")
else:
numbers.append(i)
return numbers
print(fizzBuzz())

#Fibonacci- The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, #starting from 0 and 1. That is,
#Fibonacci- The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence,
# such that each number is the sum of the two preceding ones, #starting from 0 and 1. That is,
#F(0) = 0, F(1) = 1
#F(n) = F(n - 1) + F(n - 2), for n > 1.
#Create a function that accepts any number and will create a sequence based on the fibonacci sequence.
#Create a function that accepts any number and will create a sequence based on the fibonacci sequence.
def F(n):
if n == 1:
return 0
elif n == 2:
return 1
elif n > 2:
return F(n - 1) + F(n - 2)
else:
return "That's not a positive number. Give me a positive number."
print(F(10))
61 changes: 61 additions & 0 deletions JMO_practice_12_12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#Consider the list:
prog_lang = [('Python', 3.8),
('Java', 13),
('JavaScript', 2019),
('Scala', 2.13)]
#1. Sort the list by each language's version in ascending order.
ascend = sorted(prog_lang, key=lambda x: x[1])
print(ascend)

#2. Sort the list by the length of the name of each language in descending order.
dlname = sorted(prog_lang, key=lambda x: len(x[0]), reverse = True)
print(dlname)

#3. Filter the list so that it only contains languages with 'a' in it.
a = list(filter(lambda x: 'a' in x[0], prog_lang))
print(a)

#4. Filter the list so that it only contains languages whose version is in integer form.
integers = list(filter(lambda x: isinstance(x[1], int), prog_lang))
print(integers)

#5. Transform the list so that it contains the tuples in the form,
# ("language in all lower case", length of the language string)
def newlist(old):
new = []
new2 = []
new3 = []
# "new" gets the names, new2 gets the lengths:
for x in old:
new.append(x[0].lower())
new2.append(len(str(x[0])))
# new3 is made of tuples combining new and new2 at the same index
for i in range (len(new)):
new3.append((new[i], new2[i]))
return new3
print(newlist(prog_lang))

#6. Generate a tuple in the form, ("All languages separated by commas",
# "All versions separated by commas")
def newtuple(old):
new = []
newstring = ''
new2 = []
new2string = ''
for x in old:
new.append(x[0])
new2.append(x[1])
#new holds the names, new2 holds the values
for y in new:
newstring += y
newstring += ','
newstring = newstring[:-1]
#newstring takes everything in new, followed by a comma, into a string - then the last character is removed
for z in new2:
new2string += str(z)
new2string += ','
#new2string does the same as above, for new2
new2string = new2string[:-1]
new3 = newstring, new2string
return new3
print(newtuple(prog_lang))
101 changes: 101 additions & 0 deletions closures_decorators_12_13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
def multiples_of(n):
def limit(k):
for i in range(n,k):
if i % n == 0:
yield i
return limit

m3 = multiples_of(3)
m3_under30 = m3(30)
m7_under30 = multiples_of(7)(30)
print(type(m3_under30))
print(*m3_under30)
print(*m7_under30)

#1. make_upper – make every letter of a string returned from the decorated function uppercase.
def make_upper(up):
def wrapper():
return up().upper()
return wrapper

@make_upper
def hello_world():
return 'hello young, good day!!'

uppity = make_upper(hello_world) # output: HELLO YOUNG, GOOD DAY!!
print(uppity())

#2. print_func_name – print the name of the decorated function before executing the function.
def print_func_name(machine):
def wrapper():
func_name = machine.__name__
print(f"{func_name} is running...")
print(machine())
return wrapper

@print_func_name
def my_func():
return('Python is fun!!')

my_func() # output: my_func is running...
#Python is fun

#3. give_name(name) – concatenate the given name at the end of a string
# returned from the decorated function.
def give_name(name):
def wrapper(yar):
print(yar(), name)
return wrapper
@give_name('Theresa')
def greeting():
return 'Hello'

#print(greeting()) # output: Hello Theresa

#4. print_input_type – print a data type of the input argument before executing the
# decorated function.
def print_input_type(q):
def wrapper(p):
x = type(p)
print(f"The input data type is {x}")
activate = q(p)
return activate
return wrapper
@print_input_type
def square(n):
return n ** 2

print(square(3.5)) # output: The input data type is <class 'float'>
#12.25

#5. check_return_type(return_type) – check if the return type of the
# decorated function is return_type and print the result before executing
# the function.
def check_return_type(return_type):
def wrapper(p):
x = type(p)
activate = return_type()
returned = type(activate)
if returned == x:
print("bingo")
else:
print("=========Error!!")
print(f"=========The return type is not {return_type}")
print(type(activate), p, return_type, returned)

return p
return wrapper
@check_return_type(str)
def square(n):
return n ** 2

print(square(6)) # output: =========Error!!
#=========The return type is NOT <class 'str'>
#36

@check_return_type(float)
def square(n):
return n ** 2

print(square(2.9)) # output: The return type is <class 'float'>
#8.41