title: Make a lookup index

text:
# Question
Title: Make a lookup index
Given a string, return a table where the first column has the unique letters of the string in order of occurrence and subsequent columns list the indices of that letter in the string, using zero or one-based indexing. Horizontal whitespace does not matter, as long as the left-most column is vertically aligned. Indices must be in ascending order from left to right.

### Examples

Using zero-based indexing and given "abracadabra", return

```
a 0 3 5 7 10
b 1 8
r 2 9
c 4
d 6

```

Using one-based indexing and given "3141592653589793238462643383279503", return:

```
3  1 10 16 18 25 26 28 34
1  2  4
4  3 20 24
5  5  9 11 32
9  6 13 15 31
2  7 17 22 29
6  8 21 23
8 12 19 27
7 14 30
0 33

```

# Answer
# APL (Dyalog), 4 bytes

```
,⌸

```

Try it online!

( is 3 bytes)

Uses 1-based indexing.

is the key operator. Here it behaves as a monadic operator. It applies the function `,`, concatenate the left argument with the right argument, to each unique element in its right argument and the indices of that unique element in the original argument.
> 6 votes

# Answer
# Haskell, 86 bytes

```
import Data.List
f s=unlines[x:concat[' ':show i|i<-[0..length s-1],s!!i==x]|x<-nub s]

```

Defines a function, `f`, which returns a String containing this output.

Try it online!

How?

```
import Data.List                                                            -- Imports Data.List. This contains the nub method which is necessary
f s =                                                                       -- Define a function, f, which takes one argument, s
            [                                               |x<-nub s]      -- Loop through nub s with the variable x. Nub removes duplicates from a list, in this case the input.
                     [          |i<-[0..length s-1]        ]                -- Go thourgh the list [0,1,2...] until the length of the input - 1. Basically a indexed for-loop
                                                   ,s!!i==x                 -- Filter out everything where the char at this index isn't x
                      ' ':show i                                            -- i as a string with a space before it
               concat                                                       -- Flatten the whole list
             x:                                                             -- Append x before it
     unlines                                                                -- Insert newlines between every element in the list and flatten it, effectively putting every element on it's own line

```
> 2 votes

# Answer
# kdb+/q, 5 bytes

```
group

```

Builtins are fab

```
q)group"abracadabra"
a| 0 3 5 7 10
b| 1 8
r| 2 9
c| ,4
d| ,6

```

I usually golf in , but the 2-byte k version (`=:`) doesn't format the output nicely

```
k)=:"abracadabra"
"abrcd"!(0 3 5 7 10;1 8;2 9;,4;,6)

```

The results are exactly the same, but the formatting is lost. To format, and to remove the return object, we actually pick up more bytes than the q version

```
k)f:{1@.Q.s x;} //11 bytes!
k)f"abracadabra"
a| 0 3 5 7 10
b| 1 8
r| 2 9
c| ,4
d| ,6

```
> 2 votes

# Answer
# MATL, 11 bytes

```
u"@0hG@=fVh

```

Output indices are 1-based.

Try it online!
> 1 votes

# Answer
# Python, 96 bytes

```
def f(s,r=[]):
 for c in s:
  if c not in r:print(c,*(i for i,l in enumerate(s) if l==c));r+=[c]

```

try it online!
> 1 votes

# Answer
# Jelly, 7 bytes

```
Q;"ĠṢ$G

```

Try it online!
> 0 votes

# Answer
# Pyth, 13 bytes

```
jmj;+dxCdCMQ{

```

Try it online!
> 0 votes

# Answer
# 05AB1E, 14 bytes

```
ÙvSyQƶ0Ky¸ìðý,

```

Try it online!

**Explanation**

```
Ùv              # for each unique char y in input
  S             # split input into a list of chars
   yQ           # compare each to y for equality
     ƶ          # multiply each by its 1-based index
      0K        # remove zeroes
        y¸ì     # prepend y to the list of indices
           ðý,  # join by spaces and print

```
> 0 votes

# Answer
# Mathematica, 85 bytes

using one-based indexing

```
(t=#;j=First/@t~StringPosition~#&/@(s=First/@Tally@Characters@t);Row[Column/@{s,j}])&

```
> 0 votes

# Answer
# , ~~80~~ 77 bytes

```
function(s)for(i in (z=unique(s<-strsplit(s,'')[[1]])))cat(i,which(s==i),"
")

```

Try it online!

an anonymous function; prints the result 1-indexed with a trailing newline.
> 0 votes

# Answer
## JavaScript (ES Draft), 77 bytes

```
s=>s.replace(/./g,(c,i)=>a[c]=(a[c]||c)+` `+i,a={})&&Object.values(a).join`
`

```

88 bytes in older browsers:

```
f=
s=>s.replace(/./g,(c,i)=>a[c]=(a[c]||c)+` `+i,a={})&&Object.keys(a).map(c=>a[c]).join`
`
```

```
<input oninput=o.textContent=f(this.value)><pre id=o>
```
> 0 votes

# Answer
# PHP, 82 bytes

```
for(;~$c=$argn[$i];)$r[$c].=str_pad(++$i,5," ",0);foreach($r as$k=>$v)echo"$k$v
";

```

Try it online!
> 0 votes

# Answer
# QBIC, 95 bytes

