Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
25
Task/Word-wrap/LFE/word-wrap-1.lfe
Normal file
25
Task/Word-wrap/LFE/word-wrap-1.lfe
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
(defun wrap-text (text)
|
||||
(wrap-text text 78))
|
||||
|
||||
(defun wrap-text (text max-len)
|
||||
(string:join
|
||||
(make-wrapped-lines
|
||||
(string:tokens text " ") max-len)
|
||||
"\n"))
|
||||
|
||||
(defun make-wrapped-lines
|
||||
(((cons word rest) max-len)
|
||||
(let ((`#(,_ ,_ ,last-line ,lines) (assemble-lines max-len word rest)))
|
||||
(lists:reverse (cons last-line lines)))))
|
||||
|
||||
(defun assemble-lines (max-len word rest)
|
||||
(lists:foldl
|
||||
#'assemble-line/2
|
||||
`#(,max-len ,(length word) ,word ())
|
||||
rest))
|
||||
|
||||
(defun assemble-line
|
||||
((word `#(,max ,line-len ,line ,acc)) (when (> (+ (length word) line-len) max))
|
||||
`#(,max ,(length word) ,word ,(cons line acc)))
|
||||
((word `#(,max ,line-len ,line ,acc))
|
||||
`#(,max ,(+ line-len 1 (length word)) ,(++ line " " word) ,acc)))
|
||||
9
Task/Word-wrap/LFE/word-wrap-2.lfe
Normal file
9
Task/Word-wrap/LFE/word-wrap-2.lfe
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(defun make-regex-str (max-len)
|
||||
(++ "(.{1," (integer_to_list max-len) "}|\\S{"
|
||||
(integer_to_list (+ max-len 1)) ",})(?:\\s[^\\S\\r\\n]*|\\Z)"))
|
||||
|
||||
(defun wrap-text (text max-len)
|
||||
(let ((find-patt (make-regex-str max-len))
|
||||
(replace-patt "\\1\\\n"))
|
||||
(re:replace text find-patt replace-patt
|
||||
'(global #(return list)))))
|
||||
4
Task/Word-wrap/LFE/word-wrap-3.lfe
Normal file
4
Task/Word-wrap/LFE/word-wrap-3.lfe
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
> (set test-text (++ "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.")
|
||||
> (io:format (wrap-text text 80))
|
||||
1
Task/Word-wrap/LFE/word-wrap-4.lfe
Normal file
1
Task/Word-wrap/LFE/word-wrap-4.lfe
Normal file
|
|
@ -0,0 +1 @@
|
|||
> (io:format (wrap-text text 50))
|
||||
14
Task/Word-wrap/Lasso/word-wrap.lasso
Normal file
14
Task/Word-wrap/Lasso/word-wrap.lasso
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
define wordwrap(
|
||||
text::string,
|
||||
row_length::integer = 75
|
||||
) => {
|
||||
return regexp(`(?is)(.{1,` + #row_length + `})(?:$|\W)+`, '$1<br />\n', #text, true) -> replaceall
|
||||
}
|
||||
|
||||
local(text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris consequat ornare lectus, dignissim iaculis libero consequat sed. Proin quis magna in arcu sagittis consequat sed ac risus. Ut a pharetra dui. Phasellus molestie, mauris eget scelerisque laoreet, diam dolor vulputate nulla, in porta sem sem sit amet lacus.')
|
||||
|
||||
wordwrap(#text, 40)
|
||||
'<hr />'
|
||||
wordwrap(#text)
|
||||
'<hr />'
|
||||
wordwrap(#text, 90)
|
||||
42
Task/Word-wrap/Lingo/word-wrap-1.lingo
Normal file
42
Task/Word-wrap/Lingo/word-wrap-1.lingo
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
-- in some movie script
|
||||
|
||||
----------------------------------------
|
||||
-- Wraps specified text into lines of specified width (in px), returns lines as list of strings
|
||||
-- @param {string} str
|
||||
-- @param {integer} pixelWidth
|
||||
-- @param {propList} [style]
|
||||
-- @return {list}
|
||||
----------------------------------------
|
||||
on hardWrapText (str, pixelWidth, style)
|
||||
if voidP(style) then style = [:]
|
||||
lines = []
|
||||
|
||||
-- create a new field member
|
||||
m = new(#field)
|
||||
m.text = str
|
||||
m.rect = rect(0,0,pixelWidth,0)
|
||||
|
||||
-- assign style props (if not specified, defaults are used)
|
||||
repeat with i = 1 to style.count
|
||||
m.setProp(style.getPropAt(i), style[i])
|
||||
end repeat
|
||||
|
||||
-- create an invisible temporary sprite
|
||||
s = channel(1).makeScriptedSprite(m)
|
||||
s.loc = point(0,0)
|
||||
s.visible = false
|
||||
_movie.updateStage()
|
||||
|
||||
-- get the wrapped lines
|
||||
charPos = 0
|
||||
repeat with y = 0 to s.height-1
|
||||
n = s.pointToChar(point(pixelWidth-1, y))
|
||||
if n<>charPos then
|
||||
lines.add(str.char[charPos+1..n])
|
||||
charPos = n
|
||||
end if
|
||||
end repeat
|
||||
|
||||
channel(1).removeScriptedSprite()
|
||||
return lines
|
||||
end
|
||||
12
Task/Word-wrap/Lingo/word-wrap-2.lingo
Normal file
12
Task/Word-wrap/Lingo/word-wrap-2.lingo
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
str = "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed "&\
|
||||
"eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim "&\
|
||||
"veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi "&\
|
||||
"consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu "&\
|
||||
"fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in "&\
|
||||
"culpa qui officia deserunt mollit anim id est laborum."
|
||||
|
||||
lines = hardWrapText(str, 320, [#font: "Arial", #fontSize:24])
|
||||
|
||||
repeat with l in lines
|
||||
put l
|
||||
end repeat
|
||||
6
Task/Word-wrap/Nim/word-wrap.nim
Normal file
6
Task/Word-wrap/Nim/word-wrap.nim
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import strutils
|
||||
|
||||
let txt = "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."
|
||||
echo wordWrap(txt)
|
||||
echo ""
|
||||
echo wordWrap(txt, 45)
|
||||
18
Task/Word-wrap/Sidef/word-wrap-1.sidef
Normal file
18
Task/Word-wrap/Sidef/word-wrap-1.sidef
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
class String {
|
||||
method wrap(width) {
|
||||
var txt = self.gsub(/\s+/, " ");
|
||||
var len = txt.len;
|
||||
var para = [];
|
||||
var i = 0;
|
||||
while (i < len) {
|
||||
var j = (i + width);
|
||||
while ((j < len) && (txt.char_at(j) != ' ')) { --j };
|
||||
para.append(txt.substr(i, j-i));
|
||||
i = j+1;
|
||||
};
|
||||
return para.join("\n");
|
||||
}
|
||||
}
|
||||
|
||||
var text = 'aaa bb cc ddddd';
|
||||
say text.wrap(6);
|
||||
90
Task/Word-wrap/Sidef/word-wrap-2.sidef
Normal file
90
Task/Word-wrap/Sidef/word-wrap-2.sidef
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
class SmartWordWrap {
|
||||
|
||||
has width = 80
|
||||
|
||||
method prepare_words(array, depth=0, callback) {
|
||||
|
||||
var root = []
|
||||
var len = 0
|
||||
var i = -1
|
||||
|
||||
var limit = array.end
|
||||
while (++i <= limit) {
|
||||
len += (var word_len = array[i].len)
|
||||
|
||||
if (len > width) {
|
||||
if (word_len > width) {
|
||||
len -= word_len
|
||||
array.splice(i, 1, array[i].split(width)...)
|
||||
limit = array.end
|
||||
--i; next
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
root << [
|
||||
array.first(i+1).join(' '),
|
||||
self.prepare_words(array.ft(i+1), depth+1, callback)
|
||||
]
|
||||
|
||||
if (depth.is_zero) {
|
||||
callback(root[0])
|
||||
root = []
|
||||
}
|
||||
|
||||
break if (++len >= width)
|
||||
}
|
||||
|
||||
root
|
||||
}
|
||||
|
||||
method combine(root, path, callback) {
|
||||
var key = path.shift
|
||||
path.each { |value|
|
||||
root << key
|
||||
if (value.is_empty) {
|
||||
callback(root)
|
||||
}
|
||||
else {
|
||||
value.each { |item|
|
||||
self.combine(root, item, callback)
|
||||
}
|
||||
}
|
||||
root.pop
|
||||
}
|
||||
}
|
||||
|
||||
method wrap(text, width) {
|
||||
|
||||
self.width = width
|
||||
var words = (text.kind_of(Array) ? text : text.words)
|
||||
|
||||
var best = Hash(
|
||||
score => Inf,
|
||||
value => [],
|
||||
)
|
||||
|
||||
self.prepare_words(words, callback: { |path|
|
||||
self.combine([], path, { |combination|
|
||||
var score = 0
|
||||
combination.ft(0, -2).each { |line|
|
||||
score += (width - line.len -> sqr)
|
||||
}
|
||||
|
||||
if (score < best{:score}) {
|
||||
best{:score} = score
|
||||
best{:value} = []+combination
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
best{:value}.join("\n")
|
||||
}
|
||||
}
|
||||
|
||||
var sww = SmartWordWrap();
|
||||
|
||||
var words = %w(aaa bb cc ddddd);
|
||||
var wrapped = sww.wrap(words, 6);
|
||||
|
||||
say wrapped;
|
||||
14
Task/Word-wrap/jq/word-wrap-1.jq
Normal file
14
Task/Word-wrap/jq/word-wrap-1.jq
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
# Simple greedy algorithm.
|
||||
# Note: very long words are not truncated.
|
||||
# input: a string
|
||||
# output: an array of strings
|
||||
def wrap_text(width):
|
||||
reduce splits("\\s+") as $word
|
||||
([""];
|
||||
.[length-1] as $current
|
||||
| ($word|length) as $wl
|
||||
| (if $current == "" then 0 else 1 end) as $pad
|
||||
| if $wl + $pad + ($current|length) <= width
|
||||
then .[-1] += ($pad * " ") + $word
|
||||
else . + [ $word]
|
||||
end );
|
||||
1
Task/Word-wrap/jq/word-wrap-2.jq
Normal file
1
Task/Word-wrap/jq/word-wrap-2.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
"aaa bb cc ddddd" | wrap_text(6)[] # wikipedia example
|
||||
1
Task/Word-wrap/jq/word-wrap-3.jq
Normal file
1
Task/Word-wrap/jq/word-wrap-3.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
"aaa bb cc ddddd" | wrap_text(5)[]
|
||||
3
Task/Word-wrap/jq/word-wrap-4.jq
Normal file
3
Task/Word-wrap/jq/word-wrap-4.jq
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
советских военных судов и самолетов была отмечена в Японском море после появления там двух американских авианосцев. Не
|
||||
менее 100 советских самолетов поднялись в воздух, когдаамериканские
|
||||
авианосцы "Уинсон" и "Мидуэй" приблизились на 50 миль к Владивостоку.
|
||||
8
Task/Word-wrap/jq/word-wrap-5.jq
Normal file
8
Task/Word-wrap/jq/word-wrap-5.jq
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
$ jq -M -R -s -r -f Word_wrap.jq Russian.txt
|
||||
советских военных судов и самолетов была
|
||||
отмечена в Японском море после появления
|
||||
там двух американских авианосцев. Не
|
||||
менее 100 советских самолетов поднялись
|
||||
в воздух, когдаамериканские авианосцы
|
||||
"Уинсон" и "Мидуэй" приблизились на 50
|
||||
миль к Владивостоку.
|
||||
Loading…
Add table
Add a link
Reference in a new issue