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
cities = {'London': 'UK', 'Tokyo': 'Japan', 'New York': 'US'} | |
# Method 1 | |
reversed_cities = {v: k for k, v in cities.items()} | |
print(reversed_cities) | |
# {'UK': 'London', 'Japan': 'Tokyo', 'US': 'New York'} | |
# Method 2 | |
reversed_cities = dict(zip(cities.values(), cities.keys())) | |
print(reversed_cities) |
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
transpose_mat =lambda A:[list(i) for i in zip(*A)] |
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
add_vec= lambda A,B:[ai+bi for (ai, bi) in zip(A, B)] |
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
dist = lambda w,v : (sum((wi - vi)**2 for wi,vi in zip(w,v)))**.5 |
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
fib = lambda x: x if x <= 1 else fib(x - 1) + fib(x - 2) |
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
# Definition for a binary tree node. | |
# class TreeNode: | |
# def __init__(self, val=0, left=None, right=None): | |
# self.val = val | |
# self.left = left | |
# self.right = right | |
from collections import deque | |
class Solution: | |
def maxDepth(self, root: Optional[TreeNode]) -> int: | |
depth = 0 |
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
from collections import deque | |
class Solution: | |
def isValid(self, s: str) -> bool: | |
mapping = {')': '(', ']': '[', '}': '{'} | |
stack = deque() | |
for char in s: | |
if mapping.get(char): | |
if len(stack) == 0: |
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
from collections import deque | |
dq1=deque('abcde') | |
print(dq1) | |
# deque(['a', 'b', 'c', 'd', 'e']) | |
# Insert an item in the middle | |
dq1.insert(2,'f') | |
print(dq1) | |
# deque(['a', 'b', 'f', 'c', 'd', 'e']) |
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
from collections import deque | |
dq1=deque([1,2,3,4,5]) | |
print(dq1) | |
# deque([1, 2, 3, 4, 5]) | |
dq1.popleft() | |
print(dq1) | |
# deque([2, 3, 4, 5]) | |
dq1.appendleft(9) | |
print(dq1) |
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
from collections import deque | |
dq1=deque([1,2,3,4,5]) | |
print(dq1) | |
# deque([1, 2, 3, 4, 5]) | |
dq2=deque('abcde') | |
print(dq2) | |
# deque(['a', 'b', 'c', 'd', 'e']) | |
dq3=deque([i for i in range(10) if i%2==0]) | |
print(dq3) |