Autobiographical number in python?

self-descriptive numbers
is 9000000000 an autobiographical number
self number algorithm
self numbers python
self-descriptive number program in c
self number generator
numbers that describe themselves
number that describes itself

Hey all at stackoverflow! I am learning python and need to code a program which checks if a number is autobiographical. EG: 21200 is autobiographical as it has 2 0's 1 1's 2 2's 0 3's and 0 4's. This is what i have so far:

# Autobiographical numbers
a = input("Number: ")
abn = 
if a == abn:
  print(a, "is autobiographical")
else:
  print(a, "is not autobiographical")

as you can see i have left the abn variable open as i dont know exactly how to do it. I think i have to determine length of a with len(a) then use something like

[x[::1] for x in b]

but i am not quite sure as i am pretty new to python. Thanks, A no0b at python.

I use the following code to test it. There should be a better solution.

from collections import Counter
def test(x):
    if len(x) > 10:
           return False
    c = Counter(x)
    for i, v in enumerate(x):
            if int(v) != c.get(str(i), 0):
                    return False
    return True

a = input('number: '): #2.x use raw_input
if test(a):
    print(a, 'is')
else:
    print(a, 'is not')

Demo:

>>> auto
['1210', '2020', '21200', '3211000', '42101000']
>>> map(test, auto)
[True, True, True, True, True]

>>> auto = ['12321', '13213', '134', '1231', '123124543']
>>> map(test, auto)
[False, False, False, False, False]

A much better solution from the wiki:

>>> def isSelfDescribing(n):
        s = str(n)
        return all(s.count(str(i)) == int(ch) for i, ch in enumerate(s))

Find all Autobiographical Numbers with given number of digits , Hey all at stackoverflow! I am learning python and need to code a program which checks if a number is autobiographical. EG: 21200 is autobiographical as it has� Given N as the number of digits, the task is to find all the Autobiographical Numbers whose length is equal to N. An autobiographical number is a number such that the first digit of it counts how many zeroes are there in it, the second digit counts how many ones are there and so on. For example, 1210 has 1 zero, 2 ones, 1 two and 0 threes.

As Dmitry said, Rosettacode has the answer: Self-describing numbers :

def isSelfDescribing(n):
    s = str(n)
    return all(s.count(str(i)) == int(ch) for i, ch in enumerate(s))

My solution would be the following (despite being slower):

from collections import Counter
def isAutobiographical(n):
    digits = map(int, str(x))
    actualFrequency = Counter(digits)
    claimedFrequency = dict((x,y) for x,y in enumerate(digits) if y > 0)
    return actualFrequency == claimedFrequency

Self Descriptive Number, So, each number in this range is iterated and checked if it is an Autobiographical number or not. Convert the number to a string; iterate through each digit and store� A number is called Automorphic number if and only if its square ends in the same digits as the number itself. Examples : Input : N = 76 Output : Automorphic Explanation: As 76*76 = 57 76 Input : N = 25 Output : Automorphic As 25*25 = 6 25 Input : N = 7 Output : Not Automorphic As 7*7 = 49

import math

b = int(math.log(a,10))

*b is length of a. i.e., number of digits in a*

*in fact there is another tick:*

b = len(str(a))

*of course you need to check if a is a valid natural number*

Self-describing numbers, A self-descriptive number is an integer n in given base b is b digits long in which each digit at position p (the most significant digit being at position 0 and the� Given a natural number N, the task is to print the Nth Stepping or Autobiographical number. A number is called stepping number if all adjacent digits have an absolute difference of 1. The following series is a list of Stepping natural numbers:

>>> def is_autobiographical(n):
    s = str(n)
    count_digits = ''.join([str(s.count(str(i))) for i in range(len(s))])
    return s == count_digits

>>> is_autobiographical(21200)
True
>>> is_autobiographical(22)
False

In your case you could use abn = ''.join([str(str(a).count(str(i))) for i in range(len(str(a)))]) to fit your needs.

Autobiographical number in python?, My first idea was using Redis. But there are more great tools and some are even lighter, so my solution builds on zmq. For this reason you do not have to run� An Autobiographical Number is a number such that the first digit of it counts how many zeroes are there in it, the second digit counts how many ones are there and so on. For example, 21200 has 2 zero, 1 one, 2 two and 0 three and 0 four.

A047841: Autobiographical numbers, It's a simple counter which uses the fact that each number has a limited It then creates all numbers and checks their validity. Python 2.7 - 684 bytes. Here is the full set of autobiographical numbers: 1210, 2020, 21200, 3211000, 42101000, 521001000, 6210001000. This is the sequence A104786 in the Online Encyclopedia of Integer Sequences (OEIS). The OEIS, where I first encountered the autobiographical numbers. Autobiographical numbers are very cute numbers.

[challenge] autobiographical numbers, challenge by Sujay Simha. #. # Autobiographical number. #. # An autobiographical number is a number N. # such that the first digit of N counts. position 1 has value 0 and there are no 1s in the number; position 2 has value 2 and there are two 2s; position 3 has value 0 and there are zero 3s. Self-describing numbers < 100.000.000 are: 1210, 2020, 21200, 3211000, 42101000. Task Description

Self-descriptive number, Topically, 2020 is an autobiographical number. Considering a hypothetical case where the digits are treated in the� An autobiographical number is a number such that the first digit of it counts how many zeroes are there in it, the second digit counts how many ones are there and so on. An autobiographical number is like a self-descriptive number. For example, 1210 has 1 zero, 2 ones, 1 two and 0 threes. 3211000 is another example of an autobiographical number.

Comments
  • See rosettacode.org/wiki/Self-describing_numbers
  • Thanks to all who replied! i loved the help from all of you and i appreciated the link to the site. I gave zhangyangyu first choice because he showed me how to get the desired output in python 3. Thank you!