Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
2
Task/Reverse-words-in-a-string/00-META.yaml
Normal file
2
Task/Reverse-words-in-a-string/00-META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Reverse_words_in_a_string
|
||||
41
Task/Reverse-words-in-a-string/00-TASK.txt
Normal file
41
Task/Reverse-words-in-a-string/00-TASK.txt
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
;Task:
|
||||
Reverse the order of all tokens in each of a number of strings and display the result; the order of characters within a token should not be modified.
|
||||
|
||||
|
||||
;Example:
|
||||
<big><big><code>Hey you, Bub! </code></big></big> would be shown reversed as: <big><big><code> Bub! you, Hey </code></big></big>
|
||||
|
||||
|
||||
Tokens are any non-space characters separated by spaces (formally, white-space); the visible punctuation form part of the word within which it is located and should not be modified.
|
||||
|
||||
You may assume that there are no significant non-visible characters in the input. Multiple or superfluous spaces may be compressed into a single space.
|
||||
|
||||
Some strings have no tokens, so an empty string (or one just containing spaces) would be the result.
|
||||
|
||||
'''Display''' the strings in order (1<sup>st</sup>, 2<sup>nd</sup>, 3<sup>rd</sup>, ···), and one string per line.
|
||||
|
||||
(You can consider the ten strings as ten lines, and the tokens as words.)
|
||||
|
||||
|
||||
;Input data
|
||||
<pre>
|
||||
(ten lines within the box)
|
||||
line
|
||||
╔════════════════════════════════════════╗
|
||||
1 ║ ---------- Ice and Fire ------------ ║
|
||||
2 ║ ║ ◄─── a blank line here.
|
||||
3 ║ fire, in end will world the say Some ║
|
||||
4 ║ ice. in say Some ║
|
||||
5 ║ desire of tasted I've what From ║
|
||||
6 ║ fire. favor who those with hold I ║
|
||||
7 ║ ║ ◄─── a blank line here.
|
||||
8 ║ ... elided paragraph last ... ║
|
||||
9 ║ ║ ◄─── a blank line here.
|
||||
10 ║ Frost Robert ----------------------- ║
|
||||
╚════════════════════════════════════════╝
|
||||
</pre>
|
||||
|
||||
;Cf.
|
||||
* [[Phrase reversals]]
|
||||
<br><br>
|
||||
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
V text =
|
||||
‘---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------’
|
||||
|
||||
L(line) text.split("\n")
|
||||
print(reversed(line.split(‘ ’)).join(‘ ’))
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
# returns original phrase with the order of the words reversed #
|
||||
# a word is a sequence of non-blank characters #
|
||||
PROC reverse word order = ( STRING original phrase )STRING:
|
||||
BEGIN
|
||||
STRING words reversed := "";
|
||||
STRING separator := "";
|
||||
INT start pos := LWB original phrase;
|
||||
WHILE
|
||||
# skip leading spaces #
|
||||
WHILE IF start pos <= UPB original phrase
|
||||
THEN original phrase[ start pos ] = " "
|
||||
ELSE FALSE
|
||||
FI
|
||||
DO start pos +:= 1
|
||||
OD;
|
||||
start pos <= UPB original phrase
|
||||
DO
|
||||
# have another word, find it #
|
||||
INT end pos := start pos;
|
||||
WHILE IF end pos <= UPB original phrase
|
||||
THEN original phrase[ end pos ] /= " "
|
||||
ELSE FALSE
|
||||
FI
|
||||
DO end pos +:= 1
|
||||
OD;
|
||||
( original phrase[ start pos : end pos - 1 ] + separator ) +=: words reversed;
|
||||
separator := " ";
|
||||
start pos := end pos + 1
|
||||
OD;
|
||||
words reversed
|
||||
END # reverse word order # ;
|
||||
|
||||
# reverse the words in the lines as per the task #
|
||||
print( ( reverse word order ( "--------- Ice and Fire ------------ " ), newline ) );
|
||||
print( ( reverse word order ( " " ), newline ) );
|
||||
print( ( reverse word order ( "fire, in end will world the say Some" ), newline ) );
|
||||
print( ( reverse word order ( "ice. in say Some " ), newline ) );
|
||||
print( ( reverse word order ( "desire of tasted I've what From " ), newline ) );
|
||||
print( ( reverse word order ( "fire. favor who those with hold I " ), newline ) );
|
||||
print( ( reverse word order ( " " ), newline ) );
|
||||
print( ( reverse word order ( "... elided paragraph last ... " ), newline ) );
|
||||
print( ( reverse word order ( " " ), newline ) );
|
||||
print( ( reverse word order ( "Frost Robert -----------------------" ), newline ) )
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
# syntax: GAWK -f REVERSE_WORDS_IN_A_STRING.AWK
|
||||
BEGIN {
|
||||
text[++i] = "---------- Ice and Fire ------------"
|
||||
text[++i] = ""
|
||||
text[++i] = "fire, in end will world the say Some"
|
||||
text[++i] = "ice. in say Some"
|
||||
text[++i] = "desire of tasted I've what From"
|
||||
text[++i] = "fire. favor who those with hold I"
|
||||
text[++i] = ""
|
||||
text[++i] = "... elided paragraph last ..."
|
||||
text[++i] = ""
|
||||
text[++i] = "Frost Robert -----------------------"
|
||||
leng = i
|
||||
for (i=1; i<=leng; i++) {
|
||||
n = split(text[i],arr," ")
|
||||
for (j=n; j>0; j--) {
|
||||
printf("%s ",arr[j])
|
||||
}
|
||||
printf("\n")
|
||||
}
|
||||
exit(0)
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
PROC Reverse(CHAR ARRAY src,dst)
|
||||
BYTE i,j,k,beg,end
|
||||
|
||||
i=1 j=src(0)
|
||||
WHILE j>0
|
||||
DO
|
||||
WHILE j>0 AND src(j)=$20
|
||||
DO j==-1 OD
|
||||
IF j=0 THEN
|
||||
EXIT
|
||||
ELSE
|
||||
end=j
|
||||
FI
|
||||
|
||||
WHILE j>0 AND src(j)#$20
|
||||
DO j==-1 OD
|
||||
beg=j+1
|
||||
|
||||
IF i>1 THEN
|
||||
dst(i)=$20 i==+1
|
||||
FI
|
||||
|
||||
FOR k=beg TO end
|
||||
DO
|
||||
dst(i)=src(k) i==+1
|
||||
OD
|
||||
OD
|
||||
dst(0)=i-1
|
||||
RETURN
|
||||
|
||||
PROC Test(CHAR ARRAY src)
|
||||
CHAR ARRAY dst(40)
|
||||
|
||||
Reverse(src,dst)
|
||||
PrintE(dst)
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
Test("---------- Ice and Fire ------------")
|
||||
Test("")
|
||||
Test("fire, in end will world the say Some")
|
||||
Test("ice. in say Some")
|
||||
Test("desire of tasted I've what From")
|
||||
Test("fire. favor who those with hold I")
|
||||
Test("")
|
||||
Test("... elided paragraph last ...")
|
||||
Test("")
|
||||
Test("Frost Robert -----------------------")
|
||||
RETURN
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package Simple_Parse is
|
||||
-- a very simplistic parser, useful to split a string into words
|
||||
|
||||
function Next_Word(S: String; Point: in out Positive)
|
||||
return String;
|
||||
-- a "word" is a sequence of non-space characters
|
||||
-- if S(Point .. S'Last) holds at least one word W
|
||||
-- then Next_Word increments Point by len(W) and returns W.
|
||||
-- else Next_Word sets Point to S'Last+1 and returns ""
|
||||
|
||||
end Simple_Parse;
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package body Simple_Parse is
|
||||
|
||||
function Next_Word(S: String; Point: in out Positive) return String is
|
||||
Start: Positive := Point;
|
||||
Stop: Natural;
|
||||
begin
|
||||
while Start <= S'Last and then S(Start) = ' ' loop
|
||||
Start := Start + 1;
|
||||
end loop; -- now S(Start) is the first non-space,
|
||||
-- or Start = S'Last+1 if S is empty or space-only
|
||||
Stop := Start-1; -- now S(Start .. Stop) = ""
|
||||
while Stop < S'Last and then S(Stop+1) /= ' ' loop
|
||||
Stop := Stop + 1;
|
||||
end loop; -- now S(Stop+1) is the first sopace after Start
|
||||
-- or Stop = S'Last if there is no such space
|
||||
Point := Stop+1;
|
||||
return S(Start .. Stop);
|
||||
end Next_Word;
|
||||
|
||||
end Simple_Parse;
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
with Ada.Text_IO, Simple_Parse;
|
||||
|
||||
procedure Reverse_Words is
|
||||
|
||||
function Reverse_Words(S: String) return String is
|
||||
Cursor: Positive := S'First;
|
||||
Word: String := Simple_Parse.Next_Word(S, Cursor);
|
||||
begin
|
||||
if Word = "" then
|
||||
return "";
|
||||
else
|
||||
return Reverse_Words(S(Cursor .. S'Last)) & " " & Word;
|
||||
end if;
|
||||
end Reverse_Words;
|
||||
|
||||
use Ada.Text_IO;
|
||||
begin
|
||||
while not End_Of_File loop
|
||||
Put_Line(Reverse_Words(Get_Line)); -- poem is read from standard input
|
||||
end loop;
|
||||
end Reverse_Words;
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
integer j;
|
||||
list l, x;
|
||||
text s, t;
|
||||
|
||||
l = list("---------- Ice and Fire ------------",
|
||||
"",
|
||||
"fire, in end will world the say Some",
|
||||
"ice. in say Some",
|
||||
"desire of tasted I've what From",
|
||||
"fire. favor who those with hold I",
|
||||
"",
|
||||
"... elided paragraph last ...",
|
||||
"",
|
||||
"Frost Robert -----------------------");
|
||||
|
||||
for (, t in l) {
|
||||
file().b_affix(t).list(x, 0);
|
||||
for (j, s in x.reverse) {
|
||||
o_space(sign(j));
|
||||
o_text(s);
|
||||
}
|
||||
o_newline();
|
||||
}
|
||||
|
|
@ -0,0 +1,91 @@
|
|||
on run
|
||||
|
||||
unlines(map(reverseWords, |lines|("---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------")))
|
||||
|
||||
end run
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS ---------------------------------------------------------
|
||||
|
||||
-- reverseWords :: String -> String
|
||||
on reverseWords(str)
|
||||
unwords(|reverse|(|words|(str)))
|
||||
end reverseWords
|
||||
|
||||
-- |reverse| :: [a] -> [a]
|
||||
on |reverse|(xs)
|
||||
if class of xs is text then
|
||||
(reverse of characters of xs) as text
|
||||
else
|
||||
reverse of xs
|
||||
end if
|
||||
end |reverse|
|
||||
|
||||
-- |lines| :: Text -> [Text]
|
||||
on |lines|(str)
|
||||
splitOn(linefeed, str)
|
||||
end |lines|
|
||||
|
||||
-- |words| :: Text -> [Text]
|
||||
on |words|(str)
|
||||
splitOn(space, str)
|
||||
end |words|
|
||||
|
||||
-- ulines :: [Text] -> Text
|
||||
on unlines(lstLines)
|
||||
intercalate(linefeed, lstLines)
|
||||
end unlines
|
||||
|
||||
-- unwords :: [Text] -> Text
|
||||
on unwords(lstWords)
|
||||
intercalate(space, lstWords)
|
||||
end unwords
|
||||
|
||||
-- splitOn :: Text -> Text -> [Text]
|
||||
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
|
||||
lstParts
|
||||
end splitOn
|
||||
|
||||
-- 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
|
||||
strJoined
|
||||
end intercalate
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to lambda(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- 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
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
100 DATA"---------- ICE AND FIRE ------------"
|
||||
110 DATA" "
|
||||
120 DATA"FIRE, IN END WILL WORLD THE SAY SOME"
|
||||
130 DATA"ICE. IN SAY SOME "
|
||||
140 DATA"DESIRE OF TASTED I'VE WHAT FROM "
|
||||
150 DATA"FIRE. FAVOR WHO THOSE WITH HOLD I "
|
||||
160 DATA" "
|
||||
170 DATA"... ELIDED PARAGRAPH LAST ... "
|
||||
180 DATA" "
|
||||
190 DATA"FROST ROBERT -----------------------"
|
||||
|
||||
200 FOR L = 1 TO 10
|
||||
210 READ T$
|
||||
220 I = LEN(T$)
|
||||
240 IF I THEN GOSUB 300 : PRINT W$; : IF I THEN PRINT " "; : GOTO 240
|
||||
250 PRINT
|
||||
260 NEXT L
|
||||
270 END
|
||||
|
||||
300 W$ = ""
|
||||
310 FOR I = I TO 1 STEP -1
|
||||
320 IF MID$(T$, I, 1) = " " THEN NEXT I : RETURN
|
||||
330 FOR I = I TO 1 STEP -1
|
||||
340 C$ = MID$(T$, I, 1)
|
||||
350 IF C$ <> " " THEN W$ = C$ + W$ : NEXT I
|
||||
360 RETURN
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
text: {
|
||||
---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------}
|
||||
|
||||
reversed: map split.lines text =>
|
||||
[join.with:" " reverse split.words &]
|
||||
|
||||
print join.with:"\n" reversed
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
Data := "
|
||||
(Join`r`n
|
||||
---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------
|
||||
)"
|
||||
|
||||
Loop, Parse, Data, `n, `r
|
||||
{
|
||||
Loop, Parse, A_LoopField, % A_Space
|
||||
Line := A_LoopField " " Line
|
||||
Output .= Line "`n", Line := ""
|
||||
}
|
||||
MsgBox, % RTrim(Output, "`n")
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
source = freefile
|
||||
open (source, "m:\text.txt")
|
||||
textEnt$ = ""
|
||||
dim textSal$(size(source)*8)
|
||||
linea = 0
|
||||
|
||||
while not eof(source)
|
||||
textEnt$ = readline(source)
|
||||
linea += 1
|
||||
textSal$[linea] = textEnt$
|
||||
end while
|
||||
|
||||
for n = size(source) to 1 step -1
|
||||
print textSal$[n];
|
||||
next n
|
||||
close source
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
PRINT FNreverse("---------- Ice and Fire ------------")\
|
||||
\ 'FNreverse("")\
|
||||
\ 'FNreverse("fire, in end will world the say Some")\
|
||||
\ 'FNreverse("ice. in say Some")\
|
||||
\ 'FNreverse("desire of tasted I've what From")\
|
||||
\ 'FNreverse("fire. favor who those with hold I")\
|
||||
\ 'FNreverse("")\
|
||||
\ 'FNreverse("... elided paragraph last ...")\
|
||||
\ 'FNreverse("")\
|
||||
\ 'FNreverse("Frost Robert -----------------------")
|
||||
END
|
||||
|
||||
DEF FNreverse(s$)
|
||||
LOCAL sp%
|
||||
sp%=INSTR(s$," ")
|
||||
IF sp% THEN =FNreverse(MID$(s$,sp%+1))+" "+LEFT$(s$,sp%-1) ELSE =s$
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
PRINT REV$("---------- Ice and Fire ------------")
|
||||
PRINT
|
||||
PRINT REV$("fire, in end will world the say Some")
|
||||
PRINT REV$("ice. in say Some ")
|
||||
PRINT REV$("desire of tasted I've what From ")
|
||||
PRINT REV$("fire. favor who those with hold I ")
|
||||
PRINT
|
||||
PRINT REV$("... elided paragraph last ... ")
|
||||
PRINT
|
||||
PRINT REV$("Frost Robert -----------------------")
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
@echo off
|
||||
|
||||
::The Main Thing...
|
||||
cls
|
||||
echo.
|
||||
call :reverse "---------- Ice and Fire ------------"
|
||||
call :reverse
|
||||
call :reverse "fire, in end will world the say Some"
|
||||
call :reverse "ice. in say Some"
|
||||
call :reverse "desire of tasted I've what From"
|
||||
call :reverse "fire. favor who those with hold I"
|
||||
call :reverse
|
||||
call :reverse "... elided paragraph last ..."
|
||||
call :reverse
|
||||
call :reverse "Frost Robert -----------------------"
|
||||
echo.
|
||||
pause>nul
|
||||
exit
|
||||
::/The Main Thing...
|
||||
|
||||
::The Function...
|
||||
:reverse
|
||||
set reversed=&set word=&set str=%1
|
||||
:process
|
||||
for /f "tokens=1,*" %%A in (%str%) do (
|
||||
set str=%%B
|
||||
set word=%%A
|
||||
)
|
||||
set reversed=%word% %reversed%
|
||||
set str="%str%"
|
||||
if not %str%=="" goto process
|
||||
|
||||
echo.%reversed%
|
||||
goto :EOF
|
||||
::/The Function...
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
("---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------"
|
||||
: ?text
|
||||
& ( reverse
|
||||
= token tokens reversed
|
||||
. :?tokens
|
||||
& whl
|
||||
' ( @( !arg
|
||||
: ?token (" "|\t|\r) ?arg
|
||||
)
|
||||
& !tokens !token:?tokens
|
||||
)
|
||||
& !tokens !arg:?tokens
|
||||
& :?reversed
|
||||
& whl
|
||||
' ( !tokens:%?token %?tokens
|
||||
& " " !token !reversed:?reversed
|
||||
)
|
||||
& !tokens !reversed:?reversed
|
||||
& str$!reversed
|
||||
)
|
||||
& :?output
|
||||
& whl
|
||||
' ( @(!text:?line \n ?text)
|
||||
& !output reverse$!line \n:?output
|
||||
)
|
||||
& !output reverse$!text:?output
|
||||
& out$str$!output
|
||||
);
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
blsq ) "It is not raining"wd<-wd
|
||||
"raining not is It"
|
||||
blsq ) "ice. in say some"wd<-wd
|
||||
"some say in ice."
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
//code for a C++11 compliant compiler
|
||||
template <class BidirectionalIterator, class T>
|
||||
void block_reverse_cpp11(BidirectionalIterator first, BidirectionalIterator last, T const& separator) {
|
||||
std::reverse(first, last);
|
||||
auto block_last = first;
|
||||
do {
|
||||
using std::placeholders::_1;
|
||||
auto block_first = std::find_if_not(block_last, last,
|
||||
std::bind(std::equal_to<T>(),_1, separator));
|
||||
block_last = std::find(block_first, last, separator);
|
||||
std::reverse(block_first, block_last);
|
||||
} while(block_last != last);
|
||||
}
|
||||
|
||||
//code for a C++03 compliant compiler
|
||||
template <class BidirectionalIterator, class T>
|
||||
void block_reverse_cpp03(BidirectionalIterator first, BidirectionalIterator last, T const& separator) {
|
||||
std::reverse(first, last);
|
||||
BidirectionalIterator block_last = first;
|
||||
do {
|
||||
BidirectionalIterator block_first = std::find_if(block_last, last,
|
||||
std::bind2nd(std::not_equal_to<T>(), separator));
|
||||
block_last = std::find(block_first, last, separator);
|
||||
std::reverse(block_first, block_last);
|
||||
} while(block_last != last);
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::string str1[] =
|
||||
{
|
||||
"---------- Ice and Fire ------------",
|
||||
"",
|
||||
"fire, in end will world the say Some",
|
||||
"ice. in say Some",
|
||||
"desire of tasted I've what From",
|
||||
"fire. favor who those with hold I",
|
||||
"",
|
||||
"... elided paragraph last ...",
|
||||
"",
|
||||
"Frost Robert -----------------------"
|
||||
};
|
||||
|
||||
std::for_each(begin(str1), end(str1), [](std::string& s){
|
||||
block_reverse_cpp11(begin(s), end(s), ' ');
|
||||
std::cout << s << std::endl;
|
||||
});
|
||||
|
||||
std::for_each(begin(str1), end(str1), [](std::string& s){
|
||||
block_reverse_cpp03(begin(s), end(s), ' ');
|
||||
std::cout << s << std::endl;
|
||||
});
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
#include <string>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
string invertString( string s )
|
||||
{
|
||||
string st, tmp;
|
||||
for( string::iterator it = s.begin(); it != s.end(); it++ )
|
||||
{
|
||||
if( *it != 32 ) tmp += *it;
|
||||
else
|
||||
{
|
||||
st = " " + tmp + st;
|
||||
tmp.clear();
|
||||
}
|
||||
}
|
||||
return tmp + st;
|
||||
}
|
||||
|
||||
int main( int argc, char* argv[] )
|
||||
{
|
||||
string str[] =
|
||||
{
|
||||
"---------- Ice and Fire ------------",
|
||||
"",
|
||||
"fire, in end will world the say Some",
|
||||
"ice. in say Some",
|
||||
"desire of tasted I've what From",
|
||||
"fire. favor who those with hold I",
|
||||
"",
|
||||
"... elided paragraph last ...",
|
||||
"",
|
||||
"Frost Robert -----------------------"
|
||||
};
|
||||
for( int i = 0; i < 10; i++ )
|
||||
cout << invertString( str[i] ) << "\n";
|
||||
|
||||
cout << "\n";
|
||||
return system( "pause" );
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
using System;
|
||||
|
||||
public class ReverseWordsInString
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
string text = @"
|
||||
---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------
|
||||
";
|
||||
|
||||
foreach (string line in text.Split(Environment.NewLine)) {
|
||||
//Splits on any whitespace, not just spaces
|
||||
string[] words = line.Split(default(char[]), StringSplitOptions.RemoveEmptyEntries);
|
||||
Array.Reverse(words);
|
||||
WriteLine(string.Join(" ", words));
|
||||
}
|
||||
}
|
||||
}
|
||||
35
Task/Reverse-words-in-a-string/C/reverse-words-in-a-string.c
Normal file
35
Task/Reverse-words-in-a-string/C/reverse-words-in-a-string.c
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
|
||||
void rev_print(char *s, int n)
|
||||
{
|
||||
for (; *s && isspace(*s); s++);
|
||||
if (*s) {
|
||||
char *e;
|
||||
for (e = s; *e && !isspace(*e); e++);
|
||||
rev_print(e, 0);
|
||||
printf("%.*s%s", (int)(e - s), s, " " + n);
|
||||
}
|
||||
if (n) putchar('\n');
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char *s[] = {
|
||||
"---------- Ice and Fire ------------",
|
||||
" ",
|
||||
"fire, in end will world the say Some",
|
||||
"ice. in say Some ",
|
||||
"desire of tasted I've what From ",
|
||||
"fire. favor who those with hold I ",
|
||||
" ",
|
||||
"... elided paragraph last ... ",
|
||||
" ",
|
||||
"Frost Robert -----------------------",
|
||||
0
|
||||
};
|
||||
int i;
|
||||
for (i = 0; s[i]; i++) rev_print(s[i], 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
program-id. rev-word.
|
||||
data division.
|
||||
working-storage section.
|
||||
1 text-block.
|
||||
2 pic x(36) value "---------- Ice and Fire ------------".
|
||||
2 pic x(36) value " ".
|
||||
2 pic x(36) value "fire, in end will world the say Some".
|
||||
2 pic x(36) value "ice. in say Some ".
|
||||
2 pic x(36) value "desire of tasted I've what From ".
|
||||
2 pic x(36) value "fire. favor who those with hold I ".
|
||||
2 pic x(36) value " ".
|
||||
2 pic x(36) value "... elided paragraph last ... ".
|
||||
2 pic x(36) value " ".
|
||||
2 pic x(36) value "Frost Robert -----------------------".
|
||||
1 redefines text-block.
|
||||
2 occurs 10.
|
||||
3 text-line pic x(36).
|
||||
1 text-word.
|
||||
2 wk-len binary pic 9(4).
|
||||
2 wk-word pic x(36).
|
||||
1 word-stack.
|
||||
2 occurs 10.
|
||||
3 word-entry.
|
||||
4 word-len binary pic 9(4).
|
||||
4 word pic x(36).
|
||||
1 binary.
|
||||
2 i pic 9(4).
|
||||
2 pos pic 9(4).
|
||||
2 word-stack-ptr pic 9(4).
|
||||
|
||||
procedure division.
|
||||
perform varying i from 1 by 1
|
||||
until i > 10
|
||||
perform push-words
|
||||
perform pop-words
|
||||
end-perform
|
||||
stop run
|
||||
.
|
||||
|
||||
push-words.
|
||||
move 1 to pos
|
||||
move 0 to word-stack-ptr
|
||||
perform until pos > 36
|
||||
unstring text-line (i) delimited by all space
|
||||
into wk-word count in wk-len
|
||||
pointer pos
|
||||
end-unstring
|
||||
add 1 to word-stack-ptr
|
||||
move text-word to word-entry (word-stack-ptr)
|
||||
end-perform
|
||||
.
|
||||
|
||||
pop-words.
|
||||
perform varying word-stack-ptr from word-stack-ptr
|
||||
by -1
|
||||
until word-stack-ptr < 1
|
||||
move word-entry (word-stack-ptr) to text-word
|
||||
display wk-word (1:wk-len) space with no advancing
|
||||
end-perform
|
||||
display space
|
||||
.
|
||||
end program rev-word.
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
(def poem
|
||||
"---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------")
|
||||
|
||||
(dorun
|
||||
(map println (map #(apply str (interpose " " (reverse (re-seq #"[^\s]+" %)))) (clojure.string/split poem #"\n"))))
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
strReversed = '---------- Ice and Fire ------------\n\n
|
||||
fire, in end will world the say Some\n
|
||||
ice. in say Some\n
|
||||
desire of tasted I\'ve what From\n
|
||||
fire. favor who those with hold I\n\n
|
||||
... elided paragraph last ...\n\n
|
||||
Frost Robert -----------------------'
|
||||
|
||||
reverseString = (s) ->
|
||||
s.split('\n').map((l) -> l.split(/\s/).reverse().join ' ').join '\n'
|
||||
|
||||
console.log reverseString(strReversed)
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
(defun split-and-reverse (str)
|
||||
(labels
|
||||
((iter (s lst)
|
||||
(let ((s2 (string-trim '(#\space) s)))
|
||||
(if s2
|
||||
(let ((word-end (position #\space s2)))
|
||||
(if (and word-end (< (1+ word-end) (length s2)))
|
||||
(iter (subseq s2 (1+ word-end))
|
||||
(cons (subseq s2 0 word-end) lst))
|
||||
(cons s2 lst)))
|
||||
lst))))
|
||||
(iter str NIL)))
|
||||
|
||||
(defparameter *poem*
|
||||
"---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------")
|
||||
|
||||
(with-input-from-string (s *poem*)
|
||||
(loop for line = (read-line s NIL)
|
||||
while line
|
||||
do (format t "~{~a~#[~:; ~]~}~%" (split-and-reverse line))))
|
||||
18
Task/Reverse-words-in-a-string/D/reverse-words-in-a-string.d
Normal file
18
Task/Reverse-words-in-a-string/D/reverse-words-in-a-string.d
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
void main() {
|
||||
import std.stdio, std.string, std.range, std.algorithm;
|
||||
|
||||
immutable text =
|
||||
"---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------";
|
||||
|
||||
writefln("%(%-(%s %)\n%)",
|
||||
text.splitLines.map!(r => r.split.retro));
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
program RosettaCode_ReverseWordsInAString;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses Classes, Types, StrUtils;
|
||||
|
||||
const
|
||||
TXT =
|
||||
'---------- Ice and Fire -----------'+sLineBreak+
|
||||
sLineBreak+
|
||||
'fire, in end will world the say Some'+sLineBreak+
|
||||
'ice. in say Some'+sLineBreak+
|
||||
'desire of tasted I''ve what From'+sLineBreak+
|
||||
'fire. favor who those with hold I'+sLineBreak+
|
||||
sLineBreak+
|
||||
'... elided paragraph last ...'+sLineBreak+
|
||||
sLineBreak+
|
||||
'Frost Robert -----------------------'+sLineBreak;
|
||||
|
||||
var
|
||||
i, w: Integer;
|
||||
d: TStringDynArray;
|
||||
begin
|
||||
with TStringList.Create do
|
||||
try
|
||||
Text := TXT;
|
||||
for i := 0 to Count - 1 do
|
||||
begin
|
||||
d := SplitString(Strings[i], #32);
|
||||
Strings[i] := '';
|
||||
for w := Length(d) - 1 downto 0 do
|
||||
Strings[i] := Strings[i] + #32 + d[w];
|
||||
end;
|
||||
Writeln(Text);
|
||||
finally
|
||||
Free
|
||||
end;
|
||||
ReadLn;
|
||||
end.
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
(define S #<<
|
||||
---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------
|
||||
>>#)
|
||||
|
||||
(for-each writeln
|
||||
(for/list ((line (string-split S "\n")))
|
||||
(string-join (reverse (string-split line " ")) " ")))
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
import extensions;
|
||||
import system'routines;
|
||||
|
||||
public program()
|
||||
{
|
||||
var text := new string[]{"---------- Ice and Fire ------------",
|
||||
"",
|
||||
"fire, in end will world the say Some",
|
||||
"ice. in say Some",
|
||||
"desire of tasted I've what From",
|
||||
"fire. favor who those with hold I",
|
||||
"",
|
||||
"... elided paragraph last ...",
|
||||
"",
|
||||
"Frost Robert -----------------------"};
|
||||
|
||||
text.forEach:(line)
|
||||
{
|
||||
line.splitBy:" ".sequenceReverse().forEach:(word)
|
||||
{
|
||||
console.print(word," ")
|
||||
};
|
||||
console.writeLine()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
defmodule RC do
|
||||
def reverse_words(txt) do
|
||||
txt |> String.split("\n") # split lines
|
||||
|> Enum.map(&( # in each line
|
||||
&1 |> String.split # split words
|
||||
|> Enum.reverse # reverse words
|
||||
|> Enum.join(" "))) # rejoin words
|
||||
|> Enum.join("\n") # rejoin lines
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
txt = """
|
||||
---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------
|
||||
"""
|
||||
|
||||
IO.puts RC.reverse_words(txt)
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
reversedPoem =
|
||||
String.trim """
|
||||
---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------
|
||||
"""
|
||||
|
||||
reverseWords string =
|
||||
string |> String.words |> List.reverse |> String.join " "
|
||||
|
||||
reverseLinesWords string =
|
||||
string |> String.lines |> List.map reverseWords |> String.join "\n"
|
||||
|
||||
poem =
|
||||
reverseLinesWords reversedPoem
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
(defun reverse-words (line)
|
||||
(insert
|
||||
(format "%s\n"
|
||||
(mapconcat 'identity (reverse (split-string line)) " "))))
|
||||
|
||||
(defun reverse-lines (lines)
|
||||
(mapcar 'reverse-words lines))
|
||||
|
||||
(reverse-lines
|
||||
'("---------- Ice and Fire ------------"
|
||||
""
|
||||
"fire, in end will world the say Some"
|
||||
"ice. in say Some"
|
||||
"desire of tasted I've what From"
|
||||
"fire. favor who those with hold I"
|
||||
""
|
||||
"... elided paragraph last ..."
|
||||
""
|
||||
"Frost Robert ----------------------- "))
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
//Reverse words in a string. Nigel Galloway: July 14th., 2021
|
||||
[" ---------- Ice and Fire ------------ ";
|
||||
" ";
|
||||
" fire, in end will world the say Some ";
|
||||
" ice. in say Some ";
|
||||
" desire of tasted I've what From ";
|
||||
" fire. favour who those with hold I ";
|
||||
" ";
|
||||
" ... elided paragraph last ... ";
|
||||
" ";
|
||||
" Frost Robert ----------------------- "]|>List.map(fun n->n.Split " "|>Array.filter((<>)"")|>Array.rev|>String.concat " ")|>List.iter(printfn "%s")
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
USING: io sequences splitting ;
|
||||
IN: rosetta-code.reverse-words
|
||||
|
||||
"---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------"
|
||||
|
||||
"\n" split [ " " split reverse " " join ] map [ print ] each
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
: not-empty? dup 0 > ;
|
||||
: (reverse) parse-name not-empty? IF recurse THEN type space ;
|
||||
: reverse (reverse) cr ;
|
||||
|
||||
reverse ---------- Ice and Fire ------------
|
||||
reverse
|
||||
reverse fire, in end will world the say Some
|
||||
reverse ice. in say Some
|
||||
reverse desire of tasted I've what From
|
||||
reverse fire. favor who those with hold I
|
||||
reverse
|
||||
reverse ... elided paragraph last ...
|
||||
reverse
|
||||
reverse Frost Robert -----------------------
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
character*40 words
|
||||
character*40 reversed
|
||||
logical inblank
|
||||
ierr=0
|
||||
read (5,fmt="(a)",iostat=ierr)words
|
||||
do while (ierr.eq.0)
|
||||
inblank=.true.
|
||||
ipos=1
|
||||
do i=40,1,-1
|
||||
if(words(i:i).ne.' '.and.inblank) then
|
||||
last=i
|
||||
inblank=.false.
|
||||
end if
|
||||
if(.not.inblank.and.words(i:i).eq.' ') then
|
||||
reversed(ipos:ipos+last-i)=words(i+1:last)
|
||||
ipos=ipos+last-i+1
|
||||
inblank=.true.
|
||||
end if
|
||||
if(.not.inblank.and.i.eq.1) then
|
||||
reversed(ipos:ipos+last-1)=words(1:last)
|
||||
ipos=ipos+last
|
||||
end if
|
||||
end do
|
||||
print *,words,'=> ',reversed(1:ipos-1)
|
||||
read (5,fmt="(a)",iostat=ierr)words
|
||||
end do
|
||||
end
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Sub split (s As String, sepList As String, result() As String, removeEmpty As Boolean = False)
|
||||
If s = "" OrElse sepList = "" Then
|
||||
Redim result(0)
|
||||
result(0) = s
|
||||
Return
|
||||
End If
|
||||
Dim As Integer i, j, count = 0, empty = 0, length
|
||||
Dim As Integer position(Len(s) + 1)
|
||||
position(0) = 0
|
||||
|
||||
For i = 0 To len(s) - 1
|
||||
For j = 0 to Len(sepList) - 1
|
||||
If s[i] = sepList[j] Then
|
||||
count += 1
|
||||
position(count) = i + 1
|
||||
End If
|
||||
Next j
|
||||
Next i
|
||||
|
||||
Redim result(count)
|
||||
If count = 0 Then
|
||||
result(0) = s
|
||||
Return
|
||||
End If
|
||||
|
||||
position(count + 1) = len(s) + 1
|
||||
|
||||
For i = 1 To count + 1
|
||||
length = position(i) - position(i - 1) - 1
|
||||
result(i - 1 - empty) = Mid(s, position(i - 1) + 1, length)
|
||||
If removeEmpty Andalso CBool(length = 0) Then empty += 1
|
||||
Next
|
||||
|
||||
If empty > 0 Then Redim Preserve result(count - empty)
|
||||
End Sub
|
||||
|
||||
Dim s As String = "Hey you, Bub!"
|
||||
Dim a() As String
|
||||
split(s, " ", a(), true)
|
||||
Dim reversed As String = ""
|
||||
For i As Integer = UBound(a) To LBound(a) Step -1
|
||||
reversed += a(i)
|
||||
If i > LBound(a) Then reversed += " "
|
||||
Next
|
||||
|
||||
Print "Original String = "; s
|
||||
Print "Reversed String = "; reversed
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
lines=split["\n",
|
||||
"""---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
.. elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------"""]
|
||||
|
||||
for line = lines
|
||||
println[join[" ", reverse[split[%r/\s+/, line]]]]
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
include "NSLog.incl"
|
||||
|
||||
CFStringRef frostStr
|
||||
CFArrayRef frostArr, tempArr
|
||||
CFMutableStringRef mutStr
|
||||
NSInteger i, count
|
||||
|
||||
frostStr = @"---------- Ice and Fire ------------\n¬
|
||||
\n¬
|
||||
fire, in end will world the say Some\n¬
|
||||
ice. in say Some\n¬
|
||||
desire of tasted I've what From\n¬
|
||||
fire. favor who those with hold I\n¬
|
||||
\n¬
|
||||
… elided paragraph last …\n¬
|
||||
\n¬
|
||||
Frost Robert -----------------------\n"
|
||||
|
||||
frostArr = fn StringComponentsSeparatedByString( frostStr, @"\n" )
|
||||
count = fn ArrayCount( frostArr )
|
||||
mutStr = fn MutableStringWithCapacity( 0 )
|
||||
|
||||
for i = 0 to count - 1
|
||||
tempArr = fn StringComponentsSeparatedByString( frostArr[i], @" " )
|
||||
tempArr = fn EnumeratorAllObjects( fn ArrayReverseObjectEnumerator( tempArr ) )
|
||||
MutableStringAppendString( mutStr, fn ArrayComponentsJoinedByString( tempArr, @" " ) )
|
||||
MutableStringAppendString( mutStr, @"\n" )
|
||||
next
|
||||
|
||||
NSLog( @"%@", mutStr )
|
||||
|
||||
HandleEvents
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
Public Sub Main()
|
||||
Dim sString As New String[10] 'Array for the input text
|
||||
Dim sLine As New String[] 'Array of each word in a line
|
||||
Dim siCount0, siCount1 As Short 'Counters
|
||||
Dim sOutput, sReverse, sTemp As String 'Strings
|
||||
|
||||
sString[0] = "---------- Ice And Fire ------------" 'Input text
|
||||
sString[1] = " "
|
||||
sString[2] = "fire, in end will world the say Some"
|
||||
sString[3] = "ice. in say Some "
|
||||
sString[4] = "desire of tasted I've what From "
|
||||
sString[5] = "fire. favor who those with hold I "
|
||||
sString[6] = " "
|
||||
sString[7] = "... elided paragraph last ... "
|
||||
sString[8] = " "
|
||||
sString[9] = "Frost Robert -----------------------"
|
||||
|
||||
For siCount0 = 0 To 9 'To work through each line of input text
|
||||
If Trim(sString[siCount0]) = "" Then sString[siCount0] = " " 'If the line is all spaces then make it 1 space
|
||||
|
||||
For Each sTemp In Split(Trim(sString[siCount0]), " ") 'Split the trimmed line by spaces
|
||||
sLine.Add(sTemp) 'Add each word to the sLine array
|
||||
Next
|
||||
|
||||
For siCount1 = sLine.max DownTo 0 'Loop from the last in the sLine array to 0
|
||||
sReverse &= sLine[siCount1] & " " 'Fill sReverse with words reversed, adding a space
|
||||
Next
|
||||
|
||||
sOutput &= Trim(sReverse) & gb.NewLine 'Add the reversed words to sOutput and add a newline
|
||||
sReverse = "" 'Clear sReverse
|
||||
sLine.Clear 'Clear sLine array
|
||||
Next
|
||||
|
||||
Print sOutput 'Print the output
|
||||
|
||||
End
|
||||
|
|
@ -0,0 +1 @@
|
|||
\L<G> <U>=@{$2} $1
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// a number of strings
|
||||
var n = []string{
|
||||
"---------- Ice and Fire ------------",
|
||||
" ",
|
||||
"fire, in end will world the say Some",
|
||||
"ice. in say Some ",
|
||||
"desire of tasted I've what From ",
|
||||
"fire. favor who those with hold I ",
|
||||
" ",
|
||||
"... elided paragraph last ... ",
|
||||
" ",
|
||||
"Frost Robert -----------------------",
|
||||
}
|
||||
|
||||
func main() {
|
||||
for i, s := range n {
|
||||
t := strings.Fields(s) // tokenize
|
||||
// reverse
|
||||
last := len(t) - 1
|
||||
for j, k := range t[:len(t)/2] {
|
||||
t[j], t[last-j] = t[last-j], k
|
||||
}
|
||||
n[i] = strings.Join(t, " ")
|
||||
}
|
||||
// display result
|
||||
for _, t := range n {
|
||||
fmt.Println(t)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
def text = new StringBuilder()
|
||||
.append('---------- Ice and Fire ------------\n')
|
||||
.append(' \n')
|
||||
.append('fire, in end will world the say Some\n')
|
||||
.append('ice. in say Some \n')
|
||||
.append('desire of tasted I\'ve what From \n')
|
||||
.append('fire. favor who those with hold I \n')
|
||||
.append(' \n')
|
||||
.append('... elided paragraph last ... \n')
|
||||
.append(' \n')
|
||||
.append('Frost Robert -----------------------\n').toString()
|
||||
|
||||
text.eachLine { line ->
|
||||
println "$line --> ${line.split(' ').reverse().join(' ')}"
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
revstr :: String -> String
|
||||
revstr = unwords . reverse . words -- point-free style
|
||||
--equivalent:
|
||||
--revstr s = unwords (reverse (words s))
|
||||
|
||||
revtext :: String -> String
|
||||
revtext = unlines . map revstr . lines -- applies revstr to each line independently
|
||||
|
||||
test = revtext "---------- Ice and Fire ------------\n\
|
||||
\\n\
|
||||
\fire, in end will world the say Some\n\
|
||||
\ice. in say Some\n\
|
||||
\desire of tasted I've what From\n\
|
||||
\fire. favor who those with hold I\n\
|
||||
\\n\
|
||||
\... elided paragraph last ...\n\
|
||||
\\n\
|
||||
\Frost Robert -----------------------\n" --multiline string notation requires \ at end and start of lines, and \n to be manually input
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
procedure main()
|
||||
every write(rWords(&input))
|
||||
end
|
||||
|
||||
procedure rWords(f)
|
||||
every !f ? {
|
||||
every (s := "") := genWords() || s
|
||||
suspend s
|
||||
}
|
||||
end
|
||||
|
||||
procedure genWords()
|
||||
while w := 1(tab(upto(" \t")),tab(many(" \t"))) || " " do suspend w
|
||||
end
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
([:;@|.[:<;.1 ' ',]);._2]0 :0
|
||||
---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------
|
||||
)
|
||||
------------ Fire and Ice ----------
|
||||
|
||||
Some say the world will end in fire,
|
||||
Some say in ice.
|
||||
From what I've tasted of desire
|
||||
I hold with those who favor fire.
|
||||
|
||||
... last paragraph elided ...
|
||||
|
||||
----------------------- Robert Frost
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
echo ;:inv@|.@cut;._2 {{)n
|
||||
---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------
|
||||
}}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
public class ReverseWords {
|
||||
|
||||
static final String[] lines = {
|
||||
" ----------- Ice and Fire ----------- ",
|
||||
" ",
|
||||
" fire, in end will world the say Some ",
|
||||
" ice. in say Some ",
|
||||
" desire of tasted I've what From ",
|
||||
" fire. favor who those with hold I ",
|
||||
" ",
|
||||
" ... elided paragraph last ... ",
|
||||
" Frost Robert ----------------------- "};
|
||||
|
||||
public static void main(String[] args) {
|
||||
for (String line : lines) {
|
||||
String[] words = line.split("\\s");
|
||||
for (int i = words.length - 1; i >= 0; i--)
|
||||
System.out.printf("%s ", words[i]);
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package string;
|
||||
|
||||
import static java.util.Arrays.stream;
|
||||
|
||||
public interface ReverseWords {
|
||||
public static final String[] LINES = {
|
||||
" ----------- Ice and Fire ----------- ",
|
||||
" ",
|
||||
" fire, in end will world the say Some ",
|
||||
" ice. in say Some ",
|
||||
" desire of tasted I've what From ",
|
||||
" fire. favor who those with hold I ",
|
||||
" ",
|
||||
" ... elided paragraph last ... ",
|
||||
" Frost Robert ----------------------- "
|
||||
};
|
||||
|
||||
public static String[] reverseWords(String[] lines) {
|
||||
return stream(lines)
|
||||
.parallel()
|
||||
.map(l -> l.split("\\s"))
|
||||
.map(ws -> stream(ws)
|
||||
.parallel()
|
||||
.map(w -> " " + w)
|
||||
.reduce(
|
||||
"",
|
||||
(w1, w2) -> w2 + w1
|
||||
)
|
||||
)
|
||||
.toArray(String[]::new)
|
||||
;
|
||||
}
|
||||
|
||||
public static void main(String... arguments) {
|
||||
stream(reverseWords(LINES))
|
||||
.forEach(System.out::println)
|
||||
;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
var strReversed =
|
||||
"---------- Ice and Fire ------------\n\
|
||||
\n\
|
||||
fire, in end will world the say Some\n\
|
||||
ice. in say Some\n\
|
||||
desire of tasted I've what From\n\
|
||||
fire. favor who those with hold I\n\
|
||||
\n\
|
||||
... elided paragraph last ...\n\
|
||||
\n\
|
||||
Frost Robert -----------------------";
|
||||
|
||||
function reverseString(s) {
|
||||
return s.split('\n').map(
|
||||
function (line) {
|
||||
return line.split(/\s/).reverse().join(' ');
|
||||
}
|
||||
).join('\n');
|
||||
}
|
||||
|
||||
console.log(
|
||||
reverseString(strReversed)
|
||||
);
|
||||
|
|
@ -0,0 +1 @@
|
|||
split("[ \t\n\r]+") | reverse | join(" ")
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
$ jq -R -r -M -f reverse_words.jq IceAndFire.txt
|
||||
------------ Fire and Ice ----------
|
||||
|
||||
Some say the world will end in fire,
|
||||
Some say in ice.
|
||||
From what I've tasted of desire
|
||||
I hold with those who favor fire.
|
||||
|
||||
... last paragraph elided ...
|
||||
|
||||
----------------------- Robert Frost
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
var strReversed =
|
||||
"---------- Ice and Fire ------------\n
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
\n... elided paragraph last ...\n
|
||||
Frost Robert -----------------------";
|
||||
|
||||
function reverseString(s) {
|
||||
return s.split('\n').map(
|
||||
function (line) {
|
||||
return line.split().reverse().join(' ');
|
||||
}
|
||||
).join('\n');
|
||||
}
|
||||
|
||||
;reverseString('Hey you, Bub!');
|
||||
;strReversed;
|
||||
;reverseString(strReversed);
|
||||
|
||||
/*
|
||||
=!EXPECTSTART!=
|
||||
reverseString('Hey you, Bub!') ==> Bub! you, Hey
|
||||
strReversed ==> ---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------
|
||||
reverseString(strReversed) ==> ------------ Fire and Ice ----------
|
||||
|
||||
Some say the world will end in fire,
|
||||
Some say in ice.
|
||||
From what I've tasted of desire
|
||||
I hold with those who favor fire.
|
||||
|
||||
... last paragraph elided ...
|
||||
|
||||
----------------------- Robert Frost
|
||||
=!EXPECTEND!=
|
||||
*/
|
||||
|
|
@ -0,0 +1 @@
|
|||
revstring (str) = join(reverse(split(str, " ")), " ")
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
fun reversedWords(s: String) = s.split(" ").filter { it.isNotEmpty() }.reversed().joinToString(" ")
|
||||
|
||||
fun main() {
|
||||
val s = "Hey you, Bub!"
|
||||
println(reversedWords(s))
|
||||
println()
|
||||
val sl = listOf(
|
||||
" ---------- Ice and Fire ------------ ",
|
||||
" ",
|
||||
" fire, in end will world the say Some ",
|
||||
" ice. in say Some ",
|
||||
" desire of tasted I've what From ",
|
||||
" fire. favor who those with hold I ",
|
||||
" ",
|
||||
" ... elided paragraph last ... ",
|
||||
" ",
|
||||
" Frost Robert ----------------------- ",
|
||||
)
|
||||
sl.forEach { println(reversedWords(it)) }
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
#!/bin/ksh
|
||||
|
||||
# Reverse words in a string
|
||||
|
||||
# # Variables:
|
||||
#
|
||||
typeset -a wArr
|
||||
integer i
|
||||
|
||||
|
||||
######
|
||||
# main #
|
||||
######
|
||||
|
||||
while read -A wArr; do
|
||||
for ((i=${#wArr[@]}-1; i>=0; i--)); do
|
||||
printf "%s " "${wArr[i]}"
|
||||
done
|
||||
echo
|
||||
done << EOF
|
||||
---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
Frost Robert -----------------------
|
||||
EOF
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
1) We write a function
|
||||
|
||||
{def line_reverse
|
||||
{def line_reverse.r
|
||||
{lambda {:i :txt :length}
|
||||
{if {> :i :length}
|
||||
then
|
||||
else {br}{A2S {A.reverse! {A.get :i :txt}}}
|
||||
{line_reverse.r {+ :i 1} :txt :length}}}}
|
||||
{lambda {:txt}
|
||||
{let { {:a {line_split {:txt}}} }
|
||||
{line_reverse.r 0 :a {- {A.length :a} 1}}}} }
|
||||
-> line_reverse
|
||||
|
||||
where A2S translates an array into a sentence
|
||||
|
||||
{def A2S
|
||||
{lambda {:a}
|
||||
{if {A.empty? :a}
|
||||
then
|
||||
else {A.first :a} {A2S {A.rest :a}}}}}
|
||||
-> A2S
|
||||
|
||||
and line_split is a javascript primitive directly written in the wiki page,
|
||||
added to the dictionary and returning an array of lines
|
||||
|
||||
LAMBDATALK.DICT['line_split'] = function () {
|
||||
var args = arguments[0].split("\n");
|
||||
var str = "{A.new ";
|
||||
for (var i=0; i< args.length; i++)
|
||||
str += "{A.new " + args[i] + "} ";
|
||||
str += "}";
|
||||
return LAMBDATALK.eval_forms( str )
|
||||
};
|
||||
|
||||
2) input (from a simple text source without any presetting)
|
||||
|
||||
{def rosetta
|
||||
---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I''ve what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------
|
||||
}
|
||||
-> rosetta
|
||||
|
||||
3) calling the function:
|
||||
|
||||
{line_reverse rosetta}
|
||||
->
|
||||
|
||||
3) output
|
||||
|
||||
------------ Fire and Ice ----------
|
||||
|
||||
Some say the world will end in fire,
|
||||
Some say in ice.
|
||||
From what I''ve tasted of desire
|
||||
I hold with those who favor fire.
|
||||
|
||||
... last paragraph elided ...
|
||||
|
||||
----------------------- Robert Frost
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
for i = 1 to 10
|
||||
read string$
|
||||
print reverse$(string$)
|
||||
next
|
||||
end
|
||||
|
||||
function reverse$(string$)
|
||||
token$="*"
|
||||
while token$<>""
|
||||
i=i+1
|
||||
token$ = word$(string$, i)
|
||||
output$=token$+" "+output$
|
||||
wend
|
||||
reverse$ = trim$(output$)
|
||||
end function
|
||||
|
||||
data "---------- Ice and Fire ------------"
|
||||
data ""
|
||||
data "fire, in end will world the say Some"
|
||||
data "ice. in say Some"
|
||||
data "desire of tasted I've what From"
|
||||
data "fire. favor who those with hold I"
|
||||
data ""
|
||||
data "... elided paragraph last ..."
|
||||
data ""
|
||||
data "Frost Robert -----------------------"
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
repeat for each line txtln in fld "Fieldtxt"
|
||||
repeat with i = the number of words of txtln down to 1
|
||||
put word i of txtln & space after txtrev
|
||||
end repeat
|
||||
put cr after txtrev -- preserve line
|
||||
end repeat
|
||||
put txtrev
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
poem =
|
||||
"""
|
||||
---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------
|
||||
"""
|
||||
|
||||
reverse-words = (.split ' ') >> (.reverse!) >> (.join ' ')
|
||||
reverse-string = (.split '\n') >> (.map reverse-words) >> (.join '\n')
|
||||
reverse-string poem
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
do.until [
|
||||
make "line readlist
|
||||
print reverse :line
|
||||
] [word? :line]
|
||||
bye
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
local lines = {}
|
||||
for line in (s .. "\n"):gmatch("(.-)\n") do
|
||||
local this = {}
|
||||
for word in line:gmatch("%S+") do
|
||||
table.insert(this, 1, word)
|
||||
end
|
||||
lines[#lines + 1] = table.concat(this, " ")
|
||||
end
|
||||
print(table.concat(lines, "\n"))
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
function table.reverse(a)
|
||||
local res = {}
|
||||
for i = #a, 1, -1 do
|
||||
res[#res+1] = a[i]
|
||||
end
|
||||
return res
|
||||
end
|
||||
|
||||
function splittokens(s)
|
||||
local res = {}
|
||||
for w in s:gmatch("%S+") do
|
||||
res[#res+1] = w
|
||||
end
|
||||
return res
|
||||
end
|
||||
|
||||
for line, nl in s:gmatch("([^\n]-)(\n)") do
|
||||
print(table.concat(table.reverse(splittokens(line)), ' '))
|
||||
end
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
function testReverseWords
|
||||
testStr = {'---------- Ice and Fire ------------' ; ...
|
||||
'' ; ...
|
||||
'fire, in end will world the say Some' ; ...
|
||||
'ice. in say Some' ; ...
|
||||
'desire of tasted I''ve what From' ; ...
|
||||
'fire. favor who those with hold I' ; ...
|
||||
'' ; ...
|
||||
'... elided paragraph last ...' ; ...
|
||||
'' ; ...
|
||||
'Frost Robert -----------------------' };
|
||||
for k = 1:length(testStr)
|
||||
fprintf('%s\n', reverseWords(testStr{k}))
|
||||
end
|
||||
end
|
||||
|
||||
function strOut = reverseWords(strIn)
|
||||
strOut = strtrim(strIn);
|
||||
if ~isempty(strOut)
|
||||
% Could use strsplit() instead of textscan() in R2013a or later
|
||||
words = textscan(strOut, '%s');
|
||||
words = words{1};
|
||||
strOut = strtrim(sprintf('%s ', words{end:-1:1}));
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
-- MAXScript : Reverse words in a string : N.H. 2019
|
||||
--
|
||||
(
|
||||
text = stringstream "---------- Ice and Fire ------------\n\nfire, in end will world the say Some\nice. in say Some\ndesire of tasted I've what From\nfire. favor who those with hold I\n\n... elided paragraph last ...\n\nFrost Robert -----------------------\n"
|
||||
clearListener()
|
||||
seek text 0
|
||||
while eof text == false do
|
||||
(
|
||||
nextLine = (readLine text)
|
||||
if nextLine == "" then
|
||||
(
|
||||
print ""
|
||||
continue
|
||||
) -- end of if
|
||||
revLine = ""
|
||||
eachWord = filterString nextLine " "
|
||||
for k = eachWord.count to 1 by -1 do
|
||||
(
|
||||
revLine = revLine + eachWord[k]
|
||||
-- Only add space between words not at the end of line
|
||||
if k != 1 then revLine = revLine + " "
|
||||
) -- end of for k
|
||||
print revLine
|
||||
) -- end of while eof
|
||||
)
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
while (true) do
|
||||
input := readline("input.txt"):
|
||||
if input = 0 then break: fi:
|
||||
input := StringTools:-Trim(input): # remove leading/trailing space
|
||||
input := StringTools:-Join(ListTools:-Reverse(StringTools:-Split(input, " "))," "):
|
||||
printf("%s\n", input):
|
||||
od:
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
poem = "---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------";
|
||||
lines = StringSplit[poem, "\n"];
|
||||
wordArray = StringSplit[#] & @ lines ;
|
||||
reversedWordArray = Reverse[#] & /@ wordArray ;
|
||||
linesWithReversedWords =
|
||||
StringJoin[Riffle[#, " "]] & /@ reversedWordArray;
|
||||
finaloutput = StringJoin[Riffle[#, "\n"]] & @ linesWithReversedWords
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
lines = ["==========================================",
|
||||
"| ---------- Ice and Fire ------------ |",
|
||||
"| |",
|
||||
"| fire, in end will world the say Some |",
|
||||
"| ice. in say Some |",
|
||||
"| desire of tasted I've what From |",
|
||||
"| fire. favor who those with hold I |",
|
||||
"| |",
|
||||
"| ... elided paragraph last ... |",
|
||||
"| |",
|
||||
"| Frost Robert ----------------------- |",
|
||||
"=========================================="]
|
||||
|
||||
for line in lines
|
||||
oldLine = line.split
|
||||
newLine = []
|
||||
while oldLine
|
||||
// the line below line retains the outer box format
|
||||
newLine.push oldLine.pop
|
||||
// alternate format, replace above line with below two lines below to strip all superfluous spaces
|
||||
// word = oldLine.pop
|
||||
// if word != "" then newLine.push word
|
||||
end while
|
||||
print newLine.join
|
||||
end for
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
MODULE ReverseWords;
|
||||
|
||||
FROM STextIO IMPORT
|
||||
WriteString, WriteLn;
|
||||
FROM Strings IMPORT
|
||||
Assign, Concat, Append;
|
||||
|
||||
CONST
|
||||
NL = CHR(10);
|
||||
Sp = ' ';
|
||||
Txt = "---------- Ice and Fire -----------" + NL +
|
||||
NL +
|
||||
"fire, in end will world the say Some" + NL +
|
||||
"ice. in say Some" + NL +
|
||||
"desire of tasted I've what From" + NL +
|
||||
"fire. favor who those with hold I" + NL +
|
||||
NL +
|
||||
"... elided paragraph last ..." + NL +
|
||||
NL +
|
||||
"Frost Robert -----------------------" + NL;
|
||||
|
||||
TYPE
|
||||
String400 = ARRAY [0 .. 399] OF CHAR;
|
||||
|
||||
PROCEDURE AddWord(Source: ARRAY OF CHAR; VAR INOUT Destination: ARRAY OF CHAR);
|
||||
VAR
|
||||
R: String400;
|
||||
BEGIN
|
||||
Concat(Source, Sp, R);
|
||||
Append(Destination, R);
|
||||
Assign(R, Destination);
|
||||
END AddWord;
|
||||
|
||||
VAR
|
||||
I: CARDINAL;
|
||||
SingleWord, CurrentLine: String400;
|
||||
C: CHAR;
|
||||
|
||||
BEGIN
|
||||
SingleWord := "";
|
||||
CurrentLine := "";
|
||||
FOR I := 0 TO HIGH(Txt) DO
|
||||
C := Txt[I];
|
||||
CASE C OF
|
||||
Sp:
|
||||
AddWord(SingleWord, CurrentLine);
|
||||
SingleWord := ""; |
|
||||
NL:
|
||||
AddWord(SingleWord, CurrentLine);
|
||||
WriteString(CurrentLine);
|
||||
WriteLn;
|
||||
SingleWord := "";
|
||||
CurrentLine := ""; |
|
||||
ELSE
|
||||
Append(C, SingleWord);
|
||||
END;
|
||||
END;
|
||||
END ReverseWords.
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
def reverse_words(string)
|
||||
tokens = split(string, " ")
|
||||
if len(tokens) = 0
|
||||
return ""
|
||||
end
|
||||
|
||||
ret_str = ""
|
||||
for i in range(len(tokens) - 1, 0)
|
||||
ret_str += tokens[i] + " "
|
||||
end
|
||||
return ret_str.substring(0, len(ret_str) - 1)
|
||||
end
|
||||
|
||||
data = "---------- Ice and Fire ------------\n" +\
|
||||
" \n" +\
|
||||
"fire, in end will world the say Some\n" +\
|
||||
"ice. in say Some \n" +\
|
||||
"desire of tasted I've what From \n" +\
|
||||
"fire. favor who those with hold I \n" +\
|
||||
" \n" +\
|
||||
"... elided paragraph last ... \n" +\
|
||||
"Frost Robert -----------------------\n"
|
||||
|
||||
for line in split(data, "\n")
|
||||
println reverse_words(line)
|
||||
end
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
# Define a function to convert a list of strings to a single string.
|
||||
join is rest link (' ' eachboth link)
|
||||
|
||||
iterate (write join reverse (' ' string_split)) \
|
||||
\
|
||||
\
|
||||
'------------ Eldorado ----------' \
|
||||
'' \
|
||||
'... here omitted lines ...' \
|
||||
'' \
|
||||
'Mountains the "Over' \
|
||||
'Moon, the Of' \
|
||||
'Shadow, the of Valley the Down' \
|
||||
'ride," boldly Ride,' \
|
||||
'replied,--- shade The' \
|
||||
'Eldorado!" for seek you "If' \
|
||||
'' \
|
||||
'Poe Edgar -----------------------'
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
import strutils
|
||||
|
||||
let text = """---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------"""
|
||||
|
||||
proc reversed*[T](a: openArray[T], first, last: int): seq[T] =
|
||||
result = newSeq[T](last - first + 1)
|
||||
var x = first
|
||||
var y = last
|
||||
while x <= last:
|
||||
result[x] = a[y]
|
||||
dec(y)
|
||||
inc(x)
|
||||
|
||||
proc reversed*[T](a: openArray[T]): seq[T] =
|
||||
reversed(a, 0, a.high)
|
||||
|
||||
for line in text.splitLines():
|
||||
echo line.split(' ').reversed().join(" ")
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
#load "str.cma"
|
||||
let input = ["---------- Ice and Fire ------------";
|
||||
"";
|
||||
"fire, in end will world the say Some";
|
||||
"ice. in say Some";
|
||||
"desire of tasted I've what From";
|
||||
"fire. favor who those with hold I";
|
||||
"";
|
||||
"... elided paragraph last ...";
|
||||
"";
|
||||
"Frost Robert -----------------------"];;
|
||||
|
||||
let splitted = List.map (Str.split (Str.regexp " ")) input in
|
||||
let reversed = List.map List.rev splitted in
|
||||
let final = List.map (String.concat " ") reversed in
|
||||
List.iter print_endline final;;
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
use Collection;
|
||||
|
||||
class Reverselines {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
lines := List->New();
|
||||
lines->AddBack("---------- Ice and Fire ------------");
|
||||
lines->AddBack("");
|
||||
lines->AddBack("fire, in end will world the say Some");
|
||||
lines->AddBack("ice. in say Some");
|
||||
lines->AddBack("desire of tasted I've what From");
|
||||
lines->AddBack("fire. favor who those with hold I");
|
||||
lines->AddBack("");
|
||||
lines->AddBack("... elided paragraph last ...");
|
||||
lines->AddBack("");
|
||||
lines->AddBack("Frost Robert -----------------------");
|
||||
|
||||
lines->Rewind();
|
||||
each(i : lines) {
|
||||
words := lines->Get()->As(String)->Split(" ");
|
||||
if(words <> Nil) {
|
||||
for(j := words->Size() - 1; j > -1; j-=1;) {
|
||||
IO.Console->Print(words[j])->Print(" ");
|
||||
};
|
||||
};
|
||||
IO.Console->PrintLine();
|
||||
lines->Next();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
: revWords(s)
|
||||
s words reverse unwords ;
|
||||
|
||||
: reverseWords
|
||||
"---------- Ice and Fire ------------" revWords println
|
||||
" " revWords println
|
||||
"fire, in end will world the say Some" revWords println
|
||||
"ice. in say Some " revWords println
|
||||
"desire of tasted I've what From " revWords println
|
||||
"fire. favor who those with hold I " revWords println
|
||||
" " revWords println
|
||||
"... elided paragraph last ... " revWords println
|
||||
" " revWords println
|
||||
"Frost Robert -----------------------" revWords println ;
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
function strInv ($string) {
|
||||
|
||||
$str_inv = '' ;
|
||||
|
||||
for ($i=0,$s=count($string);$i<$s;$i++){
|
||||
$str_inv .= implode(' ',array_reverse(explode(' ',$string[$i])));
|
||||
$str_inv .= '<br>';
|
||||
}
|
||||
|
||||
return $str_inv;
|
||||
|
||||
}
|
||||
|
||||
$string[] = "---------- Ice and Fire ------------";
|
||||
$string[] = "";
|
||||
$string[] = "fire, in end will world the say Some";
|
||||
$string[] = "ice. in say Some";
|
||||
$string[] = "desire of tasted I've what From";
|
||||
$string[] = "fire. favor who those with hold I";
|
||||
$string[] = "";
|
||||
$string[] = "... elided paragraph last ...";
|
||||
$string[] = "";
|
||||
$string[] = "Frost Robert ----------------------- ";
|
||||
|
||||
|
||||
echo strInv($string);
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
------------ Fire and Ice ----------
|
||||
|
||||
Some say the world will end in fire,
|
||||
Some say in ice.
|
||||
From what I've tasted of desire
|
||||
I hold with those who favor fire.
|
||||
|
||||
... last paragraph elided ...
|
||||
|
||||
----------------------- Robert Frost
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
rev: procedure options (main); /* 5 May 2014 */
|
||||
declare (s, reverse) character (50) varying;
|
||||
declare (i, j) fixed binary;
|
||||
declare in file;
|
||||
|
||||
open file (in) title ('/REV-WRD.DAT,type(text),recsize(5> Nil) {
|
||||
for(j := words->Size() - 1; j > -1; j-=1;) {
|
||||
IO.Console->Print(words[j])->Print(" ");
|
||||
};
|
||||
};
|
||||
IO.Console->PrintLine();
|
||||
lines->Next();
|
||||
};
|
||||
}
|
||||
}0)');
|
||||
|
||||
do j = 1 to 10;
|
||||
get file (in) edit (s) (L);
|
||||
put skip list (trim(s));
|
||||
|
||||
reverse = '';
|
||||
|
||||
do while (length(s) > 0);
|
||||
s = trim(s);
|
||||
i = index(s, ' ');
|
||||
if i = 0 then
|
||||
if s ^= '' then i = length(s)+1;
|
||||
if i > 0 then reverse = substr(s, 1, i-1) || ' ' || reverse;
|
||||
if length(s) = i then s = ''; else s = substr(s, i);
|
||||
end;
|
||||
put edit ('---> ', reverse) (col(40), 2 A);
|
||||
end;
|
||||
end rev;
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
program Reverse_words(Output);
|
||||
{$H+}
|
||||
|
||||
const
|
||||
nl = chr(10); // Linefeed
|
||||
sp = chr(32); // Space
|
||||
TXT =
|
||||
'---------- Ice and Fire -----------'+nl+
|
||||
nl+
|
||||
'fire, in end will world the say Some'+nl+
|
||||
'ice. in say Some'+nl+
|
||||
'desire of tasted I''ve what From'+nl+
|
||||
'fire. favor who those with hold I'+nl+
|
||||
nl+
|
||||
'... elided paragraph last ...'+nl+
|
||||
nl+
|
||||
'Frost Robert -----------------------'+nl;
|
||||
|
||||
var
|
||||
I : integer;
|
||||
ew, lw : ansistring;
|
||||
c : char;
|
||||
|
||||
function addW : ansistring;
|
||||
var r : ansistring = '';
|
||||
begin
|
||||
r := ew + sp + lw;
|
||||
ew := '';
|
||||
addW := r
|
||||
end;
|
||||
|
||||
begin
|
||||
ew := '';
|
||||
lw := '';
|
||||
|
||||
for I := 1 to strlen(TXT) do
|
||||
begin
|
||||
c := TXT[I];
|
||||
case c of
|
||||
sp : lw := addW;
|
||||
nl : begin writeln(addW); lw := '' end;
|
||||
else ew := ew + c
|
||||
end;
|
||||
end;
|
||||
readln;
|
||||
end.
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
program reverse_words;
|
||||
{$mode objfpc}{$h+}
|
||||
uses
|
||||
SysUtils;
|
||||
|
||||
function Reverse(a: TStringArray): TStringArray;
|
||||
var
|
||||
I, J: SizeInt;
|
||||
t: Pointer;
|
||||
begin
|
||||
I := 0;
|
||||
J := High(a);
|
||||
while I < J do begin
|
||||
t := Pointer(a[I]);
|
||||
Pointer(a[I]) := Pointer(a[J]);
|
||||
Pointer(a[J]) := t;
|
||||
Inc(I);
|
||||
Dec(J);
|
||||
end;
|
||||
Result := a;
|
||||
end;
|
||||
|
||||
const
|
||||
Input =
|
||||
'---------- Ice and Fire -----------' + LineEnding +
|
||||
'' + LineEnding +
|
||||
'fire, in end will world the say Some' + LineEnding +
|
||||
'ice. in say Some' + LineEnding +
|
||||
'desire of tasted I''ve what From' + LineEnding +
|
||||
'fire. favor who those with hold I' + LineEnding +
|
||||
'' + LineEnding +
|
||||
'... elided paragraph last ...' + LineEnding +
|
||||
'' + LineEnding +
|
||||
'Frost Robert -----------------------' + LineEnding;
|
||||
var
|
||||
Line: string;
|
||||
|
||||
begin
|
||||
for Line in Input.Split([LineEnding], TStringSplitOptions.ExcludeLastEmpty) do
|
||||
WriteLn(string.Join(' ', Reverse(Line.Split([' ']))));
|
||||
end.
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
print join(" ", reverse split), "\n" for <DATA>;
|
||||
__DATA__
|
||||
---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"""
|
||||
---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------
|
||||
"""</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">test</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])))</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">))</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
include ..\Utilitys.pmt
|
||||
|
||||
"---------- Ice and Fire ------------"
|
||||
""
|
||||
"fire, in end will world the say Some"
|
||||
"ice. in say Some"
|
||||
"desire of tasted I've what From"
|
||||
"fire. favor who those with hold I"
|
||||
""
|
||||
"... elided paragraph last ..."
|
||||
""
|
||||
"Frost Robert -----------------------"
|
||||
|
||||
stklen tolist
|
||||
len for var i
|
||||
i get split reverse i set
|
||||
endfor
|
||||
len for
|
||||
get len dup if
|
||||
for get print " " print endfor
|
||||
else drop endif
|
||||
drop nl
|
||||
endfor
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(in "FireIce.txt"
|
||||
(until (eof)
|
||||
(prinl (glue " " (flip (split (line) " "))))))
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
string story = #"---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------";
|
||||
|
||||
foreach(story/"\n", string line)
|
||||
write("%s\n", reverse(line/" ")*" ");
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
Function Reverse-Words($lines) {
|
||||
$lines | foreach {
|
||||
$array = $PSItem.Split(' ')
|
||||
$array[($array.Count-1)..0] -join ' '
|
||||
}
|
||||
}
|
||||
|
||||
$lines =
|
||||
"---------- Ice and Fire ------------",
|
||||
"",
|
||||
"fire, in end will world the say Some",
|
||||
"ice. in say Some",
|
||||
"desire of tasted I've what From",
|
||||
"fire. favor who those with hold I",
|
||||
"",
|
||||
"... elided paragraph last ...",
|
||||
"",
|
||||
"Frost Robert -----------------------"
|
||||
|
||||
Reverse-Words($lines)
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
a$ = "---------- Ice and Fire ------------" +#CRLF$+
|
||||
" " +#CRLF$+
|
||||
"fire, in end will world the say Some" +#CRLF$+
|
||||
"ice. in say Some " +#CRLF$+
|
||||
"desire of tasted I've what From " +#CRLF$+
|
||||
"fire. favor who those with hold I " +#CRLF$+
|
||||
" " +#CRLF$+
|
||||
"... elided paragraph last ... " +#CRLF$+
|
||||
" " +#CRLF$+
|
||||
"Frost Robert -----------------------" +#CRLF$
|
||||
a$ = "Hey you, Bub! " +#CRLF$+#CRLF$+ a$
|
||||
|
||||
OpenConsole()
|
||||
For p1=1 To CountString(a$,#CRLF$)
|
||||
b$=StringField(a$,p1,#CRLF$) : c$=""
|
||||
For p2=1 To CountString(b$,Chr(32))+1
|
||||
c$=StringField(b$,p2,Chr(32))+Space(1)+c$
|
||||
Next
|
||||
PrintN(LSet(b$,36,Chr(32))+" ---> "+Trim(c$))
|
||||
Next
|
||||
Input()
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
text = '''\
|
||||
---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------'''
|
||||
|
||||
for line in text.split('\n'): print(' '.join(line.split()[::-1]))
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
------------ Fire and Ice ----------
|
||||
|
||||
Some say the world will end in fire,
|
||||
Some say in ice.
|
||||
From what I've tasted of desire
|
||||
I hold with those who favor fire.
|
||||
|
||||
... last paragraph elided ...
|
||||
|
||||
----------------------- Robert Frost
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
' [ $ " ---------- Ice and Fire ------------ "
|
||||
$ ""
|
||||
$ " fire, in end will world the say Some "
|
||||
$ " ice. in say Some "
|
||||
$ " desire of tasted I've what From "
|
||||
$ " fire. favor who those with hold I "
|
||||
$ ""
|
||||
$ " ... elided paragraph last ... "
|
||||
$ ""
|
||||
$ " Frost Robert ----------------------- " ]
|
||||
|
||||
witheach
|
||||
[ do nest$ reverse
|
||||
witheach
|
||||
[ echo$ sp ]
|
||||
cr ]
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
whack <- function(s) {
|
||||
paste( rev( unlist(strsplit(s, " "))), collapse=' ' ) }
|
||||
|
||||
poem <- unlist( strsplit(
|
||||
'------------ Eldorado ----------
|
||||
|
||||
... here omitted lines ...
|
||||
|
||||
Mountains the "Over
|
||||
Moon, the Of
|
||||
Shadow, the of Valley the Down
|
||||
ride," boldly Ride,
|
||||
replied,--- shade The
|
||||
Eldorado!" for seek you "If
|
||||
|
||||
Poe Edgar -----------------------', "\n"))
|
||||
|
||||
for (line in poem) cat( whack(line), "\n" )
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
> `{` <- function(s) rev(unlist(strsplit(s, " ")))
|
||||
> {"one two three four five"}
|
||||
[1] "five" "four" "three" "two" "one"
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
/*REXX program reverses the order of tokens in a string (but not the letters).*/
|
||||
@.=; @.1 = "---------- Ice and Fire ------------"
|
||||
@.2 = ' '
|
||||
@.3 = "fire, in end will world the say Some"
|
||||
@.4 = "ice. in say Some"
|
||||
@.5 = "desire of tasted I've what From"
|
||||
@.6 = "fire. favor who those with hold I"
|
||||
@.7 = ' '
|
||||
@.8 = "... elided paragraph last ..."
|
||||
@.9 = ' '
|
||||
@.10 = "Frost Robert -----------------------"
|
||||
|
||||
do j=1 while @.j\=='' /*process each of the 10 lines of poem.*/
|
||||
$= /*nullify the $ string (the new line)*/
|
||||
do k=1 for words(@.j) /*process each word in a @.j string.*/
|
||||
$=word(@.j,k) $ /*prepend a word to the new line ($). */
|
||||
end /*k*/ /* [↑] we could do this another way. */
|
||||
|
||||
say $ /*display the newly constructed line. */
|
||||
end /*j*/ /*stick a fork in it, we're all done. */
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
/*REXX program reverses the order of tokens in a string (but not the letters).*/
|
||||
@.=; @.1 = "---------- Ice and Fire ------------"
|
||||
@.2 = ' '
|
||||
@.3 = "fire, in end will world the say Some"
|
||||
@.4 = "ice. in say Some"
|
||||
@.5 = "desire of tasted I've what From"
|
||||
@.6 = "fire. favor who those with hold I"
|
||||
@.7 = ' '
|
||||
@.8 = "... elided paragraph last ..."
|
||||
@.9 = ' '
|
||||
@.10 = "Frost Robert -----------------------"
|
||||
|
||||
do j=1 while @.j\=='' /*process each of the 10 lines of poem.*/
|
||||
$= /*nullify the $ string (the new line)*/
|
||||
do k=words(@.j) to 1 by -1 /*process each word in a @.j string.*/
|
||||
$=$ word(@.j,k) /*append a word to the new line ($). */
|
||||
end /*k*/ /* [↑] process last word to first word*/
|
||||
|
||||
say $ /*display the newly constructed line. */
|
||||
end /*j*/ /*stick a fork in it, we're all done. */
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
#lang racket/base
|
||||
|
||||
(require racket/string)
|
||||
|
||||
(define (split-reverse str)
|
||||
(string-join
|
||||
(reverse
|
||||
(string-split str))))
|
||||
|
||||
(define poem
|
||||
"---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------")
|
||||
|
||||
|
||||
(let ([poem-port (open-input-string poem)])
|
||||
(let loop ([l (read-line poem-port)])
|
||||
(unless (eof-object? l)
|
||||
(begin (displayln (split-reverse l))
|
||||
(loop (read-line poem-port))))))
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
#lang sweet-exp racket/base
|
||||
require racket/string
|
||||
|
||||
define split-reverse(str)
|
||||
string-join $ reverse $ string-split str
|
||||
|
||||
define poem
|
||||
"---------- Ice and Fire ------------
|
||||
|
||||
fire, in end will world the say Some
|
||||
ice. in say Some
|
||||
desire of tasted I've what From
|
||||
fire. favor who those with hold I
|
||||
|
||||
... elided paragraph last ...
|
||||
|
||||
Frost Robert -----------------------"
|
||||
|
||||
let
|
||||
\\
|
||||
poem-port $ open-input-string poem
|
||||
let loop
|
||||
\\
|
||||
l $ read-line poem-port
|
||||
unless eof-object?(l)
|
||||
begin
|
||||
displayln split-reverse(l)
|
||||
loop read-line(poem-port)
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue