Skip to content

Commit

Permalink
complete module 1 week 2
Browse files Browse the repository at this point in the history
  • Loading branch information
zoanhy20 committed Jul 1, 2024
1 parent f28dab6 commit 9f9d3b1
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Module 1/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
12 changes: 12 additions & 0 deletions Module 1/Week 2/character_frequency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from pprint import pprint


def count_chars(string):

char_count = {}
for char in string:
char_count[char] = char_count.get(char, 0) + 1
return char_count


pprint(count_chars('Happiness'))
31 changes: 31 additions & 0 deletions Module 1/Week 2/levenshtein_distance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from pprint import pprint

source = 'yu'
target = 'you'


def levenshtein_distance(source, target):
rows = len(source) + 1
cols = len(target) + 1

D = [[0] * cols for _ in range(rows)]

for i in range(cols):
D[0][i] = i

for i in range(rows):
D[i][0] = i

for i in range(1, rows):
for j in range(1, cols):
cost = 0 if source[i-1] == target[j-1] else 1

D[i][j] = min(D[i-1][j] + 1,
D[i][j-1] + 1,
D[i-1][j-1] + cost)

pprint(D)
return D[rows - 1][cols - 1]


levenshtein_distance(source, target)
13 changes: 13 additions & 0 deletions Module 1/Week 2/max_in_sliding_window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
num_list = [3, 4, 5, 1, -44, 5, 10, 12, 33, 1]
k = 3

for i in range(len(num_list) - k + 1):
sub_lst = num_list[i: i + k]

row = num_list.copy()
row[i] = f'[{num_list[i]}'
row[i + k - 1] = f'{num_list[i + k - 1]}]'

output = ', '.join([str(v) for v in row]) + f' => max {max(sub_lst)}'

print(output)
16 changes: 16 additions & 0 deletions Module 1/Week 2/word_count_in_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from pprint import pprint

file_path = 'Module 1/Week 2/P1_data.txt'


def word_count(file_path):
with open(file_path, 'r') as f:
text = f.read()
words = text.split()
word_counts = {}
for word in words:
word_counts[word] = word_counts.get(word, 0) + 1
return word_counts


pprint(word_count(file_path))

0 comments on commit 9f9d3b1

Please sign in to comment.