-2

I need to make an infinite list of numbers and list them like this:

[12345678910111213141516171819...]

or

[1,2,3,4,5,6,7,8,9,1,1,1,2,1,3,1,4,1,5,1,6,1,7,1,8,1,8,9....]

So that the numbers are pasted together and can be separately indexed(find the position of a number eg. 15th number would be 2)

So the user would have to input some numbers like

15 2022 1410169200 2147483646

and my program would output

2 0 1 2

I understand now that I can't use some sort of big list, but have to make a searching algorithm of sorts?

Could anyone help?

Eludum
  • 5
  • 1
  • 4

4 Answers4

0

I have to say that as hard as it is: You cannot have an infinite list in python. Python's lists actually require the storage for each element. Since there's no machine with infinite memory, there's no infinite python list.

However, what you can do is two things:

  1. write a function that maps a natural number (what you call index) to a natural number in (0;10). Writing that function is rather easy. You know how many numbers of each length in digits there are, so finding the right answer doesn't actually require having a pre-generated list -- it can be done for every index you actually require
  2. you can use that very same function to implement a [], though that is somewhat of a more advanced python feature.

EDIT:

Definitely don't go for a huge list! Just write a function that based on what the user enters returns the right digit... you know there's 9 numbers with 1 digit, there's 100-10 numbers with two digits, there's 10^3-10^2 numbers with three digits and so on. Write down the number of digits in your list on a piece of paper using normal math notation, and you'll quickly develop a method to get the 15001st digit in your list without having to calculate all the 15000 before it

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • Thank you just realized that, the problem is the list needs to be infinite in order for it to work maybe i'll try to make a list until 100000 an see from there on if I can paste it together and index it? It would maybe suffice. – Eludum Feb 11 '15 at 09:52
  • @Eludum: the list *never* needs to be infinite (otherwise, by definition, you're not dealing with an algorithm). My feeling is that you "just" need something that maps an arbitrarily high index number to a digit, which you can do without having a *list* ("list" is a specific python thing that can't be infinitively long), but something that just takes an index and returns an element. – Marcus Müller Feb 11 '15 at 09:54
  • Your feeling is right I need to index some big numbers but of course not infinitly big which are inputted by the user like this a = int(input()) b=int(input()),... Input 15 2022 1410169200 2147483646 Output 2 0 1 2 – Eludum Feb 11 '15 at 09:57
  • Definitely don't go for a huge list! Just write a function that based on what the user enters returns the right digit... you know there's 9 numbers with 1 digit, there's 100-10 numbers with two digits, there's 10^3-10^2 numbers with three digits and so on. Write down the number of digits in your list on a piece of paper using normal math notation, and you'll quickly develop a method to get the 15001st digit in your list without having to calculate all the 15000 before it – Marcus Müller Feb 11 '15 at 10:00
  • This helps a lot thanks Marcus, just one more question: so you are saying I shouldnt make a list at all? Just some sort of indexing algorithm? – Eludum Feb 11 '15 at 10:03
  • @Eludum: exactly :) My "trick" when considering things like these is always trying to look at things from at least two perspectives; in this case "list" actually just felt like "function(int)->int", so I proposed this indexing algorithm approach. – Marcus Müller Feb 11 '15 at 10:35
0

I'm not sure to understand, you can't list infinity, just chuck norris can do it! :p More seriously, your command can't stop because infinity never stop so it's infinite loop. If you want you can list a lot of number for example:

big_list = [i for i in range(10000000)]

But I'm afraid you can't do better...

Tiket_dev
  • 141
  • 1
  • 11
0

Apart from the obvious problems with infinity in finite space this might be what you want.

list = []
for x in xrange(100):   # You could put any number you want until you hit memory limit
    var = 'stored data ' + str(x**2)    # stores text and a math function
    list.append(var)    # Append simply adds current value to the end of the list
    print list[x]    #prints your value for debug purposes

This will create an indexed list withh all the interated data you want, to get your indexed data just type

print list[5] # or whatever number you want. You could also loop through it
for n in xrange(len(list) / 4): print list[x*4] # prints every fourth value

The while version would be:

x = 10
while x == 10: list.append('your data')

But it will just hit memory limit and crash.

25r43q
  • 613
  • 4
  • 16
  • Thank you victor but this is not exactly what I'm looking for it would be more like: the user inputing a number a=int(input()), b= int(input()),... input 15 2022 1410169200 2147483646 output 2 0 1 2 – Eludum Feb 11 '15 at 10:07
  • Ah, and i guess you want it to be pseudorandom at least? In that case you can use a list of prime numbers and a rand function. So you have primes = [3,5,7,11,13,17 etc] and the result becomes something like input * primes [random_number] – 25r43q Feb 11 '15 at 10:09
  • No the user is free to choose which numbers they want that's not even the problem atm. It's more finding the right index for the pasted numbers. – Eludum Feb 11 '15 at 10:10
  • Ok. Without knowing the exact use-case, I think a populated list at least is flexible enough for the task at hand. – 25r43q Feb 11 '15 at 10:12
0

You can do this sort of thing with a generator. Look at the function with yield below.

def natural_numbers():
    n = 0
    while True:
        n += 1
        yield n

def find_nth_character(n):
    numbers = ""
    gen = natural_numbers()

    while len(numbers) < n:
        numbers += str(gen.next())

    print numbers
    return numbers[n-1]


def main():
    print find_nth_character(15)

if __name__ == '__main__':
    main()

This code just stores a big string. If you're going to be looking for a very high index number, you'll probably want to find some other method for counting the number of characters. Then you can discard the characters before your target, and don't have to worry about storing a potentially huge string.

Jon Surrell
  • 9,444
  • 8
  • 48
  • 54
  • Thank you for helping Jon, this helps out quite a bit. Ill post later if my issue is resolved. – Eludum Feb 11 '15 at 10:13
  • @Jon_Surrel Sorry Jon completely forgot about that. I've finally realized that my problem is finding the nth digit in a row of integers and you're code, how many times I fiddle with it doesn't seem to work. I've finally found an algorithm for programming it. The only thing left now is to code it. Your code has been really helpful and I do thank you for that I will now edit my question and select your answer as right. TLDR thank you for the partial code. – Eludum Feb 12 '15 at 12:42
  • I just wanted to add that you're code indeed doesn't work for larger numbers. But I need to use larger numbers for my assignment, nevertheless you've been very helpfull. – Eludum Feb 12 '15 at 12:46