Skip to content

Instantly share code, notes, and snippets.

View FerdinaKusumah's full-sized avatar
🧑‍🚀
✌🏻

Ferdina Kusumah FerdinaKusumah

🧑‍🚀
✌🏻
View GitHub Profile
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"])
@FerdinaKusumah
FerdinaKusumah / sum.py
Created December 5, 2019 01:43
Simple Unit test 2
def sum_values(nums: list) -> int:
total = 0
for i in nums:
total = total + i
return total
@FerdinaKusumah
FerdinaKusumah / simple_unit_test.py
Created December 5, 2019 01:41
Single Unit Test
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")
@FerdinaKusumah
FerdinaKusumah / swapping_value.py
Created December 5, 2019 01:37
Swapping Value
"""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
@FerdinaKusumah
FerdinaKusumah / define_variable.py
Created December 5, 2019 01:36
Variation define variable
"""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
@FerdinaKusumah
FerdinaKusumah / single_string_from_list.py
Last active December 5, 2019 01:49
Single string from list
"""Join list to single string"""
arr = ["Python", "is", "awesome", "and", "i", "love", "it"]
print(" ".join(arr))
# Python is awesome and i love it
@FerdinaKusumah
FerdinaKusumah / most_frequent_list.py
Created December 5, 2019 01:34
Most frequent in list
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""")
@FerdinaKusumah
FerdinaKusumah / check_anagrams.py
Last active December 5, 2019 01:49
Check if 2 words is angrams
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
@FerdinaKusumah
FerdinaKusumah / reverse_string.py
Created December 5, 2019 01:32
Reverse String
"""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))
@FerdinaKusumah
FerdinaKusumah / reverse_list.py
Last active December 5, 2019 01:49
Reverse List
"""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)