search

Home  >  Q&A  >  body text

php - There are 48 strings of 1 and 0 representing the time of one day, in half-hour units

The requirement is to convert 00:30 - 11:00 of the current day into a string of only 1 and 0, where 1 means included in the time period and 0 means not included

The resulting string from 00:30 - 11:00 is 011111111111111111111110000000000000000000000000

The times may be scattered and not necessarily coherent. For example, 11:00-12:00 14:00-16:00.

Please tell me what to do. Thank you guys~

阿神阿神2773 days ago506

reply all(3)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-16 13:05:25

    Written a js version for the console

    let calcPos = arr=>arr[0]*2+(arr[1]===0?0:1);
    ([[0,30],[11,0]].reduce(
        (a,b,i,arr)=>
            a+= new Array(calcPos(b)+(i===0||i%2===0?0:1)-a.length)
                .fill(i%2===0?"0":"1")
                .join("")
        , "")+new Array(48).fill("0").join(""))
        .slice(0,48);

    Time is used[[0,30],[11,0]]表示00:30 - 11:00, so the second situation is like this

    ([[0,30],[11,0],[14,0],[16,0]].reduce((a,b,i,arr)=>a+=new Array(calcPos(b)+(i===0||i%2===0?0:1)-a.length).fill(i%2===0?"0":"1").join(""), "")+new Array(48).fill("0").join("")).slice(0,48);

    The following is the decoding

    ("0"+"011111111111111111111110000011111000000000000000")
        .split("")
        .reduce(
            (a,b,i,arr)=>
                i===0?a:b!==arr[i-1]?[...a, (b==="1"?i:(i-1)) -1]:a
            , [])
        .map(i=>[(i-(i%2===0?0:1))/2, i%2===0?0:30]);

    reply
    0
  • 黄舟

    黄舟2017-05-16 13:05:25

    Convert the time to seconds, and it’s OK to change the seconds to binary. As for - whether you can use the binary format according to the coding table or other methods, it’s up to you

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-16 13:05:25

    Isn’t the half-hour unit dividing 1 day into 48 segments? 0-47 means that the segment number corresponds to each period of time.
    The i-th string of string corresponds to the segment number i. 0 means that it is during this time. 1 means that it is not during this time.
    The specific program is not difficult to write.
    Just ask for the segment number and replace the string content.
    The segment number is to convert the time into hours and then divide it by 0.5 to round up.

    reply
    0
  • Cancelreply