47

How can I generate the sequence of numbers "1,2,5,6,9,10......" and so until 100 in Python? I even need the comma (',') included, but this is not the main problem.

The sequence: every number from 1..100, divisible by 4 with remainder 1 or 2.

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
user1460818
  • 491
  • 1
  • 4
  • 4

9 Answers9

41

Every number from 1,2,5,6,9,10... is divisible by 4 with remainder 1 or 2.

>>> ','.join(str(i) for i in xrange(100) if i % 4 in (1,2))
'1,2,5,6,9,10,13,14,...'
20
>>> ','.join('{},{}'.format(i, i + 1) for i in range(1, 100, 4))
'1,2,5,6,9,10,13,14,17,18,21,22,25,26,29,30,33,34,37,38,41,42,45,46,49,50,53,54,57,58,61,62,65,66,69,70,73,74,77,78,81,82,85,86,89,90,93,94,97,98'

That was a quick and quite dirty solution.

Now, for a solution that is suitable for different kinds of progression problems:

def deltas():
    while True:
        yield 1
        yield 3
def numbers(start, deltas, max):
    i = start
    while i <= max:
        yield i
        i += next(deltas)
print(','.join(str(i) for i in numbers(1, deltas(), 100)))

And here are similar ideas implemented using itertools:

from itertools import cycle, takewhile, accumulate, chain

def numbers(start, deltas, max):
    deltas = cycle(deltas)
    numbers = accumulate(chain([start], deltas))
    return takewhile(lambda x: x <= max, numbers)

print(','.join(str(x) for x in numbers(1, [1, 3], 100)))
Oleh Prypin
  • 33,184
  • 10
  • 89
  • 99
  • One liner, thanks! That's really nice! Initially I though something of this 'odd = []' 'even = []' 'for c in range(0,100):' 'if c%2==0:' 'odd.append(c)' 'for c in range(0,100):' 'if c%2==1:' 'even.append(c)' 'for c in range(0, 50):' 'if c % 2 == 0:' 'print(even[c],end=",")' 'else:' 'print(odd[c],end=",")' Yeah, I'm a newcomer. But I knew there must be better ways! Thanks guys! – user1460818 Jun 16 '12 at 16:19
  • Further description of the final function would be greatly appreciated. – Wolfpack'08 Dec 10 '18 at 01:37
14

Includes some guessing on the exact sequence you are expecting:

>>> l = list(range(1, 100, 4)) + list(range(2, 100, 4))
>>> l.sort()
>>> ','.join(map(str, l))
'1,2,5,6,9,10,13,14,17,18,21,22,25,26,29,30,33,34,37,38,41,42,45,46,49,50,53,54,57,58,61,62,65,66,69,70,73,74,77,78,81,82,85,86,89,90,93,94,97,98'

As one-liner:

>>> ','.join(map(str, sorted(list(range(1, 100, 4))) + list(range(2, 100, 4))))

(btw. this is Python 3 compatible)

poke
  • 369,085
  • 72
  • 557
  • 602
2

This works by exploiting the % properties of the list rather than the increments.

for num in range(1,100):
    if num % 4 == 1 or num % 4 ==2:
        n.append(num)
        continue
    pass
2

using numpy and list comprehension you can do the

import numpy as np
[num for num in np.arange(1,101) if (num%4 == 1 or num%4 == 2)]
sushmit
  • 4,369
  • 2
  • 35
  • 38
0

Assuming I've guessed the pattern correctly (alternating increments of 1 and 3), this should produce the desired result:

def sequence():
    res = []
    diff = 1
    x = 1
    while x <= 100:
        res.append(x)
        x += diff
        diff = 3 if diff == 1 else 1
    return ', '.join(res)
Xion
  • 22,400
  • 10
  • 55
  • 79
0

Assuming your sequence alternates increments between 1 and 3

numbers = [1]
while numbers[-1] < 100:
    numbers.append(numbers[-1] + 1)
    numbers.append(numbers[-1] + 3)

print ', '.join(map(str, numbers))

This could be easier to modify if your sequence is different but I think poke or BlaXpirit are nicer answers than mine.

Facundo Casco
  • 10,065
  • 8
  • 42
  • 63
-1

Write a function that takes a number as an argument and prints the Fibonacci series till that number

def Series(n):  
    a = 0  
    b = 1  
    print(a)  
    print(b)  
    S = 0  
    for i in range(0,n):  
        if S <= n-1:  
            S = a + b  
            print(S)  
            a = b  
            b = S
kdauria
  • 6,300
  • 4
  • 34
  • 53
-2

In python 3.1 you can produce a list in a way

     lst=list(range(100))
     for i in range(100)
        print (lst[i],',',end='')

In python 2.7 you can do it as

     lst=range(100)
     for i in range(100)
        print lst[i]+',' 
indiag
  • 233
  • 1
  • 4
  • 10
  • Why are you iterating over a separate range object? You should just iterate over the list items. – poke Jun 16 '12 at 16:10