LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   python overflow error (https://www.linuxquestions.org/questions/programming-9/python-overflow-error-4175455440/)

otkaz 03-24-2013 11:27 PM

python overflow error
 
I keep getting
OverflowError: Python int too large to convert to C long
on a python script when I run it on 32 bit systems but it works as expected on 64bit
Code:

for number in xrange(int("0101010101"), int("9898989898")+1):
        number = `number`.zfill(10)
        prev = ''
        i = 0
        for c in `number`:
                if c in prev:
                        break
                else:
                        i += 1
                        prev = c
                if i>=len(`number`):
                        print number

Here is a perl equivalent which works on both 32bit and 64bit
Code:

my $num = "";
for $num ("0101010101" .. "0101989898"){
        if ($num =~ /00|11|22|33|44|55|66|77|88|99/o) {
                ++$num;}
        else {
                print "$num\n";}}

I'm trying to use this on a raspberry pi which is 32bit I need to do this in python if posible. Is there an easy way to get around this error?

pan64 03-25-2013 02:56 AM

I would make something like this:
Code:

for number in xrange(int("0101010101"), int("9898989898")+1):
  number = `number`.zfill(10)
  n = str(number)
  found = 0
  for c in range(1, len(n) -1):
    found += n[c] == n[c+1]
  if not found:
    print number


otkaz 03-25-2013 11:40 PM

Quote:

Originally Posted by pan64 (Post 4918173)
I would make something like this:
Code:

for number in xrange(int("0101010101"), int("9898989898")+1):
  number = `number`.zfill(10)
  n = str(number)
  found = 0
  for c in range(1, len(n) -1):
    found += n[c] == n[c+1]
  if not found:
    print number


Thanks for the suggestion but still get overflow errors with your code. The problem seems to be with xrange. I ended up doing this which is really messy but works.
Code:

import itertools

for number in itertools.count(int("0101010101"), 1):
        number = `number`.zfill(10).strip('L')
        prev = ''
        i = 0
        if number > "9898989898":
                break
        for c in `number`:
                if c in prev:
                        break
                else:
                        i += 1
                        prev = c
                if i>=len(`number`):
                        print number

The .strip('L') is there because for some reason python after counting past 3000000000 starts adding a L to the end.


All times are GMT -5. The time now is 08:13 AM.