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
180 changes: 168 additions & 12 deletions 12_09_practice.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,185 @@
#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].
#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].

#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 make_it_big(list1):
for index,element in enumerate(list1):
if element >= 0:
list1[index] = "big"

#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
return list1


change_result = make_it_big([-1,3,5,-5])
print(change_result)

#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 count_positives(list1):
count = 0
for i in list1:
if i > 0:
count += 1
list1[-1] = count
return list1

result = count_positives([-1,1,1,1])
print(result)


#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 sum_total(list1):
sum = 0
for i in list1:
sum += i
return sum

sum_result = sum_total([1,2,3,4])
print(sum_result)

#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 avg_value(list1):
sum = 0
for i in list1:
sum += i
return sum/len(list1)

sum_result = avg_value([1,2,3,4])
print(sum_result)

#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

#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

#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.
def length_of_list(list1):
return len(list1)

result = length_of_list([1,2,3,4])
print(result)

#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(list1):
if len(list1) == 0:
return False
else:
min_value = list1[0]
for i in range(1,len(list1)):
if min_value > list1[i]:
min_value = list1[i]
return min_value

a =minimum([1,2,3,4])
print(a)


#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(list1):
if len(list1) == 0:
return False
else:
max_value = list1[0]
for i in range(1,len(list1)):
if max_value < list1[i]:
max_value = list1[i]
return max_value

a =maximum([1,2,3,4])
print(a)


#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.

#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.
#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.

#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 list_content(list1):
if len(list1) == 0:
return False
else:
d = {}
d["sum"] = sum_total(list1)
d["average"] = avg_value(list1)
d["minimum"] = minimum(list1)
d["maxmimum"] = maximum(list1)
d["length"] = length_of_list(list1)
return d

a = list_content([-1,2,0,-8,0,6])
print(a)



#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 reverse_list(list1):
list1 = list1[::-1]
return list1


a = reverse_list([1,2,3,4])
print(a)



#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 palindrome(string1):
if(string1 == string1[::-1]):
print("The given string is Palindrome")
else:
print("The given string is not a Palindrome")

palindrome("radax")

#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.

#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,
def fizzbizz():
for i in range(1,101):
if (i%3 == 0) and (i%5 == 0):
print(i , "FizzBuzz")
elif i%3 == 0:
print(i, "Fizz")
elif i%5 == 0:
print(i, "Buzz")
else:
print(i)
fizzbizz()


#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 fibonacci(n):
first = 0
second = 1

for i in range(n):
print(first)
third = first + second
first = second
second = third

number_for_fibo = int(input("Enter Number : "))
fibonacci(number_for_fibo)




155 changes: 155 additions & 0 deletions Closures_Decorators_Solutions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
from datetime import datetime
#1.Using a closure, create a function, multiples_of(n) which we can use to
# create generators that generate multiples of n less than a given
# number.


def multiples_of(n):
def inner(n1):
count = 1
for _ in range(n1):
table1 = count * n
if table1 < n1:
yield table1
count += 1
return inner

m3 = multiples_of(3)
m3_under30 = m3(30)
m7_under30 = multiples_of(7)(30)

print(type(m3_under30))
print(*m3_under30)
print(*m7_under30)

print(50 * "#")
#1. make_upper – make every letter of a string returned from the decorated
# function uppercase.


def make_upper(func):
def wrapper():
str1 = func()
return str1.upper()
return wrapper


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

print(hello_world())

print(50 * "#")

#2. print_func_name – print the name of the decorated function before
# executing the function.

def print_func_name(func):
def wrapper():
print("my_func is running...")
str1 = func()
return str1
return wrapper

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

my_func()
print(50 * "#")


#3. give_name(name) – concatenate the given name at the end of a string
# returned from the decorated function.

def give_name(val):
def innerfunc(func):
def wrapper():
return func() + " " + val
return wrapper
return innerfunc

@give_name('Therasa')
def greeting():
return 'Hello'

print(greeting())
print(50 * "#")


#4. print_input_type – print a data type of the input argument before
# executing the decorated function.


def print_input_type(func):
def wrapper(n):
str1 = func(n)
str2 = type(str1)
return f"The input data type is {str2} \n{str1}"
return wrapper

@print_input_type
def square(n):
return n ** 2

print(square(3.5))
print(50 * "#")

#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(val):
def innerfunc(func):
def wrapper(n):
value = func(n)
type_value = type(value)
if type_value == val:
return f"The return type is {type_value} \n{value}"
return f"Error!! \nThe return type is NOT {type_value} \n{value}"
return wrapper
return innerfunc



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

print(square(6))
print(50 * "#")

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

print(square(2.9))
print(50 * "#")

#6. execute_log – write a function execution log on the log file.

def execute_log(func):
def wrapper(*args):
time_value = datetime.now()
function_name = func.__name__
function_output = func(*args)
return function_output,f"{time_value} {function_name}"
return wrapper

@execute_log
def multiply(*nums):
mult = 1
for n in nums:
mult *= n
return mult


@execute_log
def hello_world():
return 'hello world!!'

print(multiply(6,2,3))
print(hello_world())
print(multiply(2.2,4))
print(hello_world())
Loading