[Udemy] JavaScript 알고리즘 & 자료구조 마스터클래스_Section 8 본문

내 생각/강의

[Udemy] JavaScript 알고리즘 & 자료구조 마스터클래스_Section 8

호랑구야 2023. 6. 28. 09:00

* 수업은 JS 기반이지만 Python으로 구현

Section 8

power

Write a function called power which accepts a base and an exponent. The function should return the power of the base to the exponent. This function should mimic the functionality of 
Math.pow()
  - do not worry about negative bases and exponents.

// power(2,0) // 1
// power(2,2) // 4
// power(2,4) // 16
# base의 exponent제곱 값을 구하시오
def power(base, exponent):
    
    # 만약 exponent 값이 0이 되면 return 1
    # base case, 함수의 재귀가 멈추는 시점
    if exponent == 0:
        return 1
    # 매번 다른 데이터를 이용해 함수를 호출하는 부분
    return base * power(base, exponent - 1)
    
power(3, 5)

factorial

Write a function factorial which accepts a number and returns the factorial of that number. A factorial is the product of an integer and all the integers below it; e.g., factorial four ( 4! ) is equal to 24, because 4 * 3 * 2 * 1 equals 24. factorial zero (0!) is always 1.

//factorial(1) // 1
// factorial(2) // 2
// factorial(4) // 24
// factorial(7) // 5040

# 입력된 숫자의 팩토리얼 값을 구하라
def factorial(num):
    # 0!은 1이므로 따로 조건문 제시
    if num == 0:
        return 1

    # 만약 입력값이 1이라면 return 1
    # base case, 함수의 재귀가 멈추는 시점
    if num == 1:
        return 1
    # 매번 다른 데이터를 이용해 함수를 호출하는 부분
    return num * factorial(num - 1)
    
factorial(5)
factorial(0)

productOfArray

Write a function called 
productOfArray
 which takes in an array of numbers and returns the product of them all.

// productOfArray([1,2,3]) // 6
// productOfArray([1,2,3,10]) // 60

# 입력된 배열 요소끼리의 곱을 구하여라
def productOfArray(arr):

    # 만약 입력 배열의 길이가 0이라면 return 1
    # base case, 함수의 재귀가 멈추는 시점
    if len(arr) == 0:
        return 1
    # 매번 다른 데이터를 이용해 함수를 호출하는 부분
    return arr[0] * productOfArray(arr[1:])
    
productOfArray([1, 2, 3])
productOfArray([1, 2, 3, 4, 6, 8])

recursiveRange 

Write a function called recursiveRange which accepts a number and adds up all the numbers from 0 to the number passed to the function

// recursiveRange(6) // 21
// recursiveRange(10) // 55
# 0부터 입력된 숫자까지 모두 더한 것의 합
def recursiveRange(num):

    # 만약 입력값이 0이라면 return 0
    # base case, 함수의 재귀가 멈추는 시점
    if num == 0:
        return 0
    # 매번 다른 데이터를 이용해 함수를 호출하는 부분
    return num + recursiveRange(num - 1)
    
recursiveRange(10)
recursiveRange(0)

fib

Write a recursive function called fib which accepts a number and returns the nth number in the Fibonacci sequence. Recall that the Fibonacci sequence is the sequence of whole numbers 1, 1, 2, 3, 5, 8, ... which starts with 1 and 1, and where every number thereafter is equal to the sum of the previous two numbers.

// fib(4) // 3
// fib(10) // 55
// fib(28) // 317811
// fib(35) // 9227465
# 입력값 번째의 피보나치 수열을 구해라
# 피보나치 수열이란, 직전 두 개의 수의 합이 다음의 수를 결정하는 수열이다.
def fib(num):

    # 만약 입력값이 1, 2 이라면 return 1
    # base case, 함수의 재귀가 멈추는 시점
    if num <= 2:
        return 1
    
    # 매번 다른 데이터를 이용해 함수를 호출하는 부분
    return fib(num - 1) + fib(num - 2)
    
fib(35)
fib(1)
fib(2)
fib(3)
반응형
Comments