jas0nhuang

Don't LeetCode Yet - 7 - NPSC Problems

NPSC = National Problem Solving Contest

  1. Contest for middle school students. Some problems are easy.
  2. Problems are interesting.
  3. Can find all test cases.
  4. Can know the difficulty via score board.

How to use the test cases of NPSC

cat pa.in | node code.js | diff pa.out -

new line character

xxd lets you read a file in HEX code. Cool!
Newline character “0d0a” VS “0a” VS “0d”.
Strip them with diff --strip-trailing-cr.

LIOJ 1008

Knowledge about binary.
(A NUMBER).toString(2) will give the binary number of A NUMBER. This problem is actually trying to find how many 1s in a binary number. And this made me think about problem 1014

LIOJ 1009

Simple reverse
Write function your self or use the built-in .reverse method.

LIOJ 1013

Fibonacci number
code on Wikipedia

function fibon(n) {
var fib_n = function(curr, next, n) {
if (n == 0) {
return curr;
} else {
return fib_n(next, curr+next, n-1);
}
}
return fib_n(0, 1, n);
}

LIOJ 1014

Nonary system (counting without nine).
parseInt(NUMBER, BASE)
parsInt(100, 9) == 81
Nonary 100 = 9^ 0* 0 + 9^ 1* 0 + 9^ 2* 1
parsInt(33, 9) == 30
Nonary 33 = 9^ 0* 3 + 9^ 1* 3

LIOJ 1016

For this part:

else if(counterA > counterB){
for(i=1; i<lines.length; i++){
if(arr[i] == "B"){
console.log(i)
}
}
} else {
for (i=1; i<lines.length; i++){
if (arr[i] == "A"){
console.log(i)
}
}
}

We can replace it with “conditional (ternary) operator”:

else{
let luuser = counterA > counterB ? "B":"A"
// Same as:
// if (counterA > counterB) { luuser="B" } else { luuser="A" }
for(i=1; i<lines.length; i++){
if(luuser == lines[i]){
console.log(i)
}
}
}