2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -8,6 +8,11 @@ In this task display a substring:
* starting from a known character within the string and of <tt>m</tt> length;
* starting from a known substring within the string and of <tt>m</tt> length.
<br>
If the program uses UTF-8 or UTF-16, it must work on any valid Unicode code point,
whether in the Basic Multilingual Plane or above it.
The program must reference logical characters (code points), not 8-bit code units for UTF-8 or 16-bit code units for UTF-16. Programs for other encodings (such as 8-bit ASCII, or EUC-JP) are not required to handle all Unicode characters.
The program must reference logical characters (code points), not 8-bit code units for UTF-8 or 16-bit code units for UTF-16.
Programs for other encodings (such as 8-bit ASCII, or EUC-JP) are not required to handle all Unicode characters.
<br><br>

View file

@ -0,0 +1,101 @@
-- SUBSTRING PRIMITIVES
-- take :: Int -> Text -> Text
on take(n, s)
text 1 thru n of s
end take
-- drop :: Int -> Text -> Text
on drop(n, s)
text (n + 1) thru -1 of s
end drop
-- breakOn :: Text -> Text -> (Text, Text)
on breakOn(strPattern, s)
set {dlm, my text item delimiters} to {my text item delimiters, strPattern}
set lstParts to text items of s
set my text item delimiters to dlm
{item 1 of lstParts, strPattern & (item 2 of lstParts)}
end breakOn
-- init :: Text -> Text
on init(s)
if length of s > 0 then
text 1 thru -2 of s
else
missing value
end if
end init
-- TEST
on run
set str to "一二三四五六七八九十"
set legends to {¬
"from n in, of n length", ¬
"from n in, up to end", ¬
"all but last", ¬
"from matching char, of m length", ¬
"from matching string, of m length"}
set parts to {¬
take(3, drop(4, str)), ¬
drop(3, str), ¬
init(str), ¬
take(3, item 2 of breakOn("五", str)), ¬
take(4, item 2 of breakOn("六七", str))}
script tabulate
property strPad : " "
on lambda(l, r)
l & drop(length of l, strPad) & r
end lambda
end script
linefeed & intercalate(linefeed, ¬
zipWith(tabulate, ¬
legends, parts)) & linefeed
end run
-- GENERIC LIBRARY FUNCTIONS FOR FORMATTING RESULTS
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
on zipWith(f, xs, ys)
set lng to length of xs
if lng is not length of ys then
missing value
else
tell mReturn(f)
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, item i of ys)
end repeat
return lst
end tell
end if
end zipWith
-- intercalate :: Text -> [Text] -> Text
on intercalate(strText, lstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set my text item delimiters to dlm
return strJoined
end intercalate
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property lambda : f
end script
end if
end mReturn

View file

@ -0,0 +1,12 @@
10 LET A$="abcdefghijklmnopqrstuvwxyz": LET la=LEN A$
20 LET n=10: LET m=7
30 PRINT A$(n TO n+m-1)
40 PRINT A$(n TO )
50 PRINT A$( TO la-1)
60 FOR i=1 TO la
70 IF A$(i)="g" THEN PRINT A$(i TO i+m-1): LET i=la
80 NEXT i
90 LET B$="ijk": LET lb=LEN b$
100 FOR i=1 TO la-lb+1
110 IF A$(i TO i+lb-1)=B$ THEN PRINT A$(i TO i+m-1): LET i=la-lb+1
120 NEXT i

View file

@ -0,0 +1,56 @@
identification division.
program-id. substring.
environment division.
configuration section.
repository.
function all intrinsic.
data division.
working-storage section.
01 original.
05 value "this is a string".
01 starting pic 99 value 3.
01 width pic 99 value 8.
01 pos pic 99.
01 ender pic 99.
01 looking pic 99.
01 indicator pic x.
88 found value high-value when set to false is low-value.
01 look-for pic x(8).
procedure division.
substring-main.
display "Original |" original "|, n = " starting " m = " width
display original(starting : width)
display original(starting :)
display original(1 : length(original) - 1)
move "a" to look-for
move 1 to looking
perform find-position
if found
display original(pos : width)
end-if
move "is a st" to look-for
move length(trim(look-for)) to looking
perform find-position
if found
display original(pos : width)
end-if
goback.
find-position.
set found to false
compute ender = length(original) - looking
perform varying pos from 1 by 1 until pos > ender
if original(pos : looking) equal look-for then
set found to true
exit perform
end-if
end-perform
.
end program substring.

View file

@ -0,0 +1,17 @@
#import system.
#import extensions.
#symbol program =
[
#var s := "0123456789".
#var n := 3.
#var m := 2.
#var c := #51.
#var z := "345".
console writeLine:(s Substring:m &at:n).
console writeLine:(s Substring:(s length - n) &at:n).
console writeLine:(s Substring:(s length - 1) &at:0).
console writeLine:(s Substring:m &at:(s indexOf:c &at:0)).
console writeLine:(s Substring:m &at:(s indexOf:z &at:0)).
].

View file

@ -3,3 +3,10 @@ String.slice(s, 2, 3) #=> "cde"
String.slice(s, 1..3) #=> "bcd"
String.slice(s, -3, 2) #=> "fg"
String.slice(s, 3..-1) #=> "defgh"
# UTF-8
s = "αβγδεζηθ"
String.slice(s, 2, 3) #=> "γδε"
String.slice(s, 1..3) #=> "βγδ"
String.slice(s, -3, 2) #=> "ζη"
String.slice(s, 3..-1) #=> "δεζηθ"

View file

@ -0,0 +1,57 @@
(function () {
'use strict';
// take :: Int -> Text -> Text
function take(n, s) {
return s.substr(0, n);
}
// drop :: Int -> Text -> Text
function drop(n, s) {
return s.substr(n);
}
// init :: Text -> Text
function init(s) {
var n = s.length;
return (n > 0 ? s.substr(0, n - 1) : undefined);
}
// breakOn :: Text -> Text -> (Text, Text)
function breakOn(strPattern, s) {
var i = s.indexOf(strPattern);
return i === -1 ? [strPattern, ''] : [s.substr(0, i), s.substr(i)];
}
var str = '一二三四五六七八九十';
return JSON.stringify({
'from n in, of m length': (function (n, m) {
return take(m, drop(n, str));
})(4, 3),
'from n in, up to end' :(function (n) {
return drop(n, str);
})(3),
'all but last' : init(str),
'from matching char, of m length' : (function (pattern, s, n) {
return take(n, breakOn(pattern, s)[1]);
})('五', str, 3),
'from matching string, of m length':(function (pattern, s, n) {
return take(n, breakOn(pattern, s)[1]);
})('六七', str, 4)
}, null, 2);
})();

View file

@ -0,0 +1,7 @@
{
"from n in, of m length": "五六七",
"from n in, up to end": "四五六七八九十",
"all but last": "一二三四五六七八九",
"from matching char, of m length": "五六七",
"from matching string, of m length": "六七八九"
}

View file

@ -0,0 +1,23 @@
\\ Returns the substring of string str specified by the start position s and length n.
\\ If n=0 then to the end of str.
\\ ssubstr() 3/5/16 aev
ssubstr(str,s=1,n=0)={
my(vt=Vecsmall(str),ve,vr,vtn=#str,n1);
if(vtn==0,return(""));
if(s<1||s>vtn,return(str));
n1=vtn-s+1; if(n==0,n=n1); if(n>n1,n=n1);
ve=vector(n,z,z-1+s); vr=vecextract(vt,ve); return(Strchr(vr));
}
{\\ TEST
my(s="ABCDEFG",ns=#s);
print(" *** Testing ssubstr():");
print("1.",ssubstr(s,2,3));
print("2.",ssubstr(s));
print("3.",ssubstr(s,,ns-1));
print("4.",ssubstr(s,2));
print("5.",ssubstr(s,,4));
print("6.",ssubstr(s,0,4));
print("7.",ssubstr(s,3,7));
print("8.|",ssubstr("",1,4),"|");
}

View file

@ -1,37 +1,32 @@
/*REXX program demonstrates various ways to extract substrings from a string of characters. */
s='abcdefghijk'; n=4; m=3 /*define some REXX constants (string, index, length of string).*/
say 'original string='s /* [↑] M can be zero (which indicates a null string). */
say '1'
u=substr(s,n,m) /*starting from N characters in and of M length. */
/*REXX program demonstrates various ways to extract substrings from a string of characters.*/
$='abcdefghijk'; n=4; m=3 /*define some constants: string, index, length of string. */
say 'original string='$ /* [↑] M can be zero (which indicates a null string).*/
L=length($) /*the length of the $ string (in bytes or characters).*/
say center(1,30,'') /*show a centered title for the 1st task requirement. */
u=substr($, n, m) /*start from N characters in and of M length. */
say u
parse var s =(n) a +(m) /*another way of doing the above by using the PARSE instruction*/
parse var $ =(n) a +(m) /*an alternate method by using the PARSE instruction. */
say a
say '2'
u=substr(s,n) /*starting from N characters in, up to the end-of-string. */
say center(2,30,'') /*show a centered title for the 2nd task requirement. */
u=substr($,n) /*start from N characters in, up to the end-of-string. */
say u
parse var s =(n) a /*another way of doing the above by using the PARSE instruction*/
parse var $ =(n) a /*an alternate method by using the PARSE instruction. */
say a
say '3'
u=substr(s,1,length(s)-1) /*OK: the whole string except the last character. */
say center(3,30,'') /*show a centered title for the 3rd task requirement. */
u=substr($, 1, L-1) /*OK: the entire string except the last character. */
say u
v=substr(s,1,max(0,length(s)-1)) /*better: this version handles the case of a null string. */
v=substr($, 1, max(0, L-1) ) /*better: this version handles the case of a null string. */
say v
L=length(s) - 1
parse var s a +(L) /*another way of doing the above by using the PARSE instruction*/
lm=L-1
parse var $ a +(lm) /*an alternate method by using the PARSE instruction. */
say a
say '4'
u=substr(s,pos('g',s),m) /*starting from a known char within the string & of M length.*/
say center(4,30,'') /*show a centered title for the 4th task requirement. */
u=substr($,pos('g',$), m) /*start from a known char within the string of length M. */
say u
parse var s 'g' a +(m) /*another way of doing the above by using the PARSE instruction*/
parse var $ 'g' a +(m) /*an alternate method by using the PARSE instruction. */
say a
say '5'
u=substr(s,pos('def',s),m) /*starting from a known substr within the string & of M length.*/
say center(5,30,'') /*show a centered title for the 5th task requirement. */
u=substr($,pos('def',$),m) /*start from a known substr within the string of length M.*/
say u
parse var s 'def' a +(m) /*another way of doing the above by using the PARSE instruction*/
say a
/*stick a fork in it sir, we're all done and Bob's your uncle. */
parse var $ 'def' a +(m) /*an alternate method by using the PARSE instruction. */
say a /*stick a fork in it, we're all done and Bob's your uncle.*/