Python Ruler Sequence Generator

MedvidekPu

I have been struggling for a long time to figure how to define a generator function of a ruler sequence in Python, that follows the rules that the first number of the sequence (starting with 1) shows up once, the next two numbers will show up twice, next three numbers will show up three times, etc.

So what I am trying to get is 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7 etc.

I understand that the way to do this is to have two separate count generators (itertools.count(1)) and then for every number in one generator yield number from the other generator:

def rul():
    num = itertools.count(1) 
    repeator = itertools.count(1)
    for x in range(next(repeator)):
        yield from num

But if I hit next() on this function, I get back just the regular 1,2,3,4.. sequence...

Any help on this would be appreciated.

AntiMatterDynamite

how about regular old python with no itertools?

def ruler():
    counter = 1
    n = 1
    while True:
        for i in range(counter):
            for j in range(counter):
                yield n
            n += 1
        counter += 1

in my humble opinion this is the clearest and most straighforward solution for these types of situations

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Optimising a fibonacci sequence generator python

How to change python generator into Keras Sequence object?

Python generator function to loop over iterable sequence while eliminating duplicates

Go Fibonacci sequence generator

Generator vs Sequence object

Correct the sequence generator start number?

Random Sequence Generator in C#

Duplicate generator sequence hibernate on subclasses

Custom sequence generator for Hibernate 5

Hibernate ignores initialValue for sequence generator

Write an generator/iterator expression for this sequence

Sequence to sequence modeling in python

VSCode adding a ruler for python files but not C files. Why?

Is setting allocateSize as 1 in Sequence generator same as using an Identity Generator?

How to make simple python generator from nested generator, "Generator in Generator"?

Can I intercept the F# sequence generator?

Hibernate/JPA persist mapping OneToOne with sequence generator

Reduce a sequence of items provided by a generator in JavaScript

How to correctly associate an id generator sequence with a table

Is it a bad idea using a single sequence generator in PostgreSQL?

Extreme Performance issue with Generic Sequence and Generator

Basic JavaScript - Fibonacci Sequence Generator Not Working

Invalid Object Name for Hibernate Sequence Generator

Spring JPA + Boot +Sequence Generator always 0

Sequence Diagram generator for Ruby or Ruby on Rails

How to create a Javascript generator to calculate the Fibonacci sequence?

sequence generator in plsql code in oracle apps

Unique sequence number generator in AWS without RDS?

Fibonacci sequence generator with fork and shared memory