This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array = [1, 2, 5, 8] | |
def sum_array(int_array: list) -> None: | |
return print(sum(int_array)) | |
if __name__ == '__main__': | |
sum_array(array) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import string | |
s = "What's wrong with ASCII?!?!?" | |
s.rstrip(string.punctuation) | |
# 'What's wrong with ASCII' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("__")}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def null_decorator(func): | |
return func | |
def uppercase(func): | |
def wrapper(): | |
original = func() | |
modified = original.upper() | |
return modified | |
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
addition = lambda x, y: x + y | |
addition(3, 3) | |
#6 | |
# OR | |
(lambda x, y: x + y)(3, 3) | |
#6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> echo '{"a": [], "b": "c"}' | python -m json.tool | |
{ | |
"a": [], | |
"b": "c" | |
} |
NewerOlder