Three numbers that make the Python sum 10

Asked 2 years ago, Updated 2 years ago, 15 views

I'm writing Python code and I'm thinking about how to approach it, so please give me your opinion. I'd appreciate it if you could give me a direction.

The code objective is to match the set value set_point.

set_point = 10

unit1 = 6, unit2 = 3, unit3 = 5

Unit1 can be controlled 0-100%: Mistakes between 0 and 6

Unit2 can only be 0n/off (0% or 100%): 0 or 3 only

Unit3 can be controlled by 10% : 0, 0.5, 1, ..., 5

For example, the goal is to have unit1=4.5 (75% output), unit2=3 (100% output), and unit3=3.5 (70% output) total of set_point.

This is one of the many cases.

But there are many cases and I'm worried about how to approach it. I don't know if I should use the decision tree... I thought about getting all the possible options for each unit and combining them, but as the number of units increases, it will take a long time to spin.

Please.

python

2022-09-20 19:26

2 Answers

It's not that hard to get the number of cases.

for unit2 in [ 0, 3 ]:
    for u3 in range(0, 51, 5):
        unit3 = u3/10
        unit1 = 10 - unit2 - unit3
        set_point = unit1 + unit2 + unit3
        print( unit1, '|', unit2, '|',  unit3, '||', set_point)
10.0 | 0 | 0.0 || 10.0
9.5 | 0 | 0.5 || 10.0
9.0 | 0 | 1.0 || 10.0
8.5 | 0 | 1.5 || 10.0
8.0 | 0 | 2.0 || 10.0
7.5 | 0 | 2.5 || 10.0
7.0 | 0 | 3.0 || 10.0
6.5 | 0 | 3.5 || 10.0
6.0 | 0 | 4.0 || 10.0
5.5 | 0 | 4.5 || 10.0
5.0 | 0 | 5.0 || 10.0
7.0 | 3 | 0.0 || 10.0
6.5 | 3 | 0.5 || 10.0
6.0 | 3 | 1.0 || 10.0
5.5 | 3 | 1.5 || 10.0
5.0 | 3 | 2.0 || 10.0
4.5 | 3 | 2.5 || 10.0
4.0 | 3 | 3.0 || 10.0
3.5 | 3 | 3.5 || 10.0
3.0 | 3 | 4.0 || 10.0
2.5 | 3 | 4.5 || 10.0
2.0 | 3 | 5.0 || 10.0


2022-09-20 19:26

I don't know exactly what you want, but if the set_point value is fixed, I think we can roughly apply the method below, so I'm writing it down.

import random
set_point = 10
unit1 = random.uniform(1,2,3,4,5,6)
unit2 = random.uniform(0,3)
unit3 = set_point - unit1 - unit2


2022-09-20 19:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.