Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
17
Task/Tokenize-a-string/PARI-GP/tokenize-a-string-1.parigp
Normal file
17
Task/Tokenize-a-string/PARI-GP/tokenize-a-string-1.parigp
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
\\ Tokenize a string str according to 1 character delimiter d. Return a list of tokens.
|
||||
\\ Using ssubstr() from http://rosettacode.org/wiki/Substring#PARI.2FGP
|
||||
\\ tokenize() 3/5/16 aev
|
||||
tokenize(str,d)={
|
||||
my(str=Str(str,d),vt=Vecsmall(str),d1=sasc(d),Lr=List(),sn=#str,v1,p1=1);
|
||||
for(i=p1,sn, v1=vt[i]; if(v1==d1, listput(Lr,ssubstr(str,p1,i-p1)); p1=i+1));
|
||||
return(Lr);
|
||||
}
|
||||
|
||||
{
|
||||
\\ TEST
|
||||
print(" *** Testing tokenize from Version #1:");
|
||||
print("1.", tokenize("Hello,How,Are,You,Today",","));
|
||||
\\ BOTH 2 & 3 are NOT OK!!
|
||||
print("2.",tokenize("Hello,How,Are,You,Today,",","));
|
||||
print("3.",tokenize(",Hello,,How,Are,You,Today",","));
|
||||
}
|
||||
28
Task/Tokenize-a-string/PARI-GP/tokenize-a-string-2.parigp
Normal file
28
Task/Tokenize-a-string/PARI-GP/tokenize-a-string-2.parigp
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
\\ Tokenize a string str according to 1 character delimiter d. Return a list of tokens.
|
||||
\\ Using ssubstr() from http://rosettacode.org/wiki/Substring#PARI.2FGP
|
||||
\\ stok() 3/5/16 aev
|
||||
stok(str,d)={
|
||||
my(d1c=ssubstr(d,1,1),str=Str(str,d1c),vt=Vecsmall(str),d1=sasc(d1c),
|
||||
Lr=List(),sn=#str,v1,p1=1,vo=32);
|
||||
if(sn==1, return(List(""))); if(vt[sn-1]==d1,sn--);
|
||||
for(i=1,sn, v1=vt[i];
|
||||
if(v1!=d1, vo=v1; next);
|
||||
if(vo==d1||i==1, listput(Lr,""); p1=i+1; vo=v1; next);
|
||||
if(i-p1>0, listput(Lr,ssubstr(str,p1,i-p1)); p1=i+1);
|
||||
vo=v1;
|
||||
);
|
||||
return(Lr);
|
||||
}
|
||||
|
||||
{
|
||||
\\ TEST
|
||||
print(" *** Testing stok from Version #2:");
|
||||
\\ pp - positional parameter(s)
|
||||
print("1. 5 pp: ", stok("Hello,How,Are,You,Today",","));
|
||||
print("2. 5 pp: ", stok("Hello,How,Are,You,Today,",","));
|
||||
print("3. 9 pp: ", stok(",,Hello,,,How,Are,You,Today",","));
|
||||
print("4. 6 pp: ", stok(",,,,,,",","));
|
||||
print("5. 1 pp: ", stok(",",","));
|
||||
print("6. 1 pp: ", stok("Hello-o-o??",","));
|
||||
print("7. 0 pp: ", stok("",","));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue