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,61 @@
\ Given a low and high limit, create an array containing the numbers in the
\ range, inclusive:
: n:gen-range \ low hi -- a
\ make sure they are in order:
2dup n:> if swap then
\ fill the array with the values:
[] ' a:push
2swap loop ;
\ Take a string, either "X" or "X-Y", and correctly return either a number (if
\ "X") or an array of numbers (if "X-Y"):
: n:expand-one \ s -- n | a[n,..m]
\ First see if we can parse a number. This works in the "X" case:
dup >n null? if
\ Failed >n because it's (possibly) "X-Y"
drop
\ not a valid number, might be a range
\ We'll use a capturing regex to handle the different cases correctly:
/(-?[0-9]+)-(-?[0-9]+)/ tuck r:match
\ If the regex matches three (the whole string, plus the two captured
\ expressions) then it's a valid "X-Y":
3 n:= if
1 r:@ >n swap 2 r:@ >n nip
\ generate the range:
n:gen-range
else
\ The regex didn't match, so we got garbage. Therefore, return a 'null':
drop null
then
else
\ It was a "X", just drop the original string:
nip
then
;
\ Take an array (possibly) containing other arrays, and flatten any contained
\ arrays so the result is a simple array:
: a:flatten \ a1 -- a2
[] >r
(
nip
array? if
a:flatten r> swap a:+ >r
else
r> swap a:push >r
then
) a:each drop r> ;
\ Take a comma-delimited string of ranges, and expand it into an array of
\ numbers:
: n:range-expand \ str -- a
"," s:/
' n:expand-one a:map
a:flatten ;
\ Process a list:
"-6,-3--1,3-5,7-11,14,15,17-20"
n:range-expand
\ print the expanded list:
. cr bye

View file