```
dim X(126)[_l;||i=asc(_sA,a,1|)┘X(i)=X(i)+@ `+!a$][_lA||_SA,b,1|i=asc(C)~X(i)<>D|?C,X(i)┘X(i)=@

```

## Explanation

This copies significant parts of my answer on this challenge:

```
dim x(126)      Create an array of 126 elements (one for each ASCII element)
[_l;||          Read cmd line input, loop over its length
i=asc(_sA,a,1|) Read the next char's ascii value
┘               (Syntactic linebreak)
X(i)=           Set the value for this ascii-codepoint to
 X(i)             everything we've put in before
 +@ `             and a literal space
 +!a$             and the current (1-based) index cast as string
 ]              close the FOR loop
 [_lA||         Loop over the original string again (to preserve order of appearance)
 _SA,b,1|       Read 1 char, assign to C$ (note the capital S in the function call,
                this auto-creates C$ abd assigns it the value of the substring-call)
 i=asc(C)       Get the index in our storage array from C$'s ascii value
 ~X(i)<>D       IF the storage array holds data for i (<> D$, which we'll set to "" in a second),
 |?C,X(i)       THEN PRINT the character, followed by the indices saved for this char
 ┘              (Syntactic linebreak)
 X(i)=@         Clear out the data stored for C$
                @ declares a string lit, ` would close it and that gets auto-added at EOF,
                creating the literal @`, which gets assigned to D$

```

## Sample run:

```
Command line: abracadabra
a              1 4 6 8 11
b              2 9
r              3 10
c              5
d              7

```
> 0 votes

# Answer
# Python 3, ~~111~~ 106 bytes

```
def q(f):
 d={a:[]for a in f}
 for a,b in enumerate(f):d[b]+=[a]
 [print(p,*d.pop(p))for p in f if p in d]

```

repl.it
> 0 votes

# Answer
# C (clang), 176 bytes

```
G(S,V,c,g,i,H,L)char*S,*V;{for(V=malloc(8),H=strlen(S),c=g=-1;++g<H;){if(strchr(V,L=S[g]))continue;printf("%c ",V[c++]=L);for(i=-1;++i<H;printf(S[i]==L?"%d ":"",i));puts("");}}

```

*Of course* this is the longest answer here...

Try it online!
> 0 votes

# Answer
# C#, 138 bytes

```
using System.Linq;s=>Console.Write(string.Join("\n",s.Distinct().Select(c=>c+string.Join("",s.Select((d,i)=>d==c?i:-1).Where(i=>i>-1)))));

```
> 0 votes

# Answer
# F#, 120 bytes

```
let f s=s|>Seq.indexed|>Seq.groupBy(fun(a,b)->b)|>Seq.iter(fun(a,b)->
 printf"\n%c"a
 Seq.iter(fun(c,_)->printf"%i"c)b)

```

A more readable version:

```
let f s =
    s
    |> Seq.indexed
    |> Seq.groupBy (fun (idx, char) -> char)
    |> Seq.iter (fun (char, charIdxSeq) ->
         printf "\n%c" char
         Seq.iter (fun (idx, _) -> printf "%i" idx) charIdxSeq)

```

`Seq.indexed` creates a new sequence containing tuples which are composed of the original element and it's 0 based index in the original sequence.

The rest is fairly self explanatory which is never a good thing for golf!
> 0 votes

# Answer
# Husk, 10 bytes

```
§mȯ§:;ms¥u

```

Try it online!

Note: Husk (or at least the command `¥`) is newer than this challenge.

## Explanation

```
§mȯ§:;ms¥u  Implicit input, say S = "ababbc".
         u  Remove duplicates: "abc"
§m          Map over this string:
             Argument is a character, say 'b'.
        ¥    1-based indices in S: [2,4,5]
      ms     Convert each to string: ["2","4","5"]
     ;       Wrap argument in a string: "b"
  ȯ§:        Prepend to list of index strings: ["b","2","4","5"]
            Result is a list of lists of strings
             [["a","1","3"],["b","2","4","5"],["c","6"]]
            Implicitly print, separating strings by spaces and lists by newlines.

```
> 0 votes

# Answer
# , 11 bytes

```
~.,.":@I.@=

```

Try it online!
> 0 votes

---
Tags: code-golf, string, classification
---


# Question
Title: Generating PBM bitmap files from ASCII text
The PBM (Portable BitMap) format is a very simple ASCII black and white bitmap format.

Here's an example for the letter 'J' (copy-pasted from the wikipedia link):

```
P1
# This is an example bitmap of the letter "J"
6 10
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
1 0 0 0 1 0
0 1 1 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0

```

It's time we build a small tool to generate files in this nifty little format!

Your **goal** is to write the shortest program (in any language) that conforms to the following rules:

1. Your program takes one string from stdin (for example `CODEGOLF.STACKEXCHANGE.COM!`)
2. It generates a PBM file with a bitmapped (readable) representation of the string.
3. Each character is constructed as a 8x8 grid.
4. You must support the characters \[A-Z\] (all uppercase), space, a point ('.') and an exclamation mark ('!').
5. No external libraries allowed (certainly no PBM related ones)!
6. The used character set must not simply be external to your program. Part of the challenge is storing the characters efficiently...

Testing for validness of the PBM format can be done with the GIMP (or others). Do show off sample input and output!

**The shortest solution will be awarded the answer points on 2012-01-31.**

Have fun golfing!

PS: I've added a bounty (percentage-wise a huge part of my codegolf reputation) to (hopefully) draw in more competitors.

# Answer
> 9 votes
## GolfScript, 133 bytes

This is based on my 164-byte Perl solution and uses the same nibble-packed 4 by 5 pixel font. Again, I'll give the readable version first:

```
{91,65>"!. "+?}%:s"P4"\,8*8'FONT DATA HERE'{16base}%[3]/{:p;{[p=0]0=}s%}%]n*

```

Here, `FONT DATA HERE` stands for 71 bytes of binary packed font data. The encoding is slightly different than in the Perl version: instead of splitting the packed string on whitespace, I expand it first and then split it on the nibble `3` (chosen because it just happens not to occur anywhere in the font).

Since the font data in the actual script contains unprintable characters, I give it as a hex dump below. Use `xxd -r` to turn the hex dump back into executable GolfScript code:

```
0000000: 7b39 312c 3635 3e22 212e 2022 2b3f 7d25  {91,65>"!. "+?}%
0000010: 3a73 2250 3422 5c2c 382a 3827 36e6 eff6  :s"P4"\,8*8'6...
0000020: 9219 8996 e6e7 7959 95f4 3999 9888 921a  ......yY..9.....
0000030: 8fd9 9998 2959 9514 3fe8 9eeb f21c 89b9  ....)Y..?.......
0000040: e9e6 2959 6564 3999 9889 929a 8999 8ba1  ..)Yed9.........
0000050: 295f 9283 9e6e f869 269f 9968 79e2 6299  )_...n.i&..hy.b.
0000060: 2f48 3327 7b31 3662 6173 657d 255b 335d  /H3'{16base}%[3]
0000070: 2f7b 3a70 3b7b 5b70 3d30 5d30 3d7d 7325  /{:p;{[p=0]0=}s%
0000080: 7d25 5d6e 2a                             }%]n*

```

Unlike the Perl script, this code prints any characters outside the set `A``Z`, `!`, `.`, `space` as funny-looking little squiggles. Replacing the squiggles with blanks would cost 2 extra chars; removing them entirely would cost 4.

This is my first GolfScript program ever, so I wouldn't be surprised if there's some room left for optimization. Here's how it works:

* `{91,65>"!. "+?}%:s` maps the valid input characters (`A``Z`, `!`, `.`, `space`) to the numbers 0 – 28 and assigns the result to `s`. Any characters outside the valid set get mapped to -1, which is what produces the squiggles when printed.
* `"P4"\,8*8` pushes the values "P4", 8 times the length of the input, and 8 onto the stack. When printed at the end, these will form the PBM header.
* `{16base}%[3]/` takes the preceding string of font data, splits each byte of it into two nibbles, and splits the result into blocks delimited by the value `3`. `{:p;{[p=0]0=}s%}%` then loops over these blocks, first assigning each block to the variable `p` and then looping over the remapped input string `s`, replacing each character with the value at the corresponding offset in `p`. The funny-looking construct `[p=0]0=` does the same as `p=`, except that it returns 0 for any offsets past the end of `p`; I don't really like it, but I haven't been able to figure out any shorter way to handle that.
* Finally, `]n*` takes everything on the stack (the three header values and the image data array) and joins them together with newlines for printing.

# Answer
> 13 votes
## Perl, 164 bytes, no zlib/gzip compression

After sleeping on the problem, I managed to figure out a much shorter solution than my first one. The trick is to take advantage of a minor loophole in the rules: the characters need to fit in 8 by 8 pixels each, but nothing says they have to *fill* all that space. So I drew my own 4 by 5 pixel font, allowing me to pack two characters into 5 bytes.

The output looks like this:

(scaled x 4)

(original size)

Before giving the actual code with the embedded font data, let me show a de-golfed version:

```
y/A-Z!./\0-\033/ for @a = <> =~ /./g;
say "P4 " . 8*@a . " 8";
for $p (qw'PACKED FONT DATA') {
    print chr vec $p, ord, 4 for @a;
}

```

In the actual code, the `PACKED FONT DATA` is replaced by a binary string consisting of eight whitespace-delimited rows (four 14-byte rows and one 13-byte one, plus three single null bytes for the blank rows). I deliberately designed my font so that the packed data contains no whitespace, single quotes or backslashes, so that it could be encoded in `qw'...'`.

Since the packed font string contains unprintable characters, I've provided the actual script as a hex dump. Use `xxd -r` to turn it back into executable Perl code:

```
0000000: 792f 412d 5a21 2e2f 002d 1b2f 666f 7240  y/A-Z!./.-./for@
0000010: 613d 3c3e 3d7e 2f2e 2f67 3b73 6179 2250  a=<>=~/./g;say"P
0000020: 3420 222e 382a 4061 2e22 2038 223b 666f  4 ".8*@a." 8";fo
0000030: 7224 7028 7177 2700 20e6 e6ff 9612 8999  r$p(qw'. .......
0000040: e6e6 7759 99f5 0420 9999 8898 128a df99  ..wY... ........
0000050: 9928 5999 1504 20ef 98ee fb12 8cb9 e9e9  .(Y... .........
0000060: 2659 6965 0420 9999 8899 928a 9989 ab21  &Yie. .........!
0000070: 599f 8220 e9e6 8f96 62f9 9986 972e 2699  Y.. ....b.....&.
0000080: f284 2000 2000 2729 7b70 7269 6e74 2063  .. . .'){print c
0000090: 6872 2076 6563 2470 2c6f 7264 2c34 666f  hr vec$p,ord,4fo
00000a0: 7240 617d                                r@a}

```

**Here's how it works:**

* The first line (in the de-golfed version) reads a single line of input, splits it into an array of characters (conveniently omitting any trailing newlines) and maps the letters `A` to `Z` and the characters `!` and `.` to the character codes 0 to 28, which normally correspond to unprintable control characters in ASCII / Unicode. (A minor side effect of this is that any tabs in the input get printed as `J`s.) The space character is left unmapped, since the output loop turns any codes above 28 into blanks anyway.
* The second line just prints the PBM header. It uses the Perl 5.10 `say` feature, so you need to run this script with `perl -M5.010` for it to work.
* The output loop takes a whitespace-delimited list of packed image rows and assigns each of them into `$p` in turn. (I designed the font so that the packed data wouldn't contain any whitespace or `'` characters.) It then loops over the input characters in `@a`, using Perl's `vec` command to extract the 4-bit nibble corresponding to the mapped character code from the image row, pads it to an 8-bit byte and prints it.

---

### Old answer, 268 bytes:

This is a quick and dirty first attempt. I stole PleaseStand's font and compressed it along with my source code. Since the resulting script is mostly unprintable, here's a hexdump; use `xxd -r` to turn it into executable Perl code:

```
0000000: 7573 6520 436f 6d70 7265 7373 275a 6c69  use Compress'Zli
0000010: 623b 6576 616c 2075 6e63 6f6d 7072 6573  b;eval uncompres
0000020: 7320 2778 da85 d03d 4b03 4118 85d1 452c  s 'x...=K.A...E,
0000030: b69c 72cb 7519 4894 552c 2c02 3319 ee5c  ..r.u.H.U,,.3..\
0000040: d7b8 5a89 6093 4634 7e82 c490 6c91 8597  ..Z.`.F4~...l...
0000050: 80fe 7267 d660 23ae e52d 0e0f dcd6 f8c3  ..rg.`#..-......
0000060: e9d1 5e6e ccec a15c ddb5 c5d5 495e 94a3  ..^n...\....I^..
0000070: 83b7 c7f9 73f3 5216 f9a8 787a 5fea 666c  ....s.R...xz_.fl
0000080: 9dd1 b763 dd98 76f8 2df6 0799 5811 7144  ...c..v.-...X.qD
0000090: 4acc ee9d b8b0 c90f 7e4a 8264 6016 cbd7  J.......~J.d`...
00000a0: 79f3 1b91 047c 4055 409e 9e54 1dda ed41  y....|@U@..T...A
00000b0: 9a20 8080 6adc 5c47 8488 7495 f621 01d7  . ..j.\G..t..!..
00000c0: 6b6c 902e b6c8 2a6a 6643 f56f e99c 115d  kl....*jfC.o...]
00000d0: 5c7a f1b2 13d0 3453 790f da74 c813 751d  \z....4Sy..t..u.
00000e0: 11ce d821 ad90 247f 2292 5b54 c14f 3c4e  ...!..$.".[T.O<N
00000f0: 49c5 4c53 a1a7 c478 391c 714c f113 0747  I.LS...x9.qL...G
0000100: ab6c 4482 9fd2 177a 5677 6327            .lD....zVwc'

```

The decompressed Perl code consists of the following preamble:

```
y;A-Z.! ;;cd,say"P4 ",8*length," 8"for$t=<>

```

followed by eight repetitions of the following code:

```
;$_=$t;y(A-Z.! )'BITMAP DATA HERE';print

```

with `BITMAP DATA HERE` replaced with 29 bytes encoding one row of the font.

# Answer
> 6 votes
## Shell script (code+data = 295 chars)

I hope tail, gzip, and dd do not count as "external libraries." Run as `echo -n 'YOUR TEXT HERE' | ./text.sh > out.pbm`. The font I used is Small Fonts size 7.5, although I did have to clip the descender off the Q.

### Example output

### Code (137 chars)

```
i=`od -tu1|cut -c9-`
echo P4
for a in {0..7}
do for b in $i
do tail -2 $0|zcat|dd bs=1 count=1 skip=$((8*b+a))
done
done>8
wc -c 8
cat 8

```

### Complete script

(use `xxd -r` to recreate original file)

```
0000000: 693d 606f 6420 2d74 7531 7c63 7574 202d  i=`od -tu1|cut -
0000010: 6339 2d60 0a65 6368 6f20 5034 0a66 6f72  c9-`.echo P4.for
0000020: 2061 2069 6e20 7b30 2e2e 377d 0a64 6f20   a in {0..7}.do
0000030: 666f 7220 6220 696e 2024 690a 646f 2074  for b in $i.do t
0000040: 6169 6c20 2d32 2024 307c 7a63 6174 7c64  ail -2 $0|zcat|d
0000050: 6420 6273 3d31 2063 6f75 6e74 3d31 2073  d bs=1 count=1 s
0000060: 6b69 703d 2428 2838 2a62 2b61 2929 0a64  kip=$((8*b+a)).d
0000070: 6f6e 650a 646f 6e65 3e38 0a77 6320 2d63  one.done>8.wc -c
0000080: 2038 0a63 6174 2038 0a1f 8b08 0000 0000   8.cat 8........
0000090: 0000 ffed cdb1 0a83 3014 8561 910e 8e8e  ........0..a....
00000a0: 193b dca1 631f 2084 9353 6ba3 a3e0 e2a8  .;..c. ..Sk.....
00000b0: 2fe0 d8e1 22d8 276f 9a50 e813 940e fdb8  /...".'o.P......
00000c0: 70f9 a753 247f 7829 f0b5 b9e2 c718 2322  p..S$.x)......#"
00000d0: 1ba9 e9a8 9688 6895 892a 7007 f0fe 701e  ......h..*p...p.
00000e0: b879 ef48 6e8c aa4f 219c d984 750d 0d91  .y.Hn..O!...u...
00000f0: e9b2 8c63 d779 3fcf c3d0 f76d eb7c e2d2  ...c.y?....m.|..
0000100: 1880 d4d7 4b6e 9296 b065 49ab 75c6 cc92  ....Kn...eI.u...
0000110: 1411 63f6 7de7 3489 9031 847c 3c9a 531d  ..c.}.4..1.|<.S.
0000120: e9a1 aa8f 803e 01                        .....>.

```

### Explanation

* `od` is the standard "octal dump" utility program. The `-tu1` option tells it to produce a decimal dump of individual bytes instead (a sufficient workaround for bash's lack of asc(), ord(), .charCodeAt(), etc.)
* `P4` is the magic number for a binary format PBM file, which packs eight pixels into each byte (versus `P1` for the ASCII format PBM file). You will see how this proves useful.
* Per row of final output, the program pulls an eight-pixel byte (corresponding to the ASCII code and line number) from the gzip-compressed data section at the end using `dd`. (`tail -2 $0` extracts the script's last two lines; the compressed data includes one 0x0a linefeed byte.) It so happens that eight pixels is the width of a single character. The null bytes that fill the gaps between supported characters are easily compressible because they are all the same.
* All this is written to a file named "8". Because there are exactly eight rows (and also eight pixels per byte), the number of bytes is the width of the output in pixels. The output's height is also included in that `wc -c` prints the input filename "8" after its byte count.
* Now that the header is complete, the image data is printed. Bash only notices that the last two lines are not valid commands (the last one actually invalid UTF-8) after it has executed everything coming before.
* I used KZIP only to compress the data section, as Ilmari Karonen did for an *entire submission* to the 12 Days of Christmas challenge. As described there, it is essentially necessary to use a hex editor to replace the ZIP header format with a gzip header. Including the CRC-32 and file size from the original ZIP header seems to be unnecessary.

# Answer
> 6 votes
**8086 Machine Code**

190 Bytes (122 Bytes using BIOS)

Here's Base64 encoded WinXP/MSDos .COM file:

```
M8COwCaKDoUEitEmxD4MAaCAAP7IfliK8MHgA7cK9ve9egEAZ
vy0APb3AUb6iMi0APb3AUb+x0YACg2DxQK+ggCK7qz24YvYJo
ohswjQ5LAwFACIRgBF/st18v7NdeRH/sp10sZGACS6cAG0Cc0
hw1AxCg0wMDAgMDA=

```

(Use something like this) to decode the text and save as "pbm.com". Then, at the command prompt, type:

> pbm text to encode\>outputfilename.pbm

I've tested this on my WinXP machine using both the standard command prompt and DosBox V0.74.

**UPDATE**

This version is 190 bytes and uses Ilmari Karonen's tiny font (no bios access here!):-

```
voAArf7Iflq7YwG/vgG6Cg20Bfbk9vIAZfsy5PbyAUX5Vqw48HQoLEFzCDQG/sAMGSQfM8
nQ6NfA0QPS6IjEsQSwJtDsENCq4vewMKrr04DDEF6A+7N1ycYFJLqzAbQJzSHDdnb/loIZ
mXZ2flmZ9QAAIJmZEZGCFb+ZmSFZmYUPDy9/kXf9ghPZeXkmWWllAAAgmZkRmZIVmRldKF
mfEgAAAHl2H5Zi+ZkWnicmmfIAICBQMQoNMDAwIDUKDQ==

```

# Answer
> 5 votes
# Python 2, ~~248~~ 247 bytes

```
s=raw_input();k=len(s);print"P1",k*8,8
for i in range(k*24):a=s[i/3%k];j=max(".!".find(a)+1,ord(a)-62)*3;print int("00080084IMVAENBSIFERBSUF4UFQQEMVDT4NAP4MNDSI9MRTMRBARA4NBQRAMNBE4E94NURDARDNRDMLD95DSL7"[j:j+3],32)>>(i/3/k*3+i%3)&1," 0"*(i%3/2*5)

```

Uses a 3x5 font, packed into a printable string, 3 bytes per character. The font is clearly legible, although the n is lowercase and the v might be mistaken for a u if it isn't seen in context.

Actual Size:

Zoomed x3:

The output is a P1 type PBM, as per the example in the challenge. It was a fun challenge.

# Answer
> 4 votes
## Ruby 1.9, 346 bytes (122 code + 224 bytes data)

Here is the result:

(It's nice, isn't it?)

```
z=0..7;puts"P1\n#{(s=gets).size*8} 8",z.map{|i|s.bytes.flat_map{|o|z.map{|j|'DATA'.unpack('Q<*')[o>64?o-63:o/46][i*8+j]}}*' '}

```

The font was generated by `figlet -f banner -w 1000 $LETTERS` and this script.

Run with `echo -n 'CODEGOLF.STACKEXCHANGE.COM!' | ruby script.rb > image.pbm`.

The script generate all the rows and simply print them.

Here is an hexdump (use `xxd -r`):

```
0000000: 7a3d 302e 2e37 3b70 7574 7322 5031 5c6e  z=0..7;puts"P1\n
0000010: 237b 2873 3d67 6574 7329 2e73 697a 652a  #{(s=gets).size*
0000020: 387d 2038 222c 7a2e 6d61 707b 7c69 7c73  8} 8",z.map{|i|s
0000030: 2e62 7974 6573 2e66 6c61 745f 6d61 707b  .bytes.flat_map{
0000040: 7c6f 7c7a 2e6d 6170 7b7c 6a7c 271c 1c1c  |o|z.map{|j|'...
0000050: 0800 1c1c 0000 0000 001c 1c1c 0008 1422  ..............."
0000060: 417f 4141 003f 4141 3f41 413f 003e 4101  A.AA.?AA?AA?.>A.
0000070: 0101 413e 003f 4141 4141 413f 007f 0101  ..A>.?AAAAA?....
0000080: 1f01 017f 007f 0101 1f01 0101 003e 4101  .............>A.
0000090: 7941 413e 0041 4141 7f41 4141 001c 0808  yAA>.AAA.AAA....
00000a0: 0808 081c 0040 4040 4041 413e 0042 2212  .....@@@@AA>.B".
00000b0: 0e12 2242 0001 0101 0101 017f 0041 6355  .."B.........AcU
00000c0: 4941 4141 0041 4345 4951 6141 007f 4141  IAAA.ACEIQaA..AA
00000d0: 4141 417f 003f 4141 3f01 0101 003e 4141  AAA..?AA?....>AA
00000e0: 4151 215e 003f 4141 3f11 2141 003e 4101  AQ!^.?AA?.!A.>A.
00000f0: 3e40 413e 007f 0808 0808 0808 0041 4141  >@A>.........AAA
0000100: 4141 413e 0041 4141 4122 1408 0041 4949  AAA>.AAAA"...AII
0000110: 4949 4936 0041 2214 0814 2241 0041 2214  III6.A"..."A.A".
0000120: 0808 0808 007f 2010 0804 027f 0027 2e75  ...... ......'.u
0000130: 6e70 6163 6b28 2751 3c2a 2729 5b6f 3e36  npack('Q<*')[o>6
0000140: 343f 6f2d 3633 3a6f 2f34 365d 5b69 2a38  4?o-63:o/46][i*8
0000150: 2b6a 5d7d 7d2a 2720 277d                 +j]}}*' '}

```

It takes 93 bytes of code when using goruby:

```
ps"P1\n#{(s=gt).sz*8} 8",8.mp{|i|s.y.fl{|o|8.mp{|j|'DATA'.ua('Q<*')[o>64?o-63:o/46][i*8+j]}}*' '}

```

Using ZLib trim the data size to 142 bytes instead of 224, but adds 43 bytes in the code, so 307 bytes:

```
#coding:binary
require'zlib';z=0..7;puts"P1\n#{(s=gets).size*8} 8",z.map{|i|s.bytes.flat_map{|o|z.map{|j|Zlib.inflate("DATA").unpack('Q<*')[o>64?o-63:o/46][i*8+j]}}*' '}

```

Which gives a total of 268 when using goruby:

```
#coding:binary
rq'zlib';ps"P1\n#{(s=gt).sz*8} 8",8.mp{|i|s.y.fl{|o|8.mp{|j|Zlib.if("DATA").ua('Q<*')[o>64?o-63:o/46][i*8+j]}}*' '}

```

# Answer
> 2 votes
### Java 862 826:

Here is a different approach. I think 'awt' does not count as external lib.

```
import java.awt.*;
class B extends Frame{String s="ABCDEFGHIJKLMNOPQRSTUVWXYZ .!";int l=29;static Robot r;int[][][]m=new int[l][8][8];
public void paint(Graphics g){for(int y=0;y<8;++y){int py=(y<3)?y:y+1;for(int a=0;a<l;++a)
for(int x=0;x<8;++x)
m[a][x][y]=(r.getPixelColor(8*a+x+17+x/4,py+81)).getRGB()<-1?1:0;}
System.out.println("P1\n"+(getTitle().length()*8)+" 8");
for(int y=0;y<8;++y){for(char c:getTitle().toCharArray()){int a=s.indexOf(c);
for(int x=0;x<8;++x)System.out.print(m[a][x][y]);}
System.out.println();}
System.exit(0);}
public B(String p){super(p);
setBackground(Color.WHITE);
setSize(400,60);
Label l=new Label(s);
l.setFont(new Font("Monospaced",Font.PLAIN,13));
add(l);
setLocation(9,49);
setVisible(true);}
public static void main(String a[])throws Exception{r=new Robot();
new B(a[0]);}}

```

And ungolfed:

```
import java.awt.*;

class PBM extends Frame
{
    String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ .!";
    int l=29;
    Robot robot;
    int[][][] map = new int[l][8][8];

    static boolean init = false;

    public void paint (Graphics g)
    {
        for (int y = 0; y < 8; ++y)
        {
            int py = (y < 3) ? y : y +1;
            for (int a = 0; a < l; ++a)
            {
                for (int x = 0; x < 8; ++x)
                {
                    map[a][x][y] = (robot.getPixelColor (8*a+x+17+x/4, py+81)).getRGB () < -1 ? 1 : 0;
                }
            }
        }

        System.out.println("P1\n"+(getTitle().length()*8)+" 8");

        for (int y = 0; y < 8; ++y) {
            for (char c : getTitle ().toCharArray ()) {
                int a = s.indexOf (c);
                for (int x = 0; x < 8; ++x) {
                    System.out.print (map[a][x][y]);
                }
            }
            System.out.println ();
        }
        System.exit (0);
    }

    public PBM (String p) throws Exception
    {
        super (p);
        robot = new Robot ();
        setBackground (Color.WHITE);
        setSize (400, 60);
        Label l=new Label(s);
        l.setFont (new Font ("Monospaced", Font.PLAIN, 13));
        add(l);
        setLocation (9,49);
        setVisible (true);
    }

    public static void main (String args[]) throws Exception
    {
        new PBM (args[0]);
    }
}

```

Robot is the somehow curious way of Java to call getPixel. I create a Label with the alphabet, and measure where a pixel is for each letter.

In the paint method, `int py = (y < 3) ? y : y +1;` and `(8*a+x+17+x/4, py+81)` is the complicated way, to adjust the position in the font. Huuuh! else it would need 9 lines, and every 4th letter, there is an additional pixel horizontally. Trial and error brought me to this solution.

Then the header of the PBM is written, and each line of the message. The message is passed as title of the frame.

That's it. Not the shortest code, but no manual font painting was necessary.

Maybe it could be shorter in BeanShell or Scala.

And now - how does it look like?

```
java B "JAVA.CAFE BABE" > jcb.pbm

```

Multiple zooms applied:

Unzoomed:

Not that the number of chars is the number of chars from the Perl solution shuffled.

(golfed a little bit more. Made Robot static, which avoids one Exception declaration.)

# Answer
> 1 votes
# C++ TOO LARGE TO WIN

I wrote a full featured PPM drawing program in C++, with my own bitmapped font. Even stripping out all non necessary functions it's still huge compared to the answers here because of the definition for the font.

Anyway, here is the output for HELLO WORLD:

And the code:

## ppmdraw.h

```
#ifndef PPMDRAW_H
#define PPMDRAW_H

#include <fstream>
#include <sstream>
#include <map>
#include <bitset>
#include <vector>

struct pixel{
    unsigned char r;
    unsigned char g;
    unsigned char b;

    bool equals(pixel p){
        return (r == p.r && g == p.g && b == p.b);
    }
};

class PPMDraw
{
    public:
        PPMDraw(int w, int h);

        virtual ~PPMDraw();

        void fill(unsigned char r, unsigned char g, unsigned char b);

        void set_color(unsigned char r, unsigned char g, unsigned char b);

        void draw_point(int x, int y);

        void draw_char(int x, int y, char c);
        void draw_string(int x, int y, std::string text);

        bool save(std::string file);

    private:

        int width;
        int height;

        pixel * image;

        std::vector<bool> checked;

        unsigned char red;
        unsigned char green;
        unsigned char blue;

        void init_alpha();
        std::map<char, std::bitset<48> > font;

};

#endif // PPMDRAW_H

```

## ppmdraw.cpp

```
#include "PPMDraw.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cmath>
#include <map>
#include <bitset>
#include <vector>

// standard constructor
PPMDraw::PPMDraw(int w, int h){
    width = w;
    height = h;

    // make an array to hold all the pixels, r, g, b for each
    image = new pixel[width * height];

    // a bitset to use for functions that have to check which pixels have been worked on
    checked = std::vector<bool>();
    for(int i = 0; i < width * height; i++){
        checked.push_back(false);
    }

    init_alpha();
}

PPMDraw::~PPMDraw(){
    if(image != nullptr){
        delete[] image;
    }
}

void PPMDraw::fill(unsigned char r, unsigned char g, unsigned char b){
    for(int i = 0; i < width * height; i++){
        image[i + 0] = pixel{r, g, b};
    }
}

void PPMDraw::set_color(unsigned char r, unsigned char g, unsigned char b){
    red = r;
    green = g;
    blue = b;
}

void PPMDraw::draw_point(int x, int y){
    if(x >= 0 && x < width && y >= 0 && y < height){
        image[y * width + x] = pixel{red, green, blue};
    }
}

void PPMDraw::draw_char(int x, int y, char c){
    std::bitset<48> letter = font[c];
    int n = 47;
    for(int i = 0; i < 6; i++){
        for(int j = 0; j < 8; j++){
            if(letter[n]){
                draw_point(x + i, y + j);
            }
            n--;
        }
    }
}
void PPMDraw::draw_string(int x, int y, std::string text){
        for(unsigned int i = 0; i < text.length(); i++){
            draw_char(x + 6 * i, y, text[i]);
        }

}

bool PPMDraw::save(std::string file){
    std::ofstream save(file.c_str(), std::ios_base::out | std::ios_base::binary);
    if(save.is_open()){
        save << "P6" << std::endl;
        save << width << " " << height << std::endl;
        save << "255" << std::endl;
        unsigned char * temp = new unsigned char[height * width * 3];
        for(int i  = 0; i < height * width; i++){
            temp[i * 3 + 0] = image[i].r;
            temp[i * 3 + 1] = image[i].g;
            temp[i * 3 + 2] = image[i].b;
        }
        save.write(reinterpret_cast<const char *> (temp), height*width*3*sizeof(unsigned char));
        delete temp;
        save.close();
        return true;
    }else{
        return false;
    }

}

void PPMDraw::init_alpha(){
    // Define a simple font for drawing text
    font[' '] = std::bitset<48>  (std::string("000000000000000000000000000000000000000000000000"));
    font['!'] = std::bitset<48>  (std::string("000000000000000011110110000000000000000000000000"));
    font['"'] = std::bitset<48>  (std::string("000000001100000000000000110000000000000000000000"));
    font['#'] = std::bitset<48>  (std::string("001010001111111000101000111111100010100000000000"));
    font['$'] = std::bitset<48>  (std::string("001001000101010011111110010101000100100000000000"));
    font['%'] = std::bitset<48>  (std::string("000000100100110000010000011000001000010000000000"));
    font['&'] = std::bitset<48>  (std::string("000111001110001010110010110011000000001000000000"));
    font['\\'] = std::bitset<48>  (std::string("100000000110000000010000000011000000001000000000"));
    font['('] = std::bitset<48>  (std::string("000000000000000001111100100000100000000000000000"));
    font[')'] = std::bitset<48>  (std::string("000000001000001001111100000000000000000000000000"));
    font['*'] = std::bitset<48>  (std::string("010010000011000011100000001100000100100000000000"));
    font['+'] = std::bitset<48>  (std::string("000100000001000001111100000100000001000000000000"));
    font[','] = std::bitset<48>  (std::string("000000000000000000000110000000000000000000000000"));
    font['-'] = std::bitset<48>  (std::string("000100000001000000010000000100000001000000000000"));
    font['.'] = std::bitset<48>  (std::string("000000000000000000000100000000000000000000000000"));
    font['/'] = std::bitset<48>  (std::string("000000100000110000010000011000001000000000000000"));
    font['0'] = std::bitset<48>  (std::string("011111001000001010000010100000100111110000000000"));
    font['1'] = std::bitset<48>  (std::string("000000001000001011111110000000100000000000000000"));
    font['2'] = std::bitset<48>  (std::string("010011101001001010010010100100100111001000000000"));
    font['3'] = std::bitset<48>  (std::string("010001001000001010000010100100100110110000000000"));
    font['4'] = std::bitset<48>  (std::string("111100000001000000010000000100001111111000000000"));
    font['5'] = std::bitset<48>  (std::string("111001001001001010010010100100101001110000000000"));
    font['6'] = std::bitset<48>  (std::string("011111001001001010010010100100101000110000000000"));
    font['7'] = std::bitset<48>  (std::string("100000001000000010000110100110001110000000000000"));
    font['8'] = std::bitset<48>  (std::string("011011001001001010010010100100100110110000000000"));
    font['9'] = std::bitset<48>  (std::string("011000001001000010010000100100000111111000000000"));
    font[':'] = std::bitset<48>  (std::string("000000000000000001000100000000000000000000000000"));
    font[';'] = std::bitset<48>  (std::string("000000000000000001000110000000000000000000000000"));
    font['<'] = std::bitset<48>  (std::string("000000000001000000101000010001000000000000000000"));
    font['='] = std::bitset<48>  (std::string("001010000010100000101000001010000000000000000000"));
    font['>'] = std::bitset<48>  (std::string("000000000100010000101000000100000000000000000000"));
    font['?'] = std::bitset<48>  (std::string("010000001000000010001010100100000110000000000000"));
    font['@'] = std::bitset<48>  (std::string("011111001000001010111010101010100111001000000000"));
    font['A'] = std::bitset<48>  (std::string("011111101001000010010000100100000111111000000000"));
    font['B'] = std::bitset<48>  (std::string("111111101001001010010010100100100110110000000000"));
    font['C'] = std::bitset<48>  (std::string("011111001000001010000010100000100100010000000000"));
    font['D'] = std::bitset<48>  (std::string("111111101000001010000010100000100111110000000000"));
    font['E'] = std::bitset<48>  (std::string("111111101001001010010010100100101000001000000000"));
    font['F'] = std::bitset<48>  (std::string("111111101001000010010000100100001000000000000000"));
    font['G'] = std::bitset<48>  (std::string("011111001000001010000010100010100100110000000000"));
    font['H'] = std::bitset<48>  (std::string("111111100001000000010000000100001111111000000000"));
    font['I'] = std::bitset<48>  (std::string("100000101000001011111110100000101000001000000000"));
    font['J'] = std::bitset<48>  (std::string("000011000000001000000010000000101111110000000000"));
    font['K'] = std::bitset<48>  (std::string("111111100001000000010000001010001100011000000000"));
    font['L'] = std::bitset<48>  (std::string("111111100000001000000010000000100000001000000000"));
    font['M'] = std::bitset<48>  (std::string("111111101000000001100000100000001111111000000000"));
    font['N'] = std::bitset<48>  (std::string("111111100100000000100000000100001111111000000000"));
    font['O'] = std::bitset<48>  (std::string("011111001000001010000010100000100111110000000000"));
    font['P'] = std::bitset<48>  (std::string("111111101001000010010000100100001111000000000000"));
    font['Q'] = std::bitset<48>  (std::string("011111001000001010001010100001000111101000000000"));
    font['R'] = std::bitset<48>  (std::string("111111101001000010010000100110001111011000000000"));
    font['S'] = std::bitset<48>  (std::string("011000101001001010010010100100101000110000000000"));
    font['T'] = std::bitset<48>  (std::string("100000001000000011111110100000001000000000000000"));
    font['U'] = std::bitset<48>  (std::string("111111000000001000000010000000101111110000000000"));
    font['V'] = std::bitset<48>  (std::string("111110000000010000000010000001001111100000000000"));
    font['W'] = std::bitset<48>  (std::string("111111100000001000001100000000101111111000000000"));
    font['X'] = std::bitset<48>  (std::string("110001100010100000010000001010001100011000000000"));
    font['Y'] = std::bitset<48>  (std::string("110000000010000000011110001000001100000000000000"));
    font['Z'] = std::bitset<48>  (std::string("100001101000101010010010101000101100001000000000"));
    font['['] = std::bitset<48>  (std::string("000000001111111010000010100000100000000000000000"));
    font['\''] = std::bitset<48>  (std::string("100000000110000000010000000011000000001000000000"));
    font[']'] = std::bitset<48>  (std::string("000000001000001010000010111111100000000000000000"));
    font['^'] = std::bitset<48>  (std::string("001000000100000010000000010000000010000000000000"));
    font['_'] = std::bitset<48>  (std::string("000000100000001000000010000000100000001000000000"));
    font['`'] = std::bitset<48>  (std::string("000000001000000001000000000000000000000000000000"));
    font['a'] = std::bitset<48>  (std::string("000001000010101000101010001010100001111000000000"));
    font['b'] = std::bitset<48>  (std::string("111111100001001000010010000100100000110000000000"));
    font['c'] = std::bitset<48>  (std::string("000111000010001000100010001000100001010000000000"));
    font['d'] = std::bitset<48>  (std::string("000011000001001000010010000100101111111000000000"));
    font['e'] = std::bitset<48>  (std::string("000111000010101000101010001010100001101000000000"));
    font['f'] = std::bitset<48>  (std::string("000100000111111010010000100100000000000000000000"));
    font['g'] = std::bitset<48>  (std::string("001100100100100101001001010010010011111000000000"));
    font['h'] = std::bitset<48>  (std::string("111111100001000000010000000100000000111000000000"));
    font['i'] = std::bitset<48>  (std::string("000000000000000001011110000000000000000000000000"));
    font['j'] = std::bitset<48>  (std::string("000000100000000100000001010111100000000000000000"));
    font['k'] = std::bitset<48>  (std::string("111111100000100000010100001000100000000000000000"));
    font['l'] = std::bitset<48>  (std::string("000000000000000011111110000000000000000000000000"));
    font['m'] = std::bitset<48>  (std::string("000111100001000000001000000100000001111000000000"));
    font['n'] = std::bitset<48>  (std::string("001111100001000000010000000100000001111000000000"));
    font['o'] = std::bitset<48>  (std::string("000111000010001000100010001000100001110000000000"));
    font['p'] = std::bitset<48>  (std::string("001111110010010000100100001001000001100000000000"));
    font['q'] = std::bitset<48>  (std::string("000110000010010000100100001001000011111100000000"));
    font['r'] = std::bitset<48>  (std::string("000000000011111000010000000100000000000000000000"));
    font['s'] = std::bitset<48>  (std::string("000000000001001000101010001010100010010000000000"));
    font['t'] = std::bitset<48>  (std::string("000000000010000011111110001000000000000000000000"));
    font['u'] = std::bitset<48>  (std::string("000111000000001000000010000000100001110000000000"));
    font['v'] = std::bitset<48>  (std::string("000110000000010000000010000001000001100000000000"));
    font['w'] = std::bitset<48>  (std::string("000111100000001000000100000000100001111000000000"));
    font['x'] = std::bitset<48>  (std::string("001000100001010000001000000101000010001000000000"));
    font['y'] = std::bitset<48>  (std::string("001100000000100000000111000010000011000000000000"));
    font['z'] = std::bitset<48>  (std::string("010001100100101001010010011000100000000000000000"));
    font['{'] = std::bitset<48>  (std::string("000000000000000001101100100100100000000000000000"));
    font['|'] = std::bitset<48>  (std::string("000000000000000011111110000000000000000000000000"));
    font['}'] = std::bitset<48>  (std::string("000000000000000010010010011011000000000000000000"));
    font['~'] = std::bitset<48>  (std::string("000100000010000000010000000010000001000000000000"));
}

```

## main.cpp

```
#include "PPMDraw.h"
#include <iostream>

int main(){
    // ask for input
    std::string input;
    std::cout << "ENTER YOUR TEXT" << std::endl;
    getline(std::cin, input);
   // get size for image
  int width = input.size() * 6;
   PPMDraw image = PPMDraw(width, 8);
   image.fill(255, 255, 255);
   image.set_color(0, 0, 0);
   image.draw_string(0, 0, input);
   image.save("text.ppm");
}

```

## Makefile

```
CC = g++
CFLAGS = -Wall -c -std=c++11
LFLAGS = -Wall
OBJS = main.o PPMDraw.o

list: $(OBJS)
    $(CC) $(LFLAGS) $(OBJS) -o text2ppm

main.o: PPMDraw.h
    $(CC) $(CFLAGS) main.cpp

PPMDraw.o: PPMDraw.h
    $(CC) $(CFLAGS) PPMDraw.cpp

clean:
    rm *.o main

```

If you're interested, the full PPMDraw library is here:

# Answer
> 1 votes
# SmileBASIC, 231 bytes

```
LINPUT C$?"P1
?8,LEN(C$)*8WHILE""<C$A=ASC(SHIFT(C$))D=ASC("*N.JZ`;O:²ÞøäüÄho"[A-65+12*(A<34)+47*(A<47)])FOR I=0TO 4B$=BIN$(VAL("7535712074617252"[D>>5<<1OR 1AND D>>I]),8)WHILE""<B$?POP(B$),
WEND?NEXT?"0 "*24WEND

```

Each character contains only 2 different row patterns, chosen from a "palette" of 8 combinations. The data for each symbol is stored in 1 byte, with the palette stored separately.

---
Tags: code-golf, string, graphical-output
---