Skip to content

Instantly share code, notes, and snippets.

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

Ferdina Kusumah FerdinaKusumah

🧑‍🚀
✌🏻
View GitHub Profile
@FerdinaKusumah
FerdinaKusumah / reverse_string.py
Created July 28, 2020 13:09
[Python] Reverse String
"""Reverse String"""
words = "python is awesome !!"
reverse_string_1 = words[::-1]
"""First way"""
# !! emosewa si nohtyp
"""Second way"""
reverse_string_2 = ''.join(reversed(words))
# !! emosewa si nohtyp
@FerdinaKusumah
FerdinaKusumah / anagram.py
Created July 28, 2020 13:04
[Python] Check if two words is anagram
from collections import Counter
"""Check if two words are anagram"""
a = "listen"
b = "silent"
is_anagram = Counter(a) == Counter(b)
# True
@FerdinaKusumah
FerdinaKusumah / most_frequent.py
Created July 28, 2020 13:01
[Python] Most Frequent in list
from collections import Counter
"""Find most frequent in list"""
arr = [1, 2, 3, 1, 2, 4, 6, 7, 3, 2, 3, 4]
cnt = Counter(arr)
# Counter({2: 3, 3: 3, 1: 2, 4: 2, 6: 1, 7: 1})
"""Show only frequent in 2 times"""
print(cnt.most_common(2))
# [(2, 3), (3, 3)]
@FerdinaKusumah
FerdinaKusumah / swapping_variable.py
Created July 28, 2020 12:55
[Python] Swapping variable
"""Swapping Variable"""
a, b = 10, True
print(f'before swapping value a = {a}, b = {b}')
# before swapping value a = 10, b = True
a, b = b, a
print(f'after swapping value a = {a}, b = {b}')
# after swapping value a = True, b = 10
@FerdinaKusumah
FerdinaKusumah / variable.py
Created July 28, 2020 12:46
[Python] - Define Variable
"""Simple Variable"""
a = 10
print(f'simple variable a = {a}')
# simple variable a = 10
"""Multiple Variable"""
a, b = 10, True
print(f'multi variable a, b = {a}, {b}')
# multi variable a, b = 10, True
@FerdinaKusumah
FerdinaKusumah / config.yml
Created July 28, 2020 12:33
Yaml Configuration Example
app:
name: "Service name"
description: "Service name description"
local:
db:
host: "iphost"
port: 5432
password: "very secret"
db-name: "dbName"
dev:
class PrefixPostfixTest:
def __init__(self):
self.__bam__ = 42
>>> PrefixPostfixTest().__bam__
42
@FerdinaKusumah
FerdinaKusumah / mangling_function.py
Created December 21, 2019 16:09
Underscore python
class MangledMethod:
def __method(self):
return 42
def call_it(self):
return self.__method()
>>> MangledMethod().__method()
AttributeError: "'MangledMethod' object has no attribute '__method'"
>>> MangledMethod().call_it()
@FerdinaKusumah
FerdinaKusumah / mangling_variable.py
Last active December 21, 2019 16:06
Underscore Python
class ManglingTest:
def __init__(self):
self.__mangled = 'hello'
def get_mangled(self):
return self.__mangled
>>> ManglingTest().get_mangled()
'hello'
>>> ManglingTest().__mangled
class ExtendedSample(Sample):
def __init__(self):
super().__init__()
self.a = 'overridden'
self._b = 'overridden'
self.__c = 'overridden'
>>> t2 = ExtendedSample()