Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6647949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:35:54+00:00 2026-05-26T00:35:54+00:00

I am currently struggling with an assignment. The solution would input a txt file

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:35:54+00:00Added an answer on May 26, 2026 at 12:35 am

    Split your string into a list before you map it. map() is for lists, sets, and dicts, not strings.

    word_list = words_str.split(" ")
    

    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:

    pal_count = {
        x: word_list.count(x)  # reduce-ish
        for x in word_list     # map-ish
        if x == x[::-1]        # filter-ish
        }
    for x, y in pal_count.iteritems():
        print x, y             # print the results!
    

    Breaking it down…

    1. Catch this in a dictionary object to print it later: pal_count = {
    2. Define the return objects: 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.
    3. Iterate through our list with a for loop, assigning the current value to ‘x’: for x in word_list
    4. We only want to return palindromes, so we add a comparison operator to filter out bad values: if x == x[::-1] # cool logic, btw
    5. Hurray! }

    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:

    pal_count = dict()
    for x in word_list:                     # same loop
        if x == x[::-1]                     # is this a palindrome?
            if x in pal_count:              # have we seen before?
                pal_count[x] += 1
            else:                           # this one is new!
                pal_count.setdefault(x, 1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My math classes are far behind, and I'm currently struggling to find a decent
I am currently struggling with a design issue involving REST. The application I am
Because I'm currently struggling to learn IBM Rational ClearCase, I'd like to hear your
I am currently struggling to identify where the bottleneck is in an enterprise web
I am currently struggling with a side project I hope you can help me
I'm currently struggling to use UI elements in Interface Builder. I keep trying to
I am currently struggling with HTTP Session replication on tomcat with complex objects. Some
I am currently struggling to get the following code to compile. First the header
I am currently struggling with about 5 nested if-statements and its becoming quite confusing
I'm currently struggling with one of the bindings I'm trying to add to my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.