In for a bumpy ride

18

3

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.

Post Rock Garf Hunter

Posted 2018-07-04T15:24:46.970

Reputation: 55 382

1A penalty for each non-zigzag in the code points might have been another approach, to allow for a wider variety of languages to participate. – ngm – 2018-07-04T15:31:54.827

7@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. – JungHwan Min – 2018-07-04T15:34:11.787

What about [4,1] is consider a zig zag? I ask because it is just a line going down and doesnt zig zag – Luis felipe De jesus Munoz – 2018-07-04T15:43:25.993

@LuisfelipeDejesusMunoz Yes it is a zigzag. All of the requirements are met. – Post Rock Garf Hunter – 2018-07-04T15:45:34.710

2Should we take Unicode code points or the code points of the encoding we're using? – Dennis – 2018-07-04T15:49:45.153

1@Dennis The code-points of the encoding you are using. – Post Rock Garf Hunter – 2018-07-04T15:50:36.653

@JungHwanMin I agree that it's arbitrary, but I can't see how it's non objective. – user202729 – 2018-07-04T16:02:05.170

Code points or byte values? Are you sure? – user202729 – 2018-07-04T16:04:41.843

@user202729 It is as the challenge says code-points. – Post Rock Garf Hunter – 2018-07-04T16:06:15.363

@user202729 It would be difficult to come up with a "one-size-fits-all" penalty. There would still be room for unfairness. Plus, choosing an arbirtary number isn't objective in the first place because it would lack thorough, objective criteria.. – JungHwan Min – 2018-07-04T16:10:51.950

1@JungHwanMin Winning criteria have to be applied objectively, not chosen objectively. – Dennis – 2018-07-04T16:14:46.950

1@Dennis an arbitrarily-chosen winning criterion would still detract from the challenge, imho. Similar to abitrary I/O rules. Perhaps I chose the wrong word in my initial comment. – JungHwan Min – 2018-07-04T16:27:54.640

1@JungHwanMin All winning criteria are essentially arbitrary. We're just more used to plain code golf. A potential solution that allows all languages to compete, without providing any length-bumpiness trade-offs, would be to score submissions by penalty points and use the byte count as tie breaker. – Dennis – 2018-07-04T16:31:26.290

