mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-04 12:12:45 +08:00
feat(ko): add the Korean online edition (#2006)
* feat(ko): add the complete Korean online edition * Use UTF-8 for Python CI output on Windows * Remove print-book promotion from the Korean site * fix(ko): preserve emphasis before Korean particles
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
"""
|
||||
File: complexity_exercises.py
|
||||
Created Time: 2026-08-18
|
||||
Author: Hello Algo Team
|
||||
"""
|
||||
|
||||
|
||||
def sum_iter(n: int) -> int:
|
||||
"""반복으로 합 구하기"""
|
||||
res = 0
|
||||
for i in range(1, n + 1):
|
||||
res += i
|
||||
return res
|
||||
|
||||
|
||||
def sum_recur(n: int) -> int:
|
||||
"""재귀로 합 구하기"""
|
||||
if n == 1:
|
||||
return 1
|
||||
return n + sum_recur(n - 1)
|
||||
|
||||
|
||||
def linear_loop(n: int) -> int:
|
||||
"""선형 차수 반복문"""
|
||||
res = 0
|
||||
for i in range(n):
|
||||
res += i
|
||||
return res
|
||||
|
||||
|
||||
def quadratic_loop(n: int) -> int:
|
||||
"""제곱 차수 반복문"""
|
||||
res = 0
|
||||
for i in range(n):
|
||||
for j in range(i, n):
|
||||
res += j
|
||||
return res
|
||||
|
||||
|
||||
def logarithmic_loop(n: int) -> int:
|
||||
"""로그 차수 반복문"""
|
||||
while n > 1:
|
||||
n //= 2
|
||||
return n
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
assert sum_iter(1) == sum_recur(1) == 1
|
||||
assert sum_iter(4) == sum_recur(4) == 10
|
||||
assert linear_loop(4) == 6
|
||||
assert quadratic_loop(4) == 20
|
||||
assert logarithmic_loop(4) == logarithmic_loop(5) == 1
|
||||
Reference in New Issue
Block a user