Skip to content

Instantly share code, notes, and snippets.

@bodhi
Last active August 29, 2015 14:26
Show Gist options
  • Save bodhi/1d748f4f4999f769b6aa to your computer and use it in GitHub Desktop.
Save bodhi/1d748f4f4999f769b6aa to your computer and use it in GitHub Desktop.
The Telegram Problem

Implement the function handle to accept lines of text and output reformatted text, wrapped at columnWidth characters, without breaking words.

So

Yr brunch Godard, readymade pug Pinterest gastropub put a bird on it Tumblr.
Sartorial swag beard, selvage bitters tofu vinyl.
Tattooed kogi organic scenester heirloom, cred four loko cardigan cray meh Portland master cleanse photo booth Shoreditch farm-to-table.

when wrapped at 30 characters becomes

Yr brunch Godard, readymade  
pug Pinterest gastropub put a  
bird on it Tumblr. Sartorial  
swag beard, selvage bitters  
tofu vinyl. Tattooed kogi  
organic scenester heirloom,  
cred four loko cardigan cray  
meh Portland master cleanse  
photo booth Shoreditch  
farm-to-table.  

Breaking like this:

Yr brunch Godard, readymade pu  
g Pinterest gastropub put a bi  
rd on it Tumblr. Sartorial swa  
g beard, selvage bitters tofu   
vinyl. Tattooed kogi organic s  
cenester heirloom, cred four l  
oko cardigan cray meh Portland  
 master cleanse photo booth Sh  
oreditch farm-to-table.  

is incorrect.

package main
import (
"bufio"
"os"
"strconv"
)
var columnWidth uint64 // How long each line should be
func handle(line string) {
// Implement this!
}
func main() {
columnWidth, _ = strconv.ParseUint(os.Args[1], 10, 8)
input := bufio.NewReader(os.Stdin)
for line, err := input.ReadString('\n'); err == nil; line, err = input.ReadString('\n') {
handle(line)
}
}
@ericwo
Copy link

ericwo commented Jul 31, 2015

BUG:

run go main.go 5

INPUT:

Yr brunch Godard, readymade pug Pinterest gastropub put a bird on it Tumblr. Sartorial swag beard, selvage bitters tofu vinyl. Tattooed kogi organic scenester heirloom, cred four loko cardigan cray meh Portland master cleanse photo booth Shoreditch farm-to-table.

OUTPUT:

Yr
brunch
Godard,
readymade
pug
Pinterest
gastropub
put
a
bird
on
it
Tumblr.
Sartorial
swag
beard,
selvage
bitters
tofu
vinyl.
Tattooed
kogi
organic
scenester
heirloom,
cred
four
loko
cardigan
cray
meh
Portland
master
cleanse
photo
booth
Shoreditch

farm-to-table.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment