Skip to main content
saved 7 bytes
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 649

JavaScript (ES6), 48 48  41 bytes

Expects the number as a stringlist of digits. Returns a Boolean value.

f=([c,..a=>a.a],i=17)=>c?fmap(a,id=>i-=c%7=d%7?1:i%7-6?2:.1,i=17):&&!i

Try it online!Try it online!

Commented

f = (                 // f is a recursive function taking:
  [c, ...a],          //   c = next digit, a[] = array of remaining digits
  i = 17              //   i = syllable counter
) =>                  //
  c ?                 // if c is defined:
    f(                //   do a recursive call:
      a,              //     pass the array of remaining digits
      i -=            //     update i:
        c % 7 ?       //       if c is neither 0 nor 7:
          1           //         decrement i
        :             //       else:
          i % 7 - 6 ? //         if i mod 7 is not equal to 6:
            2         //           subtract 2
          :           //         else:
            .1        //           invalidate the result by subtracting 1/10
    )                 //   end of recursive call
  :                   // else:
    !i                //   is i exactly equal to 0?

JavaScript (ES6), 48 bytes

Expects the number as a string. Returns a Boolean value.

f=([c,...a],i=17)=>c?f(a,i-=c%7?1:i%7-6?2:.1):!i

Try it online!

Commented

f = (                 // f is a recursive function taking:
  [c, ...a],          //   c = next digit, a[] = array of remaining digits
  i = 17              //   i = syllable counter
) =>                  //
  c ?                 // if c is defined:
    f(                //   do a recursive call:
      a,              //     pass the array of remaining digits
      i -=            //     update i:
        c % 7 ?       //       if c is neither 0 nor 7:
          1           //         decrement i
        :             //       else:
          i % 7 - 6 ? //         if i mod 7 is not equal to 6:
            2         //           subtract 2
          :           //         else:
            .1        //           invalidate the result by subtracting 1/10
    )                 //   end of recursive call
  :                   // else:
    !i                //   is i exactly equal to 0?

JavaScript (ES6),  48  41 bytes

Expects a list of digits. Returns a Boolean value.

a=>a.map(d=>i-=d%7?1:i%7-6?2:.1,i=17)&&!i

Try it online!

added an explanation
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 649

How?

We use a syllable counter \$i\$ initialized to \$17\$, subtract either \$1\$ or \$2\$ from \$i\$ after each digit and check whether we end up with \$i=0\$. The haiku is supposed to look like that:

17 16 15 14 13
12 11 10 09 08 07 06
05 04 03 02 01

When the digit is either \$0\$ or \$7\$ and \$i\equiv 6\pmod 7\$, we have an invalid hyphenation and subtract \$1/10\$ from \$i\$ instead of \$2\$. Because this test can only be triggered once, \$i\$ remains a non-integer value whatever happens next.

Commented

f = (                 // f is a recursive function taking:
  [c, ...a],          //   c = next digit, a[] = array of remaining digits
  i = 17              //   i = syllable counter
) =>                  //
  c ?                 // if c is defined:
    f(                //   do a recursive call:
      a,              //     pass the array of remaining digits
      i -=            //     update i:
        c % 7 ?       //       if c is neither 0 nor 7:
          1           //         decrement i
        :             //       else:
          i % 7 - 6 ? //         if i mod 7 is not equal to 6:
            2         //           subtract 2
          :           //         else:
            .1        //           invalidate the result by subtracting 1/10
    )                 //   end of recursive call
  :                   // else:
    !i                //   is i exactly equal to 0?

Commented

f = (                 // f is a recursive function taking:
  [c, ...a],          // c = next digit, a[] = array of remaining digits
  i = 17              // i = syllable counter
) =>                  //
  c ?                 // if c is defined:
    f(                //   do a recursive call:
      a,              //     pass the array of remaining digits
      i -=            //     update i:
        c % 7 ?       //       if c is neither 0 nor 7:
          1           //         decrement i
        :             //       else:
          i % 7 - 6 ? //         if i mod 7 is not equal to 6:
            2         //           subtract 2
          :           //         else:
            .1        //           invalidate the result by subtracting 1/10
    )                 //   end of recursive call
  :                   // else:
    !i                //   is i exactly equal to 0?

How?

We use a syllable counter \$i\$ initialized to \$17\$, subtract either \$1\$ or \$2\$ from \$i\$ after each digit and check whether we end up with \$i=0\$. The haiku is supposed to look like that:

17 16 15 14 13
12 11 10 09 08 07 06
05 04 03 02 01

When the digit is either \$0\$ or \$7\$ and \$i\equiv 6\pmod 7\$, we have an invalid hyphenation and subtract \$1/10\$ from \$i\$ instead of \$2\$. Because this test can only be triggered once, \$i\$ remains a non-integer value whatever happens next.

Commented

f = (                 // f is a recursive function taking:
  [c, ...a],          //   c = next digit, a[] = array of remaining digits
  i = 17              //   i = syllable counter
) =>                  //
  c ?                 // if c is defined:
    f(                //   do a recursive call:
      a,              //     pass the array of remaining digits
      i -=            //     update i:
        c % 7 ?       //       if c is neither 0 nor 7:
          1           //         decrement i
        :             //       else:
          i % 7 - 6 ? //         if i mod 7 is not equal to 6:
            2         //           subtract 2
          :           //         else:
            .1        //           invalidate the result by subtracting 1/10
    )                 //   end of recursive call
  :                   // else:
    !i                //   is i exactly equal to 0?
added a commented version
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 649

JavaScript (ES6), 48 bytes

Expects the number as a string. Returns a Boolean value.

f=([c,...a],i=17)=>c?f(a,i-=c%7?1:i%7-6?2:.1):!i

Try it online!

Commented

f = (                 // f is a recursive function taking:
  [c, ...a],          // c = next digit, a[] = array of remaining digits
  i = 17              // i = syllable counter
) =>                  //
  c ?                 // if c is defined:
    f(                //   do a recursive call:
      a,              //     pass the array of remaining digits
      i -=            //     update i:
        c % 7 ?       //       if c is neither 0 nor 7:
          1           //         decrement i
        :             //       else:
          i % 7 - 6 ? //         if i mod 7 is not equal to 6:
            2         //           subtract 2
          :           //         else:
            .1        //           invalidate the result by subtracting 1/10
    )                 //   end of recursive call
  :                   // else:
    !i                //   is i exactly equal to 0?

JavaScript (ES6), 48 bytes

Expects the number as a string. Returns a Boolean value.

f=([c,...a],i=17)=>c?f(a,i-=c%7?1:i%7-6?2:.1):!i

Try it online!

JavaScript (ES6), 48 bytes

Expects the number as a string. Returns a Boolean value.

f=([c,...a],i=17)=>c?f(a,i-=c%7?1:i%7-6?2:.1):!i

Try it online!

Commented

f = (                 // f is a recursive function taking:
  [c, ...a],          // c = next digit, a[] = array of remaining digits
  i = 17              // i = syllable counter
) =>                  //
  c ?                 // if c is defined:
    f(                //   do a recursive call:
      a,              //     pass the array of remaining digits
      i -=            //     update i:
        c % 7 ?       //       if c is neither 0 nor 7:
          1           //         decrement i
        :             //       else:
          i % 7 - 6 ? //         if i mod 7 is not equal to 6:
            2         //           subtract 2
          :           //         else:
            .1        //           invalidate the result by subtracting 1/10
    )                 //   end of recursive call
  :                   // else:
    !i                //   is i exactly equal to 0?
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 649
Loading