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 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 | |
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
"""Swapping value""" | |
a, b = 15, True | |
print("Before Swapping a = {}, b = {}".format(a, b)) | |
# Before Swapping a = 15, b = True | |
a, b = b, a | |
print("After Swapping a = {}, b = {}".format(a, b)) | |
# After Swapping a = True, b = 15 |
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
"""Simple variable""" | |
a = 10 | |
print("Simple variable a = {}".format(a)) | |
# Simple variable a = 10 | |
"""Multiple variable""" | |
a, b = 10, True | |
print("Multiple variable a = {}, b = {}".format(a, b)) | |
# Multiple variable a = 10, b = True |
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
"""Join list to single string""" | |
arr = ["Python", "is", "awesome", "and", "i", "love", "it"] | |
print(" ".join(arr)) | |
# Python is awesome and i love it |
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
from collections import Counter | |
"""Find most frequent in list""" | |
arr = [1, 2, 3, 1, 2, 4, 6, 7, 4, 3 ,2, 3, 4, 5] | |
cnt = Counter(arr) | |
print("""Showing all frequent in list""") | |
print(cnt) | |
# Counter({2: 3, 3: 3, 4: 3, 1: 2, 6: 1, 7: 1, 5: 1}) | |
print("""Show only frequent in 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
from collections import Counter | |
"""Check if two words is anagram""" | |
a = "listen" | |
b = "silent" | |
is_anagrams = Counter(a) == Counter(b) | |
print("Is {} and {} is Anagrams ? {}".format(a, b, is_anagrams)) | |
# Is listen and silent is Anagrams ? True |
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
"""Reverse string""" | |
a = "python" | |
reversed_words = a[::-1] | |
print("reversed {} to {}".format(a, reversed_words)) | |
# reversed python to nohtyp | |
"""Reverse number""" | |
b = 123456789 | |
reversed_num = str(b) | |
print("reversed {} to {}".format(b, reversed_num)) |
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
"""Reverse list""" | |
a = [1, 2, 3, 4, 5] | |
reversed_list = a[::-1] | |
print("Reverse from {} to {}".format(a, reversed_list)) | |
# Reverse from [1, 2, 3, 4, 5] to [5, 4, 3, 2, 1] | |
"""Iterate over reversed list efficiency""" | |
for num in reversed(a): | |
print(num) |