Skip to content

Instantly share code, notes, and snippets.

// 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:
@yashshah1
yashshah1 / safe_operator.py
Last active July 26, 2022 16:47
Safe operator in python
# 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
@yashshah1
yashshah1 / useTimeout.js
Last active November 23, 2021 11:10
useTimeout hook
import { useEffect, useRef } from "react";
const useTimeout = (callback, delay) => {
const timeoutRef = useRef(null);
const callbackRef = useRef(callback);
useEffect(() => {
callbackRef.current = callback;
}, [callback]);
@yashshah1
yashshah1 / negativeIndex.js
Last active July 20, 2020 11:03
Negative Indexing in JavaScript Arrays
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);
},
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)
@yashshah1
yashshah1 / deepCompare.js
Created May 20, 2020 19:20
It's a CommonJS function that checks if two JS Objects are the same, checks nested objects as well
/**
* 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;
#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;
@yashshah1
yashshah1 / password_validator.py
Last active May 22, 2019 10:34
Password validtor
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 [$#@]
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
@yashshah1
yashshah1 / avl_tree.py
Last active October 6, 2021 14:29
AVL Tree, py
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