Hacker News new | past | comments | ask | show | jobs | submit login

Since I read the constraint to allow for single digit numeric values*, I guess they are looking for a solution similar to:

  function isDivisibleByThree(num: string): boolean {
      let mod3 = "012012012012";
      let modulo = "0";
      for (const digit of num) {
          modulo = mod3[Number(digit) + Number(modulo)];
      }
      return modulo === "0";
  }
If adding two single digit numbers is also prohibited it can be implemented with a lookup and keep everything in string representation.

"The programmer can use whatever representation they see fit with the only restriction being that it could only contain letters, numbers and symbols that could be typed with a single stroke"






> If adding two single digit numbers is also prohibited it can be implemented with a lookup and keep everything in string representation.

Yeah uh I guess you missed the SUM_TABLE part of the article? That's what they're doing. And that's why the rule against matrices was added.

That version of the code is 80% checking if "the sum of the individual digits is divisible by 3", 20% the rest of the fizzbuzz.


The constraint only states not to use hard coded matrices as I read it. Here is a version with number addition removed, this works fully on strings.

  function isDivisibleByThree(num) {
    let modulo, mod3 = "012012012012";
    for (const digit of num) {
        [modulo] = mod3.slice(modulo).slice(digit);
    }
    return modulo === "0";
  }

Using slice twice is pretty clever. That might work.

But my real point is they did have the digit sum insight. Their code was already doing your previous suggestion, and if there's a compact way to slice in typescript types it could be adapted to this new method by replacing SUM_TABLE and changing one other line.

The only difference is that they're doing a sum modulo 9 instead of modulo 3, but both of those work fine.


I see what you mean now, you are right. My only intention was to demonstrate that a TS solution is possible - one that does not rely on the type system but one that still observes all the constraints listed. I think this was questioned by some in the thread (but not you).



Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: