Skip to content

Instantly share code, notes, and snippets.

from collection import deque
def minWindow3(source, match):
counts = {}
for c in match:
counts[c] = counts[c] + 1 if c in counts else 1
lastIndexSeen = {c: deque(maxsize=counts[c]) for c in counts}
minLength = float("inf")
accMaxIndex = None
"""
lastIndexWhereLetterWasSeen
{
A: 8
C: 11
D: 10
}
"""
# source: "ABCODEBANC"
def mult_list(x: List[int], start: int, end: int, memo: Optional[Dict[Tuple[int, int], int]] = None) -> int:
memo = {} if memo is None else memo
if (start, end) in memo:
return memo[(start, end)]
if start >= end:
memo[(start, end)] = 1
else:
memo[(start, end)] = x[0] * mult_list(x, start + 1, end, memo)
return memo[(start, end)]
# You have been provided a file with a list of strings. Each string is on a new line.
# The size of the file could be anywhere between 5MB - 1000MB.
# Write a program/script to count and print lines which have the following pattern
# Any 4 char sequence which has a pair of two different characters followed by the reverse of that pair
# e.g xaax or raar. The string is not considered valid if the pattern above exist in square brackets.
# For example:
# 3003792
# http://i.imgur.com/cz0yhtx.jpg
# http://i.imgur.com/KxyEGOn.jpg
# http://i.imgur.com/vPae8qL.jpg
# http://i.imgur.com/cz0yhtx.jpg
# bad_input
# http://www.google.com/isadsada
import sys
import urllib.request
# string processing
# remove sequential duplicate characters from a string
# "aaabbccccccfffdde" -> "abcfde"
#"abcdefghijklmnop"
# b = num of boundaries
# n * log(n)
def remove_repeated(x: str) -> str:
if len(x) <= 1:
# 1 2 3 -> 3
# 1 2 8 6 7 -> 4
# 1 2 6 7
# 1 2 -99 3 -97 -96 -95 4 5 => 4
# 1: 1
# 2: 2
# -99: 1
# 3: 3
from typing import List
#lengths
def test_max_prod(total: int, acc = None) -> List[int]:
if acc is None:
acc = []
if total == 0:
return acc
else:
max_list = []
for i in range(1, total + 1):
def find_pairs(array1: List[int], array2: List[int], x: int, y: int) -> int:
return len([(i,j) for i in array1 for j in array2 if x< i*i + j*j < y])
from typing import Dict, Tuple, List
Letter = str
Type = str
AST = List[Tuple[Letter, 'AST']]
def build_ast(tokens: List[Tuple[Letter, Type]]) -> AST:
if len(tokens) == 0:
return []