Skip to content

Instantly share code, notes, and snippets.

Avatar

Ambika E rosalogia

View GitHub Profile
@rosalogia
rosalogia / properties.hs
Last active Apr 23, 2021
A short Haskell program using QuickCheck to identify whether a defined relation R is an equivalence relation or partial ordering
View properties.hs
import Test.QuickCheck
-- unfortunately, function names cannot start with uppercase letters
r :: Positive Integer -> Positive Integer -> Bool
r a b =
let (x, y) = (getPositive a, getPositive b) in
y `mod` (x * x) == 0
implies p q = not p || q
@rosalogia
rosalogia / random_walker.ml
Created Apr 14, 2021
2d random walker in OCaml, adapted from a Rutgers CS111 assignment
View random_walker.ml
let step (x, y) =
(* Match on a random integer between 0 and 3
* incrementing or decrementing either x or y
* depending on the result. The pattern matching
* is not exhaustive, but because we know
* Random.int 4 can only return a number between
* 0 and 3, this is okay. *)
match Random.int 4 with
| 0 -> (x + 1, y)
| 1 -> (x - 1, y)
@rosalogia
rosalogia / relations.fsx
Created Apr 10, 2021
Short F# script to generate and count the number of simultaneously symmetric and anti-symmetric relations on a set A = {1, 2, 3, 4, 5}
View relations.fsx
module List =
// Some code for generating permutations of a list
// Please disregard it, I copied it off the internet :)
let rec permutations l =
let rec aux = function
| [] -> seq [List.empty]
| x :: xs -> Seq.collect (insertions x) (aux xs)
aux l |> Seq.toList
and insertions x = function
| [] -> [[x]]
@rosalogia
rosalogia / equiv.hs
Created Apr 10, 2021
Short haskell program to generate equivalence classes of a relation R defined on the natural numbers such that aRb iff there exist odd integers p and q such that ap = bq
View equiv.hs
-- The relation R defined as a function:
-- Takes two integers and returns true
-- if aRb and false otherwise
r :: Integer -> Integer -> Bool
-- Why gcd? The relation r can be
-- redefined in a way that's more
-- convenient to represent computationally:
-- aRb if the numerator and denominator
-- of the simplest form of a/b are both odd.
@rosalogia
rosalogia / velblog.fsx
Created Mar 28, 2021
Script to grab all the developer blog entries from the Veloren site and convert their content to markdown files (for the purpose of searchability with grep)
View velblog.fsx
#r "nuget: FSharp.Data"
open FSharp.Data
open System.IO
let args = System.Environment.GetCommandLineArgs()
let first = int <| args.[2]
let last = int <| args.[3]
let fetchPageAsync i =
async {
@rosalogia
rosalogia / RBT.java
Created Mar 26, 2021
Left-Leaning Red Black Tree Insertion in Java
View RBT.java
private static <Key extends Comparable<Key>, Value> boolean isRed(Node<Key, Value> root) {
return root != null && root.isRed;
}
private static <Key extends Comparable<Key>, Value> int size(Node<Key, Value> root) {
if (root == null) return 0;
return 1 + size(root.left) + size(root.right);
}
private static <Key extends Comparable<Key>, Value> Node<Key, Value> leftRotate(Node<Key, Value> root) {
@rosalogia
rosalogia / stars.fsx
Created Mar 12, 2021
F# script for assessing the popularity of the forks of a repository
View stars.fsx
#r "nuget: FSharp.Data"
open FSharp.Data
let args = System.Environment.GetCommandLineArgs()
let flags =
if args.Length > 3 then
args.[2..(args.Length - 2)]
else
[||]
@rosalogia
rosalogia / main.rs
Last active Mar 3, 2021
Mean, Median and Mode of a Vec<i32> in Rust
View main.rs
use std::collections::HashMap;
pub fn mean(values: &[i32]) -> f64 {
let mut sum = 0.0;
for x in values {
sum += *x as f64;
}
sum / values.len() as f64
@rosalogia
rosalogia / problem_set_generator.fsx
Created Feb 23, 2021
Short F# script to generate problem sets of a specified size containing problems from multiple sections of a textbook
View problem_set_generator.fsx
#r "nuget: FSharp.Json"
open FSharp.Json
open System.IO
type SubSection =
{ Name: string
; Weight: float
; ProblemRange: int * int }
type Section =
@rosalogia
rosalogia / mergesort.cpp
Created Nov 28, 2020
Heavily Annotated Implementation of MergeSort in C++
View mergesort.cpp
#include <iostream> // For input/output
using namespace std;
// The merge function is where the sorting actually happens
// int arr[] is the array we're passing in
// int l is the index of the array that the left "chunk" begins at
// int m is the index of the array that the left "chunk" ends at, whereas the right "chunk" ends at m + 1
// int r is the index of the array that the right "chunk" /ends/ at
void merge(int arr[], int l, int m, int r) {
// Here we compute the size of the two "chunks"