Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,56 @@
' FB 1.05.0 Win64
Function min(x As Integer, y As Integer) As Integer
Return IIf(x < y, x, y)
End Function
Function convertToBase (n As UInteger, b As UInteger) As String
If n < 2 OrElse b < 2 OrElse b = 10 OrElse b > 36 Then Return Str(n)
Dim result As String = ""
Dim digit As Integer
While n > 0
digit = n Mod b
If digit < 10 Then
result = digit & result
Else
result = Chr(digit + 87) + result
End If
n \= b
Wend
Return result
End Function
Function convertToDecimal (s As Const String, b As UInteger) As UInteger
If b < 2 OrElse b > 36 Then Return 0
Dim t As String = LCase(s)
Dim result As UInteger = 0
Dim digit As Integer
Dim multiplier As Integer = 1
For i As Integer = Len(t) - 1 To 0 Step - 1
digit = -1
If t[i] >= 48 AndAlso t[i] <= min(57, 47 + b) Then
digit = t[i] - 48
ElseIf b > 10 AndAlso t[i] >= 97 AndAlso t[i] <= min(122, 87 + b) Then
digit = t[i] - 87
End If
If digit = -1 Then Return 0 '' invalid digit present
If digit > 0 Then result += multiplier * digit
multiplier *= b
Next
Return result
End Function
Dim s As String
For b As UInteger = 2 To 36
Print "36 base ";
Print Using "##"; b;
s = ConvertToBase(36, b)
Print " = "; s; Tab(21); " -> base ";
Print Using "##"; b;
Print " = "; convertToDecimal(s, b)
Next
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1 @@
$stdout = int( '1a', 16 )

View file

@ -0,0 +1 @@
$stdout = str( 26, 16 )

View file

@ -0,0 +1,12 @@
> (: erlang list_to_integer '"1a" 16)
26
> #x1a
26
> (: erlang integer_to_list 26 16)
"1A"
> (: erlang list_to_integer '"101110111000" 2)
3000
> #b101110111000
3000
> (: erlang integer_to_list 3000 2)
"101110111000"

View file

@ -0,0 +1,33 @@
import strutils
proc reverse(a: string): string =
result = newString(a.len)
for i, c in a:
result[a.high - i] = c
const digits = "0123456789abcdefghijklmnopqrstuvwxyz"
proc toBase[T](num: T, base: range[2..36]): string =
if num == 0: return "0"
result = ""
if num < 0: result.add '-'
var tmp = abs(num)
var s = ""
while tmp > 0:
s.add digits[int(tmp mod base)]
tmp = tmp div base
result.add s.reverse
proc fromBase(str: string, base: range[2..36]): BiggestInt =
var str = str
let first = if str[0] == '-': 1 else: 0
for i in first .. str.high:
let c = str[i].toLower
assert c in digits[0 .. <base]
result = result * base + digits.find c
if first == 1: result *= -1
echo 26.toBase 16
echo "1a".fromBase 16

View file

@ -0,0 +1,37 @@
function to_base(integer i, integer base)
integer c
sequence s = ""
while i>0 do
c = remainder(i,base)
if c<10 then
c += '0'
else
c += 'a'-10
end if
s = prepend(s,c)
i = floor(i/base)
end while
if length(s) = 0 then
s = "0"
end if
return s
end function
function from_base(string s, integer base)
integer res = 0, c
for i=1 to length(s) do
c = s[i]
if c>='0' and c<='9' then
c -= '0'
else
c -= 'a'-10
end if
res = res*base+c
end for
return res
end function
?to_base(256,16)
?from_base("100",16)

View file

@ -0,0 +1,2 @@
say 60272032366.base(36) # convert number to string
say Number("rosetta", 36) # convert string to number

View file

@ -0,0 +1,20 @@
static to = [@|'0'..'9', @|'a'..'z']
static from = Hash(to.pairs.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

View file

@ -0,0 +1 @@
println(String(26, radix: 16)) // prints "1a"

View file

@ -0,0 +1,6 @@
import Darwin
func string2int(s: String, radix: Int) -> Int {
return strtol(s, nil, Int32(radix))
// there is also strtoul() for UInt, and strtoll() and strtoull() for Int64 and UInt64, respectively
}
println(string2int("1a", 16)) // prints "26"

View file

@ -0,0 +1,22 @@
# Convert the input integer to a string in the specified base (2 to 36 inclusive)
def convert(base):
def stream:
recurse(if . > 0 then ./base|floor else empty end) | . % base ;
if . == 0 then "0"
else [stream] | reverse | .[1:]
| if base < 10 then map(tostring) | join("")
elif base <= 36 then map(if . < 10 then 48 + . else . + 87 end) | implode
else error("base too large")
end
end;
# input string is converted from "base" to an integer, wihtin limits
# of the underlying arithmetic operations, and without error-checking:
def to_i(base):
explode
| reverse
| map(if . > 96 then . - 87 else . - 48 end) # "a" ~ 97 => 10 ~ 87
| reduce .[] as $c
# state: [power, ans]
([1,0]; (.[0] * base) as $b | [$b, .[1] + (.[0] * $c)])
| .[1];

View file

@ -0,0 +1,3 @@
(255 | convert(16)),
("ff" | to_i(16)),
("10" | to_i(10))