Skip to content

Instantly share code, notes, and snippets.

@yoyonel
yoyonel / string_deletion.ipynb
Created March 18, 2019 17:37
Code Interview: String Deletion
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@yoyonel
yoyonel / Anagrams.py
Created March 18, 2019 16:55
Coding Interview Question: Anagrams
# Question: "Given two strings, write a function to determine whether they are anagrams"
from itertools import groupby
from operator import itemgetter
sorted_histogram = lambda s: sorted([(lower(k), len(list(g))) for k, g in groupby(s)], key=itemgetter(0))
isAnagram = lambda s1, s2: sorted_histogram(s1) == sorted_histogram(s2)
assert isAnagram("", "") == True
assert isAnagram("A", "A") == True
assert isAnagram("A", "B") == False
@yoyonel
yoyonel / String_Compression.py
Last active March 18, 2019 16:45
Coding Interview Question: String Compression
# https://www.byte-by-byte.com/stringcompression/
# Question: "Given two strings, write a function to determine whether they are anagrams."
from itertools import groupby
compress = lambda s: min(("".join([f"{k}{len(list(g))}" for k, g in groupby(s)]), s), key=lambda s_: len(s_))
assert compress(“a”) == "a"
assert compress(“aaa”) == "a3"
assert compress(“aaabbb”) == "a3b3"
assert compress(“aaabccc”) == "a3b1c3"
@yoyonel
yoyonel / tqdm_logger.py
Created March 16, 2019 13:34
Python: TQDM + Logger
"""
https://github.com/tqdm/tqdm
https://stackoverflow.com/questions/38543506/change-logging-print-function-to-tqdm-write-so-logging-doesnt-interfere-wit/38739634
https://stackoverflow.com/questions/36986929/redirect-print-command-in-python-script-through-tqdm-write
https://stackoverflow.com/questions/566746/how-to-get-linux-console-window-width-in-python
"""
import logging
import time
import tqdm
@yoyonel
yoyonel / Max sum path in binary tree.ipynb
Last active January 6, 2019 00:06
This problem was asked by Google. You are given an array of arrays of integers, where each array corresponds to a row in a triangle of numbers. For example, [[1], [2, 3], [1, 5, 1]] represents the triangle: 1 2 3 1 5 1 We define a path in the triangle to start at the top and go down one row at a time to an adjacent value, eventually ending with …
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@yoyonel
yoyonel / juggling_algorithm.ipynb
Created December 30, 2018 10:06
DP: Array Rotation in Place
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@yoyonel
yoyonel / grouper_iter.py
Created December 5, 2018 07:30
Python: Build chunks iterator from an iterable (without fillvalue)
import itertools as IT
# https://stackoverflow.com/questions/3992735/python-generator-that-groups-another-iterable-into-groups-of-n/3992765
# https://stackoverflow.com/questions/31164731/python-chunking-csv-file-multiproccessing/31170795#31170795
# https://docs.python.org/3/library/functions.html#iter
def grouper(n, iterable):
"""
>>> list(grouper(3, 'ABCDEFG'))
[['A', 'B', 'C'], ['D', 'E', 'F'], ['G']]
"""
@yoyonel
yoyonel / google_translate.ipynb
Last active November 29, 2018 17:21
Jupyter Notebook - Google Translate (by web API)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.