Skip to content

Instantly share code, notes, and snippets.

@ferbass
Created May 5, 2020 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferbass/f8b356e11ab242a0375c7e9af9c96dde to your computer and use it in GitHub Desktop.
Save ferbass/f8b356e11ab242a0375c7e9af9c96dde to your computer and use it in GitHub Desktop.
1.3 Hidden Messages in the Replication Origin (Part 2) 10 out of 13 steps passed
# Input: A string Text and an integer k
# Output: A list containing all most frequent k-mers in Text
def FrequentWords(Text, k):
words = []
freq = FrequencyMap(Text, k)
m = max(freq.values())
for key in freq:
if freq[key] == m:
words.append(key)
return words
def FrequencyMap(Text, k):
freq = {}
n = len(Text)
for i in range(n - k + 1):
Pattern = Text[i:i + k]
freq[Pattern] = 0
for key in freq:
freq[key] = PatternCount(Text, key)
return freq
def PatternCount(Text, Pattern):
count = 0
for i in range(len(Text)-len(Pattern)+1):
if Text[i:i+len(Pattern)] == Pattern:
count = count+1
return count
@ferbass
Copy link
Author

ferbass commented May 5, 2020

Finally, it remains to find every key in the frequency map whose corresponding value is equal to the maximum m. To do so, we will need to range over all keys in freq. You learned about applying a for loop to a list in Unit 5 of Codecademy. The same syntax,

for key in freq:
ranges over the keys of our dictionary (you don't have to use the word "key", you can use any variable name you like).
We are now just about ready to solve the Frequent Words Problem. All that remains is to append any keys to our list words if the key's corresponding value in freq is equal to m. We leave this to you in the FrequentWords() function below.

def FrequentWords(Text, k):
words = []
freq = FrequencyMap(Text, k)
m = max(freq.values())
for key in freq:
# add each key to words whose corresponding frequency value is equal to m
return words
Code Challenge (Practice): Fill in your completed FrequentWords() function below.

Click here for this problem's test datasets.

Sample Input:
ACGTTGCATGTCGCATGATGCATGAGAGCT
4
Sample Output:
CATG GCAT
Write a program, test using stdin → stdout

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment