Skip to content

Commits on Source 2

.idea
.venv
dictionary = ['the', 'a', 'is', 'of', 'and', 'to', 'in', 'you', 'that', 'it', 'he', 'for', 'was', 'on', 'are', 'as',
'with', 'his', 'they', 'at', 'be', 'call', 'me', 'if', 'this', 'from', 'i', 'have', 'or', 'by', 'one',
'had', 'not', 'but', 'what', 'all', 'were', 'when', 'we', 'there', 'can', 'an', 'your', 'which', 'their',
'said', 'if', 'do', 'will', 'each', 'about', 'how', 'up', 'out', 'them', 'then', 'she', 'many', 'some',
'so', 'these', 'would', 'other', 'into', 'has', 'more', 'her', 'dream', 'two', 'like', 'him', 'see',
'time', 'could', 'no', 'make', 'than', 'first', 'been', 'its', 'who', 'now', 'people', 'my', 'made',
'over', 'did', 'down', 'only', 'way', 'find', 'use', 'may', 'water', 'long', 'little', 'very', 'after',
'words', 'called', 'just', 'where', 'most', 'know', 'get', 'through', 'back', 'much', 'go', 'good', 'new',
'write', 'our', 'me', 'man', 'too', 'any', 'day', 'same', 'right', 'look', 'think', 'also', 'around',
'another', 'came', 'come', 'work', 'three', 'must', 'because', 'does', 'part', 'even', 'place', 'well',
'such', 'here', 'take', 'why', 'help', 'put', 'different', 'away', 'again', 'off', 'went', 'old',
'number', 'great', 'tell', 'men', 'say', 'small', 'every', 'found', 'still', 'between', 'mane', 'should',
'home', 'big', 'give', 'air', 'line', 'set', 'own', 'under', 'read', 'last', 'never', 'us', 'left', 'end',
'along', 'while', 'might', 'next', 'sound', 'below', 'saw', 'something', 'thought', 'both', 'few',
'those', 'always', 'show', 'large', 'often', 'together', 'asked', 'house', 'world', 'going', 'want',
'school', 'important', 'until', 'form', 'food', 'keep', 'children', 'feet', 'land', 'side', 'without',
'boy', 'once', 'animal', 'life', 'enough', 'took', 'four', 'head', 'above', 'kind', 'began', 'almost',
'live', 'page', 'got', 'earth', 'need', 'far', 'hand', 'high', 'year', 'mother', 'light', 'country',
'father', 'let', 'night', 'picture', 'being', 'study', 'second', 'soon', 'story', 'since', 'white',
'ever', 'paper', 'hard', 'near', 'sentence', 'better', 'best', 'across', 'during', 'today', 'however',
'sure', 'knew', 'try', 'told', 'young', 'sun', 'thing', 'whole', 'hear', 'example', 'heard', 'several',
'change', 'answer', 'room', 'sea', 'against', 'top', 'turned', 'learn', 'point', 'city', 'play', 'toward',
'five', 'himself', 'usually', 'money', 'seen', 'car', 'morning', 'body', 'upon', 'family', 'later',
'turn', 'move', 'face', 'door', 'cut', 'done', 'group', 'true', 'half', 'red', 'fish', 'plants', 'living',
'black', 'eat', 'short', 'run', 'book', 'gave', 'order', 'open', 'ground', 'cold', 'really', 'table',
'remember', 'tree', 'course', 'front', 'space', 'inside', 'ago', 'sad', 'early', 'learned', 'brought',
'close', 'nothing', 'though', 'idea', 'before', 'lived', 'became', 'add', 'become', 'grow', 'draw', 'yet',
'less', 'wind', 'behind', 'cannot', 'letter', 'among', 'able', 'dog', 'shown', 'mean', 'english', 'rest',
'perhaps', 'certain', 'six', 'feel', 'fire', 'ready', 'green', 'yes', 'built', 'special', 'ran', 'full',
'town', 'complete', 'oh', 'person', 'hot', 'anything', 'hold', 'state', 'list', 'stood', 'hundred', 'ten',
'fast', 'felt', 'kept', 'notice', 'strong', 'voice', 'probably', 'area', 'horse', 'matter', 'stand',
'box', 'start', 'class', 'piece', 'surface', 'river', 'common', 'stop', 'am', 'talk', 'whether', 'fine',
'round', 'dark', 'past', 'ball', 'girl', 'road', 'blue', 'instead', 'either', 'held', 'already', 'warm',
'gone', 'finally', 'summer', 'understand', 'moon', 'animals', 'mind', 'outside', 'power', 'problem',
'longer', 'winter', 'deep', 'heavy', 'carefully', 'follow', 'beautiful', 'everyone', 'leave',
'everything', 'game', 'system', 'bring', 'watch', 'shell', 'dry', 'within', 'floor', 'ice', 'ship',
'themselves', 'begin', 'fact', 'third', 'quite', 'carry', 'distance', 'although', 'sat', 'possible',
'heart', 'real', 'simple', 'snow', 'rain', 'suddenly', 'easy', 'leaves', 'lay', 'size', 'wild', 'weather',
'miss', 'pattern', 'sky', 'walked', 'main', 'someone', 'center', 'field', 'stay', 'itself', 'boat',
'question', 'wide', 'least', 'tiny', 'hour', 'happened', 'foot', 'care', 'low', 'else', 'gold', 'build',
'glass', 'rock', 'tall', 'alone', 'bottom', 'check', 'reading', 'fall', 'poor', 'map', 'friend',
'language', 'job', 'music', 'buy', 'window', 'mark', 'heat', 'grew', 'listen', 'ask', 'single', 'clear',
'energy', 'week', 'explain', 'lost', 'spring', 'travel', 'wrote', 'farm', 'circle', 'whose', 'correct',
'bed', 'measure', 'straight', 'base', 'mountain', 'caught', 'hair', 'bird', 'wood', 'color', 'war', 'fly',
'yourself', 'seem', 'thus', 'square', 'moment', 'teacher', 'happy', 'bright', 'sent', 'present', 'plan',
'rather', 'length', 'speed', 'machine', 'information', 'except', 'figure', 'free', 'fell', 'suppose',
'natural', 'ocean', 'government', 'baby', 'grass', 'plane', 'street', 'reason', 'difference', 'maybe',
'history', 'mouth', 'middle', 'step', 'child', 'strange', 'wish', 'soil', 'human', 'trip', 'woman', 'eye',
'milk', 'choose', 'north', 'seven', 'famous', 'late', 'pay', 'sleep', 'iron', 'trouble', 'store',
'beside', 'oil', 'modern', 'fun', 'catch', 'business', 'reach', 'lot', 'case', 'speak', 'shape', 'eight',
'edge', 'soft', 'village', 'object', 'age', 'minute', 'wall', 'meet', 'record', 'copy', 'forest',
'especially', 'necessary', 'unit', 'flat', 'direction', 'south', 'subject', 'skin', 'yellow', 'party',
'force', 'test', 'bad', 'temperature', 'pair', 'ahead', 'wrong', 'practice', 'sand', 'tail', 'wait',
'difficult', 'general', 'cover', 'material', 'thousand', 'sign', 'guess', 'forward', 'huge', 'ride',
'region', 'nor', 'period', 'blood', 'rich', 'team', 'corner', 'cat', 'amount', 'garden', 'led', 'note',
'various', 'race', 'bit', 'result', 'brother', 'addition', 'dead', 'weight', 'thin', 'stone', 'hit',
'wife', 'island', 'opposite', 'born', 'sense', 'cattle', 'million', 'anyone', 'rule', 'science', 'afraid',
'women', 'produce', 'pull', 'son', 'meant', 'broken', 'interest', 'chance', 'thick', 'sight', 'pretty',
'train', 'fresh', 'drive', 'lead', 'break', 'sit', 'bought', 'radio', 'method', 'king', 'similar',
'return', 'corn', 'decide', 'position', 'bear', 'hope', 'song', 'engine', 'board', 'control', 'spread',
'evening', 'brown', 'clean', 'section', 'spent', 'ring', 'teeth', 'quiet', 'ancient', 'stick',
'afternoon', 'silver', 'nose', 'century', 'therefore', 'level', 'death', 'hole', 'coast', 'cross',
'sharp', 'fight', 'capital', 'fill', 'deal', 'busy', 'beyond', 'send', 'love', 'cool', 'cause', 'please',
'meat', 'lady', 'west', 'glad', 'action', 'pass', 'type', 'attention', 'gas', 'kitchen', 'pick', 'scale',
'basic', 'happen', 'safe', 'grown', 'cost', 'wear', 'act', 'hat', 'arm', 'believe', 'major', 'gray',
'wonder', 'include', 'describe', 'electric', 'sold', 'visit', 'sheep', 'office', 'row', 'contain', 'fit',
'equal', 'value', 'yard', 'beat', 'inch', 'sugar', 'key', 'product', 'desert', 'bank', 'farther', 'won',
'total', 'sell', 'wire', 'rose', 'cotton', 'spoke', 'rope', 'fear', 'shore', 'throughout', 'compare',
'movement', 'exercise', 'bread', 'process', 'nature', 'apart', 'path', 'careful', 'narrow', 'mental',
'nine', 'useful', 'public', 'according', 'steel', 'salt', 'speech', 'forth', 'nation', 'knowledge',
'appear', 'ate', 'dinner', 'hurt', 'spend', 'experiment', 'touch', 'drop', 'chair', 'east', 'separate',
'truck', 'sing', 'column', 'twice', 'particular', 'shop', 'unless', 'spot', 'neither', 'met', 'wheel',
'none', 'hill', 'television', 'bill', 'solve', 'pressure', 'report', 'farmer', 'count', 'trade', 'chief',
'month', 'clothes', 'doctor', 'indeed', 'dance', 'church', 'original', 'enjoy', 'string', 'sister',
'familiar', 'onto', 'imagine', 'blow', 'quick', 'law', 'lie', 'final', 'rise', 'loud', 'fair', 'herself',
'slow', 'noise', 'statement', 'hungry', 'join', 'tube', 'rode', 'empty', 'twenty', 'broke', 'nice',
'effect', 'paid', 'motion', 'myself', 'divide', 'supply', 'laid', 'dear', 'surprise', 'gun', 'entire',
'fruit', 'crowd', 'band', 'wet', 'solid', 'northern', 'flower', 'star', 'feed', 'wooden', 'sort',
'develop', 'shoulder', 'variety', 'season', 'share', 'jump', 'regular', 'represent', 'market', 'flew',
'finger', 'expect', 'army', 'cabin', 'camp', 'danger', 'purpose', 'breakfast', 'proper', 'coat', 'push',
'express', 'shot', 'angry', 'southern', 'dress', 'bag', 'proud', 'neck', 'breath', 'strength', 'member',
'twelve', 'mine', 'company', 'current', 'pound', 'valley', 'double', 'till', 'match', 'average', 'die',
'liquid', 'alive', 'stream', 'provide', 'drink', 'experience', 'future', 'tomorrow', 'drove',
'population', 'finish', 'station', 'shook', 'stage', 'oxygen', 'poem', 'solution', 'burn', 'cent',
'electricity', 'everybody', 'rate', 'dust', 'worth', 'community', 'captain', 'bus', 'protect', 'cook',
'raise', 'further', 'steam', 'guide', 'discover', 'plain', 'usual', 'seat', 'accept', 'police',
'consider', 'dozen', 'baseball', 'rubber', 'symbol', 'support', 'exactly', 'industry', 'beneath', 'laugh',
'groceries', 'popular', 'thank', 'quarter', 'climbed', 'continue', 'potatoes', 'receive', 'design',
'president', 'charge', 'mistake', 'hospital', 'remain', 'service', 'increase', 'students', 'insects',
'address', 'sincerely', 'dollars', 'belong', 'bottle', 'flight', 'forget', 'bicycle', 'secret', 'soldier',
'silent', 'structure', 'height', 'observe', 'indicate', 'railroad', 'knife', 'married', 'suggested',
'entered', 'magazine', 'agree', 'fifty', 'escape', 'threw', 'planet', 'dangerous', 'event', 'leader',
'peace', 'spelling', 'chapter', 'swimming', 'opportunity', 'immediately', 'favorite', 'settled',
'telephone', 'repeat', 'prepare', 'instance', 'avenue', 'newspaper', 'actually', 'employee', 'review',
'convince', 'allowed', 'nobody', 'details', 'muscles', 'model', 'climate', 'coffee', 'whenever',
'serious', 'angle', 'feather', 'determined', 'dictionary', 'ordinary', 'extra', 'rough', 'library',
'condition', 'arrived', 'located', 'program', 'pencil', 'tongue', 'title', 'enemy', 'garage', 'lose',
'vegetable', 'parents', 'style', 'education', 'required', 'political', 'daughter', 'individual',
'progress', 'altogether', 'activities', 'article', 'equipment', 'discuss', 'healthy', 'perfect',
'recognize', 'frequently', 'character', 'personal', 'disappear', 'success', 'traffic', 'yesterday',
'situation', 'realize', 'message', 'recently', 'account', 'physical', 'neighbor', 'excited', 'whisper',
'available', 'college', 'furniture', 'leather', 'husband', 'principal', 'medicine', 'excellent',
'operation', 'council', 'author', 'organize', 'concern', 'barbecue', 'accident', 'disease',
'construction', 'motor', 'affect', 'conversation', 'evidence', 'citizen', 'environment', 'influence',
'cancel', 'audience', 'apartment', 'worse', 'transportation', 'frozen', 'waste', 'couple', 'function',
'connect', 'project', 'pronounce', 'offered', 'apply', 'improve', 'stomach', 'collect', 'prevent',
'courage', 'occur', 'foreign', 'quality', 'terrible', 'instrument', 'balance', 'ability', 'arrange',
'rhythm', 'avoid', 'daily', 'identity', 'standard', 'combine', 'attached', 'frighten', 'social',
'factory', 'license', 'recommend', 'incredible']
def get_suggestions(text):
return list(filter(lambda x: x.startswith(text), dictionary))[:3]
def encode(message):
if not message:
return ""
result = ""
words = message.split(" ")
for word in words:
if word not in dictionary:
result += word + ' '
continue
for letter_index in range(-1,len(word)):
suggestions = get_suggestions(word[:letter_index] if letter_index >= 0 else "")
if word in suggestions:
result += str(suggestions.index(word) + 1)
break
if letter_index >= 0:
result += word[letter_index]
if not result[-1].isnumeric():
result += ' '
return result.strip()
def decode(message):
if not message:
return ""
result = []
current_search = ""
for letter in message:
if letter == " ":
result.append(current_search)
current_search = ""
continue
if not letter.isnumeric():
current_search += letter
continue
index = int(letter) - 1
suggestions = get_suggestions(current_search)
result.append(suggestions[index])
current_search = ''
if current_search:
result.append(current_search)
return ' '.join(result)
assert encode("") == ""
assert decode("") == ""
assert encode("the a is") == "123"
assert decode("123") == "the a is"
assert encode("that was incredibly simple") == "t3w1incredibly sim1"
assert decode("t3w1incredibly sim1") == "that was incredibly simple"
assert encode("that was simply incredible") == "t3w1simply incr2"
assert decode("t3w1simply incr2") == "that was simply incredible"
assert encode("notinthedictionary") == "notinthedictionary"
assert decode("notinthedictionary") == "notinthedictionary"
assert encode("the then they them their there these") == "1then th3them thei1the3thes1"
assert decode("1then th3them thei1the3thes1") == "the then they them their there these"
assert encode("if you can dream it you can do it") == "if y1c2d2i3y1c2d1i3"
assert decode("if y1c2d2i3y1c2d1i3") == "if you can dream it you can do it"
assert encode("abcd efgh ijkl mnop qrst uvwx yz") == "abcd efgh ijkl mnop qrst uvwx yz"
assert decode("abcd efgh ijkl mnop qrst uvwx yz") == "abcd efgh ijkl mnop qrst uvwx yz"
assert encode("those who can imagine anything can create the impossible") == "tho2who c2im2any2c2create 1impossible"
assert decode("tho2who c2im2any2c2create 1impossible") == "those who can imagine anything can create the impossible"
assert encode(
"computers are good at following instructions but not at reading your mind") == "computers a3g3at following instructions b3n1at read3y2mi2"
assert decode(
"computers a3g3at following instructions b3n1at read3y2mi2") == "computers are good at following instructions but not at reading your mind"
assert encode("before software should be reusable it should be usable") == "bef1software sh2b1reusable i3sh2b1usable"
assert decode("bef1software sh2b1reusable i3sh2b1usable") == "before software should be reusable it should be usable"
assert encode(
"a good programmer is someone who always looks both ways before crossing a one way street") == "2g3programmer 3som3who alw1looks bo1ways bef1crossing 2on2wa2str3"
assert decode(
"2g3programmer 3som3who alw1looks bo1ways bef1crossing 2on2wa2str3") == "a good programmer is someone who always looks both ways before crossing a one way street"
def duplicate_count(text):
text = text.lower()
if not text:
return 0
count_map = {letter: text.count(letter) for letter in set(text)}
return len(tuple(filter(lambda x: x[1] > 1, count_map.items())))
assert duplicate_count("") == 0
assert duplicate_count("abcde") == 0
assert duplicate_count("abcdeaa") == 1
assert duplicate_count("abcdeaB") == 2
assert duplicate_count("Indivisibilities") == 2
def solution(number):
n3 = number//3
n5 = number//5
s3 = n3/2 * (2*3 + (n3-1) * 3)
s5 = n5/2 * (2*0 + (n5-1) * 5)
print(number, '|', n3, n5, '|', s3, s5, '|', s3 + s5)
return int(s3 + s5)
assert solution(10) == 23
assert solution(20) == 78
assert solution(100) == 2318
assert solution(200) == 9168
assert solution(1000) == 233168
assert solution(10000) == 23331668
def button_sequences(seq_r, seq_b):
red_seq = [c == '1' for c in seq_r]
blue_seq = [c == '1' for c in seq_b]
r_on, b_on = False, False
result = ''
for r_state, b_state in zip(red_seq, blue_seq):
if r_state and not b_state:
if not r_on:
result += 'R'
r_on = True
b_on = False
elif b_state and not r_state:
if not b_on:
result += 'B'
b_on = True
r_on = False
r_prev, b_prev = r_state, b_state
return result
if __name__ == '__main__':
print(button_sequences('10011010', '10110111'))
def divisors(n):
divs = [i for i in range(2, n) if n % i == 0]
if not divs:
return f'{n} is prime'
return divs
def elections_winners(votes, k):
votes.sort(reverse=True)
has_leader = votes[0] != votes[1]
if k == 0:
return 1 if has_leader else 0
result = votes.count(votes[0])
for vote in votes[result:]:
if vote + k > votes[0]:
result += 1
else:
break
return result
assert elections_winners([2, 3, 5, 2], 3) == 2
assert elections_winners([1, 3, 3, 1, 1], 0) == 0
assert elections_winners([5, 1, 3, 4, 1], 0) == 1
assert elections_winners([1, 1, 1, 1], 1) == 4
assert elections_winners([1, 1, 1, 1], 0) == 0
assert elections_winners([3, 1, 1, 3, 1], 2) == 2
from functools import reduce
def disjunction(operands, is_exclusive):
if not is_exclusive:
return any(operands)
return reduce(lambda old, current: old != current, operands[1:], operands[0])
assert disjunction([False, True, True], False) == True
assert disjunction([False, True, False], False) == True
assert disjunction([False, True, True, True], True) == True
assert disjunction([True, True, True], False) == True
assert disjunction([True, True, True, True], False) == True
def sum_numbers_of_number(n):
return sum(map(int, str(n)))
def get_comfortable(n):
return tuple(range(n - sum_numbers_of_number(n), n + sum_numbers_of_number(n) + 1))
def comfortable_numbers(l, r):
comfortable_map = {
n: get_comfortable(n) for n in range(l, r+1)
}
pairs_count = 0
for a in range(l, r + 1):
for b in range(a, r + 1):
if a >= b: continue
if b in comfortable_map[a] and a in comfortable_map[b]:
pairs_count += 1
return pairs_count
assert comfortable_numbers(10, 12) == 2
assert comfortable_numbers(1, 9) == 20
assert comfortable_numbers(13, 13) == 0
assert comfortable_numbers(12, 108) == 707
assert comfortable_numbers(239, 777) == 6166
assert comfortable_numbers(1, 1000) == 11435
def sum_numbers_of_number(n):
curr = n
result = 0
while curr != 0:
result += curr % 10
curr //= 10
return result
def most_frequent_digit_sum(n):
sequence = []
while n>0:
sequence.append(n)
n -= sum_numbers_of_number(n)
new_sequence = list(map(sum_numbers_of_number, sequence))
frequent_map = {x: new_sequence.count(x) for x in set(new_sequence)}
freq_items = list(frequent_map.items())
freq_items.sort(key=lambda x: x[0], reverse=True)
freq_items.sort(key=lambda x: x[1], reverse=True)
return freq_items[0][0]