棘手的条件数学

Joe*_*oel 0 math conditional

这个对我来说很棘手:

我有四组8个LED.A是1-8,B是9-16,C是17-24,D是25-32.

我想弄清楚如何编写条件在哪里

i = 0 //this would be the LED number

loop {
 i = //gets updated here
 if (i is in the first group) {
    //  do stuff
 }  else {
    //do other stuff
 }
}
Run Code Online (Sandbox Code Playgroud)

基本上,我需要在LED关闭之前检查它,看它是否与正在点亮的新LED位于同一组中.

如果它在同一组中,它将被关闭,如果它不在同一组中则需要保持开启状态.

所以数学方面,我需要看看数字是否介于某个范围之间.我想我可以写四个版本

if (i >=8)
...
if(i <=9 && >=16)
...
Run Code Online (Sandbox Code Playgroud)

等,但这似乎不是很整洁......

Ign*_*ams 5

使用整数除法.从两个值中减1,然后整数除以8.如果它们是相同的结果,那么两个LED都在同一个库中.

def samebank(i, j):
  return ((i - 1) // 8) == ((j - 1) // 8)
Run Code Online (Sandbox Code Playgroud)