#!/usr/bin/ruby

#
## https://rosettacode.org/wiki/Non-decimal_radices/Convert
#

static to = [@|'0'..'9', @|'a'..'z']
static from = Hash(to.kv.map{@|_}.flip...)

func base_to(n, b) {
    var s = ""
    while (n) {
        s += to[n % b]
        n //= b
    }
    s.reverse
}

func base_from(n, b) {
    var t = 0
    n.each { |c| t = (b*t + from{c}) }
    t
}

say base_from("rosetta", 36)        # string to number
say base_to(60272032366, 36)        # number to string


assert_eq(base_from("rosetta", 36), 60272032366)
assert_eq(base_to(60272032366, 36), "rosetta")
