Skip to content

Instantly share code, notes, and snippets.

@zetashift
zetashift / dailyprogrammer364.scala
Created July 31, 2018 18:42
Daily Programmer #364 [Easy]
def rollDice(n: String): String = {
val input = n.split("d")
val numberOfRolls = input(0).toInt
val numberOfSides = input(1).toInt
var r = scala.util.Random
var rolls = scala.collection.mutable.ArrayBuffer[Int]()
var result = 0
for (n <- (1 to numberOfRolls)) {
val currentRoll = r.nextInt(numberOfSides + 1)
rolls += currentRoll
@zetashift
zetashift / challenge_1.nim
Created March 28, 2018 19:58
Cryptopals set 1 ch 1
import base64, strutils
let
input = """49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"""
expected = "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"
assert expected == encode(parseHexStr(strIn))
@zetashift
zetashift / base62.nim
Created February 25, 2018 06:06
DailyProgrammer #352 [EASY]
const
alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
proc base62(input: int): string =
result = ""
var q = input
while q != 0:
var i = q mod 62
q = q div 62
result = result & alphabet[i]
@zetashift
zetashift / growth_of_population.nim
Created February 25, 2018 05:24
Growth of Population
proc nbYear*(p0: int, percent: float64, aug: int, p: int): int =
var count = 0
var pop = p0
while pop != p:
count = count + 1
pop = pop + p0 * (percent / 100.float).int + aug
return count
echo(nbYear(1000, 5, 50, 2000))
@zetashift
zetashift / reparsecomb.re
Created February 18, 2018 18:25
Really barebones start of a parser combinator in ReasonML/OCaml
type result('a) =
| Success('a)
| Failure(string);
/* Encapsulate a parsing function in a type */
type parser('a) =
| Parser(string => result(('a, string)));
let reduce = (fn, list) =>
switch list {