Skip to content

Commit 4c00e3e

Browse files
committed
Update dict type hints
1 parent f5a7b29 commit 4c00e3e

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

source-code/typing/dict_correct.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22

33
import re
44
import sys
5-
from typing import Dict
65

76

8-
def word_count(text: str) -> Dict[str, int]:
9-
counts = dict() # type: Dict[str, int]
10-
words = re.split(r'\W+', text)
7+
def word_count(text: str) -> dict[str, int]:
8+
counts: dict[str, int] = dict()
9+
words = re.split(r"\W+", text)
1110
for word in words:
1211
word = word.lower()
1312
if word not in counts:
1413
counts[word] = 0
1514
counts[word] += 1
1615
return counts
1716

18-
if __name__ == '__main__':
19-
text = ' '.join(sys.stdin.readlines())
17+
18+
if __name__ == "__main__":
19+
text = " ".join(sys.stdin.readlines())
2020
counts = word_count(text)
2121
for word, count in counts.items():
2222
if word:
23-
print(f'{word}: {count}')
23+
print(f"{word}: {count}")

source-code/typing/dict_incorrect_01.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22

33
import re
44
import sys
5-
from typing import Dict
65

76

8-
def word_count(text: str) -> Dict[str, int]:
9-
counts = dict() # type: Dict[str, int]
10-
words = re.split(r'\W+', text)
7+
def word_count(text: str) -> dict[str, int]:
8+
counts: dict[str, int] = dict()
9+
words = re.split(r"\W+", text)
1110
nr_words = 0
1211
for word in words:
1312
word = word.lower()
1413
if word not in counts:
1514
counts[word] = 0
1615
counts[word] += 1
1716
nr_words += 1
18-
nr_words -= counts.pop('')
17+
nr_words -= counts.pop("")
1918
for word, count in counts.items():
2019
counts[word] /= nr_words
2120
return counts
2221

23-
if __name__ == '__main__':
24-
text = ' '.join(sys.stdin.readlines())
22+
23+
if __name__ == "__main__":
24+
text = " ".join(sys.stdin.readlines())
2525
counts = word_count(text)
2626
for word, count in counts.items():
27-
print(f'{word}: {count}')
27+
print(f"{word}: {count}")

0 commit comments

Comments
 (0)