Skip to content

Instantly share code, notes, and snippets.

View mgarod's full-sized avatar

Michael Garod mgarod

View GitHub Profile
@mgarod
mgarod / add_method.py
Last active September 30, 2023 00:13
Dynamically add a method to a class
from functools import wraps # This convenience func preserves name and docstring
class A:
pass
def add_method(cls):
def decorator(func):
@wraps(func)
def wrapper(self, *args, **kwargs):
return func(*args, **kwargs)
# colors!
green="\[\033[0;32m\]"
blue="\[\033[0;34m\]"
purple="\[\033[0;35m\]"
yellow="\[\033[0;33m\]"
red="\[\033[0;31m\]"
reset="\[\033[0m\]"
export PS1="| $green\w $yellow@ $green\h $yellow(\u)$reset \n| $ "
export PS2="| => "
bool remove(T d) {
if (!contains(d))
return false;
removeHelper(root_, d);
return true;
}
TreeNode<T>* removeHelper(TreeNode<T> *n, T d) {
if (d < n->data_) {
n->left_ = removeHelper(n->left_, d);
static String Decoding(String[] encodings, String encodedstring) {
HashMap<String, String> map = new HashMap<>(); // <code, char>
for (String s : encodings) {
String[] splited = s.split("\\t");
if (splited[0].equals("[newline]"))
splited[0] = "\n";
map.put(splited[1], splited[0]);
}
import java.io.*;
import java.util.*;
public class Solution {
private static boolean isAnagram(String a, String b) {
char[] a_arr = a.toCharArray();
char[] b_arr = b.toCharArray();
Arrays.sort(a_arr);
Arrays.sort(b_arr);
String sorted_a = new String(a_arr);
static String firstRepeatedWord(String s) {
HashSet<String> hashset = new HashSet<>();
String[] splited = s.split("\\s+");
for (String word : splited) {
word = word.trim().replaceAll("\\W", "");
if (hashset.contains(word)) {
return word;
} else {
hashset.add(word);
}
/*
* Complete the function below.
*/
static private class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int n){
#include <iostream>
using namespace std;
int reverseAsHex(int n) {
int mask = 0xF; // or 15
int answer = 0;
while (n != 0) {
answer = (answer << 4) | (n & mask);
n >>= 4;
}
@mgarod
mgarod / max2.cpp
Last active October 27, 2016 15:41
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
class Max2{
public:
Max2() : largest(INT_MIN), nextlargest(INT_MIN), lindex(-1), nlindex(-1) { };
friend ostream& operator<<(ostream& os, Max2 m);
void insert(int n, int nindex);
var callback = function(x) { coordinates = x; }
navigator.geolocation.getCurrentPosition(callback);
console.log(coordinates);