Skip to content

Instantly share code, notes, and snippets.

View BurhanH's full-sized avatar
:octocat:
Focusing

Baur Urazalinov BurhanH

:octocat:
Focusing
View GitHub Profile
# 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
array = [1, 2, 5, 8]
def sum_array(int_array: list) -> None:
return print(sum(int_array))
if __name__ == '__main__':
sum_array(array)
# 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'
import string
s = "What's wrong with ASCII?!?!?"
s.rstrip(string.punctuation)
# 'What's wrong with ASCII'
@BurhanH
BurhanH / object.py
Created March 22, 2019 00:36
How to check object attributes in python
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("__")})
@BurhanH
BurhanH / is_bracket_list_balanced.py
Created March 5, 2019 02:13
balanced bracket list
# -*- 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 March 1, 2019 18:44
decorators, python
def null_decorator(func):
return func
def uppercase(func):
def wrapper():
original = func()
modified = original.upper()
return modified
return wrapper
@BurhanH
BurhanH / lambdas.py
Created February 26, 2019 20:46
lambdas
# -*- 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 February 26, 2019 20:38
even and odd numbers
# -*- 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 February 26, 2019 17:17
Python, JSON CLI
>> echo '{"a": [], "b": "c"}' | python -m json.tool
{
"a": [],
"b": "c"
}