jas0nhuang

Don't LeetCode Yet - 4 - Let's Code

From Pseudocode to Actual code

Function filling

Write down the action inside the main structure first!

Example LIOJ problem 1020
Li’s solution:

if( X is Prime){}
function isPrime{}

to

if(isPrime(X)){
}
function isPrime(n){
if (n===1) {
return false
}
for (let i = 2; i<n; i++){
if (n % i === 0){
return false
}
}
return true
}

Divide problem into small pieces: Find Prime numbers in an array = List the array + check if it is prime
Nested loop

Simplify the problem

Solve the simplified problem first.

Example LIOJ problem 1021
print a star pyramid:

*
**
***
****

If we don’t know how to print multiple stars in one line, print out 1 star for each line first.

for (let i=1;i<=N; i++){
printStar(i) //put a function here as the first tip
}
function printStar(n){
let = starString =""
for(let j = 1; j<=n; j++){
starString += "*"
}
console.log(starString)
}

Don’t really need .repeat() method

Programing San-Bao

Iteration ,Function, Condition
Try not to solve problem with built-in function/method but the San-Bao (like the .repeat() method above.)

Example

LIOJ problem 1022 print the star pyramid

  1. First write down the basic structure:
    function solve(lines){
    for (let i=1; i<=lines[0]; i++){
    printStars(i)
    }
    }
    function printStars(n){
    }
  2. then fill in the printStars() function. When trying to write the detail of this function, I figured out that we need to pass two arguments inside. It means that I NEED TO FIND THE LOGIC before solving the problem.

Logic from the instructor:

  • Need to print out N lines
  • The line i will have 2i-1 stars
  • stars need to be centered
  • need N-i white space in front of each line

My solution logic:

  • Print out N lines
  • Print out the space in front, so the i will start from N and decreasing
  • Each line will have i-1 spaces
  • add the stars after the spaces: 1 initial star for each line + 2(N-i) stars for each line

The instructor used a support function to do the repetition

function repeat(str, n){
let s = ""
for (let i=1; i<=n; i++){
s += str
}
return s
}

Multiplication table

Practice nested loop

Find Square numbers

My solution:

var divisor = lines[0]
if (lines[0] != 1){
div = div / 2
}
for (let i = 1; i <=lines[0]; i++){
for (let j = 1; j <=divisor; j++){
if (i / j === j){
console.log(i)
}
}
}

Instructor’s solution:

  1. Math.sqrt() or Math.floor()
  2. Loop through all square numbers under 100
    while(i*i <=100){
    console.log(i*i)
    i++
    }

Project 4

Like printing a pyramid, just add the trunk.
And maybe make it prettier:

function solve(lines) {
totalLines = lines[0]
for (let i=1; i<=lines[0];i++){
printPyramid(i, lines[0])
}

function printPyramid(n, l){
let starStr = "*"
if (n != 1){
for(let i=1;i<n;i++){
starStr += "**"
}
}
console.log(repeatMe((l-n), " ") + starStr)
}

for(let i=1;i<(totalLines/2);i++){
console.log(repeatMe((totalLines -2) , " ") + "|||")
}

function repeatMe(counter, toPrint){
let Str = ""
for (i=1; i <= counter;i++){
Str += toPrint
}
return Str
}
}

N M multiplication table

Find Narcissistic numbers

NumI = NumI[0]^NumI.length + …… + NumI[NumI.length]^NumI.length

  • Loop through Number I - j
  • check if it returns true
    I put the numbers into array to get each digit.
    Didn’t pay attention to the input so got several “Wrong Answer” submitions.