2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,5 +1,6 @@
|
|||
Separate the string "Hello,How,Are,You,Today" by commas into an array (or list) so that each element of it stores a different word.
|
||||
Display the words to the 'user', in the simplest manner possible, separated by a period.
|
||||
Display the words to the 'user', in the simplest manner possible,
|
||||
separated by a period.
|
||||
To simplify, you may display a trailing period.
|
||||
|
||||
'''''Related tasks:'''''
|
||||
|
|
|
|||
|
|
@ -1,2 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- Simple
|
||||
note: String manipulation
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
on run {}
|
||||
intercalate(".", splitOn(",", "Hello,How,Are,You,Today"))
|
||||
end run
|
||||
|
||||
|
||||
-- splitOn :: String -> String -> [String]
|
||||
on splitOn(strDelim, strMain)
|
||||
set {dlm, my text item delimiters} to {my text item delimiters, strDelim}
|
||||
set lstParts to text items of strMain
|
||||
set my text item delimiters to dlm
|
||||
return lstParts
|
||||
end splitOn
|
||||
|
||||
-- intercalate :: String -> [String] -> String
|
||||
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
|
||||
30
Task/Tokenize-a-string/COBOL/tokenize-a-string.cobol
Normal file
30
Task/Tokenize-a-string/COBOL/tokenize-a-string.cobol
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
identification division.
|
||||
program-id. tokenize.
|
||||
|
||||
environment division.
|
||||
configuration section.
|
||||
repository.
|
||||
function all intrinsic.
|
||||
|
||||
data division.
|
||||
working-storage section.
|
||||
01 period constant as ".".
|
||||
01 cmma constant as ",".
|
||||
|
||||
01 start-with.
|
||||
05 value "Hello,How,Are,You,Today".
|
||||
|
||||
01 items.
|
||||
05 item pic x(6) occurs 5 times.
|
||||
|
||||
procedure division.
|
||||
tokenize-main.
|
||||
unstring start-with delimited by cmma
|
||||
into item(1) item(2) item(3) item(4) item(5)
|
||||
|
||||
display trim(item(1)) period trim(item(2)) period
|
||||
trim(item(3)) period trim(item(4)) period
|
||||
trim(item(5))
|
||||
|
||||
goback.
|
||||
end program tokenize.
|
||||
12
Task/Tokenize-a-string/Elena/tokenize-a-string.elena
Normal file
12
Task/Tokenize-a-string/Elena/tokenize-a-string.elena
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#import system.
|
||||
#import system'routines.
|
||||
|
||||
#symbol program =
|
||||
[
|
||||
#var string := "Hello,How,Are,You,Today".
|
||||
|
||||
string split &by:"," run &each:s
|
||||
[
|
||||
console writeLiteral:(s + ".").
|
||||
].
|
||||
].
|
||||
|
|
@ -1,16 +1,5 @@
|
|||
splitBy :: (a -> Bool) -> [a] -> [[a]]
|
||||
splitBy _ [] = []
|
||||
splitBy f list = first : splitBy f (dropWhile f rest) where
|
||||
(first, rest) = break f list
|
||||
{-# OPTIONS_GHC -XOverloadedStrings #-}
|
||||
import Data.Text (splitOn,intercalate)
|
||||
import qualified Data.Text.IO as T (putStrLn)
|
||||
|
||||
splitRegex :: Regex -> String -> [String]
|
||||
|
||||
joinWith :: [a] -> [[a]] -> [a]
|
||||
joinWith d xs = concat $ List.intersperse d xs
|
||||
-- "concat $ intersperse" can be replaced with "intercalate" from the Data.List in GHC 6.8 and later
|
||||
|
||||
putStrLn $ joinWith "." $ splitBy (== ',') $ "Hello,How,Are,You,Today"
|
||||
|
||||
-- using regular expression to split:
|
||||
import Text.Regex
|
||||
putStrLn $ joinWith "." $ splitRegex (mkRegex ",") $ "Hello,How,Are,You,Today"
|
||||
main = T.putStrLn . intercalate "." $ splitOn "," "Hello,How,Are,You,Today"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,16 @@
|
|||
*Main> mapM_ putStrLn $ takeWhile (not.null) $ unfoldr (Just . second(drop 1). break (==',')) "Hello,How,Are,You,Today"
|
||||
Hello
|
||||
How
|
||||
Are
|
||||
You
|
||||
Today
|
||||
splitBy :: (a -> Bool) -> [a] -> [[a]]
|
||||
splitBy _ [] = []
|
||||
splitBy f list = first : splitBy f (dropWhile f rest) where
|
||||
(first, rest) = break f list
|
||||
|
||||
splitRegex :: Regex -> String -> [String]
|
||||
|
||||
joinWith :: [a] -> [[a]] -> [a]
|
||||
joinWith d xs = concat $ List.intersperse d xs
|
||||
-- "concat $ intersperse" can be replaced with "intercalate" from the Data.List in GHC 6.8 and later
|
||||
|
||||
putStrLn $ joinWith "." $ splitBy (== ',') $ "Hello,How,Are,You,Today"
|
||||
|
||||
-- using regular expression to split:
|
||||
import Text.Regex
|
||||
putStrLn $ joinWith "." $ splitRegex (mkRegex ",") $ "Hello,How,Are,You,Today"
|
||||
|
|
|
|||
6
Task/Tokenize-a-string/Haskell/tokenize-a-string-3.hs
Normal file
6
Task/Tokenize-a-string/Haskell/tokenize-a-string-3.hs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
*Main> mapM_ putStrLn $ takeWhile (not.null) $ unfoldr (Just . second(drop 1). break (==',')) "Hello,How,Are,You,Today"
|
||||
Hello
|
||||
How
|
||||
Are
|
||||
You
|
||||
Today
|
||||
|
|
@ -1,2 +1,2 @@
|
|||
val input = "Hello,How,Are,You,Today"
|
||||
println(input.splitBy(",").join("."))
|
||||
println(input.split(',').join("."))
|
||||
|
|
|
|||
9
Task/Tokenize-a-string/Lua/tokenize-a-string.lua
Normal file
9
Task/Tokenize-a-string/Lua/tokenize-a-string.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
function string:split (sep)
|
||||
local sep, fields = sep or ":", {}
|
||||
local pattern = string.format("([^%s]+)", sep)
|
||||
self:gsub(pattern, function(c) fields[#fields+1] = c end)
|
||||
return fields
|
||||
end
|
||||
|
||||
local str = "Hello,How,Are,You,Today"
|
||||
print(table.concat(str:split(","), "."))
|
||||
|
|
@ -1,7 +1,2 @@
|
|||
let rec split_char sep str =
|
||||
try
|
||||
let i = String.index str sep in
|
||||
String.sub str 0 i ::
|
||||
split_char sep (String.sub str (i+1) (String.length str - i - 1))
|
||||
with Not_found ->
|
||||
[str]
|
||||
let words = String.split_on_char ',' "Hello,How,Are,You,Today" in
|
||||
String.concat "." words
|
||||
|
|
|
|||
|
|
@ -1,18 +1,10 @@
|
|||
(* [try .. with] structures break tail-recursion,
|
||||
so we externalise it in a sub-function *)
|
||||
let string_index str c =
|
||||
try Some(String.index str c)
|
||||
with Not_found -> None
|
||||
|
||||
let split_char sep str =
|
||||
let rec aux acc str =
|
||||
match string_index str sep with
|
||||
| Some i ->
|
||||
let this = String.sub str 0 i
|
||||
and next = String.sub str (i+1) (String.length str - i - 1) in
|
||||
aux (this::acc) next
|
||||
| None ->
|
||||
List.rev(str::acc)
|
||||
in
|
||||
aux [] str
|
||||
;;
|
||||
let split_on_char sep s =
|
||||
let r = ref [] in
|
||||
let j = ref (String.length s) in
|
||||
for i = String.length s - 1 downto 0 do
|
||||
if s.[i] = sep then begin
|
||||
r := String.sub s (i + 1) (!j - i - 1) :: !r;
|
||||
j := i
|
||||
end
|
||||
done;
|
||||
String.sub s 0 !j :: !r
|
||||
|
|
|
|||
|
|
@ -1,10 +1,8 @@
|
|||
bundle Default {
|
||||
class Parse {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
tokens := "Hello,How,Are,You,Today"->Split(",");
|
||||
each(i : tokens) {
|
||||
tokens[i]->PrintLine();
|
||||
};
|
||||
}
|
||||
class Parse {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
tokens := "Hello,How,Are,You,Today"->Split(",");
|
||||
each(i : tokens) {
|
||||
tokens[i]->PrintLine();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
17
Task/Tokenize-a-string/PARI-GP/tokenize-a-string-1.pari
Normal file
17
Task/Tokenize-a-string/PARI-GP/tokenize-a-string-1.pari
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.pari
Normal file
28
Task/Tokenize-a-string/PARI-GP/tokenize-a-string-2.pari
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("",","));
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
"Hello,How,Are,You,Today", ",,Hello,,Goodbye,," | ForEach-Object {($_.Split(',',[StringSplitOptions]::RemoveEmptyEntries)) -join "."}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
/*REXX program seperates a string of comma-delimited words, and echoes. */
|
||||
/*REXX program separates a string of comma-delimited words, and echoes. */
|
||||
sss = 'Hello,How,Are,You,Today' /*words seperated by commas (,). */
|
||||
say 'input string =' sss /*display the original string. */
|
||||
new=sss /*make a copy of the string. */
|
||||
|
|
|
|||
5
Task/Tokenize-a-string/Rust/tokenize-a-string.rust
Normal file
5
Task/Tokenize-a-string/Rust/tokenize-a-string.rust
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
fn main() {
|
||||
let s = "Hello,How,Are,You,Today";
|
||||
let tokens: Vec<&str> = s.split(",").collect();
|
||||
println!("{}", tokens.join("."));
|
||||
}
|
||||
2
Task/Tokenize-a-string/S-lang/tokenize-a-string.slang
Normal file
2
Task/Tokenize-a-string/S-lang/tokenize-a-string.slang
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
variable a = strchop("Hello,How,Are,You,Today", ',', 0);
|
||||
print(strjoin(a, "."));
|
||||
13
Task/Tokenize-a-string/UNIX-Shell/tokenize-a-string-4.sh
Normal file
13
Task/Tokenize-a-string/UNIX-Shell/tokenize-a-string-4.sh
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
string1="Hello,How,Are,You,Today"
|
||||
elements_quantity=$(echo $string1|tr "," "\n"|wc -l)
|
||||
|
||||
present_element=1
|
||||
while [ $present_element -le $elements_quantity ];do
|
||||
echo $string1|cut -d "," -f $present_element|tr -d "\n"
|
||||
if [ $present_element -lt $elements_quantity ];then echo -n ".";fi
|
||||
present_element=$(expr $present_element + 1)
|
||||
done
|
||||
echo
|
||||
|
||||
# or to cheat
|
||||
echo "Hello,How,Are,You,Today"|tr "," "."
|
||||
Loading…
Add table
Add a link
Reference in a new issue