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: https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browsers | |
const useBrowserType = () => { | |
if (!window) { | |
return {}; | |
} | |
const isIE = false || !!document.documentMode; | |
const isChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime); | |
return { | |
isOpera: |
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
# Attempt to create something like a safe operator (?. in js) for Python | |
# | |
# Solution 1: Overload objects in Python (pollute global name space) | |
# Turns out this is not actually possible: https://stackoverflow.com/a/4698550 | |
# and you have to subclass the main class, and create instances. | |
# This is not a great idea since we would have to stop instantiating inbuilt classes | |
# and would have weird looking code | |
# | |
# Solution 2: Possibly create a wrapper class that wraps instances |
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
import { useEffect, useRef } from "react"; | |
const useTimeout = (callback, delay) => { | |
const timeoutRef = useRef(null); | |
const callbackRef = useRef(callback); | |
useEffect(() => { | |
callbackRef.current = callback; | |
}, [callback]); |
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
const assert = require('assert'); | |
const applyNegativeIndex = (array) => { | |
assert(Array.isArray(array)); | |
const negativeIndexProxy = new Proxy(array, { | |
get(target, prop) { | |
if (!isNaN(prop)) prop = parseInt(prop); | |
if (prop < 0) prop += target.length; | |
return Reflect.get(target, prop); | |
}, |
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
import socket | |
# DNS runs on port 53 | |
port = 53 | |
# The host for our DNS server is our local host | |
host = '127.0.0.1' | |
# SOCK.AF_INET tells socket to use IPv4 addressing | |
# SOCK.SOCK_DGRAM tells socket to use UDP | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
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
/** | |
* Designed to work only if the object is an output of JSON.parse(obj) | |
* So Only allowed datatypes for value are: number, string, boolean, object and null | |
*/ | |
"use strict"; | |
function deepCompare(obj1, obj2) { | |
var leftChain, rightChain; | |
function compare2Objects(x, y) { | |
if (isNaN(x) && isNaN(y) && typeof x === 'number' && typeof y === 'number') | |
return true; |
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
#define _GNU_SOURCE /* See feature_test_macros(7) */ | |
#include <stdio.h> | |
#include <math.h> | |
#include <sched.h> | |
#include <pthread.h> | |
#include <errno.h> | |
#include <unistd.h> | |
double waste_time(long n) { | |
double res = 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
import re | |
""" | |
A website requires the users to input username and password to register. | |
Write a program to check the validity of password input by users. | |
Following are the criteria for checking the password: | |
1. At least 1 letter between [a-z] | |
2. At least 1 number between [0-9] | |
3. At least 1 letter between [A-Z] | |
4. At least 1 character from [$#@] |
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
import numpy | |
from keras.datasets import mnist | |
from keras.models import Sequential | |
from keras.layers import Dense | |
from keras.layers import Dropout | |
from keras.layers import Flatten | |
from keras.layers.convolutional import Conv2D | |
from keras.layers.convolutional import MaxPooling2D | |
from keras.utils import np_utils | |
from keras import backend as K |
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
class Node(): | |
def __init__(self, d = 0): | |
self.key = d | |
self.height = 1 | |
self.left = None | |
self.right = None | |
def height(node): | |
if node is None: | |
return 0 | |
return node.height |