Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
20
Task/String-matching/CoffeeScript/string-matching.coffee
Normal file
20
Task/String-matching/CoffeeScript/string-matching.coffee
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
matchAt = (s, frag, i) ->
|
||||
s[i...i+frag.length] == frag
|
||||
|
||||
startsWith = (s, frag) ->
|
||||
matchAt s, frag, 0
|
||||
|
||||
endsWith = (s, frag) ->
|
||||
matchAt s, frag, s.length - frag.length
|
||||
|
||||
matchLocations = (s, frag) ->
|
||||
(i for i in [0..s.length - frag.length] when matchAt s, frag, i)
|
||||
|
||||
console.log startsWith "tacoloco", "taco" # true
|
||||
console.log startsWith "taco", "tacoloco" # false
|
||||
console.log startsWith "tacoloco", "talk" # false
|
||||
console.log endsWith "tacoloco", "loco" # true
|
||||
console.log endsWith "loco", "tacoloco" # false
|
||||
console.log endsWith "tacoloco", "yoco" # false
|
||||
console.log matchLocations "bababab", "bab" # [0,2,4]
|
||||
console.log matchLocations "xxx", "x" # [0,1,2]
|
||||
Loading…
Add table
Add a link
Reference in a new issue