Skip to content

Commit

Permalink
Merge feature/exercise_week2 with develop
Browse files Browse the repository at this point in the history
add Week 2 module 1 exercise
  • Loading branch information
dohuyduc2002 authored Jun 15, 2024
2 parents 1f3c516 + 9dbbf95 commit 7e4fcbc
Show file tree
Hide file tree
Showing 6 changed files with 751 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Week_2/P1_data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
He who conquers himself is the mightiest warrior
Try not to become a man of success but rather become a man of value
One man with courage makes a majority
One secret of success in life is for a man to be ready for his opportunity when it comes
The successful man will profit from his mistakes and try again in a different way
A successful man is one who can lay a firm foundation with the bricks others have thrown at him
Success usually comes to those who are too busy looking for it
We cannot solve problems with the kind of thinking we employed when we came up with them
Just one small positive thought in the morning can change your whole day
You can get everything in life you want if you will just help enough other people get what they want
23 changes: 23 additions & 0 deletions Week_2/count_chars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""This module define function for AIO2024 Module 1 week 2 exercise."""
from typing import Dict
#Dictionary in python is a hashmap. Hashmap is a array contain key and value, import for convinience

def count_chars(words : str) -> Dict[str, int]:
"""Count chars in a string
Args:
words (str): input string
Returns:
Dict[str, int]: dictionary of each char and number of apperance
"""
char_dict: Dict[str, int] = {} # {char : number of apperance}
for char in range(len(words)):
char_dict[words[char]] = 1 + char_dict.get(words[char],0)
#char_dict[words[char]]: key of char_dict is a character in words
#char_dict.get(words[char],0): using .get method to retrieve key which is char if exist,else 0
print(char_dict)
return char_dict

STRING = 'Happiness'
count_chars(STRING)
44 changes: 44 additions & 0 deletions Week_2/levenshtein.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""This module define function for AIO2024 Module 1 week 2 exercise."""
def leveshtein(word1 : str, word2: str) -> float:
"""Calculate leveshtein distance
Args:
word1 (str): Source word
word2 (str): Target word
Returns:
int: Minimum distance
"""
len_word1 = len(word1)
len_word2 = len(word2)
#create matrix with len_word1 + 1 row and len_word2 col + 1
matrix = [[float('inf')] * (len_word2 + 1) for _ in range(len_word1 + 1)]
#insantiate 1st row and col value of matrix
for i in range(len_word1 + 1):
matrix[i][0] = i
#iterate through len_word1, append i to each row index
for j in range(len_word2 + 1):
matrix[0][j] = j
#iterate through len_word2, append j to each row index
# -> create the i col with value ranging from 0 -> len_word1
# -> create the j col with value ranging from 0 -> len_word2
#Fill the matrix
for i in range(1,len_word1 + 1):
for j in range(1,len_word2 + 1):
#iterate through every element of matrix, skipping the 1st row and cal which has been filled
if word1[i - 1] == word2[j - 1]: #
matrix[i][j] = matrix[i-1][j-1]
#the element of matrix gain the previous value from its main diagonal
else:
matrix[i][j] = 1 + min(matrix[i -1][j-1],matrix[i-1][j],matrix[i][j-1])
# Consider 1 of 3 operations, this lead to +1 in value of the element
# replace: i -1, j-1 cause 2 pointers decrese by 1 if matched to examine next case, len dont change
# addiction: i-1,j : the current pointer in word1 is decrese by one due to adding a char
# delete i,j-1 : if delete, the current pointer is increase by 1, len dont change
print(matrix)
print(matrix[-1][-1])
# return the last value of the main diagonal cause it's the shortest path
return matrix[-1][-1]

leveshtein('abc','cda')

Loading

0 comments on commit 7e4fcbc

Please sign in to comment.