Skip to content

Instantly share code, notes, and snippets.

View sohang3112's full-sized avatar
:octocat:

Sohang Chopra sohang3112

:octocat:
View GitHub Profile
@sohang3112
sohang3112 / send_email.py
Last active April 24, 2024 13:13
Send emails in Python using smtplib
import os
import traceback
import smptlib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
# specify credentials in the below environment variables
EMAIL_SERVER = 'email-smtp.us-west-2.amazonaws.com' # change this according to requirement
EMAIL_SERVER_PORT = 465
@sohang3112
sohang3112 / clojure-funcs.md
Created February 25, 2024 17:33
Some misclleanous Clojure functions

Clojure Helper Functions (misclleanous)

These 2 functions are from one of my incomplete projects: java-report-maker

(defmacro with-stdout [stdout & body]
  "Redirects stdout, executes body, and then resets stdout,
   NOTE: only works for Java methods, doesn't work with Clojure code"
  `(try
     (System/setOut ~stdout)
     ~@body
 (finally
# Build: docker build -t seleium_chrome -f selenium_chrome.dockerfile .
# Run: docker run selenium_chrome
FROM python:3.8
RUN apt-get update && apt-get install -y unzip
# Install chrome & chromedriver dependencies
# Source: https://github.com/puppeteer/puppeteer/blob/v5.5.0/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-unix
# see section "Debian Dependencies"
@sohang3112
sohang3112 / word-nums-dsl.clj
Last active January 25, 2024 18:30
Word nums DSL (Clojure macro practice)
(defn plus [x] #(+ % x))
(defn minus [x] #(- % x))
(defn times [x] #(* % x))
(defn dividedBy [x] #(/ % x))
(def nums '[zero one two three four five six seven eight nine])
(defmacro def-num-func [num-sym num]
`(defn ~num-sym
([] ~num)
@sohang3112
sohang3112 / tldr-update.sh
Created December 30, 2023 01:22
tldr --update workaround for Ubuntu
# Ubuntu uses an old version of tldr-hs (0.6.4),
# in which `tldr --update` fails because it tries to pull from master branch of https://github.com/tldr-pages/tldr
# but its branch has been renamed to main
# Latest tldr-hs version (0.9.2) doesn't have this issue because it fetches as zip instead of via git
function tldr-update {
cd ~/.local/share/tldr/tldr
git fetch main
git reset --hard origin/main
git pull
cd -
@sohang3112
sohang3112 / bqn_notes.md
Last active January 3, 2024 06:36
BQN Notes

BQN is a modern array programming language that improves upon APL, J, etc. in various ways.

Install CBQN

CBQN is the primary implementation of BQN. It can be installed on Linux as follows:

$ git clone https://github.com/dzaima/CBQN.git
$ cd CBQN
$ make

SQL Practice in Jupyter Notebook using In-memory database

Note: An alternative to this is to use sqlite3 REPL in terminal, or else use a GUI like Sqlite Studio (in Database > Add a Database, enter database filename as :memory: to open in-memory temporary database).

  • Install Jupyter SQL extension: pip install ipython-sql (can also try its fork: JupySQL)

Now use in Jupyter Notebook:

  • %load_ext sql
  • Create test table using in-memory database sqlite:///:memory: (all data will be lost after notebook is closed):
%%sql sqlite:///:memory:
@sohang3112
sohang3112 / mov_zeros_to_end.py
Last active December 4, 2023 15:00
Move zeros to end of list in O(N) time
from typing import List
from numbers import Number
def mov_zero_to_end_immutable(lst: List[Number]) -> List[Number]:
"""Returns new list in which all zeros are sent to the end of the list."""
num_zeros = lst.count(0)
return [x for x in lst if x != 0] + [0] * num_zeros
def mov_zero_to_end_mutable(lst: List[Number]) -> None:
"""Mutates list such that all zeros are sent to its end.
@sohang3112
sohang3112 / nth_largest_max_heap.py
Last active April 20, 2024 17:01
Nth largest element using max heap
from typing import List
from numbers import Number
from itertools import islice
from heapq import heapify, heappush, heappop, heappushpop
def nth_max(items: List[Number], n: int) -> Number:
"""Find Nth largest number from items list - eg. if n=7, finds 7th largest number.
@param n: 1 <= n <= len(items)
"""
# By default, heapq uses a min heap.
@sohang3112
sohang3112 / positive_int_set.py
Created November 16, 2023 04:37
Optimized set for small positive integers using binary mask
# TODO: test memory & performance against builtin set() containing natural numbers (1,2,..)
from typing import Iterable
class PositiveIntSet:
"""Set optimized for small positive integers (1,2,..)"""
def __init__(self, elems: Iterable[int] = []):
self._added = 0
self._size = 0
for x in elems:
self.add(x)