Pavel.D
Pavel.D

Reputation: 581

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.

Upvotes: 4

Views: 4947

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195563

I hope I understood the question right, I'm working only with examples provided in the text.

The main method is get_points(), which accepts parameters b, c, a, li as in question and returns list of computed values:

from itertools import cycle

def get_points(b, c, a, li):
    num_points_left, num_points_right = li

    final_left = [*range(0, num_points_left * c, c)]
    final_right = [*range(b, b - num_points_right * c, -c)][::-1]
    final_center = []
    points_left = a - (len(final_left) + len(final_right))

    if points_left > 0:
        step = (final_right[0] - final_left[-1]) // (points_left+1)
        final_center = [final_left[-1] + step*i for i in range(1, points_left+1)]
    elif points_left < 0:
        cycle_list = [lambda: final_left.pop() if len(final_left) > 1 else None,
                      lambda: final_right.pop(0) if len(final_right) > 1 else None]

        if len(final_left) > len(final_right):
            remove_cycle = cycle(cycle_list)
        else:
            remove_cycle = cycle(cycle_list[::-1])

        while len(final_left) + len(final_right) > a:
            next(remove_cycle)()

    return final_left + final_center + final_right

def test(b, c, a, li, expected):
    print('Testing parameters:', b, c, a, li)
    print('Expected:', expected)
    returned = get_points(b, c, a, li)
    print('Returned:', returned)

    if returned == expected:
        print('* PASSED! *')
    else:
        print('!!! Fail !!!')

test(400, 25, 5 , [2,2], [ 0, 25, 200, 375, 400])
test(200, 30, 3 , [1,1], [0, 100 ,200])
test(300, 35, 4 , [2,2], [ 0, 35, 265, 300])
test(350, 40, 3 , [4,2], [ 0, 40, 350])

Prints:

Testing parameters: 400 25 5 [2, 2]
Expected: [0, 25, 200, 375, 400]
Returned: [0, 25, 200, 375, 400]
* PASSED! *
Testing parameters: 200 30 3 [1, 1]
Expected: [0, 100, 200]
Returned: [0, 100, 200]
* PASSED! *
Testing parameters: 300 35 4 [2, 2]
Expected: [0, 35, 265, 300]
Returned: [0, 35, 265, 300]
* PASSED! *
Testing parameters: 350 40 3 [4, 2]
Expected: [0, 40, 350]
Returned: [0, 40, 350]
* PASSED! *

Upvotes: 1

Related Questions