September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
240
Task/String-matching/ARM-Assembly/string-matching.arm
Normal file
240
Task/String-matching/ARM-Assembly/string-matching.arm
Normal file
|
|
@ -0,0 +1,240 @@
|
|||
/* ARM assembly Raspberry PI */
|
||||
/* program strMatching.s */
|
||||
|
||||
/* Constantes */
|
||||
.equ STDOUT, 1 @ Linux output console
|
||||
.equ EXIT, 1 @ Linux syscall
|
||||
.equ WRITE, 4 @ Linux syscall
|
||||
|
||||
/* Initialized data */
|
||||
.data
|
||||
szMessFound: .asciz "String found. \n"
|
||||
szMessNotFound: .asciz "String not found. \n"
|
||||
szString: .asciz "abcdefghijklmnopqrstuvwxyz"
|
||||
szString2: .asciz "abc"
|
||||
szStringStart: .asciz "abcd"
|
||||
szStringEnd: .asciz "xyz"
|
||||
szStringStart2: .asciz "abcd"
|
||||
szStringEnd2: .asciz "xabc"
|
||||
szCarriageReturn: .asciz "\n"
|
||||
|
||||
/* UnInitialized data */
|
||||
.bss
|
||||
|
||||
/* code section */
|
||||
.text
|
||||
.global main
|
||||
main:
|
||||
|
||||
ldr r0,iAdrszString @ address input string
|
||||
ldr r1,iAdrszStringStart @ address search string
|
||||
|
||||
bl searchStringDeb @ Determining if the first string starts with second string
|
||||
cmp r0,#0
|
||||
ble 1f
|
||||
ldr r0,iAdrszMessFound @ display message
|
||||
bl affichageMess
|
||||
b 2f
|
||||
1:
|
||||
ldr r0,iAdrszMessNotFound
|
||||
bl affichageMess
|
||||
2:
|
||||
ldr r0,iAdrszString @ address input string
|
||||
ldr r1,iAdrszStringEnd @ address search string
|
||||
bl searchStringFin @ Determining if the first string ends with the second string
|
||||
cmp r0,#0
|
||||
ble 3f
|
||||
ldr r0,iAdrszMessFound @ display message
|
||||
bl affichageMess
|
||||
b 4f
|
||||
3:
|
||||
ldr r0,iAdrszMessNotFound
|
||||
bl affichageMess
|
||||
4:
|
||||
ldr r0,iAdrszString2 @ address input string
|
||||
ldr r1,iAdrszStringStart2 @ address search string
|
||||
|
||||
bl searchStringDeb @
|
||||
cmp r0,#0
|
||||
ble 5f
|
||||
ldr r0,iAdrszMessFound @ display message
|
||||
bl affichageMess
|
||||
b 6f
|
||||
5:
|
||||
ldr r0,iAdrszMessNotFound
|
||||
bl affichageMess
|
||||
6:
|
||||
ldr r0,iAdrszString2 @ address input string
|
||||
ldr r1,iAdrszStringEnd2 @ address search string
|
||||
bl searchStringFin
|
||||
cmp r0,#0
|
||||
ble 7f
|
||||
ldr r0,iAdrszMessFound @ display message
|
||||
bl affichageMess
|
||||
b 8f
|
||||
7:
|
||||
ldr r0,iAdrszMessNotFound
|
||||
bl affichageMess
|
||||
8:
|
||||
ldr r0,iAdrszString @ address input string
|
||||
ldr r1,iAdrszStringEnd @ address search string
|
||||
bl searchSubString @ Determining if the first string contains the second string at any location
|
||||
cmp r0,#0
|
||||
ble 9f
|
||||
ldr r0,iAdrszMessFound @ display message
|
||||
bl affichageMess
|
||||
b 10f
|
||||
9:
|
||||
ldr r0,iAdrszMessNotFound @ display substring result
|
||||
bl affichageMess
|
||||
10:
|
||||
|
||||
100: @ standard end of the program
|
||||
mov r0, #0 @ return code
|
||||
mov r7, #EXIT @ request to exit program
|
||||
svc 0 @ perform system call
|
||||
iAdrszMessFound: .int szMessFound
|
||||
iAdrszMessNotFound: .int szMessNotFound
|
||||
iAdrszString: .int szString
|
||||
iAdrszString2: .int szString2
|
||||
iAdrszStringStart: .int szStringStart
|
||||
iAdrszStringEnd: .int szStringEnd
|
||||
iAdrszStringStart2: .int szStringStart2
|
||||
iAdrszStringEnd2: .int szStringEnd2
|
||||
iAdrszCarriageReturn: .int szCarriageReturn
|
||||
/******************************************************************/
|
||||
/* search substring at begin of input string */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the input string */
|
||||
/* r1 contains the address of substring */
|
||||
/* r0 returns 1 if find or 0 if not or -1 if error */
|
||||
searchStringDeb:
|
||||
push {r1-r4,lr} @ save registers
|
||||
mov r3,#0 @ counter byte string
|
||||
ldrb r4,[r1,r3] @ load first byte of substring
|
||||
cmp r4,#0 @ empty string ?
|
||||
moveq r0,#-1 @ error
|
||||
beq 100f
|
||||
1:
|
||||
ldrb r2,[r0,r3] @ load byte string input
|
||||
cmp r2,#0 @ zero final ?
|
||||
moveq r0,#0 @ not find
|
||||
beq 100f
|
||||
cmp r4,r2 @ bytes equals ?
|
||||
movne r0,#0 @ no not find
|
||||
bne 100f
|
||||
add r3,#1 @ increment counter
|
||||
ldrb r4,[r1,r3] @ and load next byte of substring
|
||||
cmp r4,#0 @ zero final ?
|
||||
bne 1b @ no -> loop
|
||||
mov r0,#1 @ yes is ok
|
||||
100:
|
||||
pop {r1-r4,lr} @ restaur registers
|
||||
bx lr @ return
|
||||
|
||||
/******************************************************************/
|
||||
/* search substring at end of input string */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the input string */
|
||||
/* r1 contains the address of substring */
|
||||
/* r0 returns 1 if find or 0 if not or -1 if error */
|
||||
searchStringFin:
|
||||
push {r1-r5,lr} @ save registers
|
||||
mov r3,#0 @ counter byte string
|
||||
@ search the last character of substring
|
||||
1:
|
||||
ldrb r4,[r1,r3] @ load byte of substring
|
||||
cmp r4,#0 @ zero final ?
|
||||
addne r3,#1 @ no increment counter
|
||||
bne 1b @ and loop
|
||||
cmp r3,#0 @ empty string ?
|
||||
moveq r0,#-1 @ error
|
||||
beq 100f
|
||||
sub r3,#1 @ index of last byte
|
||||
ldrb r4,[r1,r3] @ load last byte of substring
|
||||
@ search the last character of string
|
||||
mov r2,#0 @ index last character
|
||||
2:
|
||||
ldrb r5,[r0,r2] @ load first byte of substring
|
||||
cmp r5,#0 @ zero final ?
|
||||
addne r2,#1 @ no -> increment counter
|
||||
bne 2b @ and loop
|
||||
cmp r2,#0 @ empty input string ?
|
||||
moveq r0,#0 @ yes -> not found
|
||||
beq 100f
|
||||
sub r2,#1 @ index last character
|
||||
3:
|
||||
ldrb r5,[r0,r2] @ load byte string input
|
||||
cmp r4,r5 @ bytes equals ?
|
||||
movne r0,#0 @ no -> not found
|
||||
bne 100f
|
||||
subs r3,#1 @ decrement counter
|
||||
movlt r0,#1 @ if zero -> ok found
|
||||
blt 100f
|
||||
subs r2,#1 @ decrement counter input string
|
||||
movlt r0,#0 @ if zero -> not found
|
||||
blt 100f
|
||||
ldrb r4,[r1,r3] @ load previous byte of substring
|
||||
b 3b @ and loop
|
||||
|
||||
100:
|
||||
pop {r1-r5,lr} @ restaur registers
|
||||
bx lr @ return
|
||||
|
||||
/******************************************************************/
|
||||
/* search a substring in the string */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the input string */
|
||||
/* r1 contains the address of substring */
|
||||
/* r0 returns index of substring in string or -1 if not found */
|
||||
searchSubString:
|
||||
push {r1-r6,lr} @ save registers
|
||||
mov r2,#0 @ counter byte input string
|
||||
mov r3,#0 @ counter byte string
|
||||
mov r6,#-1 @ index found
|
||||
ldrb r4,[r1,r3]
|
||||
1:
|
||||
ldrb r5,[r0,r2] @ load byte string
|
||||
cmp r5,#0 @ zero final ?
|
||||
moveq r0,#-1 @ yes returns error
|
||||
beq 100f
|
||||
cmp r5,r4 @ compare character
|
||||
beq 2f
|
||||
mov r6,#-1 @ no equals - > raz index
|
||||
mov r3,#0 @ and raz counter byte
|
||||
add r2,#1 @ and increment counter byte
|
||||
b 1b @ and loop
|
||||
2: @ characters equals
|
||||
cmp r6,#-1 @ first characters equals ?
|
||||
moveq r6,r2 @ yes -> index begin in r6
|
||||
add r3,#1 @ increment counter substring
|
||||
ldrb r4,[r1,r3] @ and load next byte
|
||||
cmp r4,#0 @ zero final ?
|
||||
beq 3f @ yes -> end search
|
||||
add r2,#1 @ else increment counter string
|
||||
b 1b @ and loop
|
||||
3:
|
||||
mov r0,r6
|
||||
100:
|
||||
pop {r1-r6,lr} @ restaur registers
|
||||
bx lr
|
||||
|
||||
/******************************************************************/
|
||||
/* display text with size calculation */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the message */
|
||||
affichageMess:
|
||||
push {r0,r1,r2,r7,lr} @ save registers
|
||||
mov r2,#0 @ counter length */
|
||||
1: @ loop length calculation
|
||||
ldrb r1,[r0,r2] @ read octet start position + index
|
||||
cmp r1,#0 @ if 0 its over
|
||||
addne r2,r2,#1 @ else add 1 in the length
|
||||
bne 1b @ and loop
|
||||
@ so here r2 contains the length of the message
|
||||
mov r1,r0 @ address message in r1
|
||||
mov r0,#STDOUT @ code to write to the standard output Linux
|
||||
mov r7, #WRITE @ code call system "write"
|
||||
svc #0 @ call system
|
||||
pop {r0,r1,r2,r7,lr} @ restaur registers
|
||||
bx lr @ return
|
||||
|
|
@ -1,23 +1,22 @@
|
|||
text s, t;
|
||||
text t;
|
||||
data b;
|
||||
|
||||
s = "Bangkok";
|
||||
b = "Bangkok";
|
||||
|
||||
t = "Bang";
|
||||
|
||||
b_cast(b, s);
|
||||
|
||||
o_form("starts with, embeds, ends with \"~\": ~, ~, ~\n", t, b_seek(b, t) == 0,
|
||||
b_seek(b, t) != -1,
|
||||
b_seek(b, t) != -1 && b_seek(b, t) + length(t) == length(s));
|
||||
o_form("starts with, embeds, ends with \"~\": ~, ~, ~\n", t, b.seek(t) == 0,
|
||||
b.seek(t) != -1,
|
||||
b.seek(t) != -1 && b.seek(t) + ~t == ~b);
|
||||
|
||||
t = "ok";
|
||||
|
||||
o_form("starts with, embeds, ends with \"~\": ~, ~, ~\n", t, b_seek(b, t) == 0,
|
||||
b_seek(b, t) != -1,
|
||||
b_seek(b, t) != -1 && b_seek(b, t) + length(t) == length(s));
|
||||
o_form("starts with, embeds, ends with \"~\": ~, ~, ~\n", t, b.seek(t) == 0,
|
||||
b.seek(t) != -1,
|
||||
b.seek(t) != -1 && b.seek(t) + ~t == ~b);
|
||||
|
||||
t = "Summer";
|
||||
|
||||
o_form("starts with, embeds, ends with \"~\": ~, ~, ~\n", t, b_seek(b, t) == 0,
|
||||
b_seek(b, t) != -1,
|
||||
b_seek(b, t) != -1 && b_seek(b, t) + length(t) == length(s));
|
||||
o_form("starts with, embeds, ends with \"~\": ~, ~, ~\n", t, b.seek(t) == 0,
|
||||
b.seek(t) != -1,
|
||||
b.seek(t) != -1 && b.seek(t) + ~t == ~b);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
set stringA to "I felt happy because I saw the others were happy and because I knew I should feel happy, but I wasn’t really happy."
|
||||
|
||||
set string1 to "I felt happy"
|
||||
set string2 to "I should feel happy"
|
||||
set string3 to "I wasn't really happy"
|
||||
|
||||
-- Determining if the first string starts with second string
|
||||
stringA starts with string1 --> true
|
||||
|
||||
-- Determining if the first string contains the second string at any location
|
||||
stringA contains string2 --> true
|
||||
|
||||
-- Determining if the first string ends with the second string
|
||||
stringA ends with string3 --> false
|
||||
|
||||
-- Print the location of the match for part 2
|
||||
offset of string2 in stringA --> 69
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
-- Handle multiple occurrences of a string for part 2
|
||||
on offset of needle in haystack
|
||||
local needle, haystack
|
||||
|
||||
if the needle is not in the haystack then return {}
|
||||
set my text item delimiters to the needle
|
||||
script
|
||||
property N : needle's length
|
||||
property t : {1 - N} & haystack's text items
|
||||
end script
|
||||
|
||||
tell the result
|
||||
repeat with i from 2 to (its t's length) - 1
|
||||
set x to item i of its t
|
||||
set y to item (i - 1) of its t
|
||||
set item i of its t to (its N) + (x's length) + y
|
||||
end repeat
|
||||
|
||||
items 2 thru -2 of its t
|
||||
end tell
|
||||
end offset
|
||||
|
||||
offset of "happy" in stringA --> {8, 44, 83, 110}
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
-- offsets :: String -> String -> [Int]
|
||||
on offsets(needle, haystack)
|
||||
script match
|
||||
property mx : length of haystack
|
||||
property d : (length of needle) - 1
|
||||
on |λ|(x, i, xs)
|
||||
set z to d + i
|
||||
mx ≥ z and needle = text i thru z of xs
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
findIndices(match, haystack)
|
||||
end offsets
|
||||
|
||||
|
||||
-- TEST ---------------------------------------------------
|
||||
on run
|
||||
set txt to "I felt happy because I saw the others " & ¬
|
||||
"were happy and because I knew I should " & ¬
|
||||
"feel happy, but I wasn’t really happy."
|
||||
|
||||
offsets("happy", txt)
|
||||
|
||||
--> {8, 44, 83, 110}
|
||||
end run
|
||||
|
||||
|
||||
-- GENERIC -------------------------------------------------
|
||||
|
||||
-- concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
on concatMap(f, xs)
|
||||
set lng to length of xs
|
||||
set acc to {}
|
||||
tell mReturn(f)
|
||||
repeat with i from 1 to lng
|
||||
set acc to acc & (|λ|(item i of xs, i, xs))
|
||||
end repeat
|
||||
end tell
|
||||
return acc
|
||||
end concatMap
|
||||
|
||||
|
||||
-- findIndices :: (a -> Bool) -> [a] -> [Int]
|
||||
-- findIndices :: (String -> Bool) -> String -> [Int]
|
||||
on findIndices(p, xs)
|
||||
script go
|
||||
property f : mReturn(p)
|
||||
on |λ|(x, i, xs)
|
||||
if f's |λ|(x, i, xs) then
|
||||
{i}
|
||||
else
|
||||
{}
|
||||
end if
|
||||
end |λ|
|
||||
end script
|
||||
concatMap(go, xs)
|
||||
end findIndices
|
||||
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
|
||||
on mReturn(f)
|
||||
if script is class of f then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
|
@ -1,24 +1,24 @@
|
|||
import extensions.
|
||||
import extensions;
|
||||
|
||||
program =
|
||||
[
|
||||
var s := "abcd".
|
||||
public program()
|
||||
{
|
||||
var s := "abcd";
|
||||
|
||||
console printLine(s," starts with ab: ",s startingWith:"ab").
|
||||
console printLine(s," starts with cd: ",s startingWith:"cd").
|
||||
console.printLine(s," starts with ab: ",s.startingWith:"ab");
|
||||
console.printLine(s," starts with cd: ",s.startingWith:"cd");
|
||||
|
||||
console printLine(s," ends with ab: ",s endingWith:"ab").
|
||||
console printLine(s," ends with cd: ",s endingWith:"cd").
|
||||
console.printLine(s," ends with ab: ",s.endingWith:"ab");
|
||||
console.printLine(s," ends with cd: ",s.endingWith:"cd");
|
||||
|
||||
console printLine(s," contains ab: ",s containing:"ab").
|
||||
console printLine(s," contains bc: ",s containing:"bc").
|
||||
console printLine(s," contains cd: ",s containing:"cd").
|
||||
console printLine(s," contains az: ",s containing:"az").
|
||||
console.printLine(s," contains ab: ",s.containing:"ab");
|
||||
console.printLine(s," contains bc: ",s.containing:"bc");
|
||||
console.printLine(s," contains cd: ",s.containing:"cd");
|
||||
console.printLine(s," contains az: ",s.containing:"az");
|
||||
|
||||
console printLine(s," index of az: ",s indexOf:"az" at:0).
|
||||
console printLine(s," index of cd: ",s indexOf:"cd" at:0).
|
||||
console printLine(s," index of bc: ",s indexOf:"bc" at:0).
|
||||
console printLine(s," index of ab: ",s indexOf:"ab" at:0).
|
||||
console.printLine(s," index of az: ",s.indexOf(0, "az"));
|
||||
console.printLine(s," index of cd: ",s.indexOf(0, "cd"));
|
||||
console.printLine(s," index of bc: ",s.indexOf(0, "bc"));
|
||||
console.printLine(s," index of ab: ",s.indexOf(0, "ab"));
|
||||
|
||||
console readChar.
|
||||
].
|
||||
console.readChar()
|
||||
}
|
||||
|
|
|
|||
1
Task/String-matching/Factor/string-matching-1.factor
Normal file
1
Task/String-matching/Factor/string-matching-1.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
"cheesecake" "cheese" head? ! t
|
||||
1
Task/String-matching/Factor/string-matching-2.factor
Normal file
1
Task/String-matching/Factor/string-matching-2.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
"sec" "cheesecake" subseq? ! t
|
||||
1
Task/String-matching/Factor/string-matching-3.factor
Normal file
1
Task/String-matching/Factor/string-matching-3.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
"cheesecake" "cake" tail? ! t
|
||||
1
Task/String-matching/Factor/string-matching-4.factor
Normal file
1
Task/String-matching/Factor/string-matching-4.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
"sec" "cheesecake" subseq-start ! 4
|
||||
2
Task/String-matching/Factor/string-matching-5.factor
Normal file
2
Task/String-matching/Factor/string-matching-5.factor
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
USE: regexp
|
||||
"Mississippi" "iss" <regexp> all-matching-slices [ from>> ] map ! { 1 4 }
|
||||
|
|
@ -1,9 +1,7 @@
|
|||
startswith("abcd","ab") #returns true
|
||||
search("abcd","ab") #returns 1:2, indices range where string was found
|
||||
endswith("abcd","zn") #returns false
|
||||
ismatch(r"ab","abcd") #returns true where 1st arg is regex string
|
||||
julia>for r in each_match(r"ab","abab")
|
||||
startswith("abcd","ab") #returns true
|
||||
findfirst("ab", "abcd") #returns 1:2, indices range where string was found
|
||||
endswith("abcd","zn") #returns false
|
||||
match(r"ab","abcd") != Nothing #returns true where 1st arg is regex string
|
||||
for r in eachmatch(r"ab","abab")
|
||||
println(r.offset)
|
||||
end
|
||||
1
|
||||
3
|
||||
end #returns 1, then 3 matching the two starting indices where the substring was found
|
||||
|
|
|
|||
31
Task/String-matching/VBA/string-matching.vba
Normal file
31
Task/String-matching/VBA/string-matching.vba
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
Public Sub string_matching()
|
||||
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 InStr(1, sentence, word) = 1 Then
|
||||
Debug.Print "yes(1)"
|
||||
End If
|
||||
'-- A more efficient method is to test the appropriate slice
|
||||
If Len(sentence) >= Len(word) _
|
||||
And Mid(sentence, 1, Len(word)) = word Then
|
||||
Debug.Print "yes(2)"
|
||||
End If
|
||||
'-- Which is almost identical to checking for a word at the end
|
||||
If Len(sentence) >= Len(word) _
|
||||
And Mid(sentence, Len(sentence) - Len(word) + 1, Len(word)) = word Then
|
||||
Debug.Print "yes(3)"
|
||||
End If
|
||||
'-- Or sometimes you will see this, a tiny bit more efficient:
|
||||
If Len(sentence) >= Len(word) _
|
||||
And InStr(Len(sentence) - Len(word) + 1, sentence, word) Then
|
||||
Debug.Print "yes(4)"
|
||||
End If
|
||||
'-- Finding all occurences is a snap:
|
||||
r = InStr(1, sentence, word)
|
||||
Do While r <> 0
|
||||
Debug.Print r
|
||||
r = InStr(r + 1, sentence, word)
|
||||
Loop
|
||||
End Sub
|
||||
Loading…
Add table
Add a link
Reference in a new issue