<jupyter_start><jupyter_text>Exercise ** 1. Write a function called squared that takes in one int and retrun the second power of it.**<jupyter_code>def squared(x):
    return(x**2)
squared(2)
squared(3)<jupyter_output><empty_output><jupyter_text>** 2.Write a function called concatenate that concatenate two words**<jupyter_code>def concatenate(x,y):
    return(x+y)
3 + 2
'hello' +'world'
concatenate('hello', 'world')<jupyter_output><empty_output><jupyter_text>** 3.Write a function called average that calculates the average of a list of ints**<jupyter_code>number_3 = [1,2,3,4,5,6,7]
def average(x):
    n = len(x)
    total = sum(x)
    return(total/n)
average(number_3)
def average(x):
    n = len(x)
    total = 0
    for number in x:
        total = total+number
    return(total/n)
average(number_3)<jupyter_output><empty_output><jupyter_text>** 4.Write a function called variance that calculates the variance of a list of numbers**<jupyter_code>def variance(numbers):
    n = len(numbers)
    total = sum(numbers)
    average = total/n
    
    square_total = 0
    for num in numbers:
        square_total = square_total + (num -average)**2
    return(square_total / n)
variance(number_3)<jupyter_output><empty_output><jupyter_text>Lambda Function lambda function is a type of anonymous function which is defined by the **lambda** keyword Syntax**lambda** arguments:expression<jupyter_code>double = lambda x:x*2
double(5)
mutiply = lambda a,b : a*b
mutiply(5,6)<jupyter_output><empty_output><jupyter_text>write a function that add up all there arguments using lambda<jupyter_code>add_up = lambda a,b,c:a+b+c
add_up(1,2,3)
add_up(1,2,4)<jupyter_output><empty_output><jupyter_text>Map **map** applies a function to all the items in an input list SYNTAX:map(function_to_apply, list_of_inputs)<jupyter_code>## Print out a list with its squared value
items = [1 ,2 ,3 ,4 ,5 ,6]
## Oldest way
squared = []
for num in items:
    squared.append(num**2)
## Advanced way -- List comprehenshion
squared = [num**2 for num in items]
squared
## map way
list(map(lambda x:x**2, items))
dict_a = [{'name':'python','points':10}
          ,{'name':'Java', 'points':9}]
list(map(lambda x: x['name'], dict_a))
list(map(lambda x: x['points'], dict_a))
list(map(lambda x: x['name']=='python', dict_a))
## Write a function that add up list_a and list_b using map
list_a = [1,2,3]
list_b = [4,5,6]

list(map(lambda x,y:x+y,list_a,list_b))<jupyter_output><empty_output><jupyter_text>Filter filter returns only those element for which the function_object return true SYNTAX:filter(function_object, iterable_list)<jupyter_code>list_a = [1,2,3,4,5,6]

list(filter(lambda x:x%2==0, list_a))
list(filter(lambda x:x%2==1, list_a))
list_a<jupyter_output><empty_output><jupyter_text>Apply a function to list_a that triples list_a using lambda function<jupyter_code>list(map(lambda x: x*3, list_a))<jupyter_output><empty_output><jupyter_text>from list_a, filter out everything that is mutiple of 3<jupyter_code>list(filter(lambda x: x%3==0, list_a))<jupyter_output><empty_output><jupyter_text>Nested Function Like nested loops, we simply create a function using def inside another function to nest two functions<jupyter_code>def f1():
    print("Hello")
    def f2():
        print("world")
    return f2()
f1()
def f1():
    x = 1
    def f2(a):
        print(a+x)
    f2(2)
f1()<jupyter_output>3<jupyter_text>Global & Local Variables<jupyter_code>#local
def f1():
    local = 5
    return local
##Global
glob = 5
def f1():
    print(glob)
f1()<jupyter_output>5<jupyter_text>Closure<jupyter_code>def power(exponent):
    def exponent_of(base):
        return(base**exponent)
    return exponent_of
square = power(2)
square(2)
triple = power(3)
triple(2)
def myfunc(n):
    return lambda a:a*n
mydouble = myfunc(2)
mydouble(5)<jupyter_output><empty_output><jupyter_text>Game<jupyter_code>cow_game()
guessed 1234

answer  2345


3cows 0bulls
def cow_game():
    number = '1380'
    cow = 0
    def calculare_cow_and_bulls(guess_number):
        cows = 0
        bulls = 0
        for i in range(0,4):
            if guess_number[i] == number[i]:
                bulls +=1
            elif guess_number[i] in number:
                cows+=1
        return cows, bulls

    while cow<4:
        user_number = input("Guess the 4-digits number:")
        cow,bull = calculare_cow_and_bulls(user_number)
        print('Cows: ', cow)
        print("Bulls: ", bull)
        if bull ==4:
            print('Congratulations!')
            break<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12794538647874917, 'fraction_numerical': 0.024664170887469722, 'mean_word_length': 4.607407407407408, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '544573', 'license': 'MIT', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1655, 'n_words': 406, 'path': 'Python_Class/Class_6.ipynb', 'repo_name': 'rickchen123/Portfolio', 'size': '4541'}