Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue