Make a for loop repeat forever

-1

1

for loops are useful in python in many ways, but one of the most common usages associated with a for loop is it's ability to repeat a certain amount of times. Like this:

for times in range(50):
    print "I'm repeating this 50 times..."

The challenge I propose to you is to make a for repeat forever. If you can find a way to do it without using a recursion, kudos to you. You can use any major version of Python. Just specify which one you are using. The goal is to do this in the smallest file size you can get, and a method that won't "break" after a while. Good luck!

Vladimir Putin

Posted 2014-06-20T13:00:16.390

Reputation: 165

perl6 -e 'for 0..* {print "$_\r"}' only 11 required characters, or perl6 -e 'print "$_\r" for 0..*' only 10 chars required ( there has to be something before for separated by whitespace 0 for 0..*) – Brad Gilbert b2gills – 2015-10-18T01:14:09.780

I didn't know you had time for code-golfing, M. Putin. – coredump – 2015-11-04T19:17:24.863

9

Ewwww, language-specific challenges. (See http://meta.codegolf.stackexchange.com/a/234/3 for my stance on this.) (This is unrelated to the reason why I closed the question, which is a site rule and not just a personal opinion.)

– Chris Jester-Young – 2014-06-20T13:18:33.680

2@ChrisJester-Young I personally think it's fair for programming-puzzles, as they usually tend to work around some feature of a specific language. You could have told the OP though, that your closing the question has nothing to do with your not liking language-specific challenges. So, @ Some Guy, the question is closed because you did not indicate who (among several correct solutions) you will pick the winner of the challenge. Votes? Code-length? Something else? – Martin Ender – 2014-06-20T13:32:19.700

1@m.buettner There, I edited my comment to say that now. Thanks! :-) – Chris Jester-Young – 2014-06-20T13:35:00.753

Befunge will surely win if this is non-language-specific code-golf. – jimmy23013 – 2014-06-20T13:58:06.743

1

@user23013 Of course. It was the winning entry in my stack overflow code golf. ;-)

– Chris Jester-Young – 2014-06-20T13:59:10.467

@ChrisJester-Young I have added basic criteria to help decided a winner. This probably isn't enough to re-open the question though... Sorry to the users who answered this question before I added goals in order to choose a winner. – Vladimir Putin – 2014-06-20T14:47:45.067

@SomeGuy Thank you. I have retagged the question as a code golf since the winning criterion is shortest code. – Chris Jester-Young – 2014-06-20T14:57:11.443

3Here's an idea to make this non-language-specific: "implement a foreach loop that repeats forever" – Justin – 2014-06-20T16:00:30.603

2@TheRare Changing the accepted answer is absolutely legitimate if a new winner comes along (by the specified criteria). Therefore, the currently accepted answer isn't even the correct one, actually. – Martin Ender – 2014-06-20T18:36:45.720

@m.buettner Well then, I take your word on that. – seequ – 2014-06-20T18:59:41.117

Wait... is this [tag:code-golf] or [tag:popularity-contest]? I'm confused. – Iszi – 2014-06-22T02:19:46.497

@Iszi Pop contest. – Vladimir Putin – 2014-06-22T02:36:41.547

The question and one tag state that it's code-golf. The previous comment and another tag state that it's pop-con. It can't be both. – Peter Taylor – 2014-06-22T07:58:29.907

It's code golf, but it would have been far better as an underhanded popularity contest. – nyuszika7h – 2014-07-05T12:09:52.983

@ChrisJester-Young Eww, here's another language-specific challenge. Doesn't look like it's getting hate for that.

– DatEpicCoderGuyWhoPrograms – 2014-07-07T16:00:53.697

1@DatEpicCoderGuyWhoPrograms King-of-the-Hill contests are a special exception, to make concessions for OPs who want to test out the entries and not have to install many language runtimes in order to do so. However, there are still ways to run language-independent KotH contests (e.g., using virtual machines), and I still encourage people to do it. – Chris Jester-Young – 2014-07-08T00:53:34.357

Answers

7

Because this was turned to a codegolf, have a 48 bytes version as a one-liner. 3 bytes could be removed from both by replacing print i with pass.

for i in __import__('itertools').count():print i

Or a 47 bytes version with two lines.

from itertools import*
for i in count():print i

Old message:

Easy it is.

def f(i = 0):
    while True:
        yield i
        i += 1
for i in f():
    print(i)

seequ

Posted 2014-06-20T13:00:16.390

Reputation: 1 714

11

This can be done quite easily by abusing floating point addition.

def frange(start, stop, step):
    r = start
    while r < stop:
        yield r
        r += step

