-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
123 lines (84 loc) · 2.16 KB
/
functions.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#
# FUNCTIONS
#
# Generally, the syntax for a function is:
def any_function_name(any_parameters):
# Codes
#return any_output
print("Codes related to function ...")
# As an example, we create functions for
# addition and substraction
# Calling a Function
def my_function():
print("Hello from a function")
my_function() # This is how you call the function
# Parameters
def my_function2(fname):
print(fname + " Refsnes")
my_function2("Emil") # Emil Refsnes
my_function2("Tobias") # Tobias Refsnes
my_function2("Linus") # Linus Refsnes
# Now try to swap parameters in the "addition" function
# Also, try to have less than 2 parameters
# Also, try to have more than 2 parameters
# Default Parameter Value
def my_function3(country = "Norway"):
print("I am from " + country)
my_function3("Sweden")
my_function3("India")
my_function3()
my_function3("Brazil")
#
# RECURSION
#
# Generally, the syntax for Recursion is
def any_function_name2(any_parameters):
if any_parameters==0: # apply terminating condition
print("Loop terminates at this BASE CONDITION")
else:
print("Call its self again")
# Why do you think it is necessary to have BASE CONDITION?
# Example 1
def tri_recursion(k):
if(k>0):
result = k + tri_recursion(k-1)
print(result)
else:
result = 0
return result
print("Recursion Example Results")
tri_recursion(6)
# Example 2
def fib(n):
if n==0 or n==1:
return n
else:
return fib(n-1) + fib(n-2)
#
# CHALLENGES
#
# Challenge 1
# Create a function that do the Countdown
# Hint: Use for/while loop
# Challenge 2
# Create a RECURSIVE function that do the Countdown
#
# ANSWERS TO CHALLENGES
#
#
# Challenge 1
# Create a function that do the Countdown
# Hint: Use for/while loop
def countdown1(n):
for i in range(n,-1, -1):
print(i)
countdown1(5)
# Challenge 2
# Create a RECURSIVE function that do the Countdown
def countdown2(n):
if n==0:
print(n)
else:
print(n)
return countdown2(n-1)
countdown2(5)