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 878365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:45:32+00:00 2026-05-15T11:45:32+00:00

For example, say I have string like: duck duck duck duck goose goose goose

  • 0

For example, say I have string like:

duck duck duck duck goose goose goose dog 

And I want it to be as sparsely populated as possible, say in this case

duck goose duck goose dog duck goose duck

What sort of algorithm would you recommend? Snippets of code or general pointers would be useful, languages welcome Python, C++ and extra kudos if you have a way to do it in bash.

  • 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-15T11:45:33+00:00Added an answer on May 15, 2026 at 11:45 am

    If I understood correctly your definition of “sparse”, this function should be exactly what you want:

    # python ≥ 2.5
    import itertools, heapq
    
    def make_sparse(sequence):
        grouped= sorted(sequence)
        item_counts= []
        for item, item_seq in itertools.groupby(grouped):
            count= max(enumerate(item_seq))[0] + 1
            item_counts.append( (-count, item) ) # negative count for heapq purposes
        heapq.heapify(item_counts)
    
        count1, item1= heapq.heappop(item_counts)
        yield item1; count1+= 1
        while True:
            try:
                count2, item2= heapq.heappop(item_counts)
            except IndexError: # no other item remains
                break
            yield item2; count2+= 1
            if count1 < 0:
                heapq.heappush(item_counts, (count1, item1))
            item1, count1= item2, count2
    
        # loop is done, produce remaining item1 items
        while count1 < 0:
            yield item1; count1+= 1
    
    if __name__ == "__main__":
        # initial example
        print list(make_sparse(
            "duck duck duck duck goose goose goose dog".split()))
        # updated example
        print list(make_sparse([
            'duck', 'duck', 'duck', 'duck', 'duck', 'duck',
            'goose', 'goose', 'goose', 'goose', 'dog', 'dog']))
        # now a hard case: item 'a' appears more than:
        # > total_len//2 times if total_len is even
        # > total_len//2+1 times if total_len is odd
        print list(make_sparse("aaaaaabbcc"))
    

    These examples produce this output:

    ['duck', 'goose', 'duck', 'goose', 'duck', 'dog', 'duck', 'goose']
    ['duck', 'goose', 'duck', 'goose', 'duck', 'dog', 'duck', 'goose', 'duck', 'dog', 'duck', 'goose']
    ['a', 'b', 'a', 'c', 'a', 'b', 'a', 'c', 'a', 'a']
    

    A subtle note: in the first and second examples, reversing the output order might look more optimal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a string like this some3random5string8 I want to insert spaces after
Let's say for example that I have a regular expression like this: The quick
Say for example I have the following string: var testString = Hello, world; And
i have problem use link_to_remote link_to_remote document example say link_to_remote Delete this post, :update
Say I have a site on http://example.com . I would really like allowing bots
Say I have this url: http://site.example/dir/ In this folder I have these files: test.ascx.cs
Say I have a class ObjectA (a view controller for example), that does this
Let's say I have String Table that have a few strings (like mother, father,
I'm new into PHO DOM Parser. I have a string like this : $coded_string
Let's say I have a string that I would like to obfuscate in 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.