r/PythonLearning • • 4d ago

i am stuck with the range function

sorry but i am gonna crash out because of the range function i cant understand it well to use it this is driving me crazy any one could give me a video explaining everything i dont mind it being an hour long

8 Upvotes

33 comments sorted by

View all comments

Show parent comment

3

u/ninhaomah 4d ago

I still don't know what is the question or issue you have or what your teacher asked to do with range.

Pls post the question here 

0

u/Repulsive-Song-1594 4d ago

I study statistique and lemme make it clear what did i want Lets say we have Xi = 1,2,3,4,5 Ni = 2,4,6,9,12 Ni↑ = 2,6,12,21,33 I just want to do the Ni↑ with range function

2

u/ninhaomah 4d ago

What has what you study got to do with this ?

And how is this even a programming or math/stats question ?

You have 2 groups of numbers and you want to somehow get a third group of numbers using those 2 groups.

No rules or logic ?

Imagine I ask you x = 1,2 and y= 38,67 and z=43,87 ... I want to make z. How ?

-1

u/Repulsive-Song-1594 4d ago

Ni↑ is Ni just adding number up Like first 2 Second 2+4 = 6 Third 6+6 = 12 Fourth 12+9 = 21 Till the end

4

u/tmtowtdi 4d ago

You do not get bonus points for using as few words as possible.

You're not stating your problem clearly, nobody understands what you're talking about. Take a breath and try to type out what you mean exactly. You're allowed to use punctuation, just be clear.

2

u/ninhaomah 4d ago edited 4d ago

Then where does Xi in your example comes in ?

If 2+4 = 6 then why not 6+4 = 10 ?

Why suddenly 6+6 ?

If 21 is because 12+9 then what happened to 15 which is 9+6 ?

0

u/Repulsive-Song-1594 3d ago

Maybe i missed calculating i was almost asleep writing that about xi i will use it later just not in this i care now about just the ni

1

u/mountain-snowman 3d ago

You are having trouble using range for a specific scenario, right? Write the code without range (even static code is fine), post the code here and ask how to achieve the same using range? You will get your answer with explanation.

2

u/Naive_Programmer_232 3d ago edited 3d ago

ahh i see ignore my other comment, you're talking about cumulative sums i believe.

from typing import TypeAlias, Any

Number: TypeAlias = int|float

# with 'range'  (iterative)
def rng_csum(arr: list[Number]) -> list[Number]:
    res=arr[:1]
    for i in range(1,len(arr)):
        res.append(res[-1]+arr[i])
    return res

# without range (iterative)
def reg_csum(arr: list[Number]) -> list[Number]:
    res=arr[:1]
    for n in arr[1:]:
        res.append(res[-1]+n)
    return res

# without range (recursive)
def rec_csum(arr: list[Number]) -> list[Number]:
    if len(arr)<=1:
       return arr
    res=rec_csum(arr[:-1])
    res.append(res[-1]+arr[-1])
    return res

# itertools.accumulate is another way 
from itertools import accumulate as proven_csum

# compares any number of position args for equivalency
def all_equal(*args: Any) -> bool:
    return all(args[0]==e for e in args)

def test() -> None:

    Ni=[2,4,6,9,12]

    # results
    r1=rng_csum(Ni)
    r2=reg_csum(Ni)
    r3=rec_csum(Ni)
    r4=list(proven_csum(Ni))

    assert all_equal(r1,r2,r3,r4), "failed test case"
    print("\npassed test case\n")
    print(f"Ni↑={r1})

test()

passed test case

Ni↑=[2, 6, 12, 21, 33]

hopefully that gives you some ways you could go about it. best of luck!

1

u/TJATAW 4d ago

The only way I am seeing range being useful in that would be range(len(Ni)).

range used that way would make it work for any length of Ni.

One more hint:

fruits = ["apple", "banana", "cherry", "date"]

print(fruits[0]) # Output: "apple"
print(fruits[2]) # Output: "cherry"

1

u/mountain-snowman 3d ago

I think what you want is:

Ni_arrow = [sum(Ni[:i+1]) for i in range(len(Ni))]

This is a test code and there are better ways to do it while using the range as shown.