previndexinfo

code guessing, round #103, stage 2 (guessing)

started at ; stage 2 since . guess by

specification

let's make a text editor 1 step at a time. for our first task, we need to wrap
words
. submissions can be written in any language.

word wrapping is often a necessary part of displaying words on a screen,
especially when they need to stay in a particular area. it's probably a feature
of every piece of software you've ever used that shows text (so, all of it). let
me remind you what it is.

text is generally taken to be a one-dimensional concept unless you're writing
Befunge, but it's displayed on a two-dimensional surface. the thing about two
dimensions is that it's like a lot of one-dimensional things pasted together,
so it gives you more space in your space. it'd be more convenient if we could
just print everything on paper and screens one em tall. we wouldn't need word
wrapping then. but then books and screens would have to be very long, and you
wouldn't be able to fit them through doorways very easily. so instead we have
rectangles, and are resigned to cutting up text so it can fit: word wrapping.

there are many technical details as to how one might implement this. but this is
code guessing, where we don't care about them. at the minimum, I'll ask this: my
brand new code guessing editor will target displays 80 columns wide. at the
minimum, I should be able to wrap text for such a terminal. this is word
wrapping, so I also ask that you not break up continuous series of letters.

thank you. as any language is allowed, there is no fixed API.

players

  1. evie
  2. GNU Radio Shows
  3. lexi
  4. Luna
  5. Makefile_dot_in
  6. seshoumara

entries

you can download all the entries

entry #1

comments 0

post a comment


wrap.py ASCII text
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
input = "According to all known laws of aviation, there is no way a bee should be able to fly. Its wings are too small to get its fat little body off the ground. The bee, of course, flies anyway because bees don't care what humans think is impossible."

input = input.replace("\n"," ").split()
output = ""
while input != []:
  if len(input[0]) > 80:
    line = input[0][:79] + "-"
    input[0] = input[0][79:]
  else:
    line = ""
    line += input[0] + " "
    while len(line) <= 80 and len(input) > 1:
      del input[0]
      line += input[0] + " "
    line += input[0] + " "
    del input[0]
    line = " ".join(line.split()[::-1][1:][::-1])
  output += line + "\n"
output = output.strip("\n")
print(output)

entry #2

comments 0

post a comment


entry.hs ASCII text
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
breakLine :: Int -> String -> String
breakLine maxlen = go [] . words
  where
  go acc []     = acc
  go []  (w:ws) = go w ws
  go acc (w:ws)
    | length acc' <= maxlen = go acc' ws
    | otherwise             = acc ++ "\n" ++ go w ws
    where acc' = acc ++ " " ++ w

main = interact (unlines . map (breakLine 80) . lines)

entry #3

comments 0

post a comment


cg103_word_wrap.sed ASCII text
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#!/usr/bin/sed -nrf

:loop
	s:^.{80}:&\n:
	/\n/!{
		/./H;x
		s:^\n::p
		x;q
	}
	s:^(.*) (.*)\n:\1 \n\2:
	H;x
	s:\n[^\n]*$::
	x
	s:^.*\n::
bloop

entry #4

comments 0

post a comment


cg103.tcl ASCII text, with very long lines (627)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
namespace path ::tcl::mathop

proc print {width txt} {
    set indices [concat {*}[regexp -all -inline -indices {\S+} $txt]]
    set col 0
    foreach {whstart whend} [list 0 {*}$indices [string length $txt]] {wstart wend} [list {*}$indices 0 -1] {
        set whitespace [string map {\t {    }} [string range $txt $whstart+1 $whend-1]]
        set whlen [string length $whitespace]
        set newl no
        foreach line [split $whitespace \n] {
            if {$newl} { puts ""; set col 0 }
            set newl yes
            set rows [/ [+ $col $whlen] $width]
            set col [% [+ $col $whlen] $width]
            puts -nonewline [string repeat \n $rows]
            puts -nonewline $line
        }

        set word [string range $txt $wstart $wend]
        set wlen [string length $word]
        if {$col + $wlen >= $width} {
            puts -nonewline \n$word
            set col 0
        } else {
            puts -nonewline $word
            set col [+ $col $wlen]
        }
    }

    puts ""
}

