Quantcast
Viewing latest article 23
Browse Latest Browse All 40

Map reduce problem in python

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 3cab 1dad 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?


Viewing latest article 23
Browse Latest Browse All 40

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>