18
\$\begingroup\$

Your task is to write a computer program or function that takes a list of positive integers of at least length 2 and determines if they are a "zigzag". A sequence is a zigzag if and only if the numbers alternate in being larger and smaller than the number that comes before them. For example \$[1,2,0,3,2]\$ and \$[4,2,3,0,1]\$ are zigzags but \$[1,2,0,0,3,1]\$ and \$[1,2,3,1]\$ are not.

For your decision you should output one of two different consistent values for each possibility (zigzag and not zigzag).

The code-points of your program or function must also be a zigzag itself. This means that when you take the sequence of code-points it should be a zigzag.

This is so answers will be scored in bytes with fewer bytes being better.

\$\endgroup\$
  • 1
    \$\begingroup\$ A penalty for each non-zigzag in the code points might have been another approach, to allow for a wider variety of languages to participate. \$\endgroup\$ – ngm Jul 4 '18 at 15:31
  • 5
    \$\begingroup\$ @ngm I disagree. Introducing bonuses/penalties would make users come up with multiple possible answers (e.g. short one + penalty vs long + no penalty), making answering process slower. Plus, the amount of penalty given will be quite arbitrary, meaning the scoring process wouldn't be that objective. \$\endgroup\$ – JungHwan Min Jul 4 '18 at 15:34
  • 2
    \$\begingroup\$ Should we take Unicode code points or the code points of the encoding we're using? \$\endgroup\$ – Dennis Jul 4 '18 at 15:49
  • 1
    \$\begingroup\$ @Dennis The code-points of the encoding you are using. \$\endgroup\$ – Wheat Wizard Jul 4 '18 at 15:50
  • 2
    \$\begingroup\$ @Dennis of course, technically that is true. However, we already established that awarding bonuses for code-golf isn't that ideal because they detract from the main challenge. Penalty in this case would be a negative bonus. \$\endgroup\$ – JungHwan Min Jul 4 '18 at 17:00

12 Answers 12

7
\$\begingroup\$

Jelly, 5 bytes

IṠIỊẸ

Returns \$0\$ (zigzag) or \$1\$ (not zigzag).

The code points are \$[73, 205, 73, 176, 174]\$ in the Jelly code page.

Try it online!

How it works

IṠIỊẸ  Main link. Argument: A (array)

I      Increments; compute the forward differences of A.
 Ṡ     Take their signs.
       A is zigzag iff the signs are alternating.
  I    Take the increments again.
       Alternating signs result in an increment of -2 or 2.
       Non-alternating signs result in an increment of -1, 0, or 1.
   Ị   Insignificant; map each increment j to (|j| ≤ 1).
    Ẹ  Any; return 0 if all results are 0, 1 in any other case.
\$\endgroup\$
4
\$\begingroup\$

Haskell, 87 bytes

f(a:b:c:d)|(>)a b,b<c=f$b:c:d |(<)a b,b>c=f$b:c:d |1>0=1>12
f[a ] =1<12
f(a:b:_)= a/= b

Try it online!

I wanted to get the ball rolling in terms of Haskell answers. I can't see a way to improve this yet, but I am convinced it can be done. I'm looking forward to what people can do from here.

\$\endgroup\$
4
\$\begingroup\$

MATL, 9 bytes

dt?ZSd]pA

Try it online! Or verify all test cases

My first ever MATL program! The penultimate p was added for the zigzag requirement.

Explanation:

d    %take the difference between successive elements of input
t    %duplicate that
?    %if that is all non-zero
  ZS %take the sign of those differences (so input is all `-1`s and `1`s now)
  d  %take the difference of that (so if there are successive `1`s or `-1`s, this will have a 0)
]    %end-if
p    %take the product of topmost stack vector (will be 0 if either the original difference or 
     % the difference-of-signs contained a 0)
