Skip to content

Commit

Permalink
add some type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
masterpol committed Oct 30, 2024
1 parent 201ddb5 commit 51c4110
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion anagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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.
"""

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

Expand Down
9 changes: 5 additions & 4 deletions domain-hits.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Union, List
'''
for a set of elements in an array with the shape [10, "google.com"]
being the first element the number of hit for the second the domain
Expand All @@ -14,16 +15,16 @@
]
'''

Domain = list[int, str]
Domain = List[Union[int, str]]
Result = dict[str, int]

data: [Domain] = [
data: List[Domain] = [
[10, "jsfiddle.com"],
[20, "maps.google.com"],
[30, "photos.google.com"]
]

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

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

for k, v in obj.items():
for k in obj.items():
subDomains = k.split('.')
result = getSubDomainsRecursively(subDomains[1:len(subDomains)], obj, obj)

Expand Down
2 changes: 0 additions & 2 deletions longest-substring.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
Write a function called length_of_longest_substring that takes a string s as input and returns an integer representing the
length of the longest substring without repeating characters
"""
from functools import reduce

def length_of_longest_substring(s: str) -> int:
longest = 0
current = ""
Expand Down
11 changes: 6 additions & 5 deletions word-frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""

import re
from typing import List
song = """
We're no strangers to love
You know the rules and so do I (do I)
Expand Down Expand Up @@ -64,24 +65,24 @@
Never gonna tell a lie and hurt you
"""

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

def create_freq(text):
def create_freq(text: str):
words = []
words = text.lower().split()
wfreq= [words.count(w) for w in words]
return list(zip(words,wfreq))[:]

def filter_for_freq(input):
def filter_for_freq(input: List) -> List:
filtered = filter(lambda freq: freq[1] > 1, input)
return list(set(filtered))[:]

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

def return_result_list(input):
def return_result_list(input: str):
cleanSong = clean_sp_char(input)
listOfWords = filter_for_freq(create_freq(cleanSong))
listOfWords.sort(key=lambda freq: freq[1], reverse=True)
Expand Down

0 comments on commit 51c4110

Please sign in to comment.