This is a program which find all encodings (known to Mathematica) that make the provided byte string bumpy: (won't work in some cases, see the comment) (note that your program should at least contains some unprintable character for it to be somewhat useful) – user202729 – 2018-07-04T16:35:52.903

Try it online! – user202729 – 2018-07-04T16:35:55.630

2

@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.

– JungHwan Min – 2018-07-04T17:00:41.510

Sometimes bonuses detract from the main challenge, sometimes they are the main challenge. In this particular case, I would say that it's the former, Dennis' solution may be applied here. (and it won't invalidate any answer as they all have optimal zigzag score) – user202729 – 2018-07-05T03:42:26.207

For convenience, some non-zigzag terms (in ASCII/Unicode): "while", "for", "reduce", "def", "function", "else", "loop", "foldl", "foldr","unless". Some zigzag terms: "map", "lambda", "until". – sundar - Reinstate Monica – 2018-07-05T14:59:55.600

I like your challenge, but my language can't zigzag. The type for a number doesn't zigzag so I can't even start. I'd suggest a bonus of -100 off bytes for zigzag code. If the language supports it, the playing field could be even as everyone can get the bonus. If the language doesn't, we can still play. – JohnWells – 2018-07-13T03:09:42.077

@JohnWells The community has historically been strongly against bonuses of any sort, and for good reason. I don't plan on changing the scoring for the challenge. However I will say a couple of other things. The first thing is that input is pretty flexible so you might be able to get around the problem you have currently with a little creative thinking. You could for example take input as a string, which might get you around the problem. However without knowing the language at hand it's hard to say. – Post Rock Garf Hunter – 2018-07-13T03:37:03.807

@JohnWells Second if you like this sort of challenge you might want to checkout other [tag:restricted-source] challenges. There are a similar challenges in terms of self identification, but each is a unique challenge and it is likely that some will be doable in your choice of language. Lastly I will point out that if you do find the code-golf part of this fun, but can't work with the restriction, you can still have fun golfing even if you can't post an answer. I always find the most fun part of code-golf to be competing with myself rather than with others. – Post Rock Garf Hunter – 2018-07-13T03:40:47.420

@gÓEûùIÿ even the string type won't zigzag. I could do it only if I don't pass an argument into the function getting the numbers via a global or I alias the type outside of the counted code. I have working 126 bytes of non-zigzag code in Kotlin. But, no legal function argument type can get me the input nor trick inputting data will zigzag. – JohnWells – 2018-07-13T04:24:50.657

1

Paste this into the developer console on TiO to help you write a bumpy program. It will check if your code is bumpy, and show available characters as you type.

– Lynn – 2019-02-22T17:52:36.087

Answers

8

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.

Dennis

Posted 2018-07-04T15:24:46.970

Reputation: 196 637

4

K (ngn/k), 23 bytes

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

Try it online!

ngn

Posted 2018-07-04T15:24:46.970

Reputation: 11 449

Why are the spaces needed? – Zacharý – 2018-07-04T16:14:33.063

@Zacharý the k code itself wouldn't be bumpy without them – ngn – 2018-07-04T16:15:45.277

What do you mean by that? Is it just ngn/k that neads the spaces – Zacharý – 2018-07-04T16:17:03.320

3@Zacharý This challenge is [tag:restricted-source], and the restriction is that the code must be a zigzag. – Erik the Outgolfer – 2018-07-04T16:18:14.027

Whoops, forgot about that, too. – Zacharý – 2018-07-04T16:19:54.813

4

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.

Post Rock Garf Hunter

Posted 2018-07-04T15:24:46.970

Reputation: 55 382

4

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

Rod

Posted 2018-07-04T15:24:46.970

Reputation: 17 588

Nice. You can substitute ' ' * 0 for ' ' [1: ]. – Jakob – 2018-07-04T18:17:31.697

You can use input(text) to write to STDOUT. – Dennis – 2018-07-04T18:38:28.210

4

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 ")

sundar - Reinstate Monica

Posted 2018-07-04T15:24:46.970

Reputation: 5 296

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. – sundar - Reinstate Monica – 2018-07-04T20:18:27.467

1Oh, I totally forgot about that requirement. That makes the answer more impressive! – Luis Mendo – 2018-07-04T20:29:08.843

3

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.

Mr. Xcoder

Posted 2018-07-04T15:24:46.970

Reputation: 39 774

2

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.

Shaggy

Posted 2018-07-04T15:24:46.970

Reputation: 24 623

@KamilDrakari, normally you'd be right but, sadly, they're necessary to meet the [tag:restricted-source] requirements of the challenge. Otherwise this could be 10 bytes.

– Shaggy – 2018-07-04T17:09:26.510

Oh, I didn't see that this was restricted-source. My bad – Kamil Drakari – 2018-07-04T17:11:01.593

@KamilDrakari, don't worry; it looks like you weren't the only one. – Shaggy – 2018-07-04T18:12:49.603

2

05AB1E, 7 5 bytes

¥ü*dZ

-2 bytes by taking the answer from the same challenge without source restriction. All credit goes to @Grimmy, so make sure to upvote his answer as well!!

Outputs 0 for zigzagging and 1 for non-zigzagging sequences.

The code points are [165,252,42,100,90] in the 05AB1E code page.

Try it online.

Original 7-byter:

¥.±¥Ä;P

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

The code points are [165,46,177,165,196,59,80] in the 05AB1E code page.

Try it online.

Explanation:

¥        # Take the forward differences (deltas) of the (implicit) input-list
         #  i.e. [1,2,0,3,2,3] → [1,-2,3,-1,1]
 ü       # For each overlapping pair:
  *      #  Multiply them together
         #   → [[1,-2],[-2,3],[3,-1],[-1,1]] → [-2,-6,-3,-1]
   d     # Check for each whether it's non-negative (>= 0)
         #  → [0,0,0,0]
    Z    # Take the maximum of that
         #  → 0
         # (after which it is output implicitly as result)

¥        # Take the forward differences (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)
         #  → [1,-1,1,-1,1]
   ¥     # Calculate the deltas of those
         #  → [-2,2,-2,2]
    Ä    # Take the absolute value of each
         #  → [2,2,2,2]
     ;   # Halve each
         #  → [1.0,1.0,1.0,1.0]
      P  # Take the product of the list resulting in either 1.0 or 0.0
         #  → 1.0
         # (after which it is output implicitly as result)

Kevin Cruijssen

Posted 2018-07-04T15:24:46.970

Reputation: 67 575

1

Jelly, 6 bytes

IṠµaIẠ

Try it online!

Returns 1 for truthy, 0 for falsy.

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

Erik the Outgolfer

Posted 2018-07-04T15:24:46.970

Reputation: 38 134

1

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

Arnauld

Posted 2018-07-04T15:24:46.970

Reputation: 111 334

2Luckily map is zigzag! – Neil – 2018-07-04T17:07:26.190

1

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.

Jo King

Posted 2018-07-04T15:24:46.970

Reputation: 38 234

1

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]

Emigna

Posted 2018-07-04T15:24:46.970

Reputation: 50 798