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
# Online Python - IDE, Editor, Compiler, Interpreter | |
import string | |
EXCLUDE_CHARS = {each_invalid_char: "" for each_invalid_char in string.punctuation} | |
SAME_CHARS = {**{w:w for w in string.ascii_letters}, **EXCLUDE_CHARS} | |
EXCLUDE_CHAR_MAP = str.maketrans(SAME_CHARS) | |
def reverse_string(sentence: str): |
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 largest_array_sum(arr): | |
n = len(arr) | |
if n < 1: | |
return 0 | |
if n < 2: | |
return arr[0] | |
max_sum = float("-inf") | |
current_sum = arr[0] | |
for num in arr[1:]: | |
current_sum = max(current_sum + num, num) |
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 java.util.* | |
import kotlin.reflect.KFunction0 | |
/** | |
* Rule 1: | |
* If a word begins with a vowel sound, add an "ay" sound to the end of the word. | |
* Please note that "xr" and "yt" at the beginning of a word make vowel sounds | |
* (e.g. "xray" -> "xrayay", "yttria" -> "yttriaay"). | |
* | |
* Rule 2: | |
* If a word begins with a consonant sound, move it to the end of the word and then add an "ay" sound |
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 concurrent.futures as cf | |
import asyncio | |
from time import sleep | |
def two(): | |
print('two') | |
sleep(4) # blocking | |
print('two again') |
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 functools as ft | |
# See: https://github.com/python/cpython/blob/e2d65630f36712dbdbf7711520c985c526a5cc25/Lib/functools.py#L525 | |
@ft.lru_cache | |
def min_count(L: list, target: int, sum_count = None) -> int: | |
"""Returns minimum count to reach target given necessary unlimited coins of that denomation.""" | |
if not L: return -1 | |
sum_count = sum_count or 0 | |
if target < 0: | |
return float('inf') |
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 itertools | |
def p1(): | |
res = None | |
for x in itertools.cycle(range(5)): | |
yield x | |
res = yield | |
print(f"x={res}") | |
if res == -1: | |
print(f"ending term for x={x} res={res}") |
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 nextGreaterElement(array): | |
if not array: return [] | |
n = len(array) | |
if n == 1: | |
return [-1] | |
stack = [] | |
result = [-1 for _ in range(n)] | |
# fill next greater values - non circular, from stack | |
for long_idx in range(2*n): | |
idx = long_idx%n |
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 sort(stack, element): | |
if not stack or element > stack[-1]: | |
stack += [element] | |
else: | |
top_el = stack.pop() | |
sort(stack, element) | |
stack += [top_el] | |
return stack | |
def sortStack(stack): |
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
"""https://www.algoexpert.io/questions/Sunset%20Views | |
Return an array in descending order given an array of positive non-zero integers either starting from left or right | |
""" | |
import functools | |
def sunsetViews(buildings, direction): | |
n = len(buildings) | |
def get_index(direction, n, idx): |
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 solution(A): | |
# write your code in Python 3.6 | |
max_diff = float('-inf') | |
cur_sum = 0 | |
n = len(A) | |
for idx, num in zip(range(1, n), A[1:]): | |
diff = num - A[idx-1] | |
if cur_sum > 0: | |
cur_sum += diff | |
else: |