Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
14
Task/Odd-word-problem/00DESCRIPTION
Normal file
14
Task/Odd-word-problem/00DESCRIPTION
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
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:
|
||||
# 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.
|
||||
|
||||
'''Test case''': work on both the "life" example given above, and the text <code>we,are;not,in,kansas;any,more.</code>
|
||||
75
Task/Odd-word-problem/ALGOL-68/odd-word-problem.alg
Normal file
75
Task/Odd-word-problem/ALGOL-68/odd-word-problem.alg
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
# recursively reverses the current word in the input and returns the #
|
||||
# the character that followed it #
|
||||
# "ch" should contain the first letter of the word on entry and will be #
|
||||
# updated to the punctuation following the word on exit #
|
||||
PROC reverse word = ( REF CHAR ch )VOID:
|
||||
BEGIN
|
||||
|
||||
CHAR next ch;
|
||||
|
||||
read( ( next ch ) );
|
||||
|
||||
IF ( next ch <= "Z" AND next ch >= "A" )
|
||||
OR ( next ch <= "z" AND next ch >= "a" )
|
||||
THEN
|
||||
reverse word( next ch )
|
||||
FI;
|
||||
|
||||
print( ( ch ) );
|
||||
|
||||
ch := next ch
|
||||
|
||||
END; # reverse word #
|
||||
|
||||
|
||||
|
||||
# recursively prints the current word in the input and returns the #
|
||||
# character that followed it #
|
||||
# "ch" should contain the first letter of the word on entry and will be #
|
||||
# updated to the punctuation following the word on exit #
|
||||
PROC normal word = ( REF CHAR ch )VOID:
|
||||
BEGIN
|
||||
|
||||
print( ( ch ) );
|
||||
read ( ( ch ) );
|
||||
|
||||
IF ( ch <= "Z" AND ch >= "A" )
|
||||
OR ( ch <= "z" AND ch >= "a" )
|
||||
THEN
|
||||
normal word( ch )
|
||||
FI
|
||||
|
||||
END; # normal word #
|
||||
|
||||
|
||||
|
||||
# read and print words and punctuation from the input stream, reversing #
|
||||
# every second word #
|
||||
PROC reverse every other word = VOID:
|
||||
BEGIN
|
||||
|
||||
CHAR ch;
|
||||
|
||||
read( ( ch ) );
|
||||
|
||||
WHILE
|
||||
ch /= "."
|
||||
DO
|
||||
normal word( ch );
|
||||
IF ch /= "."
|
||||
THEN
|
||||
print( ( ch ) );
|
||||
read ( ( ch ) );
|
||||
reverse word( ch )
|
||||
FI
|
||||
OD;
|
||||
|
||||
print( ( ch ) )
|
||||
|
||||
END; # reverse every other word #
|
||||
|
||||
|
||||
|
||||
main: (
|
||||
reverse every other word
|
||||
)
|
||||
55
Task/Odd-word-problem/Ada/odd-word-problem.ada
Normal file
55
Task/Odd-word-problem/Ada/odd-word-problem.ada
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Odd_Word_Problem is
|
||||
|
||||
use Ada.Text_IO; -- Get, Put, and Look_Ahead
|
||||
|
||||
function Current return Character is
|
||||
-- reads the current input character, without consuming it
|
||||
End_Of_Line: Boolean;
|
||||
C: Character;
|
||||
begin
|
||||
Look_Ahead(C, End_Of_Line);
|
||||
if End_Of_Line then
|
||||
raise Constraint_Error with "end of line before the terminating '.'";
|
||||
end if;
|
||||
return C;
|
||||
end Current;
|
||||
|
||||
procedure Skip is
|
||||
-- consumes the current input character
|
||||
C: Character;
|
||||
begin
|
||||
Get(C);
|
||||
end Skip;
|
||||
|
||||
function Is_Alpha(Ch: Character) return Boolean is
|
||||
begin
|
||||
return (Ch in 'a' .. 'z') or (Ch in 'A' .. 'Z');
|
||||
end Is_Alpha;
|
||||
|
||||
procedure Odd_Word(C: Character) is
|
||||
begin
|
||||
if Is_Alpha(C) then
|
||||
Skip;
|
||||
Odd_Word(Current);
|
||||
Put(C);
|
||||
end if;
|
||||
end Odd_Word;
|
||||
|
||||
begin -- Odd_Word_Problem
|
||||
Put(Current);
|
||||
while Is_Alpha(Current) loop -- read an even word
|
||||
Skip;
|
||||
Put(Current);
|
||||
end loop;
|
||||
if Current /= '.' then -- read an odd word
|
||||
Skip;
|
||||
Odd_Word(Current);
|
||||
Put(Current);
|
||||
if Current /= '.' then -- read the remaining words
|
||||
Skip;
|
||||
Odd_Word_Problem;
|
||||
end if;
|
||||
end if;
|
||||
end Odd_Word_Problem;
|
||||
33
Task/Odd-word-problem/Bracmat/odd-word-problem.bracmat
Normal file
33
Task/Odd-word-problem/Bracmat/odd-word-problem.bracmat
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
( ( odd-word
|
||||
= dothis doother forward backward
|
||||
. ( forward
|
||||
= ch
|
||||
. fil$:?ch
|
||||
& put$!ch
|
||||
& ( low$!ch:~<a:~>z&forward$
|
||||
| !ch:~"."
|
||||
)
|
||||
)
|
||||
& ( backward
|
||||
= ch
|
||||
. fil$:?ch
|
||||
& ( low$!ch:~<a:~>z
|
||||
& backward$() (put$!ch&) { This reduces to the return value of backwards$()}
|
||||
| '(.put$($ch)&$ch:~".") { Macro, evaluates to a function with actual ch. }
|
||||
)
|
||||
)
|
||||
& fil$(!arg,r)
|
||||
& ((=forward$).(=(backward$)$))
|
||||
: (?dothis.?doother)
|
||||
& whl
|
||||
' ( !(dothis.)
|
||||
& (!doother.!dothis):(?dothis.?doother)
|
||||
)
|
||||
& (fil$(,SET,-1)|) { This is how a file is closed: seek the impossible. }
|
||||
)
|
||||
& put$("what,is,the;meaning,of:life.","life.txt",NEW)
|
||||
& put$("we,are;not,in,kansas;any,more.","kansas.txt",NEW)
|
||||
& odd-word$"life.txt"
|
||||
& put$\n
|
||||
& odd-word$"kansas.txt" { Real file, as Bracmat cannot read a single character from stdin. }
|
||||
);
|
||||
36
Task/Odd-word-problem/C++/odd-word-problem.cpp
Normal file
36
Task/Odd-word-problem/C++/odd-word-problem.cpp
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#include <iostream>
|
||||
#include <cctype>
|
||||
#include <functional>
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool odd()
|
||||
{
|
||||
function<void ()> prev = []{};
|
||||
while(true) {
|
||||
int c = cin.get();
|
||||
if (!isalpha(c)) {
|
||||
prev();
|
||||
cout.put(c);
|
||||
return c != '.';
|
||||
}
|
||||
prev = [=] { cout.put(c); prev(); };
|
||||
}
|
||||
}
|
||||
|
||||
bool even()
|
||||
{
|
||||
while(true) {
|
||||
int c;
|
||||
cout.put(c = cin.get());
|
||||
if (!isalpha(c)) return c != '.';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
bool e = false;
|
||||
while( e ? odd() : even() ) e = !e;
|
||||
return 0;
|
||||
}
|
||||
32
Task/Odd-word-problem/C/odd-word-problem.c
Normal file
32
Task/Odd-word-problem/C/odd-word-problem.c
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
|
||||
int do_char(int odd, void (*f)(void))
|
||||
{
|
||||
int c = getchar();
|
||||
|
||||
void write_out(void) {
|
||||
putchar(c);
|
||||
if (f) f();
|
||||
}
|
||||
|
||||
if (!odd) putchar(c);
|
||||
|
||||
if (isalpha(c))
|
||||
return do_char(odd, write_out);
|
||||
|
||||
if (odd) {
|
||||
if (f) f();
|
||||
putchar(c);
|
||||
}
|
||||
|
||||
return c != '.';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int i = 1;
|
||||
while (do_char(i = !i, 0));
|
||||
|
||||
return 0;
|
||||
}
|
||||
26
Task/Odd-word-problem/Clojure/odd-word-problem-1.clj
Normal file
26
Task/Odd-word-problem/Clojure/odd-word-problem-1.clj
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
(defn next-char []
|
||||
(char (.read *in*)))
|
||||
|
||||
(defn forward []
|
||||
(let [ch (next-char)]
|
||||
(print ch)
|
||||
(if (Character/isLetter ch)
|
||||
(forward)
|
||||
(not= ch \.))))
|
||||
|
||||
(defn backward []
|
||||
(let [ch (next-char)]
|
||||
(if (Character/isLetter ch)
|
||||
(let [result (backward)]
|
||||
(print ch)
|
||||
result)
|
||||
(fn [] (print ch) (not= ch \.)))) )
|
||||
|
||||
(defn odd-word [s]
|
||||
(with-in-str s
|
||||
(loop [forward? true]
|
||||
(when (if forward?
|
||||
(forward)
|
||||
((backward)))
|
||||
(recur (not forward?)))) )
|
||||
(println))
|
||||
6
Task/Odd-word-problem/Clojure/odd-word-problem-2.clj
Normal file
6
Task/Odd-word-problem/Clojure/odd-word-problem-2.clj
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
user=> (odd-word "what,is,the;meaning,of:life.")
|
||||
what,si,the;gninaem,of:efil.
|
||||
nil
|
||||
user=> (odd-word "we,are;not,in,kansas;any,more.")
|
||||
we,era;not,ni,kansas;yna,more.
|
||||
nil
|
||||
36
Task/Odd-word-problem/CoffeeScript/odd-word-problem-1.coffee
Normal file
36
Task/Odd-word-problem/CoffeeScript/odd-word-problem-1.coffee
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
isWordChar = (c) -> /^\w/.test c
|
||||
isLastChar = (c) -> c is '.'
|
||||
|
||||
# Pass a function that returns an input character and one that outputs a
|
||||
# character. JS platforms' ideas of single-character I/O vary widely, but this
|
||||
# abstraction is adaptable to most or all.
|
||||
oddWord = (get, put) ->
|
||||
forwardWord = ->
|
||||
loop
|
||||
# No magic here; buffer then immediately output.
|
||||
c = get()
|
||||
put(c)
|
||||
unless isWordChar(c)
|
||||
return not isLastChar(c)
|
||||
|
||||
# NB: (->) is a CoffeeScript idiom for no-op.
|
||||
reverseWord = (outputPending = (->)) ->
|
||||
c = get()
|
||||
if isWordChar(c)
|
||||
# Continue word.
|
||||
# Tell recursive call to output this character, then any previously
|
||||
# pending characters, after the next word character, if any, has
|
||||
# been output.
|
||||
reverseWord ->
|
||||
put(c)
|
||||
outputPending()
|
||||
else
|
||||
# Word is done.
|
||||
# Output previously pending characters, then this punctuation.
|
||||
outputPending()
|
||||
put(c)
|
||||
return not isLastChar(c)
|
||||
|
||||
# Alternate between forward and reverse until one or the other reports that
|
||||
# the end-of-input mark has been reached (causing a return of false).
|
||||
continue while forwardWord() and reverseWord()
|
||||
23
Task/Odd-word-problem/CoffeeScript/odd-word-problem-2.coffee
Normal file
23
Task/Odd-word-problem/CoffeeScript/odd-word-problem-2.coffee
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
isWordChar = (c) -> /^\w/.test c
|
||||
isLastChar = (c) -> c is '.'
|
||||
|
||||
oddWord = (get, put) ->
|
||||
forwardWord = ->
|
||||
loop
|
||||
c = get()
|
||||
put(c)
|
||||
unless isWordChar(c)
|
||||
return not isLastChar(c)
|
||||
|
||||
reverseWord = (outputPending = (->)) ->
|
||||
c = get()
|
||||
if isWordChar(c)
|
||||
reverseWord ->
|
||||
put(c)
|
||||
outputPending()
|
||||
else
|
||||
outputPending()
|
||||
put(c)
|
||||
return not isLastChar(c)
|
||||
|
||||
continue while forwardWord() and reverseWord()
|
||||
27
Task/Odd-word-problem/CoffeeScript/odd-word-problem-3.coffee
Normal file
27
Task/Odd-word-problem/CoffeeScript/odd-word-problem-3.coffee
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
# Redefine as necessary for target platform.
|
||||
println = (z) -> console.log z
|
||||
|
||||
testData = [
|
||||
[
|
||||
"what,is,the;meaning,of:life."
|
||||
"what,si,the;gninaem,of:efil."
|
||||
]
|
||||
[
|
||||
"we,are;not,in,kansas;any,more."
|
||||
"we,era;not,ni,kansas;yna,more."
|
||||
]
|
||||
]
|
||||
|
||||
results = for [testString, expectedResult] in testData
|
||||
# This test machinery uses string buffers for input and output. If your JS
|
||||
# platform sports single-character I/O, by all means, adapt to taste.
|
||||
getCursor = 0
|
||||
putBuffer = ""
|
||||
get = ->
|
||||
testString.charAt getCursor++
|
||||
put = (c) ->
|
||||
putBuffer += c
|
||||
oddWord(get,put)
|
||||
[testString, expectedResult, putBuffer, putBuffer is expectedResult]
|
||||
|
||||
println result for result in results
|
||||
19
Task/Odd-word-problem/Common-Lisp/odd-word-problem-1.lisp
Normal file
19
Task/Odd-word-problem/Common-Lisp/odd-word-problem-1.lisp
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
(defun odd-word (s)
|
||||
(let ((stream (make-string-input-stream s)))
|
||||
(loop for forwardp = t then (not forwardp)
|
||||
while (if forwardp
|
||||
(forward stream)
|
||||
(funcall (backward stream)))) ))
|
||||
|
||||
(defun forward (stream)
|
||||
(let ((ch (read-char stream)))
|
||||
(write-char ch)
|
||||
(if (alpha-char-p ch)
|
||||
(forward stream)
|
||||
(char/= ch #\.))))
|
||||
|
||||
(defun backward (stream)
|
||||
(let ((ch (read-char stream)))
|
||||
(if (alpha-char-p ch)
|
||||
(prog1 (backward stream) (write-char ch))
|
||||
#'(lambda () (write-char ch) (char/= ch #\.)))) )
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
? (odd-word "what,is,the;meaning,of:life.")
|
||||
what,si,the;gninaem,of:efil.
|
||||
NIL
|
||||
? (odd-word "we,are;not,in,kansas;any,more.")
|
||||
we,era;not,ni,kansas;yna,more.
|
||||
NIL
|
||||
19
Task/Odd-word-problem/D/odd-word-problem.d
Normal file
19
Task/Odd-word-problem/D/odd-word-problem.d
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
bool doChar(in bool odd, in void delegate() nothrow f=null) nothrow {
|
||||
import core.stdc.stdio, std.ascii;
|
||||
|
||||
immutable int c = getchar;
|
||||
if (!odd)
|
||||
c.putchar;
|
||||
if (c.isAlpha)
|
||||
return doChar(odd, { c.putchar; if (f) f(); });
|
||||
if (odd) {
|
||||
if (f) f();
|
||||
c.putchar;
|
||||
}
|
||||
return c != '.';
|
||||
}
|
||||
|
||||
void main() {
|
||||
bool i = true;
|
||||
while (doChar(i = !i)) {}
|
||||
}
|
||||
16
Task/Odd-word-problem/Erlang/odd-word-problem.erl
Normal file
16
Task/Odd-word-problem/Erlang/odd-word-problem.erl
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
handle(S, false, I, O) when (((S >= $a) and (S =< $z)) or ((S >= $A) and (S =< $Z))) ->
|
||||
O(S),
|
||||
handle(I(), false, I, O);
|
||||
handle(S, T, I, O) when (((S >= $a) and (S =< $z)) or ((S >= $A) and (S =< $Z))) ->
|
||||
D = handle(I(), rec, I, O),
|
||||
O(S),
|
||||
case T of true -> handle(D, T, I, O); _ -> D end;
|
||||
handle(S, rec, _, _) -> S;
|
||||
handle($., _, _, O) -> O($.), done;
|
||||
handle(eof, _, _, _) -> done;
|
||||
handle(S, T, I, O) -> O(S), handle(I(), not T, I, O).
|
||||
|
||||
main([]) ->
|
||||
I = fun() -> hd(io:get_chars([], 1)) end,
|
||||
O = fun(S) -> io:put_chars([S]) end,
|
||||
handle(I(), false, I, O).
|
||||
4
Task/Odd-word-problem/FALSE/odd-word-problem.false
Normal file
4
Task/Odd-word-problem/FALSE/odd-word-problem.false
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
[$$$$'.=\',=|\';=|\':=|~[^s;!\,]?]s: {recursive reading}
|
||||
[s;!$'.=~[,^f;!]?]r: {reverse words}
|
||||
[[$$$$'.=\',=|\';=|\':=|~][,^]#$'.=~[,^r;!]?]f: {forward words}
|
||||
^f;!, {start}
|
||||
62
Task/Odd-word-problem/Factor/odd-word-problem.factor
Normal file
62
Task/Odd-word-problem/Factor/odd-word-problem.factor
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
USING: continuations kernel io io.streams.string locals unicode.categories ;
|
||||
IN: rosetta.odd-word
|
||||
|
||||
<PRIVATE
|
||||
! Save current continuation.
|
||||
: savecc ( -- continuation/f )
|
||||
[ ] callcc1 ; inline
|
||||
|
||||
! Jump back to continuation, where savecc will return f.
|
||||
: jump-back ( continuation -- )
|
||||
f swap continue-with ; inline
|
||||
PRIVATE>
|
||||
|
||||
:: read-odd-word ( -- )
|
||||
f :> first-continuation!
|
||||
f :> last-continuation!
|
||||
f :> reverse!
|
||||
! Read characters. Loop until end of stream.
|
||||
[ read1 dup ] [
|
||||
dup Letter? [
|
||||
! This character is a letter.
|
||||
reverse [
|
||||
! Odd word: Write letters in reverse order.
|
||||
last-continuation savecc dup [
|
||||
last-continuation!
|
||||
2drop ! Drop letter and previous continuation.
|
||||
] [
|
||||
! After jump: print letters in reverse.
|
||||
drop ! Drop f.
|
||||
swap write1 ! Write letter.
|
||||
jump-back ! Follow chain of continuations.
|
||||
] if
|
||||
] [
|
||||
! Even word: Write letters immediately.
|
||||
write1
|
||||
] if
|
||||
] [
|
||||
! This character is punctuation.
|
||||
reverse [
|
||||
! End odd word. Fix trampoline, follow chain of continuations
|
||||
! (to print letters in reverse), then bounce off trampoline.
|
||||
savecc dup [
|
||||
first-continuation!
|
||||
last-continuation jump-back
|
||||
] [ drop ] if
|
||||
write1 ! Write punctuation.
|
||||
f reverse! ! Begin even word.
|
||||
] [
|
||||
write1 ! Write punctuation.
|
||||
t reverse! ! Begin odd word.
|
||||
! Create trampoline to bounce to (future) first-continuation.
|
||||
savecc dup [
|
||||
last-continuation!
|
||||
] [ drop first-continuation jump-back ] if
|
||||
] if
|
||||
] if
|
||||
] while
|
||||
! Drop f from read1. Then print a cosmetic newline.
|
||||
drop nl ;
|
||||
|
||||
: odd-word ( string -- )
|
||||
[ read-odd-word ] with-string-reader ;
|
||||
6
Task/Odd-word-problem/Forth/odd-word-problem.fth
Normal file
6
Task/Odd-word-problem/Forth/odd-word-problem.fth
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
: word? dup [char] . <> over bl <> and ;
|
||||
: ?quit dup [char] . = if emit quit then ;
|
||||
: eatbl begin dup bl = while drop key repeat ?quit ;
|
||||
: even begin word? while emit key repeat ;
|
||||
: odd word? if key recurse swap emit then ;
|
||||
: main cr key eatbl begin even eatbl space odd eatbl space again ;
|
||||
52
Task/Odd-word-problem/Go/odd-word-problem-1.go
Normal file
52
Task/Odd-word-problem/Go/odd-word-problem-1.go
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
func main() {
|
||||
owp(os.Stdout, bytes.NewBufferString("what,is,the;meaning,of:life."))
|
||||
fmt.Println()
|
||||
owp(os.Stdout, bytes.NewBufferString("we,are;not,in,kansas;any,more."))
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
func owp(dst io.Writer, src io.Reader) {
|
||||
byte_in := func () byte {
|
||||
bs := make([]byte, 1)
|
||||
src.Read(bs)
|
||||
return bs[0]
|
||||
}
|
||||
byte_out := func (b byte) { dst.Write([]byte{b}) }
|
||||
var odd func() byte
|
||||
odd = func() byte {
|
||||
s := byte_in()
|
||||
if unicode.IsPunct(rune(s)) {
|
||||
return s
|
||||
}
|
||||
b := odd()
|
||||
byte_out(s)
|
||||
return b
|
||||
}
|
||||
for {
|
||||
for {
|
||||
b := byte_in()
|
||||
byte_out(b)
|
||||
if b == '.' {
|
||||
return
|
||||
}
|
||||
if unicode.IsPunct(rune(b)) {
|
||||
break
|
||||
}
|
||||
}
|
||||
b := odd()
|
||||
byte_out(b)
|
||||
if b == '.' {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
52
Task/Odd-word-problem/Go/odd-word-problem-2.go
Normal file
52
Task/Odd-word-problem/Go/odd-word-problem-2.go
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
func main() {
|
||||
owp(os.Stdout, bytes.NewBufferString("what,is,the;meaning,of:life."))
|
||||
fmt.Println()
|
||||
owp(os.Stdout, bytes.NewBufferString("we,are;not,in,kansas;any,more."))
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
func owp(dst io.Writer, src io.Reader) {
|
||||
byte_in := func () byte {
|
||||
bs := make([]byte, 1)
|
||||
src.Read(bs)
|
||||
return bs[0]
|
||||
}
|
||||
byte_out := func (b byte) { dst.Write([]byte{b}) }
|
||||
odd := func() byte {
|
||||
for {
|
||||
b := byte_in()
|
||||
if unicode.IsPunct(int(b)) {
|
||||
return b
|
||||
}
|
||||
defer byte_out(b)
|
||||
}
|
||||
panic("impossible")
|
||||
}
|
||||
for {
|
||||
for {
|
||||
b := byte_in()
|
||||
byte_out(b)
|
||||
if b == '.' {
|
||||
return
|
||||
}
|
||||
if unicode.IsPunct(rune(b)) {
|
||||
break
|
||||
}
|
||||
}
|
||||
b := odd()
|
||||
byte_out(b)
|
||||
if b == '.' {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
89
Task/Odd-word-problem/Go/odd-word-problem-3.go
Normal file
89
Task/Odd-word-problem/Go/odd-word-problem-3.go
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
func main() {
|
||||
owp(os.Stdout, bytes.NewBufferString("what,is,the;meaning,of:life."))
|
||||
fmt.Println()
|
||||
owp(os.Stdout, bytes.NewBufferString("we,are;not,in,kansas;any,more."))
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
type Coroutine struct {
|
||||
out <-chan Coroutine
|
||||
in chan<- byte
|
||||
}
|
||||
|
||||
func owp(dst io.Writer, src io.Reader) {
|
||||
byte_in := func () (byte, error) {
|
||||
bs := make([]byte, 1)
|
||||
_, err := src.Read(bs)
|
||||
return bs[0], err
|
||||
}
|
||||
byte_out := func (b byte) { dst.Write([]byte{b}) }
|
||||
|
||||
var f, r Coroutine
|
||||
|
||||
f = func () Coroutine {
|
||||
out := make(chan Coroutine)
|
||||
in := make(chan byte)
|
||||
var fwd func (byte) byte
|
||||
fwd = func (c byte) (z byte) {
|
||||
if unicode.IsLetter(rune(c)) {
|
||||
byte_out(c)
|
||||
out <- f
|
||||
z = fwd(<- in)
|
||||
} else {
|
||||
z = c
|
||||
}
|
||||
return
|
||||
}
|
||||
go func () {
|
||||
for {
|
||||
x, ok := <- in
|
||||
if !ok { break }
|
||||
byte_out(fwd(x))
|
||||
out <- r
|
||||
}
|
||||
} ()
|
||||
return Coroutine{ out, in }
|
||||
} ()
|
||||
r = func () Coroutine {
|
||||
out := make(chan Coroutine)
|
||||
in := make(chan byte)
|
||||
var rev func (byte) byte
|
||||
rev = func (c byte) (z byte) {
|
||||
if unicode.IsLetter(rune(c)) {
|
||||
out <- r
|
||||
z = rev(<- in)
|
||||
byte_out(c)
|
||||
} else {
|
||||
z = c
|
||||
}
|
||||
return
|
||||
}
|
||||
go func () {
|
||||
for {
|
||||
x, ok := <- in
|
||||
if !ok { break }
|
||||
byte_out(rev(x))
|
||||
out <- f
|
||||
}
|
||||
} ()
|
||||
return Coroutine{ out, in }
|
||||
} ()
|
||||
|
||||
for coro := f; ; coro = <- coro.out {
|
||||
c, err := byte_in()
|
||||
if err != nil { break }
|
||||
coro.in <- c
|
||||
}
|
||||
close(f.in)
|
||||
close(r.in)
|
||||
}
|
||||
20
Task/Odd-word-problem/Haskell/odd-word-problem-1.hs
Normal file
20
Task/Odd-word-problem/Haskell/odd-word-problem-1.hs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import System.IO
|
||||
|
||||
isAlpha :: Char -> Bool
|
||||
isAlpha = flip elem $ ['a'..'z'] ++ ['A'..'Z']
|
||||
|
||||
split :: String -> (String, String)
|
||||
split = break $ not . isAlpha
|
||||
|
||||
parse :: String -> String
|
||||
parse [] = []
|
||||
parse l =
|
||||
let (a, w) = split l
|
||||
(b, x) = splitAt 1 w
|
||||
(c, y) = split x
|
||||
(d, z) = splitAt 1 y
|
||||
in a ++ b ++ reverse c ++ d ++ parse z
|
||||
|
||||
main :: IO ()
|
||||
main = hSetBuffering stdin NoBuffering >> hSetBuffering stdout NoBuffering >>
|
||||
getContents >>= putStr . (takeWhile (/= '.')) . parse >> putStrLn "."
|
||||
31
Task/Odd-word-problem/Haskell/odd-word-problem-2.hs
Normal file
31
Task/Odd-word-problem/Haskell/odd-word-problem-2.hs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
isAlpha :: Char -> Bool
|
||||
isAlpha = flip elem $ ['a'..'z'] ++ ['A'..'Z']
|
||||
|
||||
parse :: IO ()
|
||||
parse = do
|
||||
x <- getChar
|
||||
putChar x
|
||||
case () of
|
||||
_ | x == '.' -> return ()
|
||||
| isAlpha x -> parse
|
||||
| otherwise -> do
|
||||
c <- revParse
|
||||
putChar c
|
||||
if c == '.'
|
||||
then return ()
|
||||
else parse
|
||||
|
||||
revParse :: IO Char
|
||||
revParse = do
|
||||
x <- getChar
|
||||
case () of
|
||||
_ | x == '.' -> return x
|
||||
| isAlpha x -> do
|
||||
c <- revParse
|
||||
putChar x
|
||||
return c
|
||||
| otherwise -> return x
|
||||
|
||||
main :: IO ()
|
||||
main = hSetBuffering stdin NoBuffering >> hSetBuffering stdout NoBuffering >>
|
||||
parse >> putStrLn ""
|
||||
22
Task/Odd-word-problem/Icon/odd-word-problem-1.icon
Normal file
22
Task/Odd-word-problem/Icon/odd-word-problem-1.icon
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
procedure main()
|
||||
every OddWord(!["what,is,the;meaning,of:life.",
|
||||
"we,are;not,in,kansas;any,more."])
|
||||
end
|
||||
|
||||
procedure OddWord(stream) #: wrapper for demonstration
|
||||
write("Input stream: ",stream)
|
||||
writes("Output stream: ") & eWord(create !stream,'.,;:') & write()
|
||||
end
|
||||
|
||||
procedure eWord(stream,marks) #: handle even words
|
||||
repeat {
|
||||
repeat
|
||||
writes(@stream) ? if ="." then return else if any(marks) then break
|
||||
if writes(oWord(stream,marks)) == '.' then return
|
||||
}
|
||||
end
|
||||
|
||||
procedure oWord(stream,marks) #: handle odd words (reverse)
|
||||
if any(marks,s := @stream) then return s
|
||||
return 1(oWord(stream,marks), writes(s))
|
||||
end
|
||||
10
Task/Odd-word-problem/Icon/odd-word-problem-2.icon
Normal file
10
Task/Odd-word-problem/Icon/odd-word-problem-2.icon
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
procedure main(A)
|
||||
repeat (while writes((any(&letters, c := reads(&input,1)),c))) |
|
||||
(writes(c) ~== "." ~== writes(rWord())) | break write()
|
||||
end
|
||||
|
||||
procedure rWord(c)
|
||||
c1 := rWord((any(&letters, c1 := reads(&input,1)),c1))
|
||||
writes(\c)
|
||||
return c1
|
||||
end
|
||||
26
Task/Odd-word-problem/J/odd-word-problem-1.j
Normal file
26
Task/Odd-word-problem/J/odd-word-problem-1.j
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
putch=: 4 :0 NB. coroutine verb
|
||||
outch y
|
||||
return x
|
||||
)
|
||||
|
||||
isletter=: toupper ~: tolower
|
||||
|
||||
do_char=: 3 :0 NB. coroutine verb
|
||||
ch=. getch''
|
||||
if. isletter ch do.
|
||||
if. odd do.
|
||||
putch&ch yield do_char '' return.
|
||||
end.
|
||||
else.
|
||||
odd=: -. odd
|
||||
end.
|
||||
return ch
|
||||
)
|
||||
|
||||
evenodd=: 3 :0
|
||||
clear_outstream begin_instream y
|
||||
odd=: 0
|
||||
whilst. '.'~:char do.
|
||||
outch char=. do_char coroutine ''
|
||||
end.
|
||||
)
|
||||
4
Task/Odd-word-problem/J/odd-word-problem-2.j
Normal file
4
Task/Odd-word-problem/J/odd-word-problem-2.j
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
evenodd 'what,is,the;meaning,of:life.'
|
||||
what,si,the;gninaem,of:efil.
|
||||
evenodd 'we,are;not,in,kansas;any,more.'
|
||||
we,era;not,ni,kansas;yna,more.
|
||||
57
Task/Odd-word-problem/Java/odd-word-problem.java
Normal file
57
Task/Odd-word-problem/Java/odd-word-problem.java
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
public class OddWord {
|
||||
interface CharHandler {
|
||||
CharHandler handle(char c) throws Exception;
|
||||
}
|
||||
final CharHandler fwd = new CharHandler() {
|
||||
public CharHandler handle(char c) {
|
||||
System.out.print(c);
|
||||
return (Character.isLetter(c) ? fwd : rev);
|
||||
}
|
||||
};
|
||||
class Reverser extends Thread implements CharHandler {
|
||||
Reverser() {
|
||||
setDaemon(true);
|
||||
start();
|
||||
}
|
||||
private Character ch; // For inter-thread comms
|
||||
private char recur() throws Exception {
|
||||
notify();
|
||||
while (ch == null) wait();
|
||||
char c = ch, ret = c;
|
||||
ch = null;
|
||||
if (Character.isLetter(c)) {
|
||||
ret = recur();
|
||||
System.out.print(c);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
public synchronized void run() {
|
||||
try {
|
||||
while (true) {
|
||||
System.out.print(recur());
|
||||
notify();
|
||||
}
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
public synchronized CharHandler handle(char c) throws Exception {
|
||||
while (ch != null) wait();
|
||||
ch = c;
|
||||
notify();
|
||||
while (ch != null) wait();
|
||||
return (Character.isLetter(c) ? rev : fwd);
|
||||
}
|
||||
}
|
||||
final CharHandler rev = new Reverser();
|
||||
|
||||
public void loop() throws Exception {
|
||||
CharHandler handler = fwd;
|
||||
int c;
|
||||
while ((c = System.in.read()) >= 0) {
|
||||
handler = handler.handle((char) c);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
new OddWord().loop();
|
||||
}
|
||||
}
|
||||
25
Task/Odd-word-problem/OCaml/odd-word-problem.ocaml
Normal file
25
Task/Odd-word-problem/OCaml/odd-word-problem.ocaml
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
let is_alpha c =
|
||||
c >= 'a' && c <= 'z' ||
|
||||
c >= 'A' && c <= 'Z'
|
||||
|
||||
let rec odd () =
|
||||
let c = input_char stdin in
|
||||
if is_alpha c
|
||||
then (let e = odd () in print_char c; e)
|
||||
else (c)
|
||||
|
||||
let rec even () =
|
||||
let c = input_char stdin in
|
||||
if is_alpha c
|
||||
then (print_char c; even ())
|
||||
else print_char c
|
||||
|
||||
let rev_odd_words () =
|
||||
while true do
|
||||
even ();
|
||||
print_char (odd ())
|
||||
done
|
||||
|
||||
let () =
|
||||
try rev_odd_words ()
|
||||
with End_of_file -> ()
|
||||
27
Task/Odd-word-problem/PHP/odd-word-problem.php
Normal file
27
Task/Odd-word-problem/PHP/odd-word-problem.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
$odd = function ($prev) use ( &$odd ) {
|
||||
$a = fgetc(STDIN);
|
||||
if (!ctype_alpha($a)) {
|
||||
$prev();
|
||||
fwrite(STDOUT, $a);
|
||||
return $a != '.';
|
||||
}
|
||||
$clos = function () use ($a , $prev) {
|
||||
fwrite(STDOUT, $a);
|
||||
$prev();
|
||||
};
|
||||
return $odd($clos);
|
||||
};
|
||||
$even = function () {
|
||||
while (true) {
|
||||
$c = fgetc(STDIN);
|
||||
fwrite(STDOUT, $c);
|
||||
if (!ctype_alpha($c)) {
|
||||
return $c != ".";
|
||||
}
|
||||
}
|
||||
};
|
||||
$prev = function(){};
|
||||
$e = false;
|
||||
while ($e ? $odd($prev) : $even()) {
|
||||
$e = !$e;
|
||||
}
|
||||
25
Task/Odd-word-problem/PL-I/odd-word-problem.pli
Normal file
25
Task/Odd-word-problem/PL-I/odd-word-problem.pli
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
test: procedure options (main); /* 2 August 2014 */
|
||||
declare (ch, ech) character (1);
|
||||
declare odd file;
|
||||
|
||||
get_word: procedure recursive;
|
||||
declare ch character (1);
|
||||
|
||||
get file (odd) edit (ch) (a(1));
|
||||
if index('abcdefghijklmnopqrstuvwxyz', ch) > 0 then call get_word;
|
||||
if index('abcdefghijklmnopqrstuvwxyz', ch) > 0 then
|
||||
put edit (ch) (a);
|
||||
else ech = ch;
|
||||
end get_word;
|
||||
|
||||
open file (odd) input title ('/ODDWORD.DAT,TYPE(text),recsize(100)');
|
||||
do forever;
|
||||
do until (index('abcdefghijklmnopqrstuvwxyz', ch) = 0 );
|
||||
get file (odd) edit (ch) (a(1)); put edit (ch) (a);
|
||||
end;
|
||||
if ch = '.' then leave;
|
||||
call get_word;
|
||||
put edit (ech) (a);
|
||||
if ech = '.' then leave;
|
||||
end;
|
||||
end test;
|
||||
12
Task/Odd-word-problem/Perl-6/odd-word-problem.pl6
Normal file
12
Task/Odd-word-problem/Perl-6/odd-word-problem.pl6
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
my &in = { $*IN.getc // last }
|
||||
|
||||
loop {
|
||||
ew(in);
|
||||
ow(in).print;
|
||||
}
|
||||
|
||||
multi ew ($_ where /\w/) { .print; ew(in); }
|
||||
multi ew ($_) { .print; next when "\n"; }
|
||||
|
||||
multi ow ($_ where /\w/) { ow(in) x .print; }
|
||||
multi ow ($_) { $_; }
|
||||
20
Task/Odd-word-problem/Perl/odd-word-problem-1.pl
Normal file
20
Task/Odd-word-problem/Perl/odd-word-problem-1.pl
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
sub r
|
||||
{
|
||||
my ($f, $c) = @_;
|
||||
return sub { print $c; $f->(); };
|
||||
}
|
||||
|
||||
$r = sub {};
|
||||
|
||||
while (read STDIN, $_, 1) {
|
||||
$w = /^[a-zA-Z]$/;
|
||||
$n++ if ($w && !$l);
|
||||
$l = $w;
|
||||
if ($n & 1 || !$w) {
|
||||
$r->(); $r = sub{};
|
||||
print;
|
||||
} else {
|
||||
$r = r($r, $_);
|
||||
}
|
||||
}
|
||||
$r->();
|
||||
26
Task/Odd-word-problem/Perl/odd-word-problem-2.pl
Normal file
26
Task/Odd-word-problem/Perl/odd-word-problem-2.pl
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
sub rev
|
||||
{
|
||||
my $c;
|
||||
read STDIN, $c, 1;
|
||||
if ($c =~ /^[a-zA-Z]$/) {
|
||||
my $r = rev();
|
||||
print $c;
|
||||
return $r;
|
||||
} else {
|
||||
return $c;
|
||||
}
|
||||
}
|
||||
|
||||
while (read STDIN, $_, 1) {
|
||||
$w = /^[a-zA-Z]$/;
|
||||
$n++ if ($w && !$l);
|
||||
$l = $w;
|
||||
if ($n & 1) {
|
||||
print;
|
||||
} else {
|
||||
my $r = rev();
|
||||
print $_;
|
||||
print $r;
|
||||
$n = 0; $l = 0;
|
||||
}
|
||||
}
|
||||
24
Task/Odd-word-problem/Perl/odd-word-problem-3.pl
Normal file
24
Task/Odd-word-problem/Perl/odd-word-problem-3.pl
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
$|=1;
|
||||
|
||||
while (read STDIN, $_, 1) {
|
||||
$w = /^[a-zA-Z]$/;
|
||||
$n++ if ($w && !$l);
|
||||
$l = $w;
|
||||
if ($n & 1 || !$w) {
|
||||
close W; while(wait()!=-1){}
|
||||
print;
|
||||
} else {
|
||||
open W0, ">&", \*W;
|
||||
close W;
|
||||
pipe R,W;
|
||||
if (!fork()) {
|
||||
close W;
|
||||
<R>;
|
||||
print $_;
|
||||
close W0;
|
||||
exit;
|
||||
}
|
||||
close W0;
|
||||
close R;
|
||||
}
|
||||
}
|
||||
14
Task/Odd-word-problem/PicoLisp/odd-word-problem-1.l
Normal file
14
Task/Odd-word-problem/PicoLisp/odd-word-problem-1.l
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
(de oddWords ()
|
||||
(use C
|
||||
(loop
|
||||
(until (sub? (prin (setq C (char))) "!,.:;?"))
|
||||
(T (= "." C))
|
||||
(setq C (char))
|
||||
(T
|
||||
(= "."
|
||||
(prin
|
||||
(recur (C)
|
||||
(if (sub? C "!,.:;?")
|
||||
C
|
||||
(prog1 (recurse (char)) (prin C)) ) ) ) ) ) )
|
||||
(prinl) ) )
|
||||
2
Task/Odd-word-problem/PicoLisp/odd-word-problem-2.l
Normal file
2
Task/Odd-word-problem/PicoLisp/odd-word-problem-2.l
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(in "txt1" (oddWords))
|
||||
(in "txt2" (oddWords))
|
||||
27
Task/Odd-word-problem/Prolog/odd-word-problem.pro
Normal file
27
Task/Odd-word-problem/Prolog/odd-word-problem.pro
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
odd_word_problem :-
|
||||
read_line_to_codes(user_input, L),
|
||||
even_word(L, Out, []),
|
||||
string_to_list(Str, Out),
|
||||
writeln(Str).
|
||||
|
||||
even_word(".") --> ".".
|
||||
|
||||
even_word([H | T]) -->
|
||||
{char_type(H,alnum)},
|
||||
[H],
|
||||
even_word(T).
|
||||
|
||||
even_word([H | T]) -->
|
||||
[H],
|
||||
odd_word(T, []).
|
||||
|
||||
odd_word(".", R) --> R, ".".
|
||||
|
||||
odd_word([H|T], R) -->
|
||||
{char_type(H,alnum)},
|
||||
odd_word(T, [H | R]).
|
||||
|
||||
odd_word([H|T], R) -->
|
||||
R,
|
||||
[H],
|
||||
even_word(T).
|
||||
58
Task/Odd-word-problem/PureBasic/odd-word-problem.purebasic
Normal file
58
Task/Odd-word-problem/PureBasic/odd-word-problem.purebasic
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
#False = 0
|
||||
#True = 1
|
||||
|
||||
Global *inputPtr.Character
|
||||
|
||||
Macro nextChar()
|
||||
*inputPtr + SizeOf(Character)
|
||||
EndMacro
|
||||
|
||||
Procedure isPunctuation(c.s)
|
||||
If FindString("!?()[]{},.;:-'" + #DQUOTE$, c)
|
||||
ProcedureReturn #True
|
||||
EndIf
|
||||
ProcedureReturn #False
|
||||
EndProcedure
|
||||
|
||||
Procedure oddWord()
|
||||
Protected c.c
|
||||
c = *inputPtr\c
|
||||
If isPunctuation(Chr(*inputPtr\c))
|
||||
ProcedureReturn
|
||||
Else
|
||||
nextChar()
|
||||
oddWord()
|
||||
EndIf
|
||||
Print(Chr(c))
|
||||
EndProcedure
|
||||
|
||||
Procedure oddWordProblem(inputStream.s)
|
||||
*inputPtr = @inputStream
|
||||
Define isOdd = #False
|
||||
While *inputPtr\c
|
||||
If isOdd
|
||||
oddWord()
|
||||
Else
|
||||
Repeat
|
||||
Print(Chr(*inputPtr\c))
|
||||
nextChar()
|
||||
Until isPunctuation(Chr(*inputPtr\c))
|
||||
EndIf
|
||||
Print(Chr(*inputPtr\c))
|
||||
isOdd ! 1 ;toggle word indicator
|
||||
nextChar()
|
||||
Wend
|
||||
EndProcedure
|
||||
|
||||
Define inputStream.s
|
||||
If OpenConsole()
|
||||
Repeat
|
||||
PrintN(#CRLF$ + #CRLF$ + "Enter a series of words consisting only of English letters (i.e. a-z, A-Z)")
|
||||
PrintN("and that are separated by a punctuation mark (i.e. !?()[]{},.;:-' or " + #DQUOTE$ + ").")
|
||||
inputStream = Input()
|
||||
oddWordProblem(inputStream) ;assume input is correct
|
||||
Until inputStream = ""
|
||||
|
||||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
28
Task/Odd-word-problem/Python/odd-word-problem-1.py
Normal file
28
Task/Odd-word-problem/Python/odd-word-problem-1.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
from sys import stdin, stdout
|
||||
|
||||
def char_in(): return stdin.read(1)
|
||||
def char_out(c): stdout.write(c)
|
||||
|
||||
def odd(prev = lambda: None):
|
||||
a = char_in()
|
||||
if not a.isalpha():
|
||||
prev()
|
||||
char_out(a)
|
||||
return a != '.'
|
||||
|
||||
# delay action until later, in the shape of a closure
|
||||
def clos():
|
||||
char_out(a)
|
||||
prev()
|
||||
|
||||
return odd(clos)
|
||||
|
||||
def even():
|
||||
while True:
|
||||
c = char_in()
|
||||
char_out(c)
|
||||
if not c.isalpha(): return c != '.'
|
||||
|
||||
e = False
|
||||
while odd() if e else even():
|
||||
e = not e
|
||||
4
Task/Odd-word-problem/Python/odd-word-problem-2.py
Normal file
4
Task/Odd-word-problem/Python/odd-word-problem-2.py
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
$ echo "what,is,the;meaning,of:life." | python odd.py
|
||||
what,si,the;gninaem,of:efil.
|
||||
$ echo "we,are;not,in,kansas;any,more." | python odd.py
|
||||
we,era;not,ni,kansas;yna,more.
|
||||
28
Task/Odd-word-problem/Python/odd-word-problem-3.py
Normal file
28
Task/Odd-word-problem/Python/odd-word-problem-3.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
from sys import stdin, stdout
|
||||
|
||||
def char_in(): return stdin.read(1)
|
||||
def char_out(c): stdout.write(c)
|
||||
|
||||
def odd():
|
||||
a = char_in()
|
||||
if a.isalpha():
|
||||
r = odd()
|
||||
char_out(a)
|
||||
return r
|
||||
|
||||
# delay printing terminator until later, in the shape of a closure
|
||||
def clos():
|
||||
char_out(a)
|
||||
return a != '.'
|
||||
|
||||
return clos
|
||||
|
||||
def even():
|
||||
while True:
|
||||
c = char_in()
|
||||
char_out(c)
|
||||
if not c.isalpha(): return c != '.'
|
||||
|
||||
e = False
|
||||
while odd()() if e else even():
|
||||
e = not e
|
||||
33
Task/Odd-word-problem/Python/odd-word-problem-4.py
Normal file
33
Task/Odd-word-problem/Python/odd-word-problem-4.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
from sys import stdin, stdout
|
||||
|
||||
def fwd(c):
|
||||
if c.isalpha():
|
||||
return [stdout.write(c), (yield from fwd((yield f)))][1]
|
||||
else:
|
||||
return c
|
||||
|
||||
def rev(c):
|
||||
if c.isalpha():
|
||||
return [(yield from rev((yield r))), stdout.write(c)][0]
|
||||
else:
|
||||
return c
|
||||
|
||||
def fw():
|
||||
while True:
|
||||
stdout.write((yield from fwd((yield r))))
|
||||
|
||||
def re():
|
||||
while True:
|
||||
stdout.write((yield from rev((yield f))))
|
||||
|
||||
f = fw()
|
||||
r = re()
|
||||
next(f)
|
||||
next(r)
|
||||
|
||||
coro = f
|
||||
while True:
|
||||
c = stdin.read(1)
|
||||
if not c:
|
||||
break
|
||||
coro = coro.send(c)
|
||||
1
Task/Odd-word-problem/README
Normal file
1
Task/Odd-word-problem/README
Normal file
|
|
@ -0,0 +1 @@
|
|||
Data source: http://rosettacode.org/wiki/Odd_word_problem
|
||||
35
Task/Odd-word-problem/REXX/odd-word-problem.rexx
Normal file
35
Task/Odd-word-problem/REXX/odd-word-problem.rexx
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
/*REXX program solves the odd word problem by just using byte I/O. */
|
||||
iFID_ = 'ODDWORD.IN' /*numeric suffix is added later. */
|
||||
oFID_ = 'ODDWORD.' /* " " " " " */
|
||||
|
||||
do case=1 for 2; #=0 /*#: number of characters read.*/
|
||||
iFID=ifid_ || case /*read ODDWORD.IN1 or ODDWORD.IN2*/
|
||||
oFID=ofid_ || case /*write ODDWORD.1 or ODDWORD.2 */
|
||||
say; say; say '──────── reading file: ' iFID "────────"
|
||||
|
||||
do until x=='.' /* [↓] perform for odd words. */
|
||||
|
||||
do until \datatype(x,'M')
|
||||
call readChar; call writeChar
|
||||
end /*until \datatype···*/
|
||||
|
||||
if x=='.' then leave /*end─of─sentence? (full stop) */
|
||||
call readLetters; punctuation_loc=#
|
||||
/* [↓] perform for even words.*/
|
||||
do j=#-1 by -1; call readChar j
|
||||
if \datatype(x,'M') then leave; call writeChar
|
||||
end /*j*/
|
||||
|
||||
call readLetters; call writeChar; #=punctuation_loc
|
||||
end /*until x ···*/
|
||||
end /*case*/ /* [↑] process both input files.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────one─liner subroutines───────────────*/
|
||||
readLetters: do until \datatype(x,'M'); call readChar; end; return
|
||||
writeChar: call charout ,x; call charout oFID,x; return
|
||||
serr: say; say '***error!***' arg(1); say; exit 13 /*oops─ay.*/
|
||||
/*──────────────────────────────────readChar subroutine─────────────────*/
|
||||
readChar: if lines(iFID)==0 then call serr 'EOF reached.' /*no file.*/
|
||||
if arg(1)=='' then do; x=charin(ifid); #=#+1; end /*read the next char*/
|
||||
else x=charin(ifid, arg(1)) /* " specific " */
|
||||
return
|
||||
26
Task/Odd-word-problem/Racket/odd-word-problem.rkt
Normal file
26
Task/Odd-word-problem/Racket/odd-word-problem.rkt
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#!/bin/sh
|
||||
#|
|
||||
exec racket -tm- "$0" "$@"
|
||||
|#
|
||||
|
||||
#lang racket
|
||||
|
||||
(define (even k)
|
||||
(define c (read-char))
|
||||
(cond [(eq? c eof) (k)]
|
||||
[(not (char-alphabetic? c)) (k) (write-char c) (odd)]
|
||||
[else (even (λ() (write-char c) (k)))]))
|
||||
|
||||
(define (odd)
|
||||
(define c (read-char))
|
||||
(unless (eq? c eof)
|
||||
(write-char c)
|
||||
(if (char-alphabetic? c) (odd) (even void))))
|
||||
|
||||
(provide main)
|
||||
(define (main) (odd) (newline))
|
||||
|
||||
;; (with-input-from-string "what,is,the;meaning,of:life." main)
|
||||
;; ;; -> what,si,the;gninaem,of:efil.
|
||||
;; (with-input-from-string "we,are;not,in,kansas;any,more." main)
|
||||
;; ;; -> we,era;not,ni,kansas;yna,more.
|
||||
13
Task/Odd-word-problem/Ruby/odd-word-problem-1.rb
Normal file
13
Task/Odd-word-problem/Ruby/odd-word-problem-1.rb
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
f, r = nil
|
||||
fwd = proc {|c|
|
||||
c =~ /[[:alpha:]]/ ? [(print c), fwd[Fiber.yield f]][1] : c }
|
||||
rev = proc {|c|
|
||||
c =~ /[[:alpha:]]/ ? [rev[Fiber.yield r], (print c)][0] : c }
|
||||
|
||||
(f = Fiber.new { loop { print fwd[Fiber.yield r] }}).resume
|
||||
(r = Fiber.new { loop { print rev[Fiber.yield f] }}).resume
|
||||
|
||||
coro = f
|
||||
until $stdin.eof?
|
||||
coro = coro.resume($stdin.getc)
|
||||
end
|
||||
71
Task/Odd-word-problem/Ruby/odd-word-problem-2.rb
Normal file
71
Task/Odd-word-problem/Ruby/odd-word-problem-2.rb
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
require 'continuation' unless defined? Continuation
|
||||
require 'stringio'
|
||||
|
||||
# Save current continuation.
|
||||
def savecc(*data)
|
||||
# With MRI 1.8 (but not 1.9), the array literal
|
||||
# [callcc {|cc| cc}, *data]
|
||||
# used the wrong return value from callcc. The workaround is to
|
||||
# put callcc outside the array literal.
|
||||
continuation = callcc {|cc| cc}
|
||||
[continuation, *data]
|
||||
end
|
||||
|
||||
# Jump back to continuation, where savecc will return [nil, *data].
|
||||
def jump_back(continuation)
|
||||
continuation[nil]
|
||||
end
|
||||
|
||||
def read_odd_word(input, output)
|
||||
first_continuation, last_continuation = nil
|
||||
reverse = false
|
||||
# Read characters. Loop until end of stream.
|
||||
while c = input.getc
|
||||
c = c.chr # For Ruby 1.8, convert Integer to String.
|
||||
if c =~ /[[:alpha:]]/
|
||||
# This character is a letter.
|
||||
if reverse
|
||||
# Odd word: Write letters in reverse order.
|
||||
saving, last_continuation, c = savecc(last_continuation, c)
|
||||
if saving
|
||||
last_continuation = saving
|
||||
else
|
||||
# After jump: print letters in reverse.
|
||||
output.print c
|
||||
jump_back last_continuation
|
||||
end
|
||||
else
|
||||
# Even word: Write letters immediately.
|
||||
output.print c
|
||||
end
|
||||
else
|
||||
# This character is punctuation.
|
||||
if reverse
|
||||
# End odd word. Fix trampoline, follow chain of continuations
|
||||
# (to print letters in reverse), then bounce off trampoline.
|
||||
first_continuation, c = savecc(c)
|
||||
if first_continuation
|
||||
jump_back last_continuation
|
||||
end
|
||||
output.print c # Write punctuation.
|
||||
reverse = false # Begin even word.
|
||||
else
|
||||
output.print c # Write punctuation.
|
||||
reverse = true # Begin odd word.
|
||||
# Create trampoline to bounce to (future) first_continuation.
|
||||
last_continuation, = savecc
|
||||
unless last_continuation
|
||||
jump_back first_continuation
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
output.puts # Print a cosmetic newline.
|
||||
end
|
||||
|
||||
def odd_word(string)
|
||||
read_odd_word StringIO.new(string), $stdout
|
||||
end
|
||||
|
||||
odd_word "what,is,the;meaning,of:life."
|
||||
odd_word "we,are;not,in,kansas;any,more."
|
||||
31
Task/Odd-word-problem/Run-BASIC/odd-word-problem.run
Normal file
31
Task/Odd-word-problem/Run-BASIC/odd-word-problem.run
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
open "oddWord.txt" for input as #f ' read input stream
|
||||
while not(eof(#f))
|
||||
line input #f, a$
|
||||
oddW$ = "" ' begin the result oddW with blank
|
||||
px = 0 ' begin word search location with 0
|
||||
count = 0 ' begin the word count to 0
|
||||
while x < len(a$) ' look at each character
|
||||
x = instr(a$,",",px) ' search for comma (,)
|
||||
if x = 0 then x = len(a$) ' no more commas?
|
||||
x1 = instr(a$,";",px) ' search for (;)
|
||||
x2 = instr(a$,":",px) ' search for (:)
|
||||
if x1 <> 0 then x = min(x,x1) ' what came first the , ; or :
|
||||
if x2 <> 0 then x = min(x,x2)
|
||||
|
||||
w$ = mid$(a$,px,x - px) ' get the word seperated by , ; or :
|
||||
|
||||
if count and 1 then ' is it the odd word
|
||||
w1$ = ""
|
||||
for i = len(w$) to 1 step -1
|
||||
w1$ = w1$ + mid$(w$,i,1) ' reverse odd words
|
||||
next i
|
||||
w$ = w1$
|
||||
end if
|
||||
oddW$ = oddW$ + w$ + mid$(a$,x,1) ' add the word to the end of oddW$
|
||||
px = x + 1 ' bump word search location for next while
|
||||
count = count + 1 ' count the words
|
||||
wend
|
||||
print a$;" -> ";oddW$ ' print the original and result
|
||||
next ii
|
||||
wend
|
||||
close #f
|
||||
17
Task/Odd-word-problem/Scala/odd-word-problem.scala
Normal file
17
Task/Odd-word-problem/Scala/odd-word-problem.scala
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import scala.io.Source
|
||||
import java.io.PrintStream
|
||||
|
||||
def process(s: Source, p: PrintStream, w: Int = 0): Unit = if (s.hasNext) s.next match {
|
||||
case '.' => p append '.'
|
||||
case c if !Character.isAlphabetic(c) => p append c; reverse(s, p, w + 1)
|
||||
case c => p append c; process(s, p, w)
|
||||
}
|
||||
|
||||
def reverse(s: Source, p: PrintStream, w: Int = 0, x: Char = '.'): Char = s.next match {
|
||||
case c if !Character.isAlphabetic(c) => p append x; c
|
||||
case c => val n = reverse(s, p, w, c);
|
||||
if (x == '.') {p append n; process(s, p, w + 1)} else p append x; n
|
||||
}
|
||||
|
||||
process(Source.fromString("what,is,the;meaning,of:life."), System.out); println
|
||||
process(Source.fromString("we,are;not,in,kansas;any,more."), System.out); println
|
||||
19
Task/Odd-word-problem/Scheme/odd-word-problem.ss
Normal file
19
Task/Odd-word-problem/Scheme/odd-word-problem.ss
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
(define (odd)
|
||||
(let ((c (read-char)))
|
||||
(if (char-alphabetic? c)
|
||||
(let ((r (odd)))
|
||||
(write-char c)
|
||||
r)
|
||||
(lambda () (write-char c) (char=? c #\.)))))
|
||||
|
||||
(define (even)
|
||||
(let ((c (read-char)))
|
||||
(write-char c)
|
||||
(if (char-alphabetic? c)
|
||||
(even)
|
||||
(char=? c #\.))))
|
||||
|
||||
(let loop ((i #f))
|
||||
(if (if i ((odd)) (even))
|
||||
(exit)
|
||||
(loop (not i))))
|
||||
35
Task/Odd-word-problem/Seed7/odd-word-problem.seed7
Normal file
35
Task/Odd-word-problem/Seed7/odd-word-problem.seed7
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
$ include "seed7_05.s7i";
|
||||
include "chartype.s7i";
|
||||
|
||||
const func char: doChar (in boolean: doReverse) is func
|
||||
result
|
||||
var char: delimiter is ' ';
|
||||
local
|
||||
var char: ch is ' ';
|
||||
begin
|
||||
ch := getc(IN);
|
||||
if ch in letter_char then
|
||||
if doReverse then
|
||||
delimiter := doChar(doReverse);
|
||||
write(ch);
|
||||
else
|
||||
write(ch);
|
||||
delimiter := doChar(doReverse);
|
||||
end if;
|
||||
else
|
||||
delimiter := ch;
|
||||
end if;
|
||||
end func;
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
var char: delimiter is ' ';
|
||||
var boolean: doReverse is FALSE;
|
||||
begin
|
||||
repeat
|
||||
delimiter := doChar(doReverse);
|
||||
write(delimiter);
|
||||
doReverse := not doReverse;
|
||||
until delimiter = '.';
|
||||
writeln;
|
||||
end func;
|
||||
18
Task/Odd-word-problem/TUSCRIPT/odd-word-problem.tu
Normal file
18
Task/Odd-word-problem/TUSCRIPT/odd-word-problem.tu
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
$$ MODE TUSCRIPT
|
||||
inputstring=*
|
||||
DATA what,is,the;meaning,of:life.
|
||||
DATA we,are;not,in,kansas;any,more.
|
||||
|
||||
BUILD C_GROUP >[pu]=".,;:-"
|
||||
|
||||
LOOP i=inputstring
|
||||
pu=STRINGS (i,"|>[pu]|")
|
||||
wo=STRINGS (i,"|<></|")
|
||||
outputstring=""
|
||||
loop n,w=wo,p=pu
|
||||
r=MOD(n,2)
|
||||
IF (r==0) w=TURN (w)
|
||||
outputstring=CONCAT(outputstring,w,p)
|
||||
ENDLOOP
|
||||
PRINT outputstring
|
||||
ENDLOOP
|
||||
13
Task/Odd-word-problem/Tcl/odd-word-problem.tcl
Normal file
13
Task/Odd-word-problem/Tcl/odd-word-problem.tcl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package require Tcl 8.6
|
||||
|
||||
proc fwd c {
|
||||
expr {[string is alpha $c] ? "[fwd [yield f][puts -nonewline $c]]" : $c}
|
||||
}
|
||||
proc rev c {
|
||||
expr {[string is alpha $c] ? "[rev [yield r]][puts -nonewline $c]" : $c}
|
||||
}
|
||||
coroutine f while 1 {puts -nonewline [fwd [yield r]]}
|
||||
coroutine r while 1 {puts -nonewline [rev [yield f]]}
|
||||
for {set coro f} {![eof stdin]} {} {
|
||||
set coro [$coro [read stdin 1]]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue