cos python is special
'a' * 5 -> 'aaaaa'
sort() -> mutate original list
sorted() -> does not mutate original list, returns a sorted list
Good practice when looping tuples with irrelevant data
Enumerate pls, it makes looping cleaner when you need both the index and the value of an element in a list
Without enumerate:
With enumerate:
Matrix creation
https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do
Good stuff! makes your life and interviewer's life easier
args param must contain a tuple, to create a single-element tuple is exactly like (elem,)
@classmethod
vs @staticmethod
classmethod
: the class of the object is implicitly passed as the first argument instead of self
. When we define something to be classmethod, it is probably because we want to call the method from the class.staticmethod
: neither self
(object instance) nor cls
(class) is implictly passed as the first argument. They behave like plain functions except that you can call them from an instance or the class.@staticmethod
vs plain outside-of-class functionReference on static and class method: https://stackoverflow.com/q/136097
Java needs to declare the class variables outside of the constructor method
Python don't need
Immediately use self.x to create object attribute x
Java uses keyword static
Python is like this
All attributes in python are public
Make use of _
and__
to conceal attributes
_attribute
technically still accessible - e.g, object._attribute
, but VSCode with PEP8 plugin would throw warning
__attribute
would not be accessible through object.__attribute
. The __
prefix would rename the attribute and technically could stil be accessed by determined developer - object._Object__attribute
setters and getters is the convention for Java, but not for python, everything is public yo
Python has functions, Java don't. That's it.
Every functions in Java have to be contained in a class. But, it can be static so that it can be called without instance.
Java single inheritance, Python multiple inheritance
Car
would have all the attributes and methods from Device
and Vehicle
Java strict typing gives rise to inheritance to accomodate polymorphism
Python has duck typing. If it walk like duck, quacks like a duck, it's a duck. https://realpython.com/python-type-checking/
Java static typing, Python dynamic typing (it checks as the code execute)
All classes in Java inherit the methods from Object
In Python, each class also has default methods - dunder
(double underscore) methods
Java needs @Override
, python does not.
Additional dunder
methods
Allows use of idiomatic python constructs without biolerplate code.
stack<T>
queue<T>
priority_queue<T>
โ there is also another signature that you can define your own orderdeque<T>
vector<T>
pair<T, R>
tuple<T, R, ...>
map<T, R>
โ RB-Tree, O(log n) access, able to access the (key, value) pair in sorted order (sorted by key)unordered_map<T, R>
โ Hashing, access & insert average O(1), worst O(n), unable to access in sorted orderset<T>
โ RB-Tree, O(log n) insert & accessunordered_set<T>
โ Hashing (not sure), access & insert average O(1), worst O(n).multiset<T>
โ Able to store multiple elementssort(start, end, [cmp])
unique(start, end)
โ Reorder the elements inside an array / vector such that repeating elements are put at the back. Returns the pointer of first element that is repeatedreverse(start, end)
erase(start, end)
Note: always best to consult with the C++ reference. The list is not exhaustive, so it is also best to be complemented with googling some tips and tricks.
combining string
single quote -> char
double quote -> string
Iterating string
front and back
char & int, typecast is unnecessary for declaration. But if you want to do arithmetic operation, it has to be typecasted into the same type.
well, sadly there is no s.split(" ")
or " ".join(words)
like in python
below is the pre-made split and join, just copy paste it to the coding assessment
here's a very clean example of problem that involves a lot of string manipulation
useful:
word.substr()
stringstream
joining strings routine
https://github.com/stevenhalim/cpbook-code/blob/master/ch6/Trie.cpp
For fixed matrix sizes, better use fixed array instead of Vector
So what if we do not know the size of the array?
What about dynamic multidimensional array of size mxn?
infinite values
Why? Because it deallocates itself
Why? So that we can pass functions as arguments to other functions e.g., custom comparator
Iterating through vector vs array
so the question is, when to use && in a function? when is it useful?
It's max-heap by default, min-heap needs extra parameter
To make a custom element & comparator
PriorityQueue can contain any elemnt type, just need to create the custom comparator
Tuple is like an unnamed struct, can also be used for typedef
this is work of art lol. Stack in c++ is strictly a stack and doesn't work like a vector, it doesn't have random access using index, only the top element is available.
There is switch case! switch case in c++ is fallthrough and requires a break
.
Iterator
Sorting vector of vectors
for dummy nodes initialization, don't use pointer
unordered_map
uses hashmap
map
uses self-balancing BST
which means, unordered_map
cannot take a pair<int, int>
as key, whereas map can, because map
uses < comparison to build the data structure.
pair<int, int>
does not have a default hash function, we need to define it and pass the function as third parameter
but alternatively, you can just do away with nested unordered_map<int, unordered_map<int, int>>
The difference is the input and output types