From 5a81f32992e004a3e49e593df5d1672ec867de17 Mon Sep 17 00:00:00 2001 From: NoamOssia Date: Sun, 1 Dec 2019 13:26:47 +0200 Subject: [PATCH 1/2] Added the python script and the sample file --- Python-Home-Challenges/NoamChallenge.py | 19 +++++++++++++++++++ Python-Home-Challenges/sample.txt | 1 + 2 files changed, 20 insertions(+) create mode 100644 Python-Home-Challenges/NoamChallenge.py create mode 100644 Python-Home-Challenges/sample.txt diff --git a/Python-Home-Challenges/NoamChallenge.py b/Python-Home-Challenges/NoamChallenge.py new file mode 100644 index 0000000..1e464a9 --- /dev/null +++ b/Python-Home-Challenges/NoamChallenge.py @@ -0,0 +1,19 @@ + +with open('sample.txt', 'r') as file: + data = file.read().replace('\n', '') + print("The content of the file is:\n\n" + data) + + +import re +words = re.sub("[^\w]", " ", data).split() + +for word in words: + from collections import Counter + word_counts = Counter(words) + +MOword = word_counts.most_common(1)[0][0] +MOtimes = word_counts.most_common(1)[0][1] + +print("\nThe most occuring word is {} and it occurs {} times".format(MOword,MOtimes)) + + diff --git a/Python-Home-Challenges/sample.txt b/Python-Home-Challenges/sample.txt new file mode 100644 index 0000000..3330968 --- /dev/null +++ b/Python-Home-Challenges/sample.txt @@ -0,0 +1 @@ +I would love to try or hear the sample audio your app can produce. I do not want to purchase, because I've purchased so many apps that say they do something and do not deliver. Can you please add audio samples with text you've converted? I'd love to see the end results.Thanks!sfsdfasf \ No newline at end of file From 20f091df1dbdff238c7bbc18e0779d8e50b7bbd5 Mon Sep 17 00:00:00 2001 From: NoamOssia Date: Sun, 1 Dec 2019 18:04:12 +0200 Subject: [PATCH 2/2] Changed according to the code review --- Python-Home-Challenges/NoamChallenge.py | 32 +++++++++++-------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/Python-Home-Challenges/NoamChallenge.py b/Python-Home-Challenges/NoamChallenge.py index 1e464a9..eb799a5 100644 --- a/Python-Home-Challenges/NoamChallenge.py +++ b/Python-Home-Challenges/NoamChallenge.py @@ -1,19 +1,15 @@ -with open('sample.txt', 'r') as file: - data = file.read().replace('\n', '') - print("The content of the file is:\n\n" + data) - - -import re -words = re.sub("[^\w]", " ", data).split() - -for word in words: - from collections import Counter - word_counts = Counter(words) - -MOword = word_counts.most_common(1)[0][0] -MOtimes = word_counts.most_common(1)[0][1] - -print("\nThe most occuring word is {} and it occurs {} times".format(MOword,MOtimes)) - - +def most_frequent(List): + dict = {} + count, itm = 0, '' + for item in reversed(List): + dict[item] = dict.get(item, 0) + 1 + if dict[item] >= count : + count, itm = dict[item], item + return(itm) + +file = open('sample.txt','r') +data = file.read().replace('\n', '') +words = data.split() + +print(f"The most occuring word is: {most_frequent(words)}") \ No newline at end of file