Skip to content Skip to sidebar Skip to footer

How To Generate Numbers Based On A Pattern In Python

I have to generate integers based on a pattern. For example, I have to generate all the numbers that contain only 4's and 0's and that start with one or more 4's and end with zero

Solution 1:

Assuming that your remark about 4s and 0s was just an example and you really want a generic solution to "generate integers based on a pattern" I would suggest the string from regex generator family of libraries. Here is one for python: https://github.com/asciimoo/exrex

As seen in the readme, for your example you can do:

import exrex
(int(n) for n in exrex.generate('4+0*'))

Solution 2:

This should work:

[int("4"*i + "0"*(n-i)) for n in range(0,31) for i in range(1,n+1)]

It generates 465 distinct integers:

4, 40, 44, 400, 440, ..., 444444444444444444444444444440, 444444444444444444444444444444

On Edit: A recursive approach works in the more general case:

def multiRepeats(chars,n,initial = True):
    strings = []
    if len(chars) == 0 or n == 0: return [""]
    c = chars[0]
    rest = chars[1:]
    base = 1 if initial else 0
    for i in range(base,n+1):
        head = c * i
        strings.extend([head + tail for tail in multiRepeats(rest,n-i,False)])
    return strings

def digitRepeats(digitString,n):
    return sorted([int(s) for s in multiRepeats(digitString,n)])

For example

>>> digitRepeats("420",4)
[4, 40, 42, 44, 400, 420, 422, 440, 442, 444, 4000, 4200, 4220, 4222, 4400, 4420, 4422, 4440, 4442, 4444]

To answer your original post, evaluate digitRepeats("40",30)


Solution 3:

Your question is way understated. There are infinite numbers in your pattern, so you cannot list them all. In order to show how your question should be improved I'll answer your question in a way that probably is not what you want, but satisfies the requirements.

Your pattern can be written as 4[40]*. There are infinite numbers with the form 4+ which satisfies your pattern. Since you will never finish these numbers, a generator that outputs only those ones satisfies your condition (you cannot have all but you can have many numbers).

from itertools import count, islice
numbers = (int('4' * i) for i in count(1))

# get 10 numbers
print list(islice(numbers, 10))
# [4, 44, 444, 4444, 44444, 444444, 4444444, 44444444, 444444444, 4444444444]

moral of the story: you should understand your problem before asking for a solution.


Solution 4:

Generate all numbers that satisfy your pattern:

def mygen():
    i = 1
    while True:
        # turn to binary, skip '0b' then change 1s to 4s and
        # finally turn it back to int
        yield int(bin(i)[2:].replace('1','4'))
        i += 1

Since you basically have only two digits '4' and '0' you can use binary representation to get all possible outcomes.
Note that this generator runs infitely!
To get the first 100 numbers use it like this:

gen = mygen()
first100 = [next(gen) for _ in xrange(100)]

Post a Comment for "How To Generate Numbers Based On A Pattern In Python"