{{ message }}

Instantly share code, notes, and snippets.

Focusing

# Baur Urazalinov BurhanH

Focusing
Last active Jul 30, 2019
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
Created Jul 27, 2019
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)
Created May 20, 2019
Convert a character into 8 bits (https://realpython.com/python-encodings-guide/)
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'
Created May 20, 2019
string manipulation (https://realpython.com/python-encodings-guide/)
View string_manipulation.py
 import string s = "What's wrong with ASCII?!?!?" s.rstrip(string.punctuation) # 'What's wrong with ASCII'
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("__")})
Created Mar 5, 2019
balanced bracket list
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
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
Created Feb 26, 2019
lambdas
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
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
Created Feb 26, 2019
Python, JSON CLI
View example.txt
 >> echo '{"a": [], "b": "c"}' | python -m json.tool { "a": [], "b": "c" }