Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
74
Task/Multisplit/FreeBASIC/multisplit.freebasic
Normal file
74
Task/Multisplit/FreeBASIC/multisplit.freebasic
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Sub Split(s As String, sepList() As String, result() As String, removeEmpty As Boolean = False, showSepInfo As Boolean = False)
|
||||
If s = "" OrElse UBound(sepList) = -1 Then
|
||||
Redim result(0)
|
||||
result(0) = s
|
||||
Return
|
||||
End If
|
||||
Dim As Integer i = 0, j, count = 0, empty = 0, length
|
||||
Dim As Integer position(len(s) + 1)
|
||||
Dim As Integer sepIndex(1 To len(s))
|
||||
Dim As Integer sepLength(len(s))
|
||||
position(0) = 0 : sepLength(0) = 1
|
||||
|
||||
While i < Len(s)
|
||||
For j = lbound(sepList) To ubound(sepList)
|
||||
length = len(sepList(j))
|
||||
If length = 0 Then Continue For '' ignore blank separators
|
||||
If mid(s, i + 1, length) = sepList(j) Then
|
||||
count += 1
|
||||
position(count) = i + 1
|
||||
sepIndex(count) = j
|
||||
sepLength(count) = length
|
||||
i += length - 1
|
||||
Exit For
|
||||
End If
|
||||
Next j
|
||||
i += 1
|
||||
Wend
|
||||
|
||||
Redim result(count)
|
||||
If count = 0 Then
|
||||
If showSepInfo Then
|
||||
Print "No delimiters were found" : Print
|
||||
End If
|
||||
result(0) = s
|
||||
Return
|
||||
End If
|
||||
position(count + 1) = len(s) + 1
|
||||
|
||||
For i = 1 To count + 1
|
||||
length = position(i) - position(i - 1) - sepLength(i - 1)
|
||||
result(i - 1 - empty) = Mid(s, position(i - 1) + sepLength(i - 1), length)
|
||||
If removeEmpty AndAlso cbool(length = 0) Then empty += 1
|
||||
Next
|
||||
|
||||
If empty > 0 Then Redim Preserve result(count - empty)
|
||||
|
||||
If showSepInfo Then
|
||||
Print "The 1-based indices of the delimiters found are : "
|
||||
Print
|
||||
For x As Integer = 1 To count
|
||||
Print "At index"; position(x), sepList(sepIndex(x))
|
||||
Next
|
||||
Print
|
||||
End If
|
||||
End Sub
|
||||
|
||||
|
||||
Dim s As String = "a!===b=!=c"
|
||||
Print "The string to be split is : "; s
|
||||
Print
|
||||
Dim a() As String '' to hold results
|
||||
Dim b(1 To 3) As String = {"==", "!=", "="} '' separators to be used in order of priority (highest first)
|
||||
split s, b(), a(), False, True '' show separator info
|
||||
Print "The sub-strings are : "
|
||||
Print
|
||||
For i As integer = 0 To ubound(a)
|
||||
Print Using "##"; i + 1;
|
||||
Print " : "; a(i)
|
||||
Next
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
19
Task/Multisplit/Nim/multisplit.nim
Normal file
19
Task/Multisplit/Nim/multisplit.nim
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import strutils
|
||||
|
||||
iterator tokenize(text, sep): tuple[token: string, isSep: bool] =
|
||||
var i, lastMatch = 0
|
||||
while i < text.len:
|
||||
for j, s in sep:
|
||||
if text[i..text.high].startsWith s:
|
||||
if i > lastMatch: yield (text[lastMatch .. <i], false)
|
||||
yield (s, true)
|
||||
lastMatch = i + s.len
|
||||
i += s.high
|
||||
break
|
||||
inc i
|
||||
if i > lastMatch: yield (text[lastMatch .. <i], false)
|
||||
|
||||
for token, isSep in "a!===b=!=c".tokenize(["==", "!=", "="]):
|
||||
if isSep: stdout.write '{',token,'}'
|
||||
else: stdout.write token
|
||||
echo ""
|
||||
9
Task/Multisplit/Sidef/multisplit.sidef
Normal file
9
Task/Multisplit/Sidef/multisplit.sidef
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
func multisplit(sep, str, keep_sep=false) {
|
||||
sep = sep.map{.escape}.join('|');
|
||||
var re = Regex.new(keep_sep ? "(#{sep})" : sep);
|
||||
str.split(re, -1);
|
||||
}
|
||||
|
||||
[false, true].each { |bool|
|
||||
say multisplit(%w(== != =), 'a!===b=!=c', keep_sep: bool);
|
||||
}
|
||||
47
Task/Multisplit/jq/multisplit.jq
Normal file
47
Task/Multisplit/jq/multisplit.jq
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
# peeloff(delims) either peels off a delimiter or
|
||||
# a single character from the input string.
|
||||
# The input should be a nonempty string, and delims should be
|
||||
# a non-empty array of delimiters;
|
||||
# return [peeledoff, remainder]
|
||||
# where "peeledoff" is either [delim] or the peeled off character:
|
||||
def peeloff(delims):
|
||||
delims[0] as $delim
|
||||
| if startswith($delim) then [ [$delim], .[ ($delim|length):]]
|
||||
elif (delims|length)>1 then peeloff(delims[1:])
|
||||
else [ .[0:1], .[1:]]
|
||||
end ;
|
||||
|
||||
# multisplit_parse(delims) produces an intermediate parse.
|
||||
# Input must be of the parse form: [ string, [ delim ], ... ]
|
||||
# Output is of the same form.
|
||||
def multisplit_parse(delims):
|
||||
if (delims|length) == 0 or length == 0 then .
|
||||
else
|
||||
.[length-1] as $last
|
||||
| .[0:length-1] as $butlast
|
||||
| if ($last|type) == "array" then . # all done
|
||||
elif $last == "" then .
|
||||
else
|
||||
($last | peeloff(delims)) as $p # [ peeledoff, next ]
|
||||
| $p[0] as $peeledoff
|
||||
| $p[1] as $next
|
||||
| if ($next|length) > 0
|
||||
then $butlast + [$peeledoff] + ([$next]|multisplit_parse(delims))
|
||||
else $butlast + $p
|
||||
end
|
||||
end
|
||||
end ;
|
||||
|
||||
def multisplit(delims):
|
||||
[.] | multisplit_parse(delims)
|
||||
# insert "" between delimiters, compress strings, remove trailing "" if any
|
||||
| reduce .[] as $x ([];
|
||||
if length == 0 then [ $x ]
|
||||
elif ($x|type) == "array"
|
||||
then if (.[length-1]|type) == "array" then . + ["", $x]
|
||||
else . + [$x]
|
||||
end
|
||||
elif .[length-1]|type == "string"
|
||||
then .[0:length-1] + [ .[length-1] + $x ]
|
||||
else . + [$x]
|
||||
end ) ;
|
||||
Loading…
Add table
Add a link
Reference in a new issue