Need a sequence that grows by 1 until a certain number
Question:
I’m very new to coding and I’m trying to figure out how to make a sequence that grows like 1+(1+1)+(2+1)+(3+1)+(4+1) etc. until it reaches exactly 180 (in python). So pretty much adds 1 to the last answer. Then when it reaches 180 it should come back down like ("last number of going up"-1)-("last result"-1) etc. until it reaches 1 again.
Sorry if it’s a little hard to understand, but I didn’t know how else to explain it really.
Answers:
I would use recursion:
def a(num=0):
list = []
if num ==0:
list.append(1)
else:
list.append(i)
if int(num) < 179:
a(num+1)
return list
list = a()
Your pseudo code looks like it reveals some C or C++ thinking.
I guess you simply need a for-loop:
import numpy as np
for angle in np.arange(0,181,1):
print(f'{angle=}')
The arange function, as it is used here, takes two or three arguments. The first is start position, the second is the end condition (excluded so will never be produced) and the third is an optional step size.
# This might work
# A lube within a lube and they are combined
for i in range(1,10):
for j in range(1,10):
print(f'{i} + {j} = {i+j}')
# some results
(1 + 1) = 2
(1 + 2) = 3
(1 + 3) = 4
(1 + 4) = 5
(1 + 5) = 6
(1 + 6) = 7
(1 + 7) = 8
(1 + 8) = 9
(1 + 9) = 10
(2 + 1) = 3
(2 + 2) = 4
(2 + 3) = 5
(2 + 4) = 6
(2 + 5) = 7
(2 + 6) = 8
The built-in range
gives us ascending and descending sequences:
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(9, -1, -1))
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Notice, each range
already "represents" the sequence, and we can iterate over it in a for
loop for example. Using list
here is just to show all the values.
If we just want to iterate over both of those ranges, we could just write two loops. However, if those loops are doing the same thing, maybe we instead want to join them together into the same sequence. For this, we use chain
from the standard library itertools
module:
>>> import itertools
>>> list(itertools.chain(range(10), range(9, -1, -1)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
The 9
was repeated, because it’s in both ranges; we can easily adjust either of the ranges to avoid that. For example:
>>> import itertools
>>> list(itertools.chain(range(9), range(9, -1, -1)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
What if we want to repeat the sequence forever (or until some break
condition is met)? We could just wrap our for
loop over that sequence, inside a while
loop; but it would be neater if we could make the sequence itself be unbounded, and repeat the pattern indefinitely when we iterate.
We can also do that with the standard library, using cycle
, also from itertools
. We will want to remove the 0
from the end to avoid duplicating it when the pattern starts over; fortunately, that’s just as easy as before – just set the end point for the second range appropriately. We get something quite elegant. I will wrap up the sequence creation in a function, too:
from itertools import cycle, chain
def up_and_down(n):
return cycle(chain(range(n), range(n, 0, -1)))
Let’s test it:
>>> import time
>>> for i in up_and_down(180):
... print(f'{i: 4}', end='r')
... time.sleep(0.1)
We can see (until we abort with ctrl-C) the counter smoothly count up to 180 and back down to 0, then repeat (if we wait 36 seconds).
I’m very new to coding and I’m trying to figure out how to make a sequence that grows like 1+(1+1)+(2+1)+(3+1)+(4+1) etc. until it reaches exactly 180 (in python). So pretty much adds 1 to the last answer. Then when it reaches 180 it should come back down like ("last number of going up"-1)-("last result"-1) etc. until it reaches 1 again.
Sorry if it’s a little hard to understand, but I didn’t know how else to explain it really.
I would use recursion:
def a(num=0):
list = []
if num ==0:
list.append(1)
else:
list.append(i)
if int(num) < 179:
a(num+1)
return list
list = a()
Your pseudo code looks like it reveals some C or C++ thinking.
I guess you simply need a for-loop:
import numpy as np
for angle in np.arange(0,181,1):
print(f'{angle=}')
The arange function, as it is used here, takes two or three arguments. The first is start position, the second is the end condition (excluded so will never be produced) and the third is an optional step size.
# This might work
# A lube within a lube and they are combined
for i in range(1,10):
for j in range(1,10):
print(f'{i} + {j} = {i+j}')
# some results
(1 + 1) = 2
(1 + 2) = 3
(1 + 3) = 4
(1 + 4) = 5
(1 + 5) = 6
(1 + 6) = 7
(1 + 7) = 8
(1 + 8) = 9
(1 + 9) = 10
(2 + 1) = 3
(2 + 2) = 4
(2 + 3) = 5
(2 + 4) = 6
(2 + 5) = 7
(2 + 6) = 8
The built-in range
gives us ascending and descending sequences:
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(9, -1, -1))
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Notice, each range
already "represents" the sequence, and we can iterate over it in a for
loop for example. Using list
here is just to show all the values.
If we just want to iterate over both of those ranges, we could just write two loops. However, if those loops are doing the same thing, maybe we instead want to join them together into the same sequence. For this, we use chain
from the standard library itertools
module:
>>> import itertools
>>> list(itertools.chain(range(10), range(9, -1, -1)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
The 9
was repeated, because it’s in both ranges; we can easily adjust either of the ranges to avoid that. For example:
>>> import itertools
>>> list(itertools.chain(range(9), range(9, -1, -1)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
What if we want to repeat the sequence forever (or until some break
condition is met)? We could just wrap our for
loop over that sequence, inside a while
loop; but it would be neater if we could make the sequence itself be unbounded, and repeat the pattern indefinitely when we iterate.
We can also do that with the standard library, using cycle
, also from itertools
. We will want to remove the 0
from the end to avoid duplicating it when the pattern starts over; fortunately, that’s just as easy as before – just set the end point for the second range appropriately. We get something quite elegant. I will wrap up the sequence creation in a function, too:
from itertools import cycle, chain
def up_and_down(n):
return cycle(chain(range(n), range(n, 0, -1)))
Let’s test it:
>>> import time
>>> for i in up_and_down(180):
... print(f'{i: 4}', end='r')
... time.sleep(0.1)
We can see (until we abort with ctrl-C) the counter smoothly count up to 180 and back down to 0, then repeat (if we wait 36 seconds).