Skip to content

Commit

Permalink
Changed output of main files to match the python tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
GuiMacielPereira committed Sep 20, 2023
1 parent cdcb67a commit 6ceefeb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
28 changes: 21 additions & 7 deletions exercises-python/Gui_Pereira/ex01_basics/main.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
#!/usr/bin/env python3
import numpy as np

from pathlib import Path
import sys


def main():
file_path = Path(__file__).parent.absolute() / 'holmes.txt'
# file_path = Path(__file__).parent.absolute() / 'holmes.txt'
file_path = Path(sys.argv[1])
full_text = file_path.read_text()

for c in '.,?"!():': # Not very elegant
full_text = full_text.replace(f'{c}', '')

full_text = full_text.replace('-', ' ') # Treat hyphenated words seperatly
full_text = full_text.replace("'", ' ') # Treat hyphenated words seperatly
full_text = full_text.lower() # Pass everything to lowercases
word_list = full_text.split() # Split words seperated with any white space
word_list = [w for w in word_list if len(w)>4] # Consider words longer than 4 ch

words, counts = np.unique(word_list, return_counts=True) # Count ocurrences
# Count words
word_count = {}
for word in word_list:
try:
word_count[word] += 1
except KeyError:
word_count[word] = 1

# Sort in decreasing order
word_count = dict(sorted(word_count.items(), key=lambda x : -x[1]))

print(f'{"Word":10} : Usage')
for i, word in enumerate(word_count):
print(f'{word:10} : {word_count[word]}')
if i==30: break

sorted_indices = np.argsort(-counts)
for i, (w, c) in enumerate(zip(words[sorted_indices], counts[sorted_indices])):
print(f'{w:10} : {c}')
if i==30:break # Print first 30 words

if __name__ == "__main__":
main()
8 changes: 4 additions & 4 deletions exercises-python/Gui_Pereira/ex02_oo_basics/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def __init__(self, side1):
self.area = self.side1**2

def __str__(self):
return f'S{self.side1}'
return f'Square {self.side1}'

class Rectangle:
def __init__(self, side1, side2):
Expand All @@ -20,7 +20,7 @@ def __init__(self, side1, side2):
self.area = self.side1 * self.side2

def __str__(self):
return f'R{self.side1}x{self.side2}'
return f'Rectangle {self.side1}x{self.side2}'

class Circle:
def __init__(self, radius):
Expand All @@ -30,7 +30,7 @@ def __init__(self, radius):
self.area = self.radius**2 * math.pi

def __str__(self):
return f'C{self.radius}'
return f'Circle {self.radius}'

class Triangle:
def __init__(self, base, height):
Expand All @@ -41,7 +41,7 @@ def __init__(self, base, height):
self.area = self.base * self.height / 2

def __str__(self):
return f'T{self.base}x{self.height}'
return f'Triangle {self.base}x{self.height}'

class ShapeSorter:

Expand Down

0 comments on commit 6ceefeb

Please sign in to comment.