9
\$\begingroup\$

(Inspired quite heavily by this challenge)

In the original challenge you were to make a horizontal zipper, however, looking down at my hoodie, my zipper is much more vertical (and a little bit broken) :P

Input

You will be given a single string (s), and a pair of integers (a and b) in any format. Where:

a < (length-of-s / 2)

b <= ((length-of-s / 2) - (a + 1)) / 6

Output and Scoring

Your program may be a full program or a function that produces a single string with the correct formatting or an array (one item per line) you choose. Leading and trailing spaces and newlines are optional as long as the characters of the output all line up. eg.

["f     r"],["u     e"],["l     p"],["l     p"],["y     i"],[" o   z "],["  p n  "],["   e   "]

or

"f     r\nu     e\nl     p\nl     p\ny     i\n o   z \n  p n  \n   e   "

are acceptable outputs for test case 1

Standard Loopholes are forbidden and the shortest code in bytes wins. Any answer with no explanation will not be able to win.

Explanation

To make a zipper you must first remove any whitespace or newline characters from the string. And then fold it in half like so (see test case 1 for more)

abcde fgh ij klmnopqr becomes the two strings below, notice the second half is reversed.

abcdefghi and rqponmlkj

Then starting at the beginnning of each string we place the same indexed character from each substring onto one line with the spacing determined as follows:

[0] a     r -- (a-3) five spaces
[1]  b   q  -- (a-2) three spaces
[2]   c p   -- (a-1) one space
[3]    d    -- (a)
[4]    o
[5]    e
[6]    n
etc...

That makes the base for our zipper. Now the integers a and b.

a is the location of our zip. To determine where the zipper is located we use the index of our output lines as the point to close our zip. eg the square bracketed [3] in my above example is where the zip is.

Before our zipper reaches a close, the above zipper must remain at a five space gap until it reaches a-2 where it closes to 3 spaces and a-1 where it closes to 1 space. Just to be clear, in the above example a = 3 (0-indexed)


b is the number of holes in my zipper. To create a hole in the zip, we split the string out again with some spacing.

Starting with the index of the example hole as h we increase the spacing of h-1 to one space, h to 3 spaces and h+1 back to one space, leaving h-2 and h+2 with just one character per line as they are counted as part of the hole.

After the zip and between each of the holes there must be a gap of one character so our zip looks fully connected between holes.

You get to choose the indexes of the holes, but they must all be present within the 'closed' section of the zipper.

Again using 'abcdefghi' and 'rqponmlkj'

[0] a     r -- (open)
[1]  b   q  -- (closing)
[2]   c p   -- (closing)
[3]    d    -- zip 
[4]    o    -- gap
[5]    e    -- start of hole  (h-2)
[6]   n f   -- one space      (h-1)
[7]  m   g  -- three spaces   (hole h)
[8]   l h   -- one space      (h+1)
[9]    k    -- end of hole    (h+2)
[10]   i    -- gap
[11]   j

I'm hoping that all makes sense, if not ask away. In anticipation of questions, you may notice that the character p in my second example is present in the second substring, however, appears at the beginning of its line, this is intended, you must follow the sequence of 1-2-1-2-1-2, taking a character from each half in-turn regardless of its position in the output.

Test Cases

"fully open zipper", 7, 0                     | "fully closed zipper", 0, 0
fullyope reppizn                              | fullyclos reppizde
                                              |
f     r   *Note: i particularly like this     | f
u     e     example as it shows the reversing | r
l     p     of the second half of the string  | u
l     p     causes the words to wrap from top | e
y     i     left to the  bottom and back up   | l
 o   z      the right side                    | p
  p n                                         | l
   e                                          | p
                                              | y
                                              | i
                                              | c
                                              | z
                                              | l
                                              | d
                                              | o
                                              | e
                                              | s
  • Column 1: "stupidly long unbroken zipper that shouldnt exist on any decent hoodie", 24, 0
  • Column 2: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris faucibus molestie mi. Aliquam nec fringilla ipsum", 15, 5

