'Compute a range python
I'm stuck on a range. It should be pretty simple by for some reason I can't figure it out. So i have a range of (x,y) and it should add every number within that range however once it gets to the third it should double and then continue adding them.
start = int(input())
end = int(input())
sum = 0
a = range (start,end)
for i in range (start,end):
sum = sum + i
i += 1
Solution 1:[1]
start = int(input())
end = int(input())
sum = 0
for i, v in enumerate(range(start,end+1)): #7+1 as it's non inclusive
sum+=v
if (i+1)%3 == 0: #every 3 it *2
sum *=2
print(sum)
and it outputs 72, as expected
Solution 2:[2]
Iterate over the range with step=3, and within that loop add the sum of each smaller range to the total before multiplying it by two.
total, start, end = 0, int(input()), int(input()) + 1
for i in range(start, end, 3):
total += sum(range(i, min(i+3, end)))
total *= 2
print(total)
2
7
72
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | 35308 |
| Solution 2 |
