Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,7 +1,15 @@
|
|||
Even today, with proportional fonts and complex layouts, there are still [[Template:Lines_too_long|cases]] where you need to wrap text at a specified column. The basic task is to wrap a paragraph of text in a simple way in your language. If there is a way to do this that is built-in, trivial, or provided in a standard library, show that. Otherwise implement the [http://en.wikipedia.org/wiki/Word_wrap#Minimum_length minimum length greedy algorithm from Wikipedia.]
|
||||
Even today, with proportional fonts and complex layouts, there are still [[Template:Lines_too_long|cases]] where you need to wrap text at a specified column.
|
||||
|
||||
The basic task is to wrap a paragraph of text in a simple way in your language.
|
||||
|
||||
If there is a way to do this that is built-in, trivial, or provided in a standard library, show that. Otherwise implement the [http://en.wikipedia.org/wiki/Word_wrap#Minimum_length minimum length greedy algorithm from Wikipedia.]
|
||||
|
||||
Show your routine working on a sample of text at two different wrap columns.
|
||||
|
||||
'''Extra credit!''' Wrap text using a more sophisticated algorithm such as the Knuth and Plass TeX algorithm. If your language provides this, you get easy extra credit, but you ''must reference documentation'' indicating that the algorithm is something better than a simple minimimum length algorithm.
|
||||
'''Extra credit!''' Wrap text using a more sophisticated algorithm such as the Knuth and Plass TeX algorithm.
|
||||
If your language provides this, you get easy extra credit,
|
||||
but you ''must reference documentation'' indicating that the algorithm
|
||||
is something better than a simple minimimum length algorithm.
|
||||
|
||||
If you have both basic and extra credit solutions, show an example where the two algorithms give different results.
|
||||
If you have both basic and extra credit solutions, show an example where
|
||||
the two algorithms give different results.
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
MsgBox, % "72`n" WrapText(Clipboard, 72) "`n80`n" WrapText(Clipboard, 80)
|
||||
MsgBox, % "72`n" WrapText(Clipboard, 72) "`n`n80`n" WrapText(Clipboard, 80)
|
||||
return
|
||||
|
||||
WrapText(Text, LineLength) {
|
||||
StringReplace, Text, Text, `r`n, %A_Space%, All
|
||||
while (p := RegExMatch(Text, "([\s\S]{1," LineLength "})(\s|\R|$)", Match, p ? p + StrLen(Match) : 1))
|
||||
Result .= Match1 (Match2 = A_Space || A_Tab ? "`n" : Match2)
|
||||
while (p := RegExMatch(Text, "(.{1," LineLength "})(\s|\R+|$)", Match, p ? p + StrLen(Match) : 1))
|
||||
Result .= Match1 ((Match2 = A_Space || Match2 = A_Tab) ? "`n" : Match2)
|
||||
return, Result
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,15 +11,15 @@
|
|||
)
|
||||
: ?Text
|
||||
& ( wrap
|
||||
= txt length line output q
|
||||
= txt length line output q rem
|
||||
. !arg:(?txt.?length)
|
||||
& :?output
|
||||
& whl
|
||||
' ( @( str$!txt
|
||||
: ?line
|
||||
(" " %?lastword [?q " " ?txt&!q:~<!length)
|
||||
(" " %?lastword [?q " " ?rem&!q:~<!length)
|
||||
)
|
||||
& !lastword " " !txt:?txt
|
||||
& !lastword " " !rem:?txt
|
||||
& !output !line \n:?output
|
||||
)
|
||||
& str$(!output !txt)
|
||||
|
|
|
|||
21
Task/Word-wrap/Haskell/word-wrap-2.hs
Normal file
21
Task/Word-wrap/Haskell/word-wrap-2.hs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import Data.List
|
||||
|
||||
teststring = "In olden times when wishing still helped one, there lived a king"
|
||||
++" whose daughters were all beautiful, but the youngest was so beautiful"
|
||||
++" that the sun itself, which has seen so much, was astonished whenever"
|
||||
++" it shone in her face. Close by the king's castle lay a great dark"
|
||||
++" forest, and under an old lime-tree in the forest was a well, and when"
|
||||
++" the day was very warm, the king's child went out into the forest and"
|
||||
++" sat down by the side of the cool fountain, and when she was bored she"
|
||||
++" took a golden ball, and threw it up on high and caught it, and this"
|
||||
++" ball was her favorite plaything."
|
||||
|
||||
wwrap'' _ [] = []
|
||||
wwrap'' i ss = (\(a,b) -> a : wwrap'' i b)
|
||||
$ last . filter ((<=i) . length . unwords . fst)
|
||||
$ zip (inits ss) (tails ss)
|
||||
|
||||
wwrap :: Int -> String -> String
|
||||
wwrap i = unlines . map unwords . wwrap'' i . words . concat . lines
|
||||
|
||||
main = putStrLn $ wwrap 80 teststring
|
||||
45
Task/Word-wrap/Lua/word-wrap.lua
Normal file
45
Task/Word-wrap/Lua/word-wrap.lua
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
function splittokens(s)
|
||||
local res = {}
|
||||
for w in s:gmatch("%S+") do
|
||||
res[#res+1] = w
|
||||
end
|
||||
return res
|
||||
end
|
||||
|
||||
function textwrap(text, linewidth)
|
||||
if not linewidth then
|
||||
linewidth = 75
|
||||
end
|
||||
|
||||
local spaceleft = linewidth
|
||||
local res = {}
|
||||
local line = {}
|
||||
|
||||
for _, word in ipairs(splittokens(text)) do
|
||||
if #word + 1 > spaceleft then
|
||||
table.insert(res, table.concat(line, ' '))
|
||||
line = {word}
|
||||
spaceleft = linewidth - #word
|
||||
else
|
||||
table.insert(line, word)
|
||||
spaceleft = spaceleft - (#word + 1)
|
||||
end
|
||||
end
|
||||
|
||||
table.insert(res, table.concat(line, ' '))
|
||||
return table.concat(res, '\n')
|
||||
end
|
||||
|
||||
local example1 = [[
|
||||
Even today, with proportional fonts and complex layouts,
|
||||
there are still cases where you need to wrap text at a
|
||||
specified column. The basic task is to wrap a paragraph
|
||||
of text in a simple way in your language. If there is a
|
||||
way to do this that is built-in, trivial, or provided in
|
||||
a standard library, show that. Otherwise implement the
|
||||
minimum length greedy algorithm from Wikipedia.
|
||||
]]
|
||||
|
||||
print(textwrap(example1))
|
||||
print()
|
||||
print(textwrap(example1, 60))
|
||||
27
Task/Word-wrap/Mathematica/word-wrap-1.math
Normal file
27
Task/Word-wrap/Mathematica/word-wrap-1.math
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
string="In olden times when wishing still helped one, there lived a king whose daughters were all beautiful, but the youngest was so beautiful that the sun itself, which has seen so much, was astonished whenever it shone in her face. Close by the king's castle lay a great dark forest, and under an old lime-tree in the forest was a well, and when the day was very warm, the king's child went out into the forest and sat down by the side of the cool fountain, and when she was bored she took a golden ball, and threw it up on high and caught it, and this ball was her favorite plaything.";
|
||||
wordWrap[textWidth_,spaceWidth_,string_]:=Module[{start,spaceLeft,masterString},
|
||||
spaceLeft=textWidth;
|
||||
start=1;
|
||||
masterString={};
|
||||
Do[
|
||||
If[i+1>Length@StringSplit@string
|
||||
,
|
||||
p=StringSplit[string][[start;;i]];
|
||||
AppendTo[masterString,{StringJoin@@Riffle[p,StringJoin@ConstantArray[" ",spaceWidth]]}]
|
||||
,
|
||||
If[StringLength[StringSplit@string][[i+1]]+spaceWidth>spaceLeft
|
||||
,
|
||||
spaceLeft=textWidth-StringLength[StringSplit@string][[i]];
|
||||
start=i;
|
||||
AppendTo[masterString,{StringJoin@@Riffle[p,StringJoin@ConstantArray[" ",spaceWidth]]}]
|
||||
,
|
||||
spaceLeft-=StringLength[StringSplit@string][[i]];
|
||||
spaceLeft-=spaceWidth;
|
||||
p=StringSplit[string][[start;;i]]
|
||||
]
|
||||
]
|
||||
,
|
||||
{i,1,Length@StringSplit@string}
|
||||
];
|
||||
StringJoin@@Riffle[masterString,"\n"]
|
||||
];
|
||||
2
Task/Word-wrap/Mathematica/word-wrap-2.math
Normal file
2
Task/Word-wrap/Mathematica/word-wrap-2.math
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
wordWrap[72, 1, string]
|
||||
wordWrap[80, 1, string]
|
||||
14
Task/Word-wrap/R/word-wrap.r
Normal file
14
Task/Word-wrap/R/word-wrap.r
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
> x <- "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. "
|
||||
> cat(paste(strwrap(x=c(x, "\n"), width=80), collapse="\n"))
|
||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus.
|
||||
Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec
|
||||
consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero
|
||||
egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem
|
||||
lacinia consectetur.
|
||||
> cat(paste(strwrap(x=c(x, "\n"), width=60), collapse="\n"))
|
||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
|
||||
Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas
|
||||
congue ligula ac quam viverra nec consectetur ante
|
||||
hendrerit. Donec et mollis dolor. Praesent et diam eget
|
||||
libero egestas mattis sit amet vitae augue. Nam tincidunt
|
||||
congue enim, ut porta lorem lacinia consectetur.
|
||||
44
Task/Word-wrap/Scala/word-wrap.scala
Normal file
44
Task/Word-wrap/Scala/word-wrap.scala
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import java.util.StringTokenizer
|
||||
|
||||
object WordWrap extends App {
|
||||
final val defaultLineWidth = 80
|
||||
final val spaceWidth = 1
|
||||
|
||||
def letsWrap(text: String, lineWidth: Int = defaultLineWidth) = {
|
||||
println(s"\n\nWrapped at: $lineWidth")
|
||||
println("." * lineWidth)
|
||||
minNumLinesWrap(ewd, lineWidth)
|
||||
}
|
||||
|
||||
final def ewd = "Vijftig jaar geleden publiceerde Edsger Dijkstra zijn kortstepadalgoritme. Daarom een kleine ode" +
|
||||
" aan de in 2002 overleden Dijkstra, iemand waar we als Nederlanders best wat trotser op mogen zijn. Dijkstra was" +
|
||||
" een van de eerste programmeurs van Nederland. Toen hij in 1957 trouwde, werd het beroep computerprogrammeur door" +
|
||||
" de burgerlijke stand nog niet erkend en uiteindelijk gaf hij maar `theoretische natuurkundige’ op.\nZijn" +
|
||||
" beroemdste resultaat is het kortstepadalgoritme, dat de kortste verbinding vindt tussen twee knopen in een graaf" +
|
||||
" (een verzameling punten waarvan sommigen verbonden zijn). Denk bijvoorbeeld aan het vinden van de kortste route" +
|
||||
" tussen twee steden. Het slimme van Dijkstra’s algoritme is dat het niet alle mogelijke routes met elkaar" +
|
||||
" vergelijkt, maar dat het stap voor stap de kortst mogelijke afstanden tot elk punt opbouwt. In de eerste stap" +
|
||||
" kijk je naar alle punten die vanaf het beginpunt te bereiken zijn en markeer je al die punten met de afstand tot" +
|
||||
" het beginpunt. Daarna kijk je steeds vanaf het punt dat op dat moment de kortste afstand heeft tot het beginpunt" +
|
||||
" naar alle punten die je vanaf daar kunt bereiken. Als je een buurpunt via een nieuwe verbinding op een snellere" +
|
||||
" manier kunt bereiken, schrijf je de nieuwe, kortere afstand tot het beginpunt bij zo’n punt. Zo ga je steeds een" +
|
||||
" stukje verder tot je alle punten hebt gehad en je de kortste route tot het eindpunt hebt gevonden."
|
||||
|
||||
def minNumLinesWrap(text: String, LineWidth: Int) {
|
||||
val tokenizer = new StringTokenizer(text)
|
||||
var SpaceLeft = LineWidth
|
||||
while (tokenizer.hasMoreTokens) {
|
||||
val word: String = tokenizer.nextToken
|
||||
if ((word.length + spaceWidth) > SpaceLeft) {
|
||||
print("\n" + word + " ")
|
||||
SpaceLeft = LineWidth - word.length
|
||||
} else {
|
||||
print(word + " ")
|
||||
SpaceLeft -= (word.length + spaceWidth)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
letsWrap(ewd)
|
||||
letsWrap(ewd, 120)
|
||||
} // 44 lines
|
||||
Loading…
Add table
Add a link
Reference in a new issue