π¨ WeCode Codekata 3μ£Όμ°¨!

solmiiΒ·2020λ 6μ 27μΌ
0

Algorithm

3/3

3μ£Όμ°¨λ ννλ‘μ νΈ κΈ°κ°μ΄λ€!
νμ§λ§, μ½λμΉ΄νλ λ©μΆκΈ° μκ³  κ³μ νλ€π±
μ¬μ€, νλ‘μ νΈ κΈ°κ° μ μ²λΌ 1μκ°μ© νλ‘ μκ³ μμ§λ λͺ»νκ³  μλ€....

Day 1

Β Β  λ΄ νμ΄ Β Β

const complexNumberMultiply = (a, b) => {
let aArr = a.split("+");
let bArr = b.split("+");

let aNum = aArr[0];
let aCom = aArr[1].split("i")[0];
let bNum = bArr[0];
let bCom = bArr[1].split("i")[0];

let aa = aNum * bCom;
let aaa = aNum * bNum;
let bb = bNum * aCom;
let bbb = -(bCom * aCom);

return ${aaa + bbb}+${aa + bb}i
}

complexNumberMultiply('3+-1i', '99+99i'); // 396+198i

....? μ΄μΌ νμλ....?

μΌλ¨ λ³΅μμ(complex number)κ° λ¬Έμ λ‘ λμ¬μ€μ μ νμ ν λͺ°λλ€......π€£

λ΄κ° μλ λ³΅μμλΌλ κ°λμ μ κ³±νλ©΄ μμμμ λλλλ°, μ½λμΉ΄ν νμλ€μ΄ κ·Έμ λ μλ©΄ νν°μ΄λΌκ³  ν¨γγγγγγγγγγγ)

μλ¬΄νΌ μΌλ¨ κ³μ°μλ§ μλ©΄ λ¬Έμ νΈλκ±΄ μ΄λ ΅μ§ μλ€.
(λμ²λΌ μΌμΌμ΄ λ€ μͺΌκ°λ©΄ μ΄λ»κ² λ΅μ΄ λμ€κΈ΄ νλ€.....γγγγγγ)

3+-1i , 99+99i λΌλ inputμμ + - i κ°μ κΈ°νΈ λ€ λΌκ³  μ«μλ§ λκ³  λ³΄μ! μ΄λ νΈμμ μΌμͺ½ μ«μλΆν° a, b, c, d λΌκ³  λΆλ₯Έλ€.

a : 3 , b : 1 , c : 99 , d : 99

μ΄λ κ²!

μ¬κΈ°κΉμ§ μκ°νκ³  λλ©΄ μ λ§ κ°λ¨νλ€.

(a * c) + {-(b * d)} κ° μμ μ€μ, (a * d) + (b * c) κ° λ€μ νμ μ΄λ€.

b * d μ - λ₯Ό λΆμΈ μ΄μ λ b μ d λͺ¨λ νμμ΄κΈ° λλ¬Έμ i^2 μ΄ λμ μμκ° λκΈ° λλ¬Έ!

λ€μ λ¬Έμ λ‘ λμκ°μ, split μΌλ‘ + μ i λ₯Ό κΈ°μ€μΌλ‘ λͺ¨λ μͺΌκ°μ μ«μλ§ λ¨κ²Όκ³ ,

μμμ λ§ ν λλ‘ κ³±ν μλ₯Ό κ°κ° aa , aaa , bb , bbb λΌλ λ³μμ λ΄μλ¨λ€.
(λ³μλͺ μ€νλ...?)

μ΄λ ΅μ§ μκ² ν μ μλ λ¬Έμ μΈ κ² κ°λ€! (μμμκ° 4~50λΆ! λμμ§ μλ€!!)

Β Β  model solution Β Β

const complexNumberMultiply = (a, b) => {
let firstArr = a.substring(0, a.length-1).split('+');
const a1 = Number(firstArr[0])
const a2 = Number(firstArr[1])

let secondArr = b.substring(0, b.length-1).split('+');
const b1 = Number(secondArr[0])
const b2 = Number(secondArr[1])

return ${a1 * b1 - a2 * b2}+${a1 * b2 + a2 * b1}i
}

λ΄ νμ΄λ κ±°μ λΉμ·νλ° λͺ¨λΈ μλ£¨μμ κ° μ«μλ§λ€ μͺΌκ° λ€μ λ°λ‘ κ³±νλ€!

νκΈ΄...μκ°ν΄λ³΄λ aa aaa bb bbb κ°μ λ³μλͺ λ§λ€μ΄λ΄€μ μ΄ ν¨μ μμμ νλ² μ°κ³  λ§κ±΄λ° μ’ λ­λΉμλκ² κ°λ€γ γ γ γ

Day 2

Β Β  λ΄ νμ΄ Β Β

const reverseString = s => {
return s.reverse();
};

reverseString(["h","e","l","l","o"]); // ["o","l","l","e","h"]

? νλ‘μ νΈμ μ§μ€νλΌλ λ©ν λλ€μ λ°°λ €...?

Day 3

Β Β  λ΄ νμ΄ Β Β

κ±°μ λ€ νμλλ°.....indexμ λ§μ§λ§μ λΆλ«νλ©΄ errorκ° λλ€π­ ν΄κ²°μ€!!!!

Day 4

Β Β  λ΄ νμ΄ Β Β

νΈλμ€....

Day 5

Β Β  λ΄ νμ΄ Β Β

const factorial = n => {
if (n <= 1) return 1;
let result = 1;
for (let i = 1; i < n; i++) {
result *= i + 1;
}
return result;
}

factorial(10); //3628800

μΌλ¨, μ¬κ·ν¨μ λ¬Έμ κΈ΄ νμ§λ§ μ΄ λ¬Έμ λ λ±ν μ¬κ·ν¨μλ₯Ό μ°μ§ μμλ ν μ μμλΉ....

μ¬κ·ν¨μμ λνκ±΄ νλ‘μ νΈκ° λλ μ£Όλ§μ μ’ λ μ λ¦¬ν΄λ³΄κΈ°λ‘ νκ³ !!!

ν©ν λ¦¬μΌμ κ΅¬νλ λ¬Έμ μΈλ°, ν©ν λ¦¬μΌ μ΄λΌλ λ§μ λ£κ³  μ€νμ κ³ λ±νμ μνμκ°μΌλ‘ κ±°μ¬λ¬ μ¬λΌκ°λ©΄μ λ©λΆμ....ν  νμλ μ ν μκ³ ,

λ¨μν 3! = 1 * 2 * 3, 5! = 1 * 2 * 3 * 4 * 5 μ΄λΉ.

μΌλ¨ 1λΆν° μμν΄μΌ νλκΉ result λΌλ λ³μμ 1μ΄λΌλ κ°μ ν λΉνκ³ , for λ¬Έμ λλ¦°λ€.

μΈμλ‘ μ£Όμ΄μ§λ nμ κ° μμλ€μ λλ©΄μ, κ±°κΈ°μ 1μ© λν κ°μ result μ λνλ€.

λ§μ½ nμ΄ 1λ³΄λ€ μκ±°λ κ°λ€λ©΄, 1 μ return νλ€.

Β Β  model solution Β Β

const factorial = n => {
if (n === 0) return 1;

return n * factorial(n-1);
}

μ¬κ· ν¨μλ, ν¨μλ₯Ό μ μνλ©΄μ κ·Έ μμμ λμμ ν¨μλ₯Ό νΈμΆνλ ν¨μλ₯Ό λ§νλλ°,

μ΄μ λν΄ ν¬κ² μμλΏλ€ λ³΄λ, for λ¬Έμ μ΄μ©ν΄μ νμλκ² κ°λ€.

μ°μ , n === 0μΌλ, μ¬κ·λ₯Ό λ©μΆκ³  1 μ λ°ννλ μ‘°κ±΄μ λ¨Όμ  κ±Έμ΄μ€λ€.

κ·Έλ¦¬κ³  nμ factorial(n-1) μ κ³±ν΄μ€λ€. μ΄ λ, μ μκ° λλμ§λ μμ factorial κ° μ¬μ©λμλ€.

κ·ΈλΌ factorial(n-1) μ κ°μ μ΄λ»κ² μ μ μμκΉ?

λ€μ ν¨μμ μ²μμΌλ‘ λμκ°μ, μ΄λ²μ factorial(n-1) μΌλ‘ μ°μ°μ νλ€.

λ§μ§λ§μ return n * factorial(n-1); μ λ§λλ©΄, μ²μμ n μ κΈ°μ€μΌλ‘ μκ°νμ λ μ¬μ€μ return (n-1) * factorial(n-2); κ° λ κ²μ΄λ€.

factorial(n-2) μ κ°μ κ΅¬νκΈ° μν΄ λ€μ μ²μμΌλ‘ λμκ°λ€.

μΈμ  μ’λ£ν  κ²μΈκ°μ λν μ‘°κ±΄μ΄ μλ€λ©΄, λ¬΄νμΌλ‘ λ°λ³΅νκ² μ§λ§ μ°λ¦¬λ nμ΄ 0μΌ κ²½μ°μ 1μ λ°ννλ€λ μ‘°κ±΄μ κ±ΈμκΈ° λλ¬Έμ μ­μ­μ­ λ΄λ €κ°λ€κ°.... nμ΄ 0μ΄ λλ μκ°! λλμ΄ μ΄ ν¨μκ° μ’λ£λκ³  κ·Έλμμ κ²°κ³Όκ°μ΄ λ°νλλ€.

10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 λ§μ΄λ€!

(μ¬κΈ°μ 1μ, λ§μ§λ§μ return n * factorial(n-1); μ μ¬μ€, return 1 * factorial(0); μΌλ‘ κ°μ΄ 0μ΄κΈ° λλ¬Έμ n === 0 μ΄λΌλ λ§μ§λ§ μ‘°κ±΄μμ 1μ return ν¨μΌλ‘μ¨ κ°μ λ§μΆ°μ€κ²!

3μ£Όμ°¨ νκΈ° : λ¨Έλ¦¬κ° μ μ  λ§λλ§λν΄μ§λ λλμ΄κΈ΄ νλ°, ννμ΄λ λ³νμ μμ§ν λ¬΄λ¦¬λ°μ°...π­π­π­π­

νλ£¨νλ£¨λ μ±μ€νκ² μΈμ μ μ²΄λ λλλλ‘