2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,14 +1,31 @@
|
|||
;Task:
|
||||
Write a program that solves the [http://c2.com/cgi/wiki?OddWordProblem odd word problem] with the restrictions given below.
|
||||
|
||||
'''Description''': You are promised an input stream consisting of English letters and punctuations. It is guaranteed that
|
||||
* the words (sequence of consecutive letters) are delimited by one and only one punctuation; that
|
||||
* the stream will begin with a word; that
|
||||
* the words will be at least one letter long; and that
|
||||
* a full stop (.) appears after, and only after, the last word.
|
||||
|
||||
For example, <code>what,is,the;meaning,of:life.</code> is such a stream with six words. Your task is to reverse the letters in every other word while leaving punctuations intact, producing e.g. "what,si,the;gninaem,of:efil.", while observing the following restrictions:
|
||||
;Description:
|
||||
You are promised an input stream consisting of English letters and punctuations.
|
||||
|
||||
It is guaranteed that:
|
||||
* the words (sequence of consecutive letters) are delimited by one and only one punctuation,
|
||||
* the stream will begin with a word,
|
||||
* the words will be at least one letter long, and
|
||||
* a full stop (a period, [<b>.</b>]) appears after, and only after, the last word.
|
||||
|
||||
|
||||
;Example:
|
||||
A stream with six words:
|
||||
:: <big><code>what,is,the;meaning,of:life.</code></big>
|
||||
|
||||
|
||||
The task is to reverse the letters in every other word while leaving punctuations intact, producing:
|
||||
:: <big><code>what,si,the;gninaem,of:efil.</code></big>
|
||||
while observing the following restrictions:
|
||||
# Only I/O allowed is reading or writing one character at a time, which means: no reading in a string, no peeking ahead, no pushing characters back into the stream, and no storing characters in a global variable for later use;
|
||||
# You '''are not''' to explicitly save characters in a collection data structure, such as arrays, strings, hash tables, etc, for later reversal;
|
||||
# You '''are''' allowed to use recursions, closures, continuations, threads, coroutines, etc., even if their use implies the storage of multiple characters.
|
||||
# You '''are''' allowed to use recursions, closures, continuations, threads, co-routines, etc., even if their use implies the storage of multiple characters.
|
||||
|
||||
'''Test case''': work on both the "life" example given above, and the text <code>we,are;not,in,kansas;any,more.</code>
|
||||
|
||||
;Test cases:
|
||||
Work on both the "life" example given above, and also the text:
|
||||
:: <big><code>we,are;not,in,kansas;any,more.</code></big>
|
||||
<br><br>
|
||||
|
|
|
|||
26
Task/Odd-word-problem/Elixir/odd-word-problem.elixir
Normal file
26
Task/Odd-word-problem/Elixir/odd-word-problem.elixir
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
defmodule Odd_word do
|
||||
def handle(s, false, i, o) when ((s >= "a" and s <= "z") or (s >= "A" and s <= "Z")) do
|
||||
o.(s)
|
||||
handle(i.(), false, i, o)
|
||||
end
|
||||
def handle(s, t, i, o) when ((s >= "a" and s <= "z") or (s >= "A" and s <= "Z")) do
|
||||
d = handle(i.(), :rec, i, o)
|
||||
o.(s)
|
||||
if t == true, do: handle(d, t, i, o), else: d
|
||||
end
|
||||
def handle(s, :rec, _, _), do: s
|
||||
def handle(?., _, _, o), do: o.(?.); :done
|
||||
def handle(:eof, _, _, _), do: :done
|
||||
def handle(s, t, i, o) do
|
||||
o.(s)
|
||||
handle(i.(), not t, i, o)
|
||||
end
|
||||
|
||||
def main do
|
||||
i = fn() -> IO.getn("") end
|
||||
o = fn(s) -> IO.write(s) end
|
||||
handle(i.(), false, i, o)
|
||||
end
|
||||
end
|
||||
|
||||
Odd_word.main
|
||||
41
Task/Odd-word-problem/Fortran/odd-word-problem.f
Normal file
41
Task/Odd-word-problem/Fortran/odd-word-problem.f
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
MODULE ELUDOM !Uses the call stack for auxiliary storage.
|
||||
INTEGER MSG,INF !I/O unit numbers.
|
||||
LOGICAL DEFER !To stumble, or not to stumble.
|
||||
CONTAINS
|
||||
CHARACTER*1 RECURSIVE FUNCTION GET(IN) !Returns one character, going forwards.
|
||||
INTEGER IN !The input file.
|
||||
CHARACTER*1 C !The single character to be read therefrom.
|
||||
READ (IN,1,ADVANCE="NO",EOR=3,END=4) C !Thus. Not advancing to the next record.
|
||||
1 FORMAT (A1,$) !For output, no advance to the next line either.
|
||||
2 IF (("A"<=C .AND. C<="Z").OR.("a"<=C .AND. C<="z")) THEN !Unsafe for EBCDIC.
|
||||
IF (DEFER) THEN !Are we to reverse the current text?
|
||||
GET = GET(IN) !Yes. Go for the next letter.
|
||||
WRITE (MSG,1) C !And now, backing out, reveal the letter at this level.
|
||||
RETURN !Retreat another level.
|
||||
END IF !Thus passing back the ending non-letter that was encountered.
|
||||
ELSE !And if we've encountered a non-letter,
|
||||
DEFER = .NOT. DEFER !Then our backwardness flips.
|
||||
END IF !Enough inspection of C.
|
||||
3 GET = C !Pass it back.
|
||||
RETURN !And we're done.
|
||||
4 GET = CHAR(0) !Reserving this for end-of-file.
|
||||
END FUNCTION GET!That was strange.
|
||||
END MODULE ELUDOM !But as per the specification.
|
||||
|
||||
PROGRAM CONFUSED !Just so.
|
||||
USE ELUDOM !Forwards? Backwards?
|
||||
CHARACTER*1 C !A scratchpad for multiple inspections.
|
||||
MSG = 6 !Standard output.
|
||||
INF = 10 !This will do.
|
||||
OPEN (INF,NAME = "Confused.txt",STATUS="OLD",ACTION="READ") !Go for the file.
|
||||
|
||||
Chew through the input. A full stop marks the end.
|
||||
10 DEFER = .FALSE. !Start off going forwards.
|
||||
11 C = GET(INF) !Get some character from file INF.
|
||||
IF (ICHAR(C).LE.0) STOP !Perhaps end-of-file is reported.
|
||||
IF (C.NE." ") WRITE (MSG,12) C !Otherwise, write it. A blank for end-of-record.
|
||||
12 FORMAT (A1,$) !Obviously, not finishing the line each time.
|
||||
IF (C.NE.".") GO TO 11 !And if not a full stop, do it again.
|
||||
WRITE (MSG,"('')") !End the line of output.
|
||||
GO TO 10 !And have another go.
|
||||
END !That was confusing.
|
||||
|
|
@ -1,28 +1,30 @@
|
|||
/*REXX program solves the odd word problem by only using byte input/output.*/
|
||||
iFID_ = 'ODDWORD.IN' /*Note: numeric suffix is added later.*/
|
||||
oFID_ = 'ODDWORD.' /* " " " " " " */
|
||||
/*REXX program solves the odd word problem by only using byte input/output. */
|
||||
iFID_ = 'ODDWORD.IN' /*Note: numeric suffix is added later.*/
|
||||
oFID_ = 'ODDWORD.' /* " " " " " " */
|
||||
|
||||
do case=1 for 2; #=0 /*#: is the number of characters read.*/
|
||||
iFID=iFID_ || case /*read ODDWORD.IN1 or ODDWORD.IN2 */
|
||||
oFID=oFID_ || case /*write ODDWORD.1 o r ODDWORD.2 */
|
||||
say; say; say '════════ reading file: ' iFID "════════"
|
||||
do case=1 for 2; #=0 /*#: is the number of characters read.*/
|
||||
iFID=iFID_ || case /*read ODDWORD.IN1 or ODDWORD.IN2 */
|
||||
oFID=oFID_ || case /*write ODDWORD.1 o r ODDWORD.2 */
|
||||
say; say; say '════════ reading file: ' iFID "════════" /* ◄■■■■■■■■■ optional. */
|
||||
|
||||
do until x==. /* [↓] perform DO loop for odd words.*/
|
||||
do until \isMix(x); call readChar; call writeChar; end
|
||||
if x==. then leave /*is this end─of─sentence? (full stop)*/
|
||||
call readLetters; punctuation_location=#
|
||||
do j=#-1 by -1; call readChar j
|
||||
if \isMix(x) then leave; call writeChar
|
||||
end /*j*/ /* [↑] perform for the "even" words.*/
|
||||
call readLetters; call writeChar; #=punctuation_location
|
||||
end /*until x ···*/
|
||||
end /*case*/ /* [↑] process both of the input files*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────one─liner subroutines─────────────────────*/
|
||||
isMix: return datatype(arg(1),'M') /*return 1 if arg is a letter.*/
|
||||
readLetters: do until \isMix(x); call readChar; end; return
|
||||
writeChar: call charout ,x; call charout oFID,x; return
|
||||
/*──────────────────────────────────readChar subroutine───────────────────────*/
|
||||
readChar: if arg(1)=='' then do; x=charin(iFID); #=#+1; end /*read next char*/
|
||||
else x=charin(iFID, arg(1)) /* " specific "*/
|
||||
return
|
||||
do until x==. /* [↓] perform for "odd" words.*/
|
||||
do until \isMix(x); /* [↓] perform until punct found.*/
|
||||
call readChar; call writeChar /*read and write a letter. */
|
||||
end /*until ¬isMix(x)*/ /* [↑] keep reading " " */
|
||||
if x==. then leave /*is this the end─of─sentence ? */
|
||||
call readLetters; punct=# /*save the location of punctuation*/
|
||||
do j=#-1 by -1; call readChar j /*read previous word (backwards). */
|
||||
if \isMix(x) then leave; call writeChar /*Found punctuation? Then leave. */
|
||||
end /*j*/ /* [↑] perform for "even" words.*/
|
||||
call readLetters; call writeChar; #=punct /*read/write letters; new location*/
|
||||
end /*until x==.*/
|
||||
end /*case*/ /* [↑] process both input files. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
isMix: return datatype( arg(1), 'M') /*return 1 if argument is a letter.*/
|
||||
readLetters: do until \isMix(x); call readChar; end; return
|
||||
writeChar: call charout , x; call charout oFID, x; return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
readChar: if arg(1)=='' then do; x=charin(iFID); #=#+1; end /*read the next char*/
|
||||
else x=charin(iFID, arg(1)) /* " specific " */
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue