Skip to content

Instantly share code, notes, and snippets.

@fero23
fero23 / Combinatorics.cs
Created October 18, 2023 18:42
Combinatorics
namespace Scripts;
public class Combinatorics
{
public static IEnumerable<IEnumerable<T>> GetPermutations<T>(List<T> input, int i = 0)
{
if (i >= input.Count - 1)
{
yield return input.ToList();
}
@fero23
fero23 / my_date_time.py
Last active December 5, 2017 22:36
Basic datetime implementation in Python
import time
import datetime
SECONDS_EVERY_FOUR_YEARS = (365 * 4 + 1) * 24 * 3600
SECONDS_EVERY_YEAR = 365 * 24 * 3600
SECONDS_EVERY_DAY = 24 * 3600
DAYS_EVERY_MONTH = [31, (28, 29), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
SECONDS_AFTER_FIRST_LEAP_YEAR = SECONDS_EVERY_YEAR * 3 + SECONDS_EVERY_DAY
def _get_numbers_of_days_the_month(m, years_since_last_leap_year):
@fero23
fero23 / libfibonacci.rs
Last active November 18, 2017 04:22
Fibonacci sequence creation in Rust to bind in Python
#[macro_use] extern crate cpython;
use std::vec::Vec;
use cpython::{PyResult, Python};
// add bindings to the generated python module
// N.B: names: "libfibonacci" must be the name of the `.so` or `.pyd` file
py_module_initializer!(libfibonacci, initlibfibonacci, PyInit_libfibonacci, |py, m| {
try!(m.add(py, "__doc__", "Fibonacci sequence creation in Rust."));
@fero23
fero23 / wget.sh
Created May 26, 2017 05:17 — forked from vadviktor/wget.sh
Download rails guide and api doc for specific versions
wget -r -k -p -L -R '*.mobi' http://guides.rubyonrails.org/v4.2.4/
wget -r -k -p -L http://api.rubyonrails.org/v4.2.4/
Test grammar:
glurp_grammar! {
R: String => { Optional(R) ~ Char('R') } on {|opt, r| {
if Some(acc) = opt {
push(r);
acc
} else {
String::new()
}
}};
@fero23
fero23 / tuple_and.rs
Last active November 12, 2016 21:09
Tuple concatenation in Rust
trait And<B> {
type Result;
fn and(self, rhs: B) -> Self::Result;
}
impl<A, B> And<B> for (A,) {
type Result = (A, B);
fn and(self, rhs: B) -> (A, B) {
(self.0, rhs)
}
@fero23
fero23 / pushdownAutomaton.hs
Last active November 8, 2016 16:53
Pushdown automaton computation in Haskell
data PushdownAutomaton a b =
Automaton { currentState :: Int
, stack :: [b]
, transitions :: [((Int, Maybe a, Maybe b), [(Int, Maybe b)])]
, acceptStates :: [Int]
}
checkMaybe m h = if m == Nothing then True else m == Just h
eval :: (Eq a, Show a, Eq b) => PushdownAutomaton a b -> [a] -> Bool
@fero23
fero23 / triangle.hs
Created September 30, 2016 03:09
Haskell triangle calculation
data Side = Adjacent Double | Opposite Double | Hypotenuse Double deriving Show
type Angles = Maybe (Double, Double, Double)
type Sides = Maybe (Double, Double, Double)
data Triangle = Triangle Angles Sides
toDegrees :: Double -> Double
toDegrees rad = rad * 360 / (2 * pi)
@fero23
fero23 / hkt_2.rs
Last active February 10, 2021 15:45
Another take on higher kinded types for Rust
trait HKT<N> {
type C;
type NC;
}
trait Functor<N>: HKT<N> {
fn fmap<F: Fn(&Self::C) -> N>(&self, f: F) -> Self::NC;
}
impl<C, N> HKT<N> for Option<C> {
@fero23
fero23 / presupuesto.rs
Last active August 20, 2016 20:18
Creador de presupuestos en Rust on the fly
use std::io::{self, Write};
use std::fs::File;
use std::path::Path;
use std::rc::Rc;
use std::cmp;
use std::collections::HashMap;
enum TableCell {
TableHeader {
content: String,