Skip to content

Instantly share code, notes, and snippets.

View Denommus's full-sized avatar

Yuri Albuquerque Denommus

  • Brick Abode
  • Florianópolis, SC, Brazil
View GitHub Profile
import Data.Char (chr, ord)
import System.Environment (getArgs)
cycleRange :: Char -> Int -> Char -> Char
cycleRange start n c | a <= x && x < a+26 = chr $ ((x - a + n) `mod` 26) + a
| otherwise = c
where x = ord c
a = ord start
caesarCypher :: Int -> String -> String
import Control.Monad
import Control.Arrow
splitListInHalf :: [a] -> ([a], [a])
splitListInHalf = splitAt =<< flip div 2 . (+1) . length
checkOneDifferent :: Eq a => [a] -> [a] -> Bool
checkOneDifferent a b = 1==(length . filter not . map (uncurry (==)) $ zip a b)
transformTuple :: ((a,b),(c,d)) -> ((a,c),(b,d))
@Denommus
Denommus / permutations.ml
Last active August 29, 2015 14:20
Permutations for an arbitrary container type
module type PERMUT_CONTAINER = sig
type 'a t
val append: 'a t -> 'a t -> 'a t
val empty: 'a t
val cons: 'a -> 'a t -> 'a t
val rev: 'a t -> 'a t
val hd: 'a t -> 'a
val tl: 'a t -> 'a t
val fold_left: ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
end
import Data.Maybe
import Prelude hiding (drop, head, tail, (++), mapM_, zip, length, null)
import Data.Vector
import qualified Data.List as L
import System.Environment
import Control.Applicative
calculate :: (Int, Int, Vector Int) -> (Int, Int)
calculate (c, _, vec) = aux 0
where aux i = let p = vec ! i in
@Denommus
Denommus / quicksort.hs
Last active August 29, 2015 14:09
Quicksort in different languages
import Data.List
import Control.Monad
import Control.Arrow
qsort :: (a -> a -> Bool) -> [a] -> [a]
qsort _ [] = []
qsort comp (x:xs) = smaller ++ [x] ++ bigger
where (smaller, bigger) = join (***) (qsort comp) $ comp x `partition` xs
main :: IO ()
@Denommus
Denommus / pizza.rs
Last active August 29, 2015 14:05
Solution for another dojo at FPF
use std::collections::HashMap;
use std::num::abs;
pub struct Preference {
name: String,
flavors: HashMap<String, int>
}
impl Preference {
pub fn compare_preference<'a>(&'a self, preferences: &'a[Preference]) -> Option<&'a str> {
@Denommus
Denommus / monad.ml
Last active August 26, 2020 03:53
Monad sigs and functors for OCaml
(* I'm using the new syntax sugars on 4.08, which are (let+), (and+) and ( let* ) *)
module type FUNCTOR = sig
type 'a t
val map : ('a -> 'b) -> 'a t -> 'b t
val (let+): 'a t -> ('a -> 'b) -> 'b t
end
module DefaultLetPlus(M: sig
type 'a t
@Denommus
Denommus / repeated_files.rs
Last active August 29, 2015 14:03
Second Rust dojo
extern crate rustc;
use rustc::util::sha2::{Sha256, Digest};
struct File {
path: String,
contents: Vec<u8>
}
impl File {
@Denommus
Denommus / pff.hs
Last active August 29, 2015 14:02
Showing off for Python programmers
import Control.Applicative
import Data.List.Split
import Data.List
main :: IO ()
main = do
content <- lines <$> readFile "massa.txt"
let lasts = last . splitOn "." <$> content
let counting = (\xs@(x:_) -> (length xs, x)) . group . sort <$> lasts
let output = sortBy (flip compare) counting
@Denommus
Denommus / prime_factors.rs
Last active August 29, 2015 14:02
Prime factors of a number
#![crate_id = "dojo"]
#![crate_type = "lib"]
use std::iter::range_step_inclusive;
fn prime_number_list(number: uint) -> Vec<uint> {
let mut prime_list:Vec<uint> = vec!(2);
for i in range_step_inclusive(3, number, 2) {
if !prime_list.iter().any(|x| i % *x == 0) {