for x in frange(0, 1e16, 1):
    print (x)

IEEE754 64-bit floats have 53 bits of mantissa (including the implicit bit), so if x > 1<<53 then x + 1 == x. 1 << 53 is approximately 9e15.

Peter Taylor

Posted 2014-06-20T13:00:16.390

Reputation: 41 901

Are you sure this won't transform to a long? – seequ – 2014-06-20T13:18:17.817

@TheRare, why would it transform to a long? That aside, a simple test shows arithmetic behaving as expected.

– Peter Taylor – 2014-06-20T13:23:30.410

That is actually pretty interesting. – seequ – 2014-06-20T13:24:40.050

6

This is an interesting way of doing it (32 chars for class x, 46 not counting the print):

class x:__iter__=next=lambda s:s
for i in x():print "Hello"

This defines an iterable whose next method never raises a StopIteration

Justin

Posted 2014-06-20T13:00:16.390

Reputation: 19 757

Actually, the next method is the __iter__ method (which is supposed to return the iterable). Both return self (s) – Justin – 2014-06-22T05:13:37.850

3

I don't usually golf in Python, probably some reason people haven't already done posted this 23-character solution.

x=[1]
for i in x:
 x+=x

Much less memory-hungry if you do x+=[i] instead. Either one hangs my 2.7.5

histocrat

Posted 2014-06-20T13:00:16.390

Reputation: 20 600

"and a method that won't "break" after a while." So that's why no one has done this. Or maybe it's because no one was smart enough to think of it. – Justin – 2014-06-20T16:28:13.450

Well, this does explode the memory very quickly, because the memory usage raises exponentially. – seequ – 2014-06-20T18:56:42.410

Yeah, the +=[i] version seems almost legitimate though, I don't know if that would break within any reasonable amount of time. Either way, not as interesting a solution as the others. – histocrat – 2014-06-20T21:12:05.977

It would probably break eventually, when the list reaches sys.maxsize items. – nyuszika7h – 2014-07-05T12:11:10.637

3

for i in iter(lambda : 0, 1): 
    print (i) # prints 0 forever 

The iter function is normally used to create an iterator from an iterable.

The iter function can also take two arguments, a callable and a sentinel value. In that case it returns an iterator that yields the result of the callable until the callable returns the sentinel value.

In this case, the callable is a lambda that always returns 0. Since it never can return the sentinel value (1), this will loop forever.

Since this is code golf, this version is 28 characters and only one line:

for i in iter(lambda:0,1):i

Tolli

Posted 2014-06-20T13:00:16.390

Reputation: 131

2

28 bytes, infinite, and no itertools

for x in iter(int,1):print 1

Shouldn't end without system program cutoffs (i.e. not my fault).

22 bytes without printing anything:

for x in iter(int,1):1

Rɪᴋᴇʀ

Posted 2014-06-20T13:00:16.390

Reputation: 7 410

1

Itertools, to the rescue:

from itertools import count
for i in count():
    print i

or

from itertools import repeat
for i in repeat(42):
    print i

ɐɔıʇǝɥʇuʎs

Posted 2014-06-20T13:00:16.390

Reputation: 4 449

Just an fyi, itertools.count is the same as my answer and itertools.repeat is almost the same as my original answer. – seequ – 2014-06-20T13:13:50.457

@TheRare I suppose, but I'd rather use itertools.count in an production environment than something home-brew. – ɐɔıʇǝɥʇuʎs – 2014-06-20T13:15:03.703

Absolutely. Just wanted to inform that the implementation is the same. – seequ – 2014-06-20T13:16:30.667

1

39 Characters, no itertools and no crashing

def f():
 while 1:yield
for i in f():1

Itertools is unnecessary, you can define your own generator.

Ian D. Scott

Posted 2014-06-20T13:00:16.390

Reputation: 1 841

1You don't need to use pass - 1 or i will do just as well. – isaacg – 2014-07-04T22:16:12.117

1

Python 2 & 3: 37 characters

class P:__getitem__=id
for t in P():1

¡No infinite memory required!

Juan I Carrano

Posted 2014-06-20T13:00:16.390

Reputation: 71

0

Python 3.3: 2 lines, 29 characters

j=[1]  
for i in j: j.append(1)

Does this break? I've run it for a short while, doesn't seem to hang.

jimsug

Posted 2014-06-20T13:00:16.390

Reputation: 101

2This will run out of memory, just like histocrat's version. – Martin Ender – 2014-06-21T14:31:36.427

@m.buettner indeed it will. Hmm. – jimsug – 2014-06-21T14:34:12.337