-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_loops.py
More file actions
154 lines (126 loc) · 4.19 KB
/
Copy pathexample_loops.py
File metadata and controls
154 lines (126 loc) · 4.19 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
## GitHub: dark-teal-coder
## First Published Date: 2022-03-23
## Program Input(s):
### (1) N/A
## Program Process(es):
### (1) N/A
## Program Output(s):
### (1) N/A
## Program Description: This is a note containing examples of while- and for-loops.
####################################################################################################
## Break & Continue
### break statement: to skip the current iteration
### continue statement: to continue with the next one
## While Loops
### A while loop executes the body of the loop while the condition remains True.
### While loops are mostly used when there is an unknown number of operations to be performed and a condition needs to be checked at each iteration.
### Common pitfalls:
#### 01) Unintended infinite loops
#### 02) Failures to initialize all the variables used in the condition before the loop
### Prime Factors
def print_prime_factors(num):
## 2 is the first prime
factor = 2
## Keep going until the factor is larger than the number
while factor <= num:
## Check if factor is a divisor of the number
if num % factor == 0:
## If it is, print it and divide the original number
print(factor)
num = num / factor
else:
## If it's not, increment the factor by 1
factor += 1
return "Done"
print_prime_factors(100)
### Power of 2
def is_power_of_two(num):
'''This function checks if the number at parameter 1 can be divided by 2 without a remainder.'''
while num != 0 and num % 2 == 0:
num = num / 2
## If after dividing by 2 the number is 1, it's a power of 2
if num == 1:
return True
return False
print(is_power_of_two(0)) # Output: False
print(is_power_of_two(1)) # Output: True
print(is_power_of_two(8)) # Output: True
print(is_power_of_two(9)) # Output: False
### Sum of Divisors
def sum_divisors(num):
'''This function returns the sum of all divisors of the number at parameter 1, exclusive of itself.'''
sum = 0
divisor = 1
while divisor < num:
if num % divisor == 0:
sum += divisor
divisor += 1
return sum
print(sum_divisors(0)) # Output: 0
print(sum_divisors(3)) # Output: 1
print(sum_divisors(36)) # Output: 55 (1 + 2 + 3 + 4 + 6 + 9 + 12 + 18)
print(sum_divisors(102)) # Output: 114 (2 + 3 + 6 + 17 + 34 + 51)
## Counting Up or Down
def counter(start, stop):
x = start
if start > stop:
return_string = "Counting down: "
while x >= stop:
return_string += str(x)
if x != stop:
return_string += ","
x -= 1
else:
return_string = "Counting up: "
while x <= stop:
return_string += str(x)
if x != stop:
return_string += ","
x += 1
return return_string
print(counter(1, 10)) # Output: "Counting up: 1,2,3,4,5,6,7,8,9,10"
print(counter(2, 1)) # Output: "Counting down: 2,1"
print(counter(5, 5)) # Output: "Counting up: 5"
## Multiplication Table
def multiplication_table(start, stop):
for x in range(start, stop + 1):
for y in range(1, stop + 1):
print(str(x * y), end=" ")
print()
## This will output the following multiplication table:
### 1 2 3
### 2 4 6
### 3 6 9
multiplication_table(1, 3)
## For Loops
### A for loop iterates over a sequence of elements, executing the body of the loop for each element in the sequence.
### For loops are mostly used when there is a pre-defined sequence or range of numbers to iterate.
### Common pitfalls:
#### 01) Forgetting that the upper limit of a range() isn't included
#### 02) Iterating over non-iterables
### Factorial
def get_factorial(num):
'''This function returns the factorial of the number at parameter 1.'''
if n == 0:
return 1
result = 1
for i in range(1, num + 1):
result *= i
return result
print(get_factorial(4)) # Output: 24
print(get_factorial(5)) # Output: 120
## For-loops VS. While-loops
### Use for-loops when there is a sequence of elements to interate
### Use while-loops when you want to repeat an action until a condition changes
## Nested For Loops
### Domino Tiles
for left in range(7):
for right in range(left, 7):
print("[" + str(left) + "|" + str(right) + "]", end=" ")
print()
### Home Team VS. Away Team
teams = ['Dragons', 'Wolves', 'Pandas', 'Unicorns']
for home_team in teams:
for away_team in teams:
if home_team != away_team:
print(home_team + " vs. " + away_team)