A    %convert positive products to 1 (since OP specifies "you should output one of two different consistent values for each possibility ")
\$\endgroup\$
  • \$\begingroup\$ Thank you! Yeah, like I mentioned in the answer, I added it only for the zigzag requirement (since the code itself has to go zigzag). ] apparently lives between capital letters and small letters, so d to ] and ] to A would both have been decrements, which isn't allowed. So the p is mainly there to have a codepoint increment between the two. \$\endgroup\$ – sundar Jul 4 '18 at 20:18
  • 1
    \$\begingroup\$ Oh, I totally forgot about that requirement. That makes the answer more impressive! \$\endgroup\$ – Luis Mendo Jul 4 '18 at 20:29
4
\$\begingroup\$

Python 2, 225 223 161 139 bytes

-2 bytes thanks to Jakob
-62 bytes thanks to Dennis

e={eval }.pop()
p ="i"+"n"+"p"+"u"+"t ( "
s=e(p +")")
e(p +"` a"+"l"+"l([(x<y>z)+(x>y<z)f"+"o"+"r x,y,z i"+"n zip(s,s [1: ],s [2: ])])` )")

Try it online!

Credits for the bumpy algorithm goes to this answer

input, print, exec, def and lambda aren't bumpy so I only got eval left, which is stored on e
There are 2 main ways to bypass the restriction, placing "+" or between the non-bumpy pairs, I opted for the former ( is shorter for each use, but it would need replace(' ','') resulting in more bytes)
Since print isn't bumpy, I can't use it directly, and since it isn't a funcion I can't use it inside eval(), so I had to use input(result) to output the result

\$\endgroup\$
  • \$\begingroup\$ Nice. You can substitute ' ' * 0 for ' ' [1: ]. \$\endgroup\$ – Jakob Jul 4 '18 at 18:17
  • \$\begingroup\$ You can use input(text) to write to STDOUT. \$\endgroup\$ – Dennis Jul 4 '18 at 18:38
4
\$\begingroup\$

K (ngn/k), 23 bytes

{*/ 0 >1_ *':1_ -': x }

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Why are the spaces needed? \$\endgroup\$ – Zacharý Jul 4 '18 at 16:14
  • \$\begingroup\$ @Zacharý the k code itself wouldn't be bumpy without them \$\endgroup\$ – ngn Jul 4 '18 at 16:15
  • \$\begingroup\$ What do you mean by that? Is it just ngn/k that neads the spaces \$\endgroup\$ – Zacharý Jul 4 '18 at 16:17
  • 3
    \$\begingroup\$ @Zacharý This challenge is restricted-source, and the restriction is that the code must be a zigzag. \$\endgroup\$ – Erik the Outgolfer Jul 4 '18 at 16:18
  • \$\begingroup\$ Whoops, forgot about that, too. \$\endgroup\$ – Zacharý Jul 4 '18 at 16:19
3
\$\begingroup\$

Ohm v2, 5 bytes

δyδ½Å

Try it online!

The indices of the characters are \$[131,121,131,16,165]\$ in the linked code page.

How it works

δyδ½Å – Full program / Single-argument block.
δy    – The signs of the deltas of the input 
  δ   – The differences of the signs. Results in a sequences of 2's or -2's for
        bumpy arrays, as the signs alternate, giving either -1-1=-2 or 1-(-1)=2.
    Å – Check if all elements yield truthy results when...
   ½  – Halved.
\$\endgroup\$
2
\$\begingroup\$

Japt -!, 16 14 bytes

Well, this ain't pretty but I'm just happy it works!

Outputs true for zig-zag or false if not.

ä'- m'g ä'a èÍ

Try it

Codepoints are [228,39,45,32,109,39,103,32,228,39,97,32,232,205] and included as the test in the link above.


Explanation

                   :Implicit input of array
ä'-                :Consecutive differences
    m'g            :Map signs
        ä'a        :Consecutive absolute differences
             Í     :Subtract each from 2
            è      :Count the truthy (non-zero) elements
                   :Implicitly negate and output resulting boolean.
