일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 알고리즘
- Joseph Samuel Nye Jr.
- ADsP
- 자료구조
- Hacker Rank
- 데이터분석전문가가이드
- 위대한 수업
- 조지프 나이
- Great Minds
- 정치학
- ADP
- CNN10
- K-MOOC
- Udemy
- 당신이 몰랐던 진화론
- Progate
- MySQL
- KMOOC
- 빅데이터
- python
- 코테
- 백준
- 후기
- 미분적분학
- 누가 진정한 리더인가
- 공부정리
- EBS
- Baekjoon
- 맛집
- 데이터분석전문가
Archives
- Today
- Total
ㅇ
[Udemy] JavaScript 알고리즘 & 자료구조 마스터클래스_Section 8 본문
* 수업은 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)
반응형
'내 생각 > 강의' 카테고리의 다른 글
[EBS_위대한 수업] [경제학] 폴 크루그먼, 세계 경제 예측_2강 2020 팬데믹 (0) | 2023.06.29 |
---|---|
[EBS_위대한 수업] [경제학] 폴 크루그먼, 세계 경제 예측_1강 2019 폭풍전야 (0) | 2023.06.28 |
[EBS_위대한 수업] [정치학] 조지프 나이, 누가 진정한 리더인가_6강 글로벌 리더의 자격 (0) | 2023.06.27 |
[Udemy] JavaScript 알고리즘 & 자료구조 마스터클래스_Section 10 (0) | 2023.06.27 |
[EBS_위대한 수업] [정치학] 조지프 나이, 누가 진정한 리더인가_5강 리더의 도덕 (0) | 2023.06.26 |
Comments