-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopdracht1a.py
170 lines (125 loc) · 3.69 KB
/
opdracht1a.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Formatieve opdracht 1a: algoritmisch programmeren
# Jonathan Williams (1790472)
import random # Gebruikt voor opdracht 7
# Opdracht 1: Pyramide
def pyramide1(n): # Normale pyramide, twee for-loops
for i in range(1, n+1):
print("*" * i)
for i in range(n - 1, 0, -1):
print("*" * i)
def pyramide2(n): # Normale pyramide, twee while-loops
i = 1
while i < n:
print("*" * i)
i += 1
while i > 0:
print("*" * i)
i -= 1
def pyramide3(n): # Inverted pyramide, twee for-loops
for i in range(1, n+1):
print(" " * (n-i) + "*" * i)
for i in range(n - 1, 0, -1):
print(" " * (n-i) + "*" * i)
def pyramide4(n): # Inverted pyramide, twee while-loops
i = 1
while i < n:
print(" " * (n-i) + "*" * i)
i += 1
while i > 0:
print(" " * (n-i) + "*" * i)
i -= 1
# Opdracht 2: Tekstcheck
def differ_at():
a = input("Please enter some text: ")
b = input("Please enter other text: ")
if a == b:
print("The two strings are equal")
else:
n = 0
if a and b:
while n < min(len(a), len(b)) and a[n] == b[n]:
n += 1
print(f"The two strings differ at index {n}")
# Opdracht 3: Lijstcheck
# a:
def count(x, lst):
c = 0
for n in lst:
c += 1 if n == x else 0
return c
# b:
def consecutive_range(lst):
r = 0
for i in range(len(lst) - 1):
rnew = abs(lst[i] - lst[i + 1])
if rnew > r:
r = rnew
return r
# c:
def qualify(lst):
if count(0, lst) > 12:
return False
return count(1, lst) > count(0, lst)
# Opdracht 4: Palindroom
def palindroom(s): # Gebruikmaking van bibliotheekfunctie
return s == "".join(reversed(s))
def palindrome(s): # Zelf omdraaien verzorgen
return s == s[::-1]
# Opdracht 5: Sorteren
def minsort(lst): # Een sorteerfunctie die nummers sorteert door ze van klein naar groot te pakken
result = []
while lst:
m = min(lst)
result.append(m)
lst.remove(m)
return result
# Opdracht 6: Gemiddelde berekenen
def average(lst): # Een lijst van cijfers als input
return sum(lst) / len(lst)
def averages(megalst): # Een lijst van lijsten met cijfers als input
return [sum(lst) / len(lst) for lst in megalst]
# Opdracht 7: Random
def guess():
r = random.randint(0, 9)
while True:
g = int(input("Guess a number (0-9): "))
if g == r:
print("Correct! You win!")
break
print("Too bad! Guess again!")
# Opdracht 8: Compressie
def compress(file):
result = []
f = open(file, 'r')
for line in f:
result.append(line.lstrip())
f.close()
f = open("compressed-" + file, 'w')
for line in result:
f.write(line)
f.close()
# Opracht 9: Cyclisch verschuiven
def cyclic(ch, n):
binary = f"{ord(ch):b}"
return binary[n:] + binary[:n]
# Opdracht 10: Fibonacci
def fibonacci(n, a=0, b=1):
return b if n <= 1 else fibonacci(n-1, b, a+b)
# Opdracht 11: Caesarcijfer
def caesar(s, shift):
result = ""
for c in s:
v = ord(c)
if v in range(65, 91):
result += chr(((v - 65 + shift) % 26) + 65)
elif v in range(97, 123):
result += chr(((v - 97 + shift) % 26) + 97)
else:
result += c
return result
# Opdracht 12: FizzBuzz
def fizzbuzz():
for n in range(1, 101):
fizz = "fizz" if not n % 3 else ""
buzz = "buzz" if not n % 5 else ""
print(f"{fizz+buzz or n}")