File tree Expand file tree Collapse file tree 2 files changed +15
-15
lines changed Expand file tree Collapse file tree 2 files changed +15
-15
lines changed Original file line number Diff line number Diff line change 2
2
3
3
import re
4
4
import sys
5
- from typing import Dict
6
5
7
6
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 )
11
10
for word in words :
12
11
word = word .lower ()
13
12
if word not in counts :
14
13
counts [word ] = 0
15
14
counts [word ] += 1
16
15
return counts
17
16
18
- if __name__ == '__main__' :
19
- text = ' ' .join (sys .stdin .readlines ())
17
+
18
+ if __name__ == "__main__" :
19
+ text = " " .join (sys .stdin .readlines ())
20
20
counts = word_count (text )
21
21
for word , count in counts .items ():
22
22
if word :
23
- print (f' { word } : { count } ' )
23
+ print (f" { word } : { count } " )
Original file line number Diff line number Diff line change 2
2
3
3
import re
4
4
import sys
5
- from typing import Dict
6
5
7
6
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 )
11
10
nr_words = 0
12
11
for word in words :
13
12
word = word .lower ()
14
13
if word not in counts :
15
14
counts [word ] = 0
16
15
counts [word ] += 1
17
16
nr_words += 1
18
- nr_words -= counts .pop ('' )
17
+ nr_words -= counts .pop ("" )
19
18
for word , count in counts .items ():
20
19
counts [word ] /= nr_words
21
20
return counts
22
21
23
- if __name__ == '__main__' :
24
- text = ' ' .join (sys .stdin .readlines ())
22
+
23
+ if __name__ == "__main__" :
24
+ text = " " .join (sys .stdin .readlines ())
25
25
counts = word_count (text )
26
26
for word , count in counts .items ():
27
- print (f' { word } : { count } ' )
27
+ print (f" { word } : { count } " )
You can’t perform that action at this time.
0 commit comments