jas0nhuang

Don't LeetCode Yet - 5 - Classic Problems

10 classic problems. Try not to use built-in functions/methods but only iteration, condition, and self-written functions.
Only use the necessary ones: split(), floor() ……

Check arithmetic sequence

Loop through 0 to input.length-2, and let num1 = input[0], num2 = input[1], num3=[2]
Check if they have the same subtraction value.
Better check for edge case like only 1 or 2 numbers, input strings, no input…etc.

function isArith(input) {
if (input.length <= 2){
if (input.length === 0 || isNaN(input[0]) || isNaN(input[1])){
console.log("NG")
} else {
console.log("OK")
}
} else {
for (i=0;i<input.length-2;i++){
let num1 = input[i]
let num2 = input[i+1]
let num3 = input[i+2]
if (num2-num1 != num3-num2){
console.log("NG")
return
}
}
console.log("OK")
}
}

Check Taiwan ID

function TWIDChecker(input){
let TWID = i nput
let firstLetter = TWID[0]

function TWIDnumbersSum(TWID){
let numberSum = 0
for (let i=1;i<TWID.length-1;i++){
numberSum += (Number(TWID[i]) * (9-i))
}
return (numberSum + Number(TWID[9]))
}

function letterToNumber(firstLetter){
let letters =
["A","B","C","D","E","F","G","H",
"J","K","","M","N","P","Q","","",
"T","U","V","X","","W","Z","I","O"]
for (let i=0;i<letters.length;i++){
if (letters[i]==firstLetter){
let nums = []
let Sum = 0
let orgNum= i+10
for (let j=0; j<2;j++){
nums.push(orgNum % 10)
orgNum = Math.floor(orgNum/10)
}
Sum = nums[0]*9 + nums[1]*1
return Sum
}
}
}
let TOTLESum = Number(TWIDnumbersSum(TWID))+Number(letterToNumber(firstLetter))
if (TOTLESum % 10 === 0){
console.log("OK")
} else {
console.log("NG")
}
}

Add up all digits in a number

Project 5

LIOJ problem 1026

Check Geometric sequence
Same as Arithmetic sequence

LIOJ problem 1027

Check Credit card number

LIOJ problem 1028

I didn’t think about the birthdays like “2999 6 30” or “3999 12 31”, so I use if while checking if the number is more than 2 digits, witch will only check once.
But here we need to use while in order to loop through all the cases(in case there are more than 3 digits):

while (lifeCode >= 10){
lifeCode = (lifeCode % 10) + Math.floor(lifeCode/10)
}

LIOJ problem 1034

When try to find the new ASCII code, if the added number is more than 27, there will be a problem if we only subtract 26.
ex: z will become { adding 26
a will also become { adding 52.
So the added number should be divided by 26 and use the remainder.

let newCharCode = orgStr.charCodeAt(i)+addNum % 26
if (newCharCode>122){
newCharCode -=26
}

LIOJ problem 1030

Check if it’s PALINDROME (what a word!)
Two ways : 1. Reverse the original string 2. Compaire the (first + n) and (last - n) character…etc.

LIOJ problem 1031

Square pyramidal number sequence
1 + 121 + 12321 + 1234321 + ……

function solve(lines){
let startNum = 1
let endNum = Math.floor(Math.sqrt(lines[0]))
let times = endNum * 10 / 2
let countNum1 = (endNum + startNum) * times / 10
let countNum2 = 0
for (i=1;i<=endNum;i++){
countNum2 += i * (endNum + 1 - i)
}
let finalNum = countNum2 * 2 - countNum1
console.log(finalNum)
}

Or just simply check if a number is squared, and add them up!

LIOJ problem 1032

Practice the usage of Math.sart, Math.abs, Math.toFixed()

LIOJ problem 1033

let dots =[]
dots.push{
x: temp[0],
y: temp[1]
}

This will get the data look like:

[{x:1, y:2},
{x:3, y:5},
{x:10, y:13}]

LIOJ problem 1046

Brute-force or some simple loops for 3 vertical and 3 horizontal lines
multidimensional arrays