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,44 @@
shared void run() {
value numbers = [
0, 1, 2, 4, 6, 7, 8, 11, 12, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
37, 38, 39
];
function asRangeFormattedString<Value>([Value*] values)
given Value satisfies Enumerable<Value> {
value builder = StringBuilder();
void append(Range<Value> range) {
if(!builder.empty) {
builder.append(",");
}
if(1 <= range.size < 3) {
builder.append(",".join(range));
} else {
builder.append("``range.first``-``range.last``");
}
}
if(nonempty values) {
variable value currentRange = values.first..values.first;
for(val in values.rest) {
if(currentRange.last.successor == val) {
currentRange = currentRange.first..val;
} else {
append(currentRange);
currentRange = val..val;
}
}
append(currentRange);
}
return builder.string;
}
value rangeString = asRangeFormattedString(numbers);
assert(rangeString == "0-2,4,6-8,11,12,14-25,27-33,35-39");
print(rangeString);
}

View file

@ -0,0 +1,24 @@
(define task '(0 1 2 4 6 7 8 11 12 14 15 16 17 18 19 20 21 22 23 24 25 27 28 29 30 31 32 33 35 36 37 38 39))
;; 1- GROUPING
(define (group-range item acc)
(if
(or (empty? acc) (!= (caar acc) (1- item)))
(cons (cons item item) acc)
(begin (set-car! (car acc) item) acc)))
;; intermediate result
;; (foldl group-range () task)
;; → ((39 . 35) (33 . 27) (25 . 14) (12 . 11) (8 . 6) (4 . 4) (2 . 0))
;; 2- FORMATTING
(define (range->string range)
(let ((from (rest range)) (to (first range)))
(cond
((= from to) (format "%d " from))
((= to (1+ from)) (format "%d, %d " from to))
(else (format "%d-%d " from to)))))
;; 3 - FINAL
(string-join (map range->string (reverse (foldl group-range () task))) ",")
→ "0-2 ,4 ,6-8 ,11, 12 ,14-25 ,27-33 ,35-39 "

View file

@ -0,0 +1,46 @@
' FB 1.05.0 Win64
Function formatRange (a() As Integer) As String
Dim lb As Integer = LBound(a)
Dim ub As Integer = UBound(a)
If ub = - 1 Then Return ""
If lb = ub Then Return Str(a(lb))
Dim rangeCount As Integer = 1
Dim range As String = Str(a(lb))
For i As Integer = lb + 1 To ub
If a(i) = a(i - 1) + 1 Then
rangeCount += 1
ElseIf rangeCount = 1 Then
range += "," + Str(a(i))
ElseIf rangeCount = 2 Then
rangeCount = 1
range += "," + Str(a(i-1)) + "," + Str(a(i))
Else
rangeCount = 1
range += "-" + Str(a(i-1)) + "," + Str(a(i))
End If
Next
If rangeCount = 2 Then
range += "," + Str(a(ub))
ElseIf rangeCount > 2 Then
range += "-" + Str(a(ub))
End If
Return range
End Function
Dim a(1 To 20) As Integer = {-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20}
Print formatRange(a())
Print
Dim b(1 To 33) As Integer => _
{ _
0, 1, 2, 4, 6, 7, 8, 11, 12, 14, _
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, _
25, 27, 28, 29, 30, 31, 32, 33, 35, 36, _
37, 38, 39 _
}
Print formatRange(b())
Print
Print "Press any key to continue"
Sleep

View file

@ -0,0 +1,29 @@
function rangeExtract nums
local prevNum, znums, rangedNums
set itemDelimiter to ", "
put the first item of nums into prevNum
repeat for each item n in nums
if n is (prevNum + 1) then
put n into prevNum
put "#" & n after znums
else
put n into prevNum
put return & n after znums
end if
end repeat
set itemDelimiter to "#"
repeat for each line z in znums
if z is empty then next repeat
switch the number of items of z
case 1
put z & "," after rangedNums
break
case 2
put item 1 of z & "," & item -1 of z & "," after rangedNums
break
default
put item 1 of z & "-" & item -1 of z & "," after rangedNums
end switch
end repeat
return char 1 to -2 of rangedNums --strip off trailing comma
end rangeExtract

View file

@ -0,0 +1,8 @@
command testRangeExtract
local numbers
put "0, 1, 2, 4, 6, 7, 8, 11, 12, 14," \
&& "15, 16, 17, 18, 19, 20, 21, 22, 23, 24," \
&& "25, 27, 28, 29, 30, 31, 32, 33, 35, 36," \
&& "37, 38, 39" into numbers
put rangeExtract(numbers)
end testRangeExtract

View file

@ -0,0 +1 @@
0-2,4,6-8,11,12,14-25,27-33,35-39

View file

@ -0,0 +1,30 @@
import parseutils, re, strutils
proc extractRange(input: string): string =
var list = input.replace(re"\s+").split(',').map(parseInt)
var ranges: seq[string] = @[]
var i = 0
while i < list.len:
var first = list[i] # first element in the current range
var offset = i
while True: # skip ahead to the end of the current range
if i + 1 >= list.len:
# reached end of the list
break
if list[i + 1] - (i + 1) != first - offset:
# next element isn't in the current range
break
i.inc
var last = list[i] # last element in the current range
case last - first
of 0: ranges.add($first)
of 1: ranges.add("$1,$2".format([$first, $last]))
else: ranges.add("$1-$2".format([$first, $last]))
i.inc
return ranges.join(",")
echo("""
0, 1, 2, 4, 6, 7, 8, 11, 12, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
37, 38, 39""".extractRange)

View file

@ -0,0 +1,26 @@
function spout(integer first, integer this, sequence s)
string res
if first=this-1 then
res = sprintf("%d",s[first])
else
res = sprintf("%d%s%d",{s[first],iff(first=this-2?',':'-'),s[this-1]})
end if
return res
end function
function extract_ranges(sequence s)
integer first = 1
string out = ""
if length(s)!=0 then
for i=2 to length(s) do
if s[i]!=s[i-1]+1 then
out &= spout(first,i,s)&','
first = i
end if
end for
out &= spout(first,length(s)+1,s)
end if
return out
end function
puts(1,extract_ranges({0,1,2,4,6,7,8,11,12,14,15,16,17,18,19,20,21,22,23,24,25,27,28,29,30,31,32,33,35,36,37,38,39}))

View file

@ -0,0 +1,47 @@
func rangesFromInts(ints:[Int]) -> [(Int, Int)] {
var range : (Int, Int)?
var ranges = [(Int, Int)]()
for this in ints {
if let (start, end) = range {
if this == end + 1 {
range = (start, this)
}
else {
ranges.append(range!)
range = (this, this)
}
}
else { range = (this, this) }
}
ranges.append(range!)
return ranges
}
func descriptionFromRanges(ranges:[(Int, Int)]) -> String {
var desc = ""
for (start, end) in ranges {
desc += desc.isEmpty ? "" : ","
if start == end {
desc += "\(start)"
}
else if end == start + 1 {
desc += "\(start),\(end)"
}
else {
desc += "\(start)-\(end)"
}
}
return desc
}
let ex = [-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]
let longer = [0, 1, 2, 4, 6, 7, 8, 11, 12, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
37, 38, 39]
descriptionFromRanges(rangesFromInts(ex))
descriptionFromRanges(rangesFromInts(longer))

View file

@ -0,0 +1,35 @@
extension NSIndexSet {
var rangesDescription : String {
var out = ""
indexSet.enumerateRangesUsingBlock { range, _ in
let start = range.location
let end = range.location+range.length-1
out += out.isEmpty ? "" : ","
if start == end {
out += "\(start)"
}
else if end == start + 1 {
out += "\(start),\(end)"
}
else {
out += "\(start)-\(end)"
}
}
return out
}
}
let ex = [-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]
let longer = [0, 1, 2, 4, 6, 7, 8, 11, 12, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
37, 38, 39]
var indexSet = NSMutableIndexSet()
ex.map { indexSet.addIndex($0) }
indexSet.rangesDescription
var indexSet2 = NSMutableIndexSet()
longer.map { indexSet2.addIndex($0) }
indexSet2.rangesDescription

View file

@ -0,0 +1,20 @@
# Input should be an array
def extract:
reduce .[] as $i
# state is an array with integers or [start, end] ranges
([];
if length == 0 then [ $i ]
else ( .[-1]) as $last
| if ($last|type) == "array" then
if ($last[1] + 1) == $i then setpath([-1,1]; $i)
else . + [ $i ]
end
elif ($last + 1) == $i then setpath([-1]; [$last, $i])
else . + [ $i ]
end
end)
| map( if type == "number" then tostring
elif .[0] == .[1] -1
then "\(.[0]),\(.[1])" # satisfy special requirement
else "\(.[0])-\(.[1])" end )
| join(",") ;