Skip to content

Commit ddcbb26

Browse files
committed
updated part07
1 parent 0a5e162 commit ddcbb26

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

part07/16_spellchecker_2.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from difflib import get_close_matches
2+
3+
4+
user = input('write text: ')
5+
6+
with open('wordlist.txt') as file:
7+
word_list = []
8+
for line in file:
9+
word_list.append(line.strip())
10+
11+
wrong_words = []
12+
for word in user.split(' '):
13+
if word.lower() in word_list:
14+
print(word, end=' ')
15+
else:
16+
print(f'*{word}*', end=' ')
17+
wrong_words.append(word)
18+
19+
print('\nsuggestions:')
20+
for word in wrong_words:
21+
suggestions = get_close_matches(word, word_list)
22+
print(f'{word}: {", ".join(suggestions)[:-2]}')
23+

part07/17_string_helper.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from string import ascii_letters, digits
2+
3+
def change_case(orig_string: str):
4+
new_str = ''
5+
for letter in orig_string:
6+
if letter.isupper():
7+
new_str = new_str + letter.lower()
8+
else:
9+
new_str = new_str + letter.upper()
10+
return new_str
11+
12+
13+
def split_in_half(orig_string: str):
14+
half_index = int(len(orig_string) / 2)
15+
return orig_string[:half_index], orig_string[half_index:]
16+
17+
18+
def remove_special_characters(orig_string: str):
19+
allowed = ascii_letters + digits + ' '
20+
new_str = ''
21+
for letter in orig_string:
22+
if letter in allowed:
23+
new_str = new_str + letter
24+
return new_str
25+
26+
27+
28+
if __name__ == '__main__':
29+
print(remove_special_characters('Thi§ is a test: test?'))

part07/18_own_language.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from string import ascii_uppercase
2+
3+
def basic_commands():
4+
pass
5+
6+
7+
def run(program):
8+
9+
variables = dict.fromkeys(ascii_uppercase, 0)
10+
printed = []
11+
12+
for line in program:
13+
if line.split(' ')[0] == 'END':
14+
return printed
15+
if line.split(' ')[0] == 'PRINT':
16+
printed.append(variables[line.split(' ')[1]])
17+
else:
18+
command, variable, value = line.split(' ')
19+
if command == 'MOV':
20+
if value.isdigit():
21+
variables[variable] = int(value)
22+
else:
23+
variables[variable] = variables[value]
24+
elif command == 'ADD':
25+
if value.isdigit():
26+
variables[variable] += int(value)
27+
else:
28+
variables[variable] += int(variables[value])
29+
elif command == 'SUB':
30+
if value.isdigit():
31+
variables[variable] -= int(value)
32+
else:
33+
variables[variable] -= int(variables[value])
34+
elif command == 'MUL':
35+
if value.isdigit():
36+
variables[variable] *= int(value)
37+
else:
38+
variables[variable] *= variables[value]
39+
40+
return printed
41+
42+
43+
if __name__ == '__main__':
44+
program1 = []
45+
# program1.append("MOV A 10")
46+
# program1.append("MOV B 2")
47+
program1.append("PRINT A")
48+
# program1.append("PRINT B")
49+
# program1.append("ADD A B")
50+
# program1.append("PRINT A")
51+
program1.append("END")
52+
# program1.append("MOV B A")
53+
# program1.append("SUB B 8")
54+
# program1.append("PRINT B")
55+
# program1.append("SUB A B")
56+
# program1.append("PRINT A")
57+
result = run(program1)
58+
print(result)

0 commit comments

Comments
 (0)