Wednesday, 26 June 2019

Return result as list with special behaviour from function based on input

I want to achieve an algorithm, which allows to return some results according to inputs.

I pass those values ( Changeable, values varies)

b, c, a, li = 200, 30, 3 , [3,3]

into a function with algorithm, and my result should return as a list.

[x1, x2, x3, ...]

x1, xn is just for illustration.

Sample of code:

b, c, a, li = 200, 30, 3 , [1,1]

def func(b,c,a,li):
    for index, item in enumerate(li):
        if index == 0:
            if a %2 == 0:
                Result = []
                start= 0
                for num in range(a):
                    Result[:0]=[start+num*c]
                    Result[-1:]=[b-num*c]
            else:
                Result = []
                start= 0
                for num in range(a):
                    Result[:0]=[start+num*c]
                    Result[-1:]=[b-num*c]
    return Result


g = func(b,c,a,li)
print(g)

Output:

[60, 30, 140]

I expect to get an output as: [0, 100 ,200]

What I want to achieve is as below:

For instance in above example:

A for-loop inside function first checks first value of list which is 1. then place x1 and second value of list which is 1 and place x2, the remaining is 1 , this should be placed as x3, se figur, and xn values are calulated as x1=b-b=0, x2=b, x3= x1 + b/(a-len(li)+1)

enter image description here

Another example with even numbers:

b, c, a, li = 300, 35, 4 , [2,2]

enter image description here

Output:

[ 0, 35, 265, 300]

Another example with odd numbers:

b, c, a, li = 400, 25, 5 , [2,2]

enter image description here

Output:

[ 0, 25, 200, 375, 400] 

Another example with some other numbers:

b, c, a, li = 350, 40, 3 , [4,2]

Output:

[ 0, 40, 350] 

I really find it difficult to write an algorithm pattern which solves the puzzle. I appreciate much any help. Thanks.



from Return result as list with special behaviour from function based on input

No comments:

Post a Comment