I am currently struggling with an assignment. The solution would input a txt file and run through counting the number of palindromes and their frequency. I need to use Map reduce to create to do so
For example: the string “bab bab bab cab cac dad” would output:
bab 3
cab 1
dad 1
Here is what I have so far
def palindrome(string):
palindromes = []
for word in string.split(" "):
if (word == word[::-1]):
palindromes.append(word)
return palindromes
string = "abc abd bab tab cab tat yay uaefdfdu"
print map(lambda x: palindrome(x), ["bab abc dab bab bab dad crap pap pap "])
Currently prints
[['bab', 'bab', 'bab', 'dad', 'pap', 'pap', '']]
Here is my attempt so far at the reduce section
def p(lists):
for list in lists:
set_h = set(list)
return set_h
with the p function I want to create a set of all palindromes found. Then run a count of the palindromes on the list and make a dict out of this
print reduce(p, [['bab', 'bab', 'bab', 'dad', 'pap', 'pap', '']])
Am I on the right track?
Split your string into a list before you map it. map() is for lists, sets, and dicts, not strings.
Avoid using map-filter-reduce unless your assignment dictates it; GVR says so. The proper solution uses Python’s list comprehension syntax. In fact, you can do it with a pretty nasty one-liner:
Breaking it down…
pal_count = {x: word_list.count(x)We use key:value syntax to associate the palindrome, x, with its number of occurrences. count() is like a built-in reduce function for lists.for x in word_listif x == x[::-1] # cool logic, btw}By the way, I’m only doing your homework because I never did mine.
The slower, less flexible, less portable, less awesome equivalent uses nested for loops: