Skip to content

Instantly share code, notes, and snippets.

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

Ferdina Kusumah FerdinaKusumah

🧑‍🚀
✌🏻
View GitHub Profile
@FerdinaKusumah
FerdinaKusumah / ternary.py
Last active December 5, 2019 01:50
Ternary Operator
"""Ternary operator"""
a = 15
b = 12
is_a_greater = False if b > a else True
print("Is a is greater than b ? {}".format(is_a_greater))
# Is a is greater than b ? True
@FerdinaKusumah
FerdinaKusumah / chain_comparison.py
Created December 5, 2019 01:28
Chain Comparison
"""Chain comparison"""
min_num = 5
max_num = 10
search = 3
is_middle = search < min_num < max_num
is_name = search == 3 < min_num < max_num
print("is number {} in middle {} and {} ? {}".format(search, min_num, max_num, is_middle))
# is number 3 in middle 5 and 10 ? True
@FerdinaKusumah
FerdinaKusumah / transpose_2d_list.py
Created December 5, 2019 01:29
Transpose 2d list
"""Transpose 2d list"""
a = [["a", "b"], ["c", "d"], ["e", "f"]]
transposed = zip(*a)
to_list = list(transposed)
print("Transpose list from {} to {}".format(a, to_list))
# Transpose list from [['a', 'b'], ['c', 'd'], ['e', 'f']] to [('a','c', 'e'), ('b', 'd', 'f')]
@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)
@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 / 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 / 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 / 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 / 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