[Tutor] Fw: Issues With Map Reduce
alan.gauld at btinternet.com
Mon Oct 3 22:36:04 CEST 2011
On 26/09/11 18:00, StevenJ Hennessy wrote:
> I am currently struggling with a homework 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
I assume this should be cac?
> dad 1
> here is what I have so far
> def palendrome(string):
> palendromes = 
> for word in string.split(" "):
> if (word == word[::-1]):
> return palendromes
This seems pretty fair
> string = "abc abd bab tab cab tat yay uaefdfdu"
And the purpose of this is?
> print map(lambda x: palendrome(x), ["bab abc dab bab bab dad crap pap
> pap "])
> #returns a list of lists [['bab', 'bab', 'bab', 'dad', 'pap', 'pap', '']]
That last element seems odd...
Why are you applying map() to a list containing just one element?
> #Here is my attempt so far at the reduce section
> def p(lists):
> for list in lists:
> set_h = set(list)
> return set_h
The problem description mentioned using a dictionary.
I don't see any dictionaries?
> #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', '']])
Again, reduce isn't really doing anything here, you are just applying
the function to a single element in the list.
> Am I on the right track?
Sadly no. You are on track to produce a solution of sorts but not one
that matches your original problem description/requirements statement.
You need to use map/reduce/a dictionary. Now, I'm not sure the
map/reduce/dictionary solution is a good idea, but it is the one you
have been asked to write.
Author of the Learn to Program web site
More information about the Tutor