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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:55:37+00:00 2026-05-11T20:55:37+00:00

I have a generator that takes a number as an argument and yields other

  • 0

I have a generator that takes a number as an argument and yields other numbers.
I want to use the numbers yielded by this generator and pass them as arguments to the same generator, creating a chain of some length.

For example, mygenerator(2) yields 5, 4 and 6. Apply mygenerator to each of these numbers, over and over again to the numbers yielded. The generator always yields bigger numbers than the one passed as argument, and for 2 different numbers will never yield the same number.

mygenerator(2): 4 5
mygenerator(4) : 10 11 12
mygenerator(5): 9 300 500

So the set (9,10,11,12,300,500) has “distance” 2 from the original number, 2. If I apply it to the number 9, I will get a set of numbers with distance “3” from the original 2.

Essentially what I want is to create a set that has a specified distance from a given number and I have problems figuring out how to do that in Python. Help much appreciated 🙂

  • 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-11T20:55:37+00:00Added an answer on May 11, 2026 at 8:55 pm

    This solution does not require to keep all results in memory: (in case it doesn’t fit in memory etc)

    def grandKids(generation, kidsFunc, val):
      layer = [val]
      for i in xrange(generation):
        layer = itertools.chain.from_iterable(itertools.imap(kidsFunc, layer))
      return layer
    

    Example:

    def kids(x): # children indices in a 1-based binary heap
      yield x*2
      yield x*2+1
    
    >>> list(grandKids(3, kids, 2))
    [16, 17, 18, 19, 20, 21, 22, 23]
    

    Btw, solution in Haskell:

    grandKids generation kidsFunc val =
      iterate (concatMap kidsFunc) [val] !! generation
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pseudo random number generator (PRNG) class that I want to unit
So lets assume I have a Gaussian random number generator that takes 2 parameters
I have a SQL query that takes a date and reports number of pallets
I have an application that takes some input and generates configuration files as output.
I have a generator that generates a series, for example: def triangle_nums(): '''Generates a
I have a form generator that makes all the dbEdits the same width. The
I'm developing a system, and I have build a code generator that emits a
Suppose I have a function that returns a closure: sub generator { my $resource
I have code that generates this error: ** Message: pygobject_register_sinkfunc is deprecated (GstObject) Traceback
I have homework that should use fork() and wait() system calls: 1) Write two

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.