\$\endgroup\$
  • \$\begingroup\$ @KamilDrakari, normally you'd be right but, sadly, they're necessary to meet the restricted-source requirements of the challenge. Otherwise this could be 10 bytes. \$\endgroup\$ – Shaggy Jul 4 '18 at 17:09
  • \$\begingroup\$ Oh, I didn't see that this was restricted-source. My bad \$\endgroup\$ – Kamil Drakari Jul 4 '18 at 17:11
  • \$\begingroup\$ @KamilDrakari, don't worry; it looks like you weren't the only one. \$\endgroup\$ – Shaggy Jul 4 '18 at 18:12
1
\$\begingroup\$

Jelly, 6 bytes

IṠµaIẠ

Try it online!

Returns 1 for truthy, 0 for falsy.

Codepoints: [73, 205, 9, 97, 73, 171] (valid)

\$\endgroup\$
1
\$\begingroup\$

Perl 6, 61 bytes

{ [*] ($_[{1…*} ] Z<@$_)Z+^ ($_[{1…*} ] Z>@$_[{2…*} ])}

Try it online!

The code points are:

(123 32 91 42 93 32 40 36 95 91 123 49 8230 42 125 32 93 32 90 60 64 36 95 41 90 43 94 32 40 36 95 91 123 49 8230 42 125 32 93 32 90 62 64 36 95 91 123 50 8230 42 125 32 93 41 125)

And yes, those are unicode characters in there. This is more or less my original solution, with a few spaces and curly braces mixed in.

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 10 bytes

¥DÄ/¥(Ä2QP

Try it online!

Explanation

¥           # calculate deltas of input
 DÄ/        # divide each by its absolute value
    ¥       # calculate deltas
     (      # negate each
      Ä     # absolute value of each
       2Q   # equals 2
         P  # product

Code points are: [165, 68, 196, 47, 165, 40, 196, 50, 81, 80]

\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 62 60 bytes

a=> a.map(n=> e&=!~(p | q)| q <(q=p)^ p <(p=n), e=p=q=~ 0)|e

Try it online!

Code points:

61 3d 3e 20 61 2e 6d 61 70 28 6e 3d 3e 20 65 26
3d 21 7e 28 70 20 7c 20 71 29 7c 20 71 20 3c 28
71 3d 70 29 5e 20 70 20 3c 28 70 3d 6e 29 2c 20
65 3d 70 3d 71 3d 7e 20 30 29 7c
\$\endgroup\$
  • 2
    \$\begingroup\$ Luckily map is zigzag! \$\endgroup\$ – Neil Jul 4 '18 at 17:07
0
\$\begingroup\$

05AB1E, 8 bytes

¥.±¥Ä2/P

Returns 1.0 for zigzagging and 0.0 for non-zigzagging sequences.

The code points are [164,108,176,164,195,2,109,25] in the 05AB1E code page.

Try it online.

Explanation:

¥           # Take the deltas of the (implicit) input-list
            #  i.e. [1,2,0,3,2,3] → [1,-2,3,-1,1]
 .±         # Calculate the sign for each of them (-1 if a<0; 0 if 0; 1 if a>0)
            #  i.e. [1,-2,3,-1,1] → [1,-1,1,-1,1]
   ¥        # Calculate the deltas of those
            #  i.e. [1,-1,1,-1,1] → [-2,2,-2,2]
    Ä       # Take the absolute value of each
            #  i.e. [-2,2,-2,2] → [2,2,2,2]
     2/     # Divide them by 2
            #  i.e. [2,2,2,2] → [1.0,1.0,1.0,1.0]
            # (`;` {halve} would have been shorter, but doesn't comply to the challenge)
       P    # Take the product of the list resulting in either 1.0 or 0.0
            #  i.e. [1.0,1.0,1.0,1.0] → 1.0
\$\endgroup\$

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.