Irrigation scheduling

Question:

I’m trying to code an irrigation controller. I have a preliminary tkinter UI that uses checkbuttons to get the days of the week when 4 valves will be turned on:
enter image description here

After I’ve checked the boxes, the data looks like this:

raw_data= [
    [1, 0, 0, 1, 0, 0, 0],
    [0, 1, 0, 0, 1, 0, 0],
    [0, 0, 1, 0, 0, 1, 0],
    [0, 0, 0, 1, 0, 0, 1]
]

Where ‘1’s represent days that the valve will be turned on. What I need to know (and don’t know how to write the code for) is a way to calculate the number of days since the last irrigation. It’s easy for the second day of the week, I just subtract the weekday index for the second day from the first. For Valve 1 in the present example that would be Thursday and Monday (3 days). But…on the first day of the week, I need to know how many days since the valve was turned on the previous week. Again, for Valve 1, on Monday I need to know how many days since the previous Thursday (4 days). It’s a trivial calculation for me, but I can’t seem to get how to code it so a computer would understand.

What approach can I take (what logic can I use) to solve this problem?

Asked By: dwburger

||

Answers:

I think you can do it in the following way.

Let’s take as an example the valve 1 whose array would be: [1, 0, 0, 1, 0, 0, 0].

As you say, the easiest thing to do is to subtract the indexes, so we simply add the same array at the end to repeat the week with the same data.

a = [1, 0, 0, 1, 0, 0, 0]
b = a+a

aux = []

for i,v in enumerate(b):
    if v == 1:
        aux.append(i)

print(aux)

This code adds in aux index each time it finds a 1 in the array. Now we only need to subtract. In this case the last value does not interest us, so if you want you can remove it with aux.pop().

Output:

[0, 3, 7, 10]

Where 3-0=3 and 7-3=4

Answered By: FG94

It appears that I now have two solutions to the problem I raised. The one above and one that I came up with myself based on a answer to my previous question. Unless I’m mistaken (which is possible, since I’m a beginner Python coder), the answer to the previous question didn’t include today’s date as a benchmark. Once I put that into the code:

if today > day1:
    days_since_last_irrigation = day2 - day1
else:
    days_since_last_irrigation = ((day1 - day2) + 7)

I started to get results that made sense. This exercise is an illustration that there’s more than one way to solve most problems.

Answered By: dwburger

Easier way from @FG94.

a = [1, 0, 0, 1, 0, 0, 0]
b = a + a

aux = [i for i,v in enumerate(b) if v]
print(aux)
Answered By: toyota Supra
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.