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
5
Task/String-matching/EchoLisp/string-matching.echolisp
Normal file
5
Task/String-matching/EchoLisp/string-matching.echolisp
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(string-suffix? "nette" "Antoinette") → #t
|
||||
(string-prefix? "Simon" "Simon & Garfunkel") → #t
|
||||
|
||||
(string-match "Antoinette" "net") → #t ;; contains
|
||||
(string-index "net" "Antoinette") → 5 ;; substring location
|
||||
26
Task/String-matching/FreeBASIC/string-matching.freebasic
Normal file
26
Task/String-matching/FreeBASIC/string-matching.freebasic
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Dim As String s1 = "abracadabra"
|
||||
Dim As String s2 = "abra"
|
||||
Print "First string : "; s1
|
||||
Print "Second string : "; s2
|
||||
Print
|
||||
Print "First string begins with second string : "; CBool(s2 = Left(s1, Len(s2)))
|
||||
Dim As Integer i1 = Instr(s1, s2)
|
||||
Dim As Integer i2
|
||||
Print "First string contains second string : ";
|
||||
If i1 Then
|
||||
Print "at index"; i1;
|
||||
i2 = Instr(i1 + Len(s2), s1, s2)
|
||||
If i2 Then
|
||||
Print " and at index"; i2
|
||||
Else
|
||||
Print
|
||||
End If
|
||||
Else
|
||||
Print "false";
|
||||
End If
|
||||
Print "First string ends with second string : "; CBool(s2 = Right(s1, Len(s2)))
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
14
Task/String-matching/Lasso/string-matching.lasso
Normal file
14
Task/String-matching/Lasso/string-matching.lasso
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
local(
|
||||
a = 'a quick brown peanut jumped over a quick brown fox',
|
||||
b = 'a quick brown'
|
||||
)
|
||||
|
||||
//Determining if the first string starts with second string
|
||||
#a->beginswith(#b) // true
|
||||
|
||||
//Determining if the first string contains the second string at any location
|
||||
#a >> #b // true
|
||||
#a->contains(#b) // true
|
||||
|
||||
//Determining if the first string ends with the second string
|
||||
#a->endswith(#b) // false
|
||||
22
Task/String-matching/Lingo/string-matching.lingo
Normal file
22
Task/String-matching/Lingo/string-matching.lingo
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
a = "Hello world!"
|
||||
b = "Hello"
|
||||
|
||||
-- Determining if the first string starts with second string
|
||||
put a starts b
|
||||
-- 1
|
||||
|
||||
-- Determining if the first string contains the second string at any location
|
||||
put a contains b
|
||||
-- 1
|
||||
|
||||
-- Determining if the first string ends with the second string
|
||||
put a.char[a.length-b.length+1..a.length] = b
|
||||
-- 0
|
||||
|
||||
b = "world!"
|
||||
put a.char[a.length-b.length+1..a.length] = b
|
||||
-- 1
|
||||
|
||||
-- Print the location of the match for part 2
|
||||
put offset(b, a)
|
||||
-- 7
|
||||
10
Task/String-matching/Nim/string-matching.nim
Normal file
10
Task/String-matching/Nim/string-matching.nim
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import strutils
|
||||
|
||||
var s: string = "The quick brown fox"
|
||||
if startsWith(s, "The quick"):
|
||||
echo("Starts with: The quick")
|
||||
if endsWith(s, "brown Fox"):
|
||||
echo("Ends with: brown fox")
|
||||
var pos = find(s, " brown ") # -1 if not found
|
||||
if contains(s, " brown "): # showing the contains() proc, but could use if pos!=-1:
|
||||
echo('"' & " brown " & '"' & " is located at position: " & $pos)
|
||||
14
Task/String-matching/Oforth/string-matching.oforth
Normal file
14
Task/String-matching/Oforth/string-matching.oforth
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
: stringMatching(s1, s2)
|
||||
| i |
|
||||
s2 isAllAt(s1, 1) ifTrue: [ System.Out s1 << " begins with " << s2 << cr ]
|
||||
s2 isAllAt(s1, s1 size s2 size - 1 + ) ifTrue: [ System.Out s1 << " ends with " << s2 << cr ]
|
||||
|
||||
s1 indexOfAll(s2) ->i
|
||||
i ifNotNull: [ System.Out s1 << " includes " << s2 << " at position : " << i << cr ]
|
||||
|
||||
"\nAll positions : " println
|
||||
1 ->i
|
||||
while (s1 indexOfAllFrom(s2, i) dup ->i notNull) [
|
||||
System.Out s1 << " includes " << s2 << " at position : " << i << cr
|
||||
i s2 size + ->i
|
||||
] ;
|
||||
29
Task/String-matching/Phix/string-matching.phix
Normal file
29
Task/String-matching/Phix/string-matching.phix
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
constant word = "the", -- (also try this with "th"/"he")
|
||||
sentence = "the last thing the man said was the"
|
||||
-- sentence = "thelastthingthemansaidwasthe" -- (practically the same results)
|
||||
|
||||
-- A common, but potentially inefficient idiom for checking for a substring at the start is:
|
||||
if match(word,sentence)=1 then
|
||||
?"yes(1)"
|
||||
end if
|
||||
-- A more efficient method is to test the appropriate slice
|
||||
if length(sentence)>=length(word)
|
||||
and sentence[1..length(word)]=word then
|
||||
?"yes(2)"
|
||||
end if
|
||||
-- Which is almost identical to checking for a word at the end
|
||||
if length(sentence)>=length(word)
|
||||
and sentence[-length(word)..-1]=word then
|
||||
?"yes(3)"
|
||||
end if
|
||||
-- Or sometimes you will see this, a tiny bit more efficient:
|
||||
if length(sentence)>=length(word)
|
||||
and match(word,sentence,length(sentence)-length(word)+1) then
|
||||
?"yes(4)"
|
||||
end if
|
||||
-- Finding all occurences is a snap:
|
||||
integer r = match(word,sentence)
|
||||
while r!=0 do
|
||||
?r
|
||||
r = match(word,sentence,r+1)
|
||||
end while
|
||||
4
Task/String-matching/Ring/string-matching.ring
Normal file
4
Task/String-matching/Ring/string-matching.ring
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
aString = "Welcome to the Ring Programming Language"
|
||||
bString = "Ring"
|
||||
bStringIndex = substr(aString,bString)
|
||||
if bStringIndex > 0 see "" + bStringIndex + " : " + bString ok
|
||||
15
Task/String-matching/Sidef/string-matching.sidef
Normal file
15
Task/String-matching/Sidef/string-matching.sidef
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
var first = "abc-abcdef-abcd";
|
||||
var second = "abc";
|
||||
|
||||
say first.begins_with(second); #=> true
|
||||
say first.contains(second); #=> true
|
||||
say first.ends_with(second); #=> false
|
||||
|
||||
# Get and print the location of the match
|
||||
say first.index(second); #=> 0
|
||||
|
||||
# Find multiple occurrences of a string
|
||||
var pos = -1;
|
||||
while (pos = first.index(second, pos+1) != -1) {
|
||||
say "Match at pos: #{pos}";
|
||||
}
|
||||
9
Task/String-matching/Swift/string-matching.swift
Normal file
9
Task/String-matching/Swift/string-matching.swift
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
var str = "Hello, playground"
|
||||
str.hasPrefix("Hell") //True
|
||||
str.hasPrefix("hell") //False
|
||||
|
||||
str.containsString("llo") //True
|
||||
str.containsString("xxoo") //False
|
||||
|
||||
str.hasSuffix("playground") //True
|
||||
str.hasSuffix("world") //False
|
||||
3
Task/String-matching/jq/string-matching-1.jq
Normal file
3
Task/String-matching/jq/string-matching-1.jq
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# startswith/1 is boolean:
|
||||
"abc" | startswith("ab")
|
||||
#=> true
|
||||
6
Task/String-matching/jq/string-matching-2.jq
Normal file
6
Task/String-matching/jq/string-matching-2.jq
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# index/1 returns the index or null,
|
||||
# so the jq test "if index(_) then ...." can be used
|
||||
# without any type conversion.
|
||||
|
||||
"abcd" | index( "bc")
|
||||
#=> 1
|
||||
3
Task/String-matching/jq/string-matching-3.jq
Normal file
3
Task/String-matching/jq/string-matching-3.jq
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# endswith/1 is also boolean:
|
||||
"abc" | endswith("bc")
|
||||
#=> true
|
||||
5
Task/String-matching/jq/string-matching-4.jq
Normal file
5
Task/String-matching/jq/string-matching-4.jq
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
"abc" | test( "^ab")
|
||||
|
||||
"abcd" | test("bc")
|
||||
|
||||
"abcd" | test("cd$")
|
||||
6
Task/String-matching/jq/string-matching-5.jq
Normal file
6
Task/String-matching/jq/string-matching-5.jq
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# In jq 1.4 or later:
|
||||
jq -n '"abcdabcd" | indices("bc")'
|
||||
[
|
||||
1,
|
||||
5
|
||||
]
|
||||
3
Task/String-matching/jq/string-matching-6.jq
Normal file
3
Task/String-matching/jq/string-matching-6.jq
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
$ jq -n '"abcdabcd" | match("bc"; "g") | .offset'
|
||||
1
|
||||
5
|
||||
Loading…
Add table
Add a link
Reference in a new issue