Python for Leetcode
Python for Leetcode

Python for Leetcode

Tags
Python
Leetcode
Data Structures
Algorithms
Published
October 26, 2023
Author
Chase Sizemore
🚨
Post in progress! Feel free to check out the current state!
💡
This article is meant for those with a beginner understanding of Python and looking to expand their knowledge.
 
Let's be honest: no one likes the leetcode-style technical interview. You get in a Zoom call with an engineer, they might ask you a couple of behavioral questions, and then they present you a leetcode question that somehow shows you are qualified for the job. Well
 
Each built-in method will follow the following format:
  1. Description
  1. Example
  1. Leetcode Examples to Practice
 

 

Counter

Counter is a subclass of the ‘dict’ class in python. It is designed for counting hashable objects.
from collections import Counter myArray = ["a", "b", "c", "a", "d", "e", "b", "f"] myCounter = Counter(myArray) print(myCounter) #Counter({'a': 2, 'b': 2, 'c': 1, 'd': 1, 'e': 1, 'f': 1})
 

Default Dictionary

Defaultdict is also a subclass of the built-in dic class in Python. It is used to provide default values for keys that do not exist in a dictionary. This can be useful for a variety of tasks, such as counting, grouping, and accumulating data.
from collections import defaultdict # create a defaultdict with int as the default factory d = defaultdict(int) # Here you can think of d = {} # add some values to the defaultdict d['a'] += 1 # Even though our object d is initially empty, # it will give an unknown key a value of 0 to start since we passed in int d['b'] += 2 d['c'] += 3 # print the defaultdict print(d) #defaultdict(<class 'int'>, {'a': 1, 'b': 2, 'c': 3})
 

Set

# Using set() with lists list1 = [1, 2, 3, 4, 5] list2 = [4, 5, 6, 7, 8] set1 = set(list1) set2 = set(list2) # Intersection of two sets intersection = set1.intersection(set2) print(intersection) # Output: {4, 5} # Union of two sets union = set1.union(set2) print(union) # Output: {1, 2, 3, 4, 5, 6, 7, 8} # Difference of two sets difference = set1.difference(set2) print(difference) # Output: {1, 2, 3} --------------------- my_string = "hello world" my_set = set(my_string) print(my_set)
 

Deque

A deque, or double-ended queue, is a data structure that supports adding and removing elements from both ends. Deques are more efficient than lists for adding and removing form both ends because they don’t need to shift all of the elements in the list. Instead, they use a circular buffer to store the elements, allowing access to the front and back quickly.
from collections import deque # Create a deque dq = deque() # Add some items to the deque dq.append(1) dq.append(2) dq.append(3) dq.appendleft(4) # Print the deque print(dq) #--> [4,1,2,3] # Remove an item from the right end of the deque dq.pop() #--> [4,1,2]
 

Tuple

# Create a tuple my_tuple = ("apple", "banana", "cherry") # Print the tuple print(my_tuple) # Access an element of the tuple print(my_tuple[0]) # Slice the tuple print(my_tuple[1:3]) # Check if an element is in the tuple print("banana" in my_tuple) array = [1,2,3] my_tuple2 = tuple(array) # ---> (1,2,3)
 

List

# Create a string my_string = "This is a string." # Convert the string to a list my_list = list(my_string) # Print the list print(my_list) # ['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g', '.']
 

Heap Queue