This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
class ExampleTestUnit(unittest.TestCase): | |
def test_sum(self): | |
self.assertEqual(sum([1, 2, 3]), 6, "Hasilnya adalah 6") | |
def test_count_array(self): | |
array = [1, 2, 3, 4] | |
self.assertEqual(len(array), 4, "Jumlah arraynya ada 5") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sum_values(nums: list) -> int: | |
total = 0 | |
for i in nums: | |
total = total + i | |
return total |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
import sum | |
class ExampleTestUnit(unittest.TestCase): | |
def test_sum(self): | |
self.assertEqual(sum.sum_values([1, 2, 3]), 6, "Hasilnya adalah 6") | |
def test_error(self): | |
with self.assertRaises(TypeError): | |
sum.sum_values([1, "2", "3"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fibonacci(n: int): | |
""" Define function fibonacci with recursion""" | |
if n == 0: | |
return 0 | |
elif n == 1: | |
return 1 | |
return fibonacci(n-1) + fibonacci(n-2) | |
N = 35 | |
fib = fibonacci(N) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
from functools import wraps | |
def memoize(func): | |
cache = dict() | |
@wraps(func) | |
def to_cache(*args): | |
if args in cache: | |
return cache[args] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
from functools import lru_cache | |
@lru_cache(maxsize=256) | |
def fibonacci_lru_cache(n: int): | |
if n == 0: | |
return 0 | |
elif n == 1: | |
return 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def deterministic_function(a, b): | |
return a + b | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def nondeterministic_function(a, b): | |
if a % b == 0: | |
return "Even number" | |
return "Odd number" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Example: | |
def __init__(self): | |
self.foo = 21 | |
self._bar = 25 | |
>>> t = Example() | |
>>> t.foo | |
21 | |
>>> t._bar |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is example_module.py: | |
def external_func(): | |
return 23 | |
def _internal_func(): | |
return 42 | |
>>> from example_module import * |