:

s     e          |          L     m
t     i          |          o     u
u     d          |          r     s
p     o          |          e     p
i     o          |          m     i
d     h          |          i     a
l     t          |          p     l
y     n          |          s     l
l     e          |          u     i
o     c          |          m     g
n     e          |          d     n
g     d          |          o     i
u     y          |          l     r
n     n          |           o   f
b     a          |            r c
r     n          |             s
o     o          |             e
k     t          |             i
e     s          |            n t
n     i          |           m   a
z     x          |            a m
i     e          |             u
 p   t           |             e
  p n            |             q
   e             |             t
   d             |             i
   r             |             ,
   l             |            l c
   t             |           A   o
   u             |            . n
   h             |             i
   o             |             s
   a             |             m
   h             |            e e
   t             |           c   i
   s             |            t t
                 |             e
                 |             s
                 |             t
                 |             s
                 |             u
                 |             l
                 |             r
                 |             o
                 |             a
                 |             m
                 |             d
                 |             s
                 |             i
                 |             u
                 |             p
                 |            b i
                 |           i   s
                 |            c c
                 |             u
                 |             i
                 |             a
                 |             n
                 |             f
                 |             g
                 |             s
                 |             e
                 |             i
                 |            l r
                 |           i   u
                 |            t a
                 |             .
                 |             M

EDIT: Added Test Cases

"abcde fgh ij klmnopqr","3","1"

a     r
 b   q
  c p
   d
   o
   e
  n f
 m   g
  l h
   k
   i
   j
\$\endgroup\$
  • 1
    \$\begingroup\$ I think I like this challenge but I've read it three times and I'm still not 100% on exactly what I'm supposed to do. I think it would be worth making the rules and specs shorter and clearer rather than full paragraphs. \$\endgroup\$ – TheLethalCoder Aug 15 '17 at 14:02
  • \$\begingroup\$ I can give it a go :P \$\endgroup\$ – MysticVagabond Aug 15 '17 at 14:07
  • \$\begingroup\$ any clearer? @TheLethalCoder \$\endgroup\$ – MysticVagabond Aug 15 '17 at 14:23
  • \$\begingroup\$ Hi, I removed the printable-ascii tag, as that is used for restricted-source challenges rather than output layout. \$\endgroup\$ – AdmBorkBork Aug 15 '17 at 14:30
  • \$\begingroup\$ I saw that thanks @AdmBorkBork I edited again to alphabetise the explanation to hopefully make it clearer to see where the characters are coming from. \$\endgroup\$ – MysticVagabond Aug 15 '17 at 14:36
1
\$\begingroup\$

PHP 7.1, 421 412 218 195 192 191 195 bytes

for([,$s,$a,$b]=$argv;$i-$k<strlen($s=strtr($s,[" "=>""]));$p=0)for($t=++$y>$a-2?$y>=$a?$y>$a?$b-->0?_4_4_4_31_23_31:_4:_31:_23:_15;$c=$t[$p++];)echo$c<_?str_pad("",$c).$s[++$f&1?$i++:$k-=1]:"
";

Run with -nr or try it online.
Prints a leading, but no trailing newline and one leading space in each line.

\$\endgroup\$
  • \$\begingroup\$ TIO gives me this error: <br /> <b>Parse error</b>: syntax error, unexpected ',', expecting ']' in <b>[...][...]</b> on line <b>7</b><br /> \$\endgroup\$ – MysticVagabond Aug 16 '17 at 8:18
  • \$\begingroup\$ @Xynos Set the PHP version to 7.1 \$\endgroup\$ – Titus Aug 16 '17 at 8:43
  • \$\begingroup\$ your answer places the hole too close to the zip, ill add the correct output for your input as a test case :) \$\endgroup\$ – MysticVagabond Aug 16 '17 at 8:57
  • \$\begingroup\$ @Xynos gaps added \$\endgroup\$ – Titus Aug 16 '17 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.