Skip to content

Commit 51c4110

Browse files
committed
add some type hints
1 parent 201ddb5 commit 51c4110

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

anagram.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Write a function called is_anagram that takes in two strings as input and returns True if the two strings are anagrams of each other, and False otherwise. The function should be case-insensitive and should ignore spaces.
77
"""
88

9-
def is_anagram(str1, str2):
9+
def is_anagram(str1: str, str2: str) -> bool:
1010
first_list = list(str1.lower().replace(" ", ""))
1111
second_list = list(str2.lower().replace(" ", ""))
1212

domain-hits.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from typing import Union, List
12
'''
23
for a set of elements in an array with the shape [10, "google.com"]
34
being the first element the number of hit for the second the domain
@@ -14,16 +15,16 @@
1415
]
1516
'''
1617

17-
Domain = list[int, str]
18+
Domain = List[Union[int, str]]
1819
Result = dict[str, int]
1920

20-
data: [Domain] = [
21+
data: List[Domain] = [
2122
[10, "jsfiddle.com"],
2223
[20, "maps.google.com"],
2324
[30, "photos.google.com"]
2425
]
2526

26-
def getDicFromList(input: [Domain]) -> Result:
27+
def getDicFromList(input: List[Domain]) -> Result:
2728
return { key: value for [value, key] in input }
2829

2930
def getSubDomainsRecursively(input: list[str], acc: Result, ref: Result) -> Result:
@@ -51,7 +52,7 @@ def main():
5152
obj = getDicFromList(data)
5253
result: Result;
5354

54-
for k, v in obj.items():
55+
for k in obj.items():
5556
subDomains = k.split('.')
5657
result = getSubDomainsRecursively(subDomains[1:len(subDomains)], obj, obj)
5758

longest-substring.py

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
Write a function called length_of_longest_substring that takes a string s as input and returns an integer representing the
77
length of the longest substring without repeating characters
88
"""
9-
from functools import reduce
10-
119
def length_of_longest_substring(s: str) -> int:
1210
longest = 0
1311
current = ""

word-frequency.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88

99
import re
10+
from typing import List
1011
song = """
1112
We're no strangers to love
1213
You know the rules and so do I (do I)
@@ -64,24 +65,24 @@
6465
Never gonna tell a lie and hurt you
6566
"""
6667

67-
def clean_sp_char(text):
68+
def clean_sp_char(text: str):
6869
return re.sub(r"[!@#$%^?/,()!&*]", "",text)
6970

70-
def create_freq(text):
71+
def create_freq(text: str):
7172
words = []
7273
words = text.lower().split()
7374
wfreq= [words.count(w) for w in words]
7475
return list(zip(words,wfreq))[:]
7576

76-
def filter_for_freq(input):
77+
def filter_for_freq(input: List) -> List:
7778
filtered = filter(lambda freq: freq[1] > 1, input)
7879
return list(set(filtered))[:]
7980

80-
def print_result(input):
81+
def print_result(input: List) -> None:
8182
for freq in input:
8283
print(f"word: {freq[0]} count: {freq[1]}")
8384

84-
def return_result_list(input):
85+
def return_result_list(input: str):
8586
cleanSong = clean_sp_char(input)
8687
listOfWords = filter_for_freq(create_freq(cleanSong))
8788
listOfWords.sort(key=lambda freq: freq[1], reverse=True)

0 commit comments

Comments
 (0)