Skip to content

Instantly share code, notes, and snippets.

@ebresafegaga
ebresafegaga / add.sh
Last active July 22, 2022 16:21
little shell script
#! /bin/sh
for line in `echo add.txt | cat | awk '{print $2}'`; do
git add "$line"
done
@ebresafegaga
ebresafegaga / grep.ml
Last active July 5, 2022 10:38
I'd rather write grep in OCaml. Just a small utility to search the output of `ls -R`, and then search each file. Basically searching all files in a directory for a particular "key" (or filtering some files out)
let (>>) f g a = a |> f |> g
let input_lines file =
let rec loop chan =
match input_line chan with
| line -> line :: loop chan
| exception End_of_file -> []
in
let chan = open_in file in
let result = loop chan in
close_in chan;
@ebresafegaga
ebresafegaga / one.cs
Created April 13, 2022 11:02
MS Codility Test
using System;
using System.Collections.Generic;
using System.Linq;
namespace interview {
class Program {
// check if the last 2 chars with c are the same
public static bool predicate (string s, char c) {
if (s.Length < 2) {
@ebresafegaga
ebresafegaga / rank.ml
Last active March 28, 2022 12:01
Rank Transform of a Matrix Leetcode in OCaml (https://leetcode.com/problems/rank-transform-of-a-matrix/)
type constr = Equal | Less | Greater
let adjacents matrix (x,y) =
matrix
|> Array.mapi (fun ri row -> row |> Array.mapi (fun ci _col -> ri,ci))
|> Array.to_list
|> Array.concat
|> Array.to_list
|> List.filter (fun (ri, ci) -> (ri = x) <> (ci = y))
@ebresafegaga
ebresafegaga / excel.ml
Last active March 27, 2022 21:31
Excel Formula Leetcode problem in OCaml (https://leetcode.com/problems/design-excel-sum-formula/)
let rec range start stop = if start > stop then [] else start :: range (succ start) stop
let sum xs = List.fold_left (+) 0 xs
let (>>) f g a = a |> f |> g
module type Excel = sig
type t
val create: int -> char -> unit
val set: int -> char -> int -> unit
val get: int -> char -> int
val sum: int -> char -> string list -> int
let (>=>) p1 p2 input k = p1 input (fun input -> p2 input k)
let (>=>>) p1 p2 input = p1 input (fun input -> p2 input List.isEmpty)
let digit input k =
let rec loop input =
match input with
| [] -> k []
| x :: input when Char.IsDigit x -> loop input
| input -> k input
match input with
@ebresafegaga
ebresafegaga / main.cc
Last active February 11, 2022 12:20
Just a functional programmer playing with pointers.
#include <vector>
#include <string>
#include <iostream>
struct Node {
int element;
Node* next;
Node* prev;
};
@ebresafegaga
ebresafegaga / match.fs
Last active February 14, 2022 19:22
Leetcode wildcard matching solution in F#. https://leetcode.com/problems/wildcard-matching/
type pattern =
| Char of char * pattern
| Any of pattern
| Wildcard of pattern
| End
let parsePattern (str: string) =
let str = [for s in str do s]
let folder c pat =
match c with
let rec min count counts =
if count = 0 then 0
else if not (List.contains count counts) then
0
else
1 + min (count - 1) counts
let minDeletions (str : string) =
let step m elem =
@ebresafegaga
ebresafegaga / transpose.ml
Created December 19, 2021 19:27
Traspose now!
let rec transpose xss =
match xss with
| [] :: _ | [] -> []
| xs :: xss ->
let hds = List.map List.hd xss in
let tls = List.map List.tl xss in
let hd = List.hd xs in
let tl = List.tl xs in
(hd :: hds) :: transpose (tl :: tls)