set text {
    let's make a text editor 1 step at a time. for our first task, we need to wrap words. submissions can be written in any language.

    word wrapping is often a necessary part of displaying words on a screen, especially when they need to stay in a particular area. it's probably a feature of every piece of software you've ever used that shows text (so, all of it). let me remind you what it is.

    text is generally taken to be a one-dimensional concept unless you're writing Befunge, but it's displayed on a two-dimensional surface. the thing about two dimensions is that it's like a lot of one-dimensional things pasted together, so it gives you more space in your space. it'd be more convenient if we could just print everything on paper and screens one em tall. we wouldn't need word wrapping then. but then books and screens would have to be very long, and you wouldn't be able to fit them through doorways very easily. so instead we have rectangles, and are resigned to cutting up text so it can fit: word wrapping.

    there are many technical details as to how one might implement this. but this is code guessing, where we don't care about them. at the minimum, I'll ask this: my brand new code guessing editor will target displays 80 columns wide. at the minimum, I should be able to wrap text for such a terminal. this is word wrapping, so I also ask that you not break up continuous series of letters.

    thank you. as any language is allowed, there is no fixed API.
}

print 40 [regsub -all -line {^    } $text {}]

entry #5

comments 0

post a comment


cg103.el Unicode text, UTF-8 text
1
2
3
4
5
6
7
8
9
;; This buffer is for text that is not saved, and for Lisp evaluation.
;; To create a file, visit it with ‘C-x C-f’ and enter text in its buffer.

(defun cg103 (text len)
  (with-temp-buffer
    (insert text)
    (setq fill-column len)
    (fill-region (point-min) (point-max))
    (buffer-string)))

entry #6

comments 0

post a comment


submission.uiua Unicode text, UTF-8 text, with very long lines (625)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ let's make a text editor 1 step at a time. for our first task, we need to wrap words. submissions can be written in any language.
$ 
$ word wrapping is often a necessary part of displaying words on a screen, especially when they need to stay in a particular area. it's probably a feature of every piece of software you've ever used that shows text (so, all of it). let me remind you what it is.
$ 
$ text is generally taken to be a one-dimensional concept unless you're writing Befunge, but it's displayed on a two-dimensional surface. the thing about two dimensions is that it's like a lot of one-dimensional things pasted together, so it gives you more space in your space. it'd be more convenient if we could just print everything on paper and screens one em tall. we wouldn't need word wrapping then. but then books and screens would have to be very long, and you wouldn't be able to fit them through doorways very easily. so instead we have rectangles, and are resigned to cutting up text so it can fit: word wrapping.
$ 
$ there are many technical details as to how one might implement this. but this is code guessing, where we don't care about them. at the minimum, I'll ask this: my brand new code guessing editor will target displays 80 columns wide. at the minimum, I should be able to wrap text for such a terminal. this is word wrapping, so I also ask that you not break up continuous series of letters.
$ 
$ thank you. as any language is allowed, there is no fixed API.
N ← 80
# whitespace arrays (w = p ∨ ¬s)
◡≥⊸⊃> =@ 
# idx of next whitespace (j)
↘1⍜⇌\↧+°⊏¯°ₑ˜⊂1
# index of wrapped start-of-line (k)
# F k i j p s := k'
F ← ⨬⊙⋅⋅⋅◌⋅⊙⋅⋅◌↥◡⊃(×>N-⊙⋅⊙◌)⋅⋅⋅=
+1°⊏
# scan using F, k0=0
⬚0\(F⊙°⊟₄)≡⊟₄
# group runs by same k
⊕□⊛
# strip whitespace around each line & print
⍚(&p▽×˙⍜⇌∩\↥⊸>@ )