r/dailyprogrammer • u/jnazario 2 0 • Apr 08 '15
[2015-04-08] Challenge #209 [Intermediate] Packing a Sentence in a Box
Description
You're moving, and you have a bunch of sentences to pack up. To accomplish this, you'll be using a small program you should write to pack these sentences efficiently into a box for shipping. Leave no unused space, you have a lot of sentences to pack and you don't want to waste precious shipping space.
For this challenge you're free to choose any legal dimensions of a rectangle, and you're free to start in any position you wish. Your program (and thus your output) should walk the grid to adjacent squares using only left, right, up, down (no diagonal moves allowed).
Input
You'll be given a sentence to pack into a box
EVERYWHERE IS WITHIN WALKING DISTANCE IF YOU HAVE THE TIMEOutput
Your program should emit the starting position (column and row, 1-indexed) for the sentence, and then the box with the sentence packed into it. The sentence must be packed in the original word order with only spaces removed. You can chose your own box dimensions. The above example is a 49 character sentence (minus spaces), so that's a 7x7 box. Here's one possible solution:
4 4
E T I M E D I
H W S I E G S
T I E V R N T
E T R E E I A
V H Y W H K N
A I N W A L C
H U O Y F I EChallenge Input
IT IS RAINING CATS AND DOGS OUT THEREChallenge Output
Here's one possible solution
1 1
I T I N I
E I A G N
R S R C A
E G O D T
H S O D S
T T U N ACredit
Many thanks to /u/Godspiral for the suggestion. Got any cool challenge ideas? Submit them to /r/DailyProgrammer_Ideas!
4
u/Godspiral 3 3 Apr 08 '15
I don't remember suggesting this. A much more interesting solution than this would be to draw a hilbert curve (or similar) to use to select the letters.
Here is a boring back and forth solution in J
3
u/NoobOfProgramming Apr 08 '15
Can letters be re-used?
1
u/jnazario 2 0 Apr 08 '15
here's a simple back and forth example in scala:
sadly my hamiltonian path code is b0rked ... yields the following:
5
u/franza73 Apr 08 '15 edited Apr 08 '15
Perl solution. Makes the box as close to a square as possible. Aways begin at top left of the box.
Here's the output:
5
u/chunes 1 2 Apr 08 '15 edited Apr 08 '15
Java:
Output:
It always just starts at 1, 1. Pretty simple.
4
u/Elite6809 1 1 Apr 08 '15
My Haskell solution, also available on Gist:
3
u/cvpcs Apr 08 '15
C#
Tries to create a box as close to a square as possible, picks random starting locations, and generates a random path each time.
Leverages recursion and stacks to keep track of its path so far and double-back if it hits a dead-end. Could potentially lead to a stack overflow depending on how long the sentence fed into the program is, so reworking it to be iterative would be more fault-tolerant.
3
u/dohaqatar7 1 1 Apr 08 '15
Haskell
3
u/patrickwonders Apr 08 '15
Am I missing something? Can I not just take all of the spaces out and call it an n-by-1 box that starts at (1,1) and always moves left?
2
u/adrian17 1 4 Apr 08 '15
Python 3.
First, a "snake" shape:
As a one-liner:
Output:
Second, a spiral shape:
Output:
2
u/Xilov 0 1 Apr 08 '15
C
That works, right ? :D
2
u/codeman869 Apr 09 '15
Ruby
3
u/knrDev Apr 08 '15
F#
Out:
1
u/hutsboR 3 0 Apr 08 '15 edited Apr 08 '15
Elixir: Clever little solution that generates a back and forth rectangle.
Usage:
1
u/ffs_lemme_in Apr 08 '15
C++ solution:
python test:
1
u/hedzup456 Apr 08 '15
Horrendously inefficient Java solution, written by a terrible programmer! (On that note, feel free to advise on ways to improve.)
That gives an output of:
1
u/[deleted] Apr 08 '15 edited Apr 08 '15
Ok, here's my answer in Rust, as requested. It solves the problem only for strings with a non-prime length, and it does it in the most boring way possible.
Git repository available here: https://github.com/archer884/pack
Edit for posterity:
rustc 1.0.0-nightly (d9146bf8b 2015-04-07) (built 2015-04-08)
(I like my regex!() macro too much to go to the beta.)
Another edit: I just realized the
iter::repeat(&' ')I use for padding is only useful in the event of prime-length strings, which don't work here because of the way they interact with themiddle_factors()--that is, the only factors returned are always (1, n).1
u/mozem_jebat Apr 08 '15
C, Because i dont like fun.
1
u/elerah Apr 08 '15
Objective C solution. Any critiques and improvements are welcomed
Output: Starting at 1x1 because i'm lazy
1
u/marchelzo Apr 08 '15
Cheaty one-liner in Haskell:
1
u/dohaqatar7 1 1 Apr 09 '15
Java
This solution in java makes use of the new streams in Java 8.
1
u/razeal113 Apr 10 '15
python submission
sample input/output
1
u/ekosynth Apr 10 '15
PHP This side to side snakey solution only works with odd numbered strings (after removing spaces, of course.)
Output
1
u/curtmack Apr 10 '15 edited Apr 10 '15
Groovy
Generates a random path by using DFS to generate a maze of appropriate dimensions, converting it into a unicursal maze by bisecting each passage and closing the loop, "solving" the maze, and then mapping the letters of the sentence along the path. Because it bisects passages to generate the unicursal maze, the input letters have to have a length that's a multiple of 4, and the width and height of the finished box each have to be even. It keeps trying different lengths until it finds one that can meet these criteria, then pads the string with % to make it that length.
The code is actually too long for a reddit comment, so I put it up as a gist here.
1
u/[deleted] Apr 19 '15
My first r/dailyprogrammer submission: https://gist.github.com/toddbranch/7c1730c7989ce24867a8
1
u/errorseven May 08 '15 edited May 08 '15
AutoHotkey - Only outputs 1,1 ...
Output:
1
u/Zifendale May 21 '15
Python 3.3 solution, first submission.
1
u/[deleted] Apr 08 '15 edited Apr 08 '15
What do you guys think, should I put this in my resumé? :P
0
u/king_m1k3 Apr 08 '15 edited Apr 08 '15
Python 2.7 - Always starts at (1, 1), wraps back and forth in as close of a square as possible
Output:
0
u/[deleted] Apr 08 '15
[deleted] — view deleted comment
0
u/[deleted] Apr 08 '15
[deleted] — view deleted comment
0
u/[deleted] Apr 08 '15
[deleted] — view deleted comment