Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e1a3f13

Browse files
fix: prevent error if passed word not present in the list
1 parent 1a70a93 commit e1a3f13

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

‎main.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@ def main():
2525
if word == '':
2626
break
2727

28-
print(
29-
f'Number of comparison for word "{word}": {numOfComparisons(root_node, word, words)}\n')
28+
comparisons = numOfComparisons(root_node, word, words)
29+
30+
if comparisons == -1:
31+
print(f'Word {word} not found in the tree\n')
32+
else:
33+
print(
34+
f'Number of comparison for word "{word}": {comparisons}\n')
3035

3136

3237
if __name__ == '__main__':

‎numOfComaprisons.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ def numOfComparisons(root_node, searched_word, words):
44
55
One level of the tree equals one comparison.
66
7-
If word isnt present in the tree, prints out message
7+
Returns number of comparisons on success, -1 if word not in the list
88
"""
99

1010
count = 0
11-
searched_value = words.index(searched_word)
11+
12+
try:
13+
searched_value = words.index(searched_word)
14+
except:
15+
return -1
16+
1217
actual_node = root_node
1318

1419
while True:

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /