1. Add Binary (will do mock interview next class)
https://leetcode.com/problems/add-binary/
Ref: https://www.programiz.com/article/python-self-why#:~:text=The%20self%20keyword%20is%20used,information%20for%20both%20these%20objects. Python is a object-oriented programming. In obejct-oriented programming, whenever we define methods for a class, we use 'self' as the first parameter in each case. Class is a blueprint for the objects. class Cat: def __init__(self, name, age): self.name = name self.age = age def info(self): print(f"I am a cat. My name is {self.name}. I am {self.age} years old.") def make_sound(self): print("Meow") In this case all the methods, including __init__ have the first parameter as 'self'. We know that class is a blueprint for the objects. The blueprint can be used to create multiple numbers of objects. Let's create two different objects from the above class. cat1 = Cat('Andy', 2) cat2 = Cat('Phoebe', 3) The self keyword is used to represent an instance(object) of the given class. In this case, the two Cat objects cat1 and cat2 have their own 'name' and 'age' attributes. If there was no self argument, the same class couldn't hold the information for both these objects. However, since the class is just a blueprint, 'self' allows access to the attributes and methods of each object in python. This allows each object to have its own attributes and methods. Thus, even long before creating these objects, we reference the objects as 'self' while defining the class. "Explicit is better than implicit" A peculiar thing about methods in Python is that the object itself is passed as the first argument to the corresponding function. In the case of the above example, the method call p1.distance() is actually equivalent to Point.distance(p1). class Point(object): def __init__(self, x = 0, y = 0): self.x = x self.y = y def distance(self): """Find distance from origin""" return (self.x**2 + self.y**2) ** 0.5 When we instantiate this class and find the distance: p1 = Point(6,8) p1.distance() # 10.0 __init__() defines three parameters, but we just passed two (6 and 8). Similarly distance() requires one, but zero arguments were passed. Why is Python not complaining about this argument number mismatch? We can see that the Point.distance is a function and p1.distance is a method. In Python, the object itself is passed as the first argument to the corresponding function. In the case of the above example, the method call p1.distance() is actually equivalent to Point.distance(p1). |
#https://www.youtube.com/watch?v=keuWJ47xG8g
class Solution:
def addBinary(self, a: str, b: str) -> str:
res = "" #declare an empty string
carry = 0 #need a variable for our carry
a, b = a[::-1], b[::-1]
#reversing a and b input string.
#so that we can actually compute the digits in order and a and b are not necessarily going to be the same size.
for i in range(max(len(a), len(b))):
digitA = ord(a[i]) - ord("0") if i<len(a) else 0
digitB = ord(b[i]) - ord("0") if i<len(b) else 0
total = digitA + digitB + carry
char = str(total %2)
res = char + res
carry = total // 2
if carry:
res = "1" + res
return res
2. Reverse string 1
https://leetcode.com/problems/reverse-string/
3. Length of the last word
https://leetcode.com/problems/length-of-last-word/
4. The longest common prefix
https://leetcode.com/problems/longest-common-prefix/
'Coding 공부기록' 카테고리의 다른 글
Monotonic Stack - Identify Pattern (0) | 2022.10.24 |
---|---|
[R] Convert Data between Wide and Long Format (0) | 2022.10.02 |
Top 8 Data Structures for Coding Interviews (0) | 2022.09.03 |
Behavior Questions Preparation (~6/28) (0) | 2022.06.28 |
[statistics] sample size 구하기 (0) | 2021.11.29 |