Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:
Focusing

Baur Urazalinov BurhanH

:octocat:
Focusing
View GitHub Profile
View eratosthenes.py
# Python 3.7
# the goal is to verify a given number is prime
def is_prime_number(x: int) -> bool:
prime_numbers = [2] # initial state
for i in range(x + 2): # creating list of prime numbers till target number
count = 0
for item in prime_numbers:
if (i + 2) % item != 0:
count += 1
View main_program.py
array = [1, 2, 5, 8]
def sum_array(int_array: list) -> None:
return print(sum(int_array))
if __name__ == '__main__':
sum_array(array)
View bitseq.py
# python 3.7
def make_bitseq(s: str) -> str:
if not s.isascii():
raise ValueError("ASCII only allowed")
return " ".join(f"{ord(i):08b}" for i in s)
make_bitseq("bits")
# '01100010 01101001 01110100 01110011'
View string_manipulation.py
import string
s = "What's wrong with ASCII?!?!?"
s.rstrip(string.punctuation)
# 'What's wrong with ASCII'
@BurhanH
BurhanH / object.py
Created Mar 22, 2019
How to check object attributes in python
View object.py
def verify_attributes(obj):
for key, value in obj.__dict__.items():
if not key.startswith("__"):
print('{0} --> {1}'.format(key, value))
print(' -------------------------------------------------------------------------------')
# or more pythonic way
# print({key: value for key, value in obj.__dict__.items() if not key.startswith("__")})
View is_bracket_list_balanced.py
# -*- coding: utf-8 -*-
# Requirements:
# get diff for balanced bracket list
# string as input, you can use only '(' or ')' symbols
# length is 1 to 1000
# Examples:
# str = '((()))'; Answer is : 0
# str = '((()'; Answer is : 2
@BurhanH
BurhanH / decorators.py
Created Mar 1, 2019
decorators, python
View decorators.py
def null_decorator(func):
return func
def uppercase(func):
def wrapper():
original = func()
modified = original.upper()
return modified
return wrapper
View lambdas.py
# -*- coding: utf-8 -*-
addition = lambda x, y: x + y
addition(3, 3)
#6
# OR
(lambda x, y: x + y)(3, 3)
#6
@BurhanH
BurhanH / even_odd.py
Created Feb 26, 2019
even and odd numbers
View even_odd.py
# -*- coding: utf-8 -*-
# even
[x for x in range(100) if x % 2 == 0]
# an analog of above expression with lambda, bad example
list(filter(lambda x: x % 2 == 0, range(100)))
# odd
@BurhanH
BurhanH / example.txt
Created Feb 26, 2019
Python, JSON CLI
View example.txt
>> echo '{"a": [], "b": "c"}' | python -m json.tool
{
"a": [],
"b": "c"
}