Nested Lists

Published

Nested Lists

Here are some examples of nested lists I found interesting while practicing in Leetcode. Sometimes you need an example to create you own.

Richest Customer Wealth

Richest Customer Wealth
1
return reduce(lambda a, b: a if a > (s:=sum(b)) else s, accounts, 0)

LeetCode source

XOR Operation in an Array

XOR Operation in an Array
1
return functools.reduce(lambda a, b: a ^ b, [start + 2 * i for i in range(n)])

LeetCode source

Decode XORed Array

decode-xored-array
1
return list(accumulate([first] + A, lambda x, y: x ^ y))
2
3
return [first] + [first:=first^e for e in encoded]

LeetCode source

Merge Strings Alternately

Merge Strings Alternately
1
return ''.join(a + b for a, b in zip_longest(w1, w2, fillvalue=''))
2
3
return ''.join(chain.from_iterable(zip_longest(w1, w2, fillvalue='')))

LeetCode source

To Lower Case

To Lower Case
1
return "".join(chr(ord(c) + 32) if "A" <= c <= "Z" else c for c in str)

LeetCode source

Minimum Time Visiting All Points

Minimum Time Visiting All Points
1
return sum(max(abs(cur[0] - prev[0]), abs(cur[1] - prev[1])) for cur, prev in zip(points, points[1 :]))
2
3
return sum(max(abs(c - p) for c, p in zip(cur, prev)) for cur, prev in zip(points, points[1 :]))

LeetCode source

Remove Outermost Parentheses

Remove Outermost Parentheses
1
opened += 1 if c == '(' else -1
2
3
opened += c == '(' ? 1 : -1

LeetCode source

Split a String in Balanced Strings

Split a String in Balanced Strings
1
res += r == 0

LeetCode source

Unique Morse Code Words

unique-morse-code-words
1
return len(set([''.join([abc[ord(j) - 97] for j in i]) for i in words]))

LeetCode source

Unique Morse Code Words
1
return len({''.join(d[ord(i) - ord('a')] for i in w) for w in words})

LeetCode source

Flipping an Image

Flipping an Image
1
return [[1 ^ i for i in reversed(row)] for row in A]

LeetCode source

Matrix Diagonal Sum

Matrix Diagonal Sum
1
return sum(sum(r[j] for j in {i, len(r) - i - 1}) for i, r in enumerate(m))

LeetCode source

Decrypt String from Alphabet to Integer Mapping

Decrypt String from Alphabet to Integer Mapping
1
return ''.join(chr(int(i[:2]) + 96) for i in re.findall(r'\d\d#|\d', s))
2
3
return "".join([chr(int(n)+96) for n in findall(r'..(?=#)|\d', s)]) -> drop "#" right in regular expression:
4
5
return re.sub[r'\d{2}#|\d', lambda x: chr(int(x.group(](:2))+96), s)

LeetCode source

Split a String in Balanced String

Split a String in Balanced String
1
for s in S: c, m = c + D[s], m + (c == 0)

LeetCode source

Execute dict.get on every char in s, e.g. {'L': 1, 'R': -1}[s[0]] , if s[0] is 'L' then result is 1, and -1 when 'R', etc

Split a String in Balanced String 2
1
return list(acc(1 if c == 'L' else -1 for c in s)).count(0)
2
3
return list(acc(map(' L'.find, s))).count(0)
4
5
return list(acc(map({'L': 1, 'R': -1}.get, s))).count(0)

LeetCode source

Number of Students Doing Homework at a Given Time

Number of Students Doing Homework at a Given Time
1
class Solution:busyStudent=lambda s,a,e,q:sum(q in range(a[i],e[i]+1)for i in range(len(a)))

LeetCode source

Increasing Decreasing String

Increasing Decreasing String
1
for i in range(10): print(i, ~i)

LeetCode source

Increasing Decreasing String 2
1
for c in sorted(cnt.keys()) if asc else reversed(sorted(cnt.keys())):

LeetCode source

Generate a String with Characters That Have Odd Counts

Generate a String with Characters That Have Odd Counts
1
return ("a" *(n-1) + "b" if n%2 == 0 else "b"* n)

LeetCode source

Merge Two Binary Trees

Using t1 and t1.left as a shortcut for t1.left if t1 is not None else None

Merge Two Binary Trees
1
ans.left = self.mergeTrees(t1 and t1.left, t2 and t2.left)

LeetCode source

Reverse Words in a String III

Reverse Words in a String III
1
return " ".join(map(lambda x: x[::-1], s.split()))

LeetCode source