Skip to content

Instantly share code, notes, and snippets.

Avatar
🙈
ignoring bugs

Jay Mody jaymody

🙈
ignoring bugs
View GitHub Profile
@jaymody
jaymody / main.py
Created Jan 7, 2022
A very bad and slow way to find the shortest path between two wikipedia pages. If the degree of separation is more than one page, you may need to wait a couple years for the program to finish.
View main.py
import requests
from bs4 import BeautifulSoup
base_url = "https://en.wikipedia.org/wiki/"
def page_to_url(page):
return base_url + page
@jaymody
jaymody / Collections.scala
Created Nov 23, 2021
Recursive, functional re-implementation of iterable functions (map, reduce, groupBy, etc ...) in Scala.
View Collections.scala
case class MyList[A](l: Seq[A]) {
def filter(f: A => Boolean): Seq[A] = l match {
case Nil => l
case x :: xs if !f(x) => xs.filter(f)
case x :: xs if f(x) => x +: xs.filter(f)
}
def map[B](f: A => B): Seq[B] = l match {
case Nil => Nil
case x :: xs => f(x) +: xs.map(f)
@jaymody
jaymody / Coin.scala
Created Nov 23, 2021
Simple CLI coin flip game in functional Scala.
View Coin.scala
import scala.io.StdIn.readLine
import scala.util.Random
import scala.annotation.tailrec
case class GameState(nFlips: Int, nCorrect: Int)
object Coin {
def prompt(): Unit = { print("(H) Heads, (T) Tails, (Q) End Game: ") }
def update(g: GameState): Unit = { println(s"nFlips = ${g.nFlips}, nCorrect = ${g.nCorrect}\n") }
@jaymody
jaymody / Main.scala
Created Nov 23, 2021
The many ways to write the signature of a function in Scala
View Main.scala
object Main extends App {
// lambda function type are specified, type signature is inferred (works for both val and def)
val map1 = (f: (Int) => Int, l: Seq[Int]) => { for (x <- l) yield f(x) }
// lambda function types and result type are specified, type signature is inferred (works for both val and def)
val map2 = (f: (Int) => Int, l: Seq[Int]) => { for (x <- l) yield f(x) } : Seq[Int]
// type signature is specified, lambda function parameter types inferred (works for both val and def)
val map3: ((Int) => Int, Seq[Int]) => Seq[Int] = (f, l) => { for (x <- l) yield f(x) }
@jaymody
jaymody / main.rs
Created Nov 23, 2021
map, reduce, filter, forall, count, group_by in Rust
View main.rs
use std::collections::HashMap;
use std::hash::Hash;
fn map<A, B, F>(l: &Vec<A>, f: F) -> Vec<B>
where
F: Fn(&A) -> B,
{
let mut result = Vec::new();
for e in l.iter() {
result.push(f(e));
View main.py
import random
N = 1000000 # number of frogs of each gender
assert N >= 2 # there must be at least 2 frogs of each gender
frogs = ["M"] * N + ["F"] * N
max_num_simulations = 2000000 # number of simulations to run
num_simulations = 0
num_clearing_successes = 0
@jaymody
jaymody / shopify-ds-intern-challenge.ipynb
Last active Sep 16, 2021
Shopify DS Intern Challenge
View shopify-ds-intern-challenge.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View tokenizer.py
class Tokenizer:
"""Tokenizes text into tokens and sequence of tokens.
token (str)
the smallest divisable component in a tokenization model
seq (list of str)
a sequence of tokens that encodes some meaning as an ordered (or
unordered) collection
text (str)
a string that can be broken down into a seq
View plot.py
import statistics
import seaborn as sns
import matplotlib.pyplot as plt
def stats(values, plot=False):
# statistics.mode throws an error if there are multiple modes
try:
mode = statistics.mode(values)
@jaymody
jaymody / get_send.py
Created Mar 4, 2021
Get and send data asyncio queue.
View get_send.py
import asyncio
from queue import Queue
q = asyncio.Queue()
async def get_data():
while True:
await asyncio.sleep(0.5) # simulate work
print("receiving:", await q.get())