Skip to content

Instantly share code, notes, and snippets.

View ElectricCoffee's full-sized avatar

Niko Lepka ElectricCoffee

View GitHub Profile
@ElectricCoffee
ElectricCoffee / grokking-algorithms-1.rs
Last active November 25, 2018 14:54
Rust implementations of all the algorithms found in the book "Grokking Algorithms"
//! Chapter 1: Introduction to Algorithms. Binary Search.
// NB: input must be sorted
fn binary_search<T>(lst: &[T], item: &T) -> Option<usize>
where T: PartialEq + PartialOrd
{
let mut low = 0;
let mut high = lst.len() - 1;
while low <= high {
/** Python-style range function */
function range(n) {
let arr = new Array(n);
let result = [...arr.keys()].map(m => Number(m));
return result;
}
@ElectricCoffee
ElectricCoffee / vector2d.rs
Last active October 17, 2018 17:30
Vector2D struct in the style of Unity's Vector2
use std::f32;
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct Vector2D {
pub x: f32,
pub y: f32,
}
impl Default for Vector2D {
fn default() -> Self {
@ElectricCoffee
ElectricCoffee / prompt.rs
Last active September 28, 2018 18:29
provides a simple python-esque prompt
use std::io::{self, Write};
fn prompt(input: &str) -> io::Result<String> {
let mut output = String::new();
print!("{}", input);
io::stdout().flush()?;
io::stdin().read_line(&mut output)?;
Ok(output)
}
use std::marker::PhantomData;
use std::convert::From;
#[derive(Debug, Clone, Copy)]
struct H;
#[derive(Debug, Clone, Copy)]
struct L;
#[derive(Debug, Clone, Copy)]
struct Sec<S, A> {
@ElectricCoffee
ElectricCoffee / phantom.rs
Created September 19, 2018 17:36
A test of phantom types in Rust. A bit more cumbersome than Haskell, but definitely doable.
use std::marker::PhantomData;
use std::convert::From;
// Define the empty temperature markers
#[derive(Debug, Copy, Clone)] struct C;
#[derive(Debug, Copy, Clone)] struct F;
// Temperature Struct with Phantom Data
#[derive(Debug, Copy, Clone)]
struct Temp<T> {
@ElectricCoffee
ElectricCoffee / i32_vec.c
Created September 19, 2018 10:05
A simple "for the hell of it" implementation of a dynamically resizing array in C
//
// i32_vec.c
// vector_idea
//
// Created by Nikolaj Lepka on 2018-09-19.
// Copyright © 2018 wausoft.eu. All rights reserved.
//
#include "i32_vec.h"
/**
@ElectricCoffee
ElectricCoffee / mmclock.css
Last active June 13, 2018 19:21
The Majora's Mask screen that appears whenever you start a new day
html {
font-family: "Arial Narrow", sans-serif
}
div {
font-size: 200%;
padding: 50px;
background-color: black;
color: white;
height: 1000px;
extern crate rand;
use rand::Rng;
use std::env;
const MAX_DAYS: usize = 365;
/// Creates an array with `count` number of birthdays in it represented by numbers
/// if more than one birthday occurs on the same day, then that day's entry will be > 1
fn paradox(count: u16) -> [u16; MAX_DAYS] {
let mut arr = [0; MAX_DAYS];
// Rudimentary implementation of the Collatz conjecture
fn next(input: u128) -> u128 {
if input % 2 == 0 {
input / 2
} else {
input * 3 + 1
}
}