- Joined
- Sep 26, 2011
- Messages
- 1
- Reaction score
- 0
Hello,
I am currently struggling with an assignment. I need to use Map reduce to create a dictionary of palendromes -> number of palendrome
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 palendrome(string):
palendromes = []
for word in string.split(" "):
if (word == word[::-1]):
palendromes.append(word)
return palendromes
string = "abc abd bab tab cab tat yay uaefdfdu"
print map(lambda x: palendrome(x), ["bab abc dab bab bab dad crap pap pap "])
# current 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 palendromes found. Then run a count of the palendroms 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?
Thanks
I am currently struggling with an assignment. I need to use Map reduce to create a dictionary of palendromes -> number of palendrome
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 palendrome(string):
palendromes = []
for word in string.split(" "):
if (word == word[::-1]):
palendromes.append(word)
return palendromes
string = "abc abd bab tab cab tat yay uaefdfdu"
print map(lambda x: palendrome(x), ["bab abc dab bab bab dad crap pap pap "])
# current 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 palendromes found. Then run a count of the palendroms 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?
Thanks