Skip to content

Instantly share code, notes, and snippets.

View pat-lau's full-sized avatar

Patrick Lau pat-lau

View GitHub Profile

Keybase proof

I hereby claim:

  • I am pat-lau on github.
  • I am patricklau (https://keybase.io/patricklau) on keybase.
  • I have a public key ASBohjsFGSTlShhWbqSiSKNeQUgRE7uGI5OS7x3la6ajmwo

To claim this, I am signing this object:

@pat-lau
pat-lau / longestrun.py
Created March 12, 2016 07:44
A function called longestRun, which takes as a parameter a list of integers named L (assume L is not empty). This function returns the length of the longest run of monotonically increasing numbers occurring in L. A run of monotonically increasing numbers means that a number at position k+1 in the sequence is either greater than or equal to the n…
def getSublists(L, n):
return [L[i:i+n] for i in range(len(L)-n+1)]
def longestRun(L):
master = []
longest = 0
for i in range(len(L)+1):
for k in getSublists(L, i):
master.append(k)
for h in master:
@pat-lau
pat-lau / inversedictionary.py
Last active March 2, 2021 10:56
A function called dict_invert that takes in a dictionary with immutable values and returns the inverse of the dictionary. The inverse of a dictionary d is another dictionary whose keys are the unique dictionary values in d. The value for a key in the inverse dictionary is a sorted list of all keys in d that have the same value in d.
def dict_invert(d):
'''
d: dict
Returns an inverted dictionary according to the instructions above
'''
inv_map = {}
for k, v in d.iteritems():
inv_map[v] = inv_map.get(v, [])
inv_map[v].append(k)
inv_map[v] = sorted(inv_map[v])