@ -0,0 +1,25 @@
;; parsing [spaces][-]digit(s)-[-]digit(s)[spaces]
(define R (make-regexp "^ *(\-?\\d+)\-(\-?\\d+) *$" ))
;; the native (range a b) is [a ... b[
;; (range+ a b) is [a ... b]
(define (range+ a b)
(if (< a b) (range a (1+ b))
(if (> a b) (range a (1- b) -1)
(list a))))
;; in : string : "number" or "number-number"
;; out : a range = list of integer(s)
(define (do-range str)
(define from-to (regexp-exec R str)) ;; "1-3" --> ("1" "3")
(if from-to
(range+ (string->number (first from-to)) (string->number (second from-to)))
(list (string->number str))))
(define (ranges str)
(apply append (map do-range (string-split str ","))))
(define task "-6,-3--1,3-5,7-11,14,15,17-20")
(ranges task)
→ (-6 -3 -2 -1 3 4 5 7 8 9 10 11 14 15 17 18 19 20)

View file

@ -0,0 +1,75 @@
' FB 1.05.0 Win64
Sub split (s As Const String, sepList As Const String, result() As String)
If s = "" OrElse sepList = "" Then
Redim result(0)
result(0) = s
Return
End If
Dim As Integer i, j, count = 0, empty = 0, length
Dim As Integer position(Len(s) + 1)
position(0) = 0
For i = 0 To len(s) - 1
For j = 0 to Len(sepList) - 1
If s[i] = sepList[j] Then
count += 1
position(count) = i + 1
End If
Next j
Next i
Redim result(count)
If count = 0 Then
result(0) = s
Return
End If
position(count + 1) = len(s) + 1
For i = 1 To count + 1
length = position(i) - position(i - 1) - 1
result(i - 1) = Mid(s, position(i - 1) + 1, length)
Next
End Sub
Function expandRange(s As Const String) As String
If s = "" Then Return ""
Dim b() As String
Dim c() As String
Dim result As String = ""
Dim As Integer start = 0, finish = 0, length
split s, ",", b()
For i As Integer = LBound(b) To UBound(b)
split b(i), "-", c()
length = UBound(c) - LBound(c) + 1
If length = 1 Then
start = ValLng(c(LBound(c)))
finish = start
ElseIf length = 2 Then
If Left(b(i), 1) = "-" Then
start = -ValLng(c(UBound(c)))
finish = start
Else
start = ValLng(c(LBound(c)))
finish = ValLng(c(UBound(c)))
End If
ElseIf length = 3 Then
start = -ValLng(c(LBound(c) + 1))
finish = ValLng(c(UBound(c)))
Else
start = -ValLng(c(LBound(c) + 1))
finish = -ValLng(c(UBound(c)))
End If
For j As Integer = start To finish
result += Str(j) + ", "
Next j
Next i
Return Left(result, Len(result) - 2) '' get rid of final ", "
End Function
Dim s As String = "-6,-3--1,3-5,7-11,14,15,17-20"
Print expandRange(s)
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,13 @@
define range_expand(expression::string) => {
local(parts) = regexp(`^(-?\d+)-(-?\d+)$`)
return (
with elm in #expression->split(`,`)
let isRange = #parts->setInput(#elm)&matches
select #isRange
? (integer(#parts->matchString(1)) to integer(#parts->matchString(2)))->asString
| integer(#elm)->asString
)->join(', ')
}
range_expand(`-6,-3--1,3-5,7-11,14,15,17-20`)

View file

@ -0,0 +1,21 @@
-- Note: currently does not support extra white space in input string
on expandRange (str)
res = ""
_player.itemDelimiter = ","
cnt = str.item.count
repeat with i = 1 to cnt
part = str.item[i]
pos = offset("-", part.char[2..part.length])
if pos>0 then
a = integer(part.char[1..pos])
b = integer(part.char[pos+2..part.length])
repeat with j = a to b
put j&"," after res
end repeat
else
put part&"," after res
end if
end repeat
delete the last char of res
return res
end

View file

@ -0,0 +1,2 @@
put expandRange("-6,-3--1,3-5,7-11,14,15,17-20")
-- "-6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20"

View file

@ -0,0 +1,38 @@
function range beginning ending stepping
local tRange, tBegin, tEnd, tstep
if stepping is empty or stepping is 0 then
put 1 into tstep
else
put abs(stepping) into tstep
end if
if ending is empty or isNumber(ending) is not true then
put 0 into tEnd
else
put ending into tEnd
end if
if beginning is empty or isNumber(beginning) is not true then
put 0 into tBegin
else
put beginning into tBegin
end if
repeat with r = tBegin to tEnd step tstep
put space & r after tRange
end repeat
return word 1 to -1 of tRange
end range
function expandRange rangeExpr
put rangeExpr into tRange
split tRange by comma
repeat with n = 1 to the number of elements of tRange
if matchText(tRange[n],"^(\-*\d+)\-(\-*\d+)",beginning, ending) then
put range(beginning, ending, 1) & space after z
else
put tRange[n] & space after z
end if
end repeat
return z
end expandRange

View file

@ -0,0 +1,2 @@
expandRange("-6,-3--1,3-5,7-11,14,15,17-20")
-6 -3 -2 -1 3 4 5 7 8 9 10 11 14 15 17 18 19 20

View file

@ -0,0 +1,24 @@
import parseutils, re, strutils
proc expandRange(input: string): string =
var output: seq[string] = @[]
for range in input.split(','):
var sep = range.find('-', 1)
if sep > 0: # parse range
var first = -1
if range.substr(0, sep-1).parseInt(first) == 0:
break
var last = -1
if range.substr(sep+1).parseInt(last) == 0:
break
for i in first..last:
output.add($i)
else: # parse single number
var n = -1
if range.parseInt(n) > 0:
output.add($n)
else:
break
return output.join(",")
echo("-6,-3--1,3-5,7-11,14,15,17-20".expandRange)

View file

@ -0,0 +1,10 @@
: addRange( s res -- )
| i n |
s asInteger dup ifNotNull: [ res add return ] drop
s indexOfFrom('-', 2) ->i
s left( i 1- ) asInteger s right( s size i - ) asInteger
for: n [ n res add ]
;
: rangeExpand ( s -- [ n ] )
ArrayBuffer new s wordsWith( ',' ) apply( #[ over addRange ] ) ;

View file

@ -0,0 +1,12 @@
func rangex(str) {
str.split(',').map { |r|
var m = r.match(/^
(?(DEFINE) (?<int>[+-]?[0-9]+) )
(?<from>(?&int))-(?<to>(?&int))
$/x)
m ? do {var c = m.ncap; (Num(c{:from}) .. Num(c{:to}))...}
: Num(r)
}
}
say rangex('-6,-3--1,3-5,7-11,14,15,17-20').join(',')

View file

@ -0,0 +1,11 @@
def expand_range:
def number: "-?[0-9]+";
def expand: [range(.[0]; .[1] + 1)];
split(",")
| reduce .[] as $r
( []; . +
($r | if test("^\(number)$") then [tonumber]
else sub( "(?<x>\(number))-(?<y>\(number))"; "\(.x):\(.y)")
| split(":") | map(tonumber) | expand
end));

View file

@ -0,0 +1 @@
"-6,-3--1,3-5,7-11,14,15,17-20" | expand_range