Snakes all around

12

4

Challenge

The challenge is simple: print a snake.
You will get the length of the snake as input.
A snake of length 2 looks like this:

==(:)-

A snake of length 7 looks like this:

=======(:)-

In other words, the length of a snake is how many equal signs are before the head.

Usage

Let's say I made a C++ implementation and compiled it to ./getsnake.
I could run it like so:

$ ./getsnake 10
==========(:)-

Clarifications

  • Standard loopholes are disallowed.
  • You can get input and output in any acceptable way.
  • You can assume all inputs given are positive integers.
  • You may write a function instead of a regular program.

InitializeSahib

Posted 2016-07-31T20:01:07.933

Reputation: 491

8It's kinda useless to leave a challenge in the Sandbox for only half an hour. You did get some feedback, but it's usually best to leave it in the Sandbox for 24-72 hours. (Also, you should either flesh out the "Origins" part or remove it.) – El'endia Starman – 2016-07-31T20:15:36.520

Is a function enough or do you want a full program? – betseg – 2016-07-31T20:17:12.483

@betseg function is enough – InitializeSahib – 2016-07-31T20:17:57.140

What if the input is 0? – Fatalize – 2016-07-31T22:10:41.317

@Fatalize Or NaN, Infinity, any negative number, or a blank string? – haykam – 2016-08-01T01:57:50.023

@Fatalize the program will only ever get positive integers – InitializeSahib – 2016-08-01T05:31:03.413

@Peanut the program will only ever get positive integers – InitializeSahib – 2016-08-01T05:31:12.540

4

I'm afraid all My Squiggly Lamp answers are effortlessly portable to this.

– manatwork – 2016-08-01T12:49:46.330

Is zero positive? – haykam – 2016-08-01T22:58:48.720

@Peanut no, it isn't – InitializeSahib – 2016-08-01T23:15:03.250

3Negative integers should cause the snake to swallow itself. – GuitarPicker – 2016-08-02T12:35:36.187

Are functions that return a string (rather than printing it) valid? – Jordan – 2016-08-02T15:19:54.620

Is hardcoding the input (i.e. you have to replace "n" with the number in the program to run it) allowed, rather than taking input through stdin or equivalent? – Theo – 2016-08-06T15:31:50.833

@T.Lukin it isn't – InitializeSahib – 2016-08-06T19:29:35.367

Answers

18

Hexagony, 33 bytes

Not gonna win, but still cool. Might be able to get golfed more.

Try it online!

Golfed:

61}?.$)@$j;(3<./;.}l/.400;5\j;.'\

Formatted:

    6 1 } ?
   . $ ) @ $
  j ; ( 3 < .
 / ; . } l / .
  4 0 0 ; 5 \
   j ; . ' \
    . . . .

Colored (Made using Timwi's Hexagony Colorer)

u found da secret snek! =======(:)-

Explanation:

Loop, print out "=" until the counter reaches 0.

    6 1 } ?
   . $ . . $
  . . ( . . .
 / . . } . . .
  . . . ; . .
   . . . ' .
    . . . .

Print "("

    . . . .
   . . . . .
  . . . . . .
 / ; . . . . .
  4 . . . . .
   j . . . .
    . . . .

Print ":"

    . . . .
   . . . . .
  . ; ( 3 < .
 . . . } l / .
  . . . . . .
   . . . . .
    . . . .

Print ")"

    . . . .
   . . . . .
  j . . . . .
 . . . . . . .
  . . . ; 5 \
   . . . . \
    . . . .

The above values were generated using a simple python script. However, I kind of ran out of room for the "-". So, I had to resort to more advanced tricks.

When the program prints out ")", the value of the cell isn't 41, it's 1065. Hexagony just mods the value when printing. As it turns out, (1065*1000+4)%256=44, just one away from 45, the ascii value for "-". Then, I just increment, print, and insert a @ somewhere after printing.

    . . . .
   . $ ) @ $
  j . . 3 . .
 / . . } . . .
  4 0 0 . 5 \
   . ; . . \
    . . . .

Blue

Posted 2016-07-31T20:01:07.933

Reputation: 1 986

How can it be called a programming language when .j4/; is ... Print "("??? Which part of .j4/; is the print and which part is the string to print? – Fogmeister – 2016-08-01T23:36:09.397

2@Fogmeister [Part 1/2] In Hexagony, any alphabetical character sets the value of the current memory cell to the ascii value of that character. "j" has an ascii value of 106, so that is what the memory is set to. Also, and numerical characters multiply the value of the current cell by 10, then add themselves to it (this allows for easy construction of numbers, like the 61 at the beginning. – Blue – 2016-08-02T00:23:35.477

3[Part 2/2] So, j4 sets the memory cell to 1064. When the IP reaches the "/", it gets reflected into ";", which prints out the ascii character corresponding to the current memory cell % 256. 1064%256=40, the ascii value for "(". I hope this answers your question. – Blue – 2016-08-02T00:25:43.380

thanks. Yeah it does. I guess this is more of a language to use as a way of testing your brain. Rather than a way to write programmes then? Interesting. – Fogmeister – 2016-08-02T00:27:45.840

@Fogmeister there are many esoteric programming languages that are more brain twisting exercises than useful for doing real work, although they CAN do real work if you try hard enough. – Sparr – 2016-08-09T01:53:30.100

12

Cinnamon Gum, 7 bytes

0000000: 7043 dc95 6d4f ce                        pC..mO.

Try it online.

Would have been 6 bytes with the old p syntax :/

Explanation

Decompresses to p~=~(:)-, the p stage then simply reads input and repeats the = n times.

a spaghetto

Posted 2016-07-31T20:01:07.933

Reputation: 10 647

Can you link to language spec for this? – Robert Fraser – 2016-08-01T05:07:50.703

@RobertFraser I don't have a formal specification or any documentation currently, but you can check out the source at the GitHub repo: https://github.com/quartata/cinnamon-gum

– a spaghetto – 2016-08-01T12:52:10.060

12

Brian & Chuck, 31 bytes

,{-?>}-):(=?
#}<<.{?_<.<.<.<.<.

Input in the form of a byte value, so e.g. input ! gives a snake of length 33.

Try it online!

It's been a while...

Explanation

A quick Brian & Chuck primer:

  • The first line of the program is Brian, the second is Chuck.
  • Brian and Chuck are two Brainfuck-like instances. The main catch is that Chuck's program is Brian's tape and vice versa. The tape heads/instruction pointers start on the first cell of each tape and execution starts on Brian.
  • As for the commands, there are a few differences. Only Brian can use , (input) and only Chuck can use . (output). In addition to < and > there are { and } which move the tape head up to the next zero cell (or in the case of { to the left end of the tape if there is no zero cell on the way). Instead of [...], the only control flow is ? which switches control to the other instance if the current cell is non-zero. The first executed instruction on the other cell is the one after the condition. And finally, _ is just an alias for null-bytes, for convenience.

Now the code. Brian starts with this:

,{-?

This reads the input into Chuck's first cell, then moves the tape head to the left with { (does nothing right now) and decrements the input with - before switching control for Chuck if the value is still non-zero. This begins the main loop. Chuck then runs this bit:

}<<.{?

This moves the tape head on Brian to the very end, moves two cells left onto the = and prints it before the tape head all the way to the left and switching control back to Brian. This is how loops generally work in B&C.

Once the input has been reduced to zero, the ? on Brian's tape will do nothing. Then Brian executes this part:

>}-):(=?

The ):(= are no-ops, so the actual code is just >}-?. We move off the zero cell with >, move up to _ with }, decrement it to make it non-zero and switch to Chuck with ?. Then the last bit on Chuck is run:

<.<.<.<.<.

This simply prints the five characters in front of Chuck, i.e. =(:)-. Note that we need to print another = since the main loop is only executed N-1 times for input N.

Martin Ender

Posted 2016-07-31T20:01:07.933

Reputation: 184 808

10Congrats on 100k – Insane – 2016-08-01T10:47:01.160

10

V, 8 bytes

Àé=A(:)-

Try it online!

V uses the "Latin1" encoding.

Explanation:

À        "Arg1 times:
 é=      "Insert an '='
   A(:)- "Append the head

James

Posted 2016-07-31T20:01:07.933

Reputation: 54 537

I count 10 bytes – Zwei – 2016-08-01T06:32:20.590

2@zwei see my edit. – James – 2016-08-01T06:37:03.023

Got it! That's better. – Zwei – 2016-08-01T06:39:08.127

6

Retina, 10 bytes

.+
$*=(:)-

Try it online!

This is a simple regex substitution.

It matches .+ which matches the whole input, and then substitutes it with $*=(;)-.

The $* is a feature unique in Retina: it is the character-repetition special operator.

For example, 5$*x would become xxxxx.

In the case that the previous argument is absent, the whole match is used as the default argument.

Leaky Nun

Posted 2016-07-31T20:01:07.933

Reputation: 45 011

5

Python, 21 bytes

lambda n:"="*n+"(:)-"

Ideone it!

Leaky Nun

Posted 2016-07-31T20:01:07.933

Reputation: 45 011

You can cut 3 bytes by doing: "="*input()+"(:)-" – gowrath – 2016-08-29T16:57:14.107

@gowrath Then it won't be printing anything – Leaky Nun – 2016-08-29T18:31:36.540

It'll output it if you're in the interpreter no? Op said you can get output in any way. – gowrath – 2016-08-29T18:59:30.843

@gowrath You can't assume that we're in REPL – Leaky Nun – 2016-08-29T19:00:54.257

Is that a general rule for code golfing? New here :) – gowrath – 2016-08-29T19:02:37.537

@gowrath Usually. – Leaky Nun – 2016-08-30T02:24:31.153

5

Java 8, 52 bytes

n->new String(new char[n]).replace("\0","=")+"(:)-";

Test suite. (Compile > Execute)

Credits.

teh traditional way, 61 54 53 bytes

7 bytes thanks to Kevin Cruijssen.

1 byte thanks to Dom Hastings.

n->{String s="";for(;n-->0;)s+="=";return s+"(:)-";};

Leaky Nun

Posted 2016-07-31T20:01:07.933

Reputation: 45 011

+1 Oh, and for the traditional one: for(int i=0;i<n;i++) can be golfed to for(;n>0;n--), since you don't need the input for anything else than the for-loop anyway. – Kevin Cruijssen – 2016-08-01T07:00:52.663

@KevinCruijssen Thanks, edited – Leaky Nun – 2016-08-01T07:04:19.240

Testing via the link you provided it looks like you can change the for loop to: for(;n-->0;) to save another byte! – Dom Hastings – 2016-08-01T15:58:42.883

@DomHastings edited with thanks – Leaky Nun – 2016-08-01T16:18:54.857

5

Haskell, 25 bytes

f n=('='<$[1..n])++"(:)-"

'='<$[1..n] is equivalent to replicate n '='.

Michael Klein

Posted 2016-07-31T20:01:07.933

Reputation: 2 111

4

Python, 24 bytes.

print"="*input()+"(:)-"

input() gets input from user

*, when used on strings and an integer, creates a new string, which is made of joined copies of the original. For example: "hello "*3 outputs hello hello hello.

By multiplying = and input(), you get a string of = the length that the user specifies.

Using + joins two strings, in this case, our body "=…=" with our head, "(:)-" to make the snake.

print outputs the result.

i..

Posted 2016-07-31T20:01:07.933

Reputation: 351

4

Jelly, 9 bytes

”=x;“(:)-

Try it online!

”=x;“(:)-
”=          '='
  x         repeat (argument) that many times
   ;        append
    “(:)-   "(:)-"

Leaky Nun

Posted 2016-07-31T20:01:07.933

Reputation: 45 011

4

05AB1E, 10 9 bytes

'=×"ÿ(:)-

Explanation

'=         # push equal-sign
  ×        # repeat input nr of times
   "ÿ(:)-  # interpolate snake body with head as a string
           # implicitly display

Try it online

1 byte saved thanks to Adnan.

Emigna

Posted 2016-07-31T20:01:07.933

Reputation: 50 798

'=×"ÿ(:)- for 9 bytes :). This uses string interpolation. – Adnan – 2016-07-31T20:30:10.550

1@Adnan: Aaah, so that's what ÿ does :) – Emigna – 2016-07-31T20:31:25.827

4

C, 38

f(n){for(;n--;printf(n?"=":"=(:)-"));}

Try it on ideone.

owacoder

Posted 2016-07-31T20:01:07.933

Reputation: 1 556

1Not shorter, but cooler: f(n){~-printf(n?"=":"=(:)-")||f(~-n);}. – orlp – 2016-08-01T00:45:30.680

f(n){for(;n;printf(n--?"=":"(:)-"));} for -1. – Erik the Outgolfer – 2016-08-01T17:59:53.980

@EʀɪᴋᴛʜᴇGᴏʟғᴇʀ - That doesn't output the head. – owacoder – 2016-08-01T18:20:27.137

@owacoder Well, C is not my primary language... – Erik the Outgolfer – 2016-08-01T18:33:57.493

1@EʀɪᴋᴛʜᴇGᴏʟғᴇʀ And that's why you shouldn't post untested golfing suggestions (especially if it would have taken you 5 seconds to test your suggestion with the link provided in the answer). – Martin Ender – 2016-08-01T18:40:42.903

4

Javascript, 23 bytes

n=>"=".repeat(n)+"(:)-"

eithed

Posted 2016-07-31T20:01:07.933

Reputation: 1 229

1I was just about to post exactly that code! Beat me to it! Have an upvote – MayorMonty – 2016-07-31T22:18:39.420

Today I learned about arrow functions. Thank you. Didn't know that was a thing

– bobkingof12vs – 2016-08-02T21:55:14.320

How do you call that instance of the function? I just tried n(3) and received a Uncaught ReferenceError: n is not defined error... – WallyWest – 2016-08-09T00:46:50.797

@WallyWest - try x=n=>"=".repeat(n)+"(:)-"; x(7); – eithed – 2016-08-09T06:11:53.970

@eithedog Then shouldn't the answer have the initial x= and changed to 25 chars? – WallyWest – 2016-08-09T06:36:58.017

@WallyWest no, because the requirement is You may write a function instead of a regular program. x= is the assignment of the anonymous function – eithed – 2016-08-09T07:07:38.647

4

C#, 28 bytes

n=>new string('=',n)+"(:)-";

TheLethalCoder

Posted 2016-07-31T20:01:07.933

Reputation: 6 930

im always interested by the lambda snippets. how can you even run this? – downrep_nation – 2016-08-01T19:54:48.663

@downrep_nation it takes in an int and automatically returns the string because it is only one statement – TheLethalCoder – 2016-08-02T07:58:01.587

The answer should be a full program or a function, not just a snippet. So this answer seems incomplete. – raznagul – 2016-08-02T14:57:22.960

@raznagul This is an anonymous function therefore it is complete – TheLethalCoder – 2016-08-02T14:59:23.077

@TheLethalCoder: Maybe try online link would help to understand it. (http://csharppad.com/)

– raznagul – 2016-08-02T15:12:28.727

Under Clarifications OP wrote "You may write a function instead of a regular program," so I think this is valid on that count. However, it does not actually print anything when invoked, and I'm not sure that's kosher since the requirements say "print a snake." (If it was n=>Console.Write(new string...); I believe that would be valid.) – Jordan – 2016-08-02T15:15:36.650

@Jordan •You can get input and output in any acceptable way.. I take that to mean returning the string is valid output – TheLethalCoder – 2016-08-02T15:17:19.390

@raznagul It is essentially a Func<int, string> just anonymous – TheLethalCoder – 2016-08-02T15:17:44.750

3

GolfScript, 11 10 bytes

~"="*"(:)-"

Multiplies "=" by input, and adds head.

-1 thanks to Leaky Nun

NoOneIsHere

Posted 2016-07-31T20:01:07.933

Reputation: 1 916

3

Mathematica, 21 20 bytes

"="~Table~#<>"(:)-"&

Anonymous function. Takes a number n as input, and returns a snake of length n as output. "="~Table~# generates a list {"=", "=", ..., "="} of length n, and <>"(:)-" concatenates the list's elements and appends "(:)-" to the resulting string.

LegionMammal978

Posted 2016-07-31T20:01:07.933

Reputation: 15 731

1golfable by one more byte: "="~Table~#<>"(:)-"& – LLlAMnYP – 2016-08-02T07:43:40.923

3

Batch, 68 bytes

@set h=(:)-
@for /l %%i in (1,1,%1)do @call set h==%%h%%
@echo %h%

Neil

Posted 2016-07-31T20:01:07.933

Reputation: 95 035

3

R, 32 27 bytes

This solution is pretty straightforward, rep function repeats the first element ("=") scan() times, which is in fact the user's input.

a=scan();cat(rep("=",a),"(:)-")

EDIT:

cat(rep("=",scan()),"(:)-")

Slighly shorter answer, using scan() directly.

Alternatively,

cat(rep("=",scan()),"(:)-",sep="")

for a non-chopped snake (34 bytes)

Frédéric

Posted 2016-07-31T20:01:07.933

Reputation: 2 059

I would say the sep="" is needed here, otherwise your snake looks like the one that traversed the railway: = = = (:)-. – manatwork – 2016-08-02T11:09:25.920

The snake produced here is indeed a bit minced, but OP's consigns only state that the length of the snake is the number of equal signs before the head. I will add the sep as a side note nonetheless ;) – Frédéric – 2016-08-02T11:13:45.033

2

C, 46 45 43 bytes

saved 2 bytes thanks to owacoder! saved 3 bytes thanks to rici!

f(n){while(4-printf("=\0(:)-"+2*!n--));}

Try it on Ideone!

betseg

Posted 2016-07-31T20:01:07.933

Reputation: 8 493

3

The mysterious --> operator strikes again.

– Leaky Nun – 2016-07-31T20:37:12.803

@LeakyNun 46 bytes version was without the goes to operator, than I remembered the goes to operator ;) – betseg – 2016-07-31T20:41:08.463

1You can remove the >0 and save two bytes. – owacoder – 2016-07-31T20:47:37.373

Slightly shorter: f(n){while(4-printf("=\0(:)-"+2*!n--));} – rici – 2016-08-02T02:38:28.613

2

CJam, 13 11 bytes

qi'=*"(:)-"

Test it here.

-2 bytes thanks to quartata

acrolith

Posted 2016-07-31T20:01:07.933

Reputation: 3 728

You can do '=* instead of {'=}*. – a spaghetto – 2016-07-31T20:46:11.720

2

JAISBaL, 9 bytes

t=*Qb(:)-

Verbose:

# \# enable verbose parsing #\
push1 =        \# push = onto the stack #\
mul            \# multiply the top two values of the stack #\
popout         \# pop the top value of a stack and print it #\
print4 (:)-    \# print (:)- #\

Tested with JAISBaL-0.0.7 (The compiled .jar was just pushed, but the source has been up on git for a while)

Socratic Phoenix

Posted 2016-07-31T20:01:07.933

Reputation: 1 629

Use a # before the language name to make it look like everybody else's. – Michael Klein – 2016-07-31T22:42:35.737

@MichaelKlein okay – Socratic Phoenix – 2016-07-31T22:57:02.150

2

Perl 6,  16 15  12 bytes

{"{'='x$_}(:)-"}
{'='x$_~'(:)-'}
'='x*~'(:)-'

Explanation:

'=' x * # 「=」 string repeated by the only parameter 「*」
~        # concatenated with
'(:)-'   # the head

Usage:

# store it in the lexical namespace
my &snake = '='x*~'(:)-';

put snake 10;

# put ^5 .map: &snake;
put ^5 .map: '='x*~'(:)-';
==========(:)-
(:)- =(:)- ==(:)- ===(:)- ====(:)- =====(:)-

Brad Gilbert b2gills

Posted 2016-07-31T20:01:07.933

Reputation: 12 713

2

PowerShell v2+, 19 bytes

'='*$args[0]+'(:)-'

Full program. Takes input $args[0], uses string multiplication to construct the body, then string concatenation to tack on the head.

PS C:\Tools\Scripts\golfing> .\snakes-all-around.ps1 7
=======(:)-

PS C:\Tools\Scripts\golfing> .\snakes-all-around.ps1 77
=============================================================================(:)-

AdmBorkBork

Posted 2016-07-31T20:01:07.933

Reputation: 41 581

Kudos to you, I got as far as 26: "$("="*[int]$args[0])(:)-" – Chirishman – 2016-08-29T21:33:53.120

2

Cheddar, 15 bytes (noncompeting)

n->'='*n+'(:)-'

A straightforward answer.

Deimos

Posted 2016-07-31T20:01:07.933

Reputation: 41

1

Befunge 93, 25 bytes

Try it Online!

1+:&`#v_"=",
"(:)-"<@,,,,

Not compatible with '98, because the & character acts like r when there is no input, instead of repeating the last token

MildlyMilquetoast

Posted 2016-07-31T20:01:07.933

Reputation: 2 907

1

C++14, 59 bytes

As unnamed lambda:

#import<string>
[](int n){return std::string(n,61)+"(:)-";}

Usage:

#include<iostream>

#import<string>
auto f=
  [](int n){return std::string(n,61)+"(:)-";}
;

int main(){
  std::cout << f(2) << std::endl;
  std::cout << f(5) << std::endl;
  std::cout << f(10) << std::endl;
}

Karl Napf

Posted 2016-07-31T20:01:07.933

Reputation: 4 131

1

Turing machine code, 242 bytes

0 * * r 0
0 _ _ l A
A 0 9 l A
A 1 0 r 1
A 2 1 r 1
A 3 2 r 1
A 4 3 r 1
A 5 4 r 1
A 6 5 r 1
A 7 6 r 1
A 8 7 r 1
A 9 8 r 1
A _ _ r 3
1 _ = l 2
1 * * r 1
2 = = l 2
2 * * * A
3 = = r 4
3 * _ r 3
4 = = r 4
4 _ ( r 6
6 _ : r 7
7 _ ) r 8
8 _ - * halt

Try it online.

SuperJedi224

Posted 2016-07-31T20:01:07.933

Reputation: 11 342

1

Common Lisp, 29 bytes

(format t"~v{=~}(:)-"(read)1)

user65167

Posted 2016-07-31T20:01:07.933

Reputation:

1

Befunge-98 (FBBI), 22 18 bytes

Saved 4 bytes by putting the "-):(" at the beginning. This let me get rid of the separate printing of the head, and also the ' for pushing the =.

"-):(="&k:$$>:#,_@

Try it online!

Explanation

"-):(="                   Pushes '-):(=' to the stack - the head reversed
       &k:                Duplicates the = N+1 times (now there are N+2 '='s on the stack)
          $$              Discards 2 so that there are N '='s
            >:#,_         Prints all the top non-zero chars on the stack (the whole snake)
                 @        Exits

MildlyMilquetoast

Posted 2016-07-31T20:01:07.933

Reputation: 2 907

1

VBA Excel, 23 bytes

using Immediate window and cell [A1] as input

?string([A1],"=")"(:)-"

remoel

Posted 2016-07-31T20:01:07.933

Reputation: 511

1

Sesos, 11 bytes

Hexdump:

0000000: aaaa5e a0f7b4 ed4cee 5d3b                         ..^....L.];

Try it online!

Assembler:

set numin
add 61
fwd 1
get
jmp,sub 1,rwd 1,put,fwd 1,jnz
add 40,put
rwd 1,sub 3,put
fwd 1,add 1,put
add 4,put

Leaky Nun

Posted 2016-07-31T20:01:07.933

Reputation: 45 011

1

K, 17 Bytes

{,[x#"=";"(:)-"]}

Example;

f:{,[x#"=";"(:)-"]}
-1(f'!10);          /print out the result of calling f where x is 0 1 2 3 4 5....
(:)-
=(:)-
==(:)-
===(:)-
====(:)-
=====(:)-
======(:)-
=======(:)-
========(:)-
=========(:)-

Explanation;

{}                 /function x is implicit and is an int
x#"="              /take (#) x of "=" --> so 3#"=" gives "==="
,[x#"=";"(:)-"]    /comma is a join that takes 2 args --> ,[x;y] gives the concatination of x and y --> "a","abc" is the same as ,["a";"abc"] and gives "aabc"

Chromozorz

Posted 2016-07-31T20:01:07.933

Reputation: 338

What is -1(...);? – Adám – 2016-08-01T16:04:23.263

Save a byte: {(x#"="),"(:)-"} – Adám – 2016-08-01T16:06:09.763

Thanks Adam. -1 Prints to the console. -1"Hello"; will print Hello – Chromozorz – 2016-08-01T16:09:56.707

But isn't printing implicit? – Adám – 2016-08-01T16:11:46.247

Yes but it includes the double quotes... "====(:)-" rather than; =====(:)- – Chromozorz – 2016-08-01T17:22:06.927

save 2 bytes with {|"-):(",x#"="}. Try it online!.

– streetster – 2017-12-12T11:54:18.067

1

Perl, 16 + 1 (-p flag) = 17 bytes

$_="="x$_."(:)-"

Needs -p flag, so run with :

perl -pe '$_="="x$_."(:)-"'

Dada

Posted 2016-07-31T20:01:07.933

Reputation: 8 279

1

Minkolang 0.15, 12 bytes

"=(:)-"nD$O.

Try it here!

Explanation

"=(:)-"         Push this to the stack in reverse order - ["-",")",":","(","="]
       n        Take number from input
        D       Pop k and duplicate top of stack (the "=") k times
         $O.    Output whole stack as characters and stop.

El'endia Starman

Posted 2016-07-31T20:01:07.933

Reputation: 14 504

1

Befunge-98, 24 bytes

Takes numerical input from the user, then prints the snake.

'=&:00pk:00gk,"-):("4k,@

owacoder

Posted 2016-07-31T20:01:07.933

Reputation: 1 556

1

Matlab / Octave, 22 bytes

@(n)[~(1:n)+61 '(:)-']

This is an anonymous function.

Try it on Ideone.

Explanation

Assume n= 5.

1:n produces the row vector [1 2 3 4 5].

~(1:n) negates each entry, so it gives [0 0 0 0 0].

...+61 adds 61 to each entry, so it gives [61 61 61 61 61]. 61 is the ASCII value of character =.

[... '(:)-'] concatenates that with the string '(:)-'. This automatically converts [61 61 61 61 61] into the string '=====' before the concatenation.

Luis Mendo

Posted 2016-07-31T20:01:07.933

Reputation: 87 464

1

Dyalog APL, 13 bytes

'(:)-',⍨⍴∘'='

'(:)-',⍨ the snake head appended to
⍴∘'=' n repetitions of a body segment

TryAPL online!

A proper function. The equivalent program is '(:)-',⍨⎕⍴'='.

Adám

Posted 2016-07-31T20:01:07.933

Reputation: 37 779

You forgot the byte count. – m654 – 2016-08-01T14:34:41.340

3@m654 The snake nibbled it! – Adám – 2016-08-01T15:56:03.437

1

Brainfuck, 53 bytes

++++[->+++++<]>[->+++>++<<],>+<[->.<]>>.<---.>+.++++.

Explanation:

++++[->+++++<]    We build a 20 as 5*4
>[->+++>++<<]     We build a 60 and a 40 in two neighboring cells out of the 20, by multiplying the 20 with 3 and with 2
,>+<[->.<]        Read input, and print 61 ('=') as many times as the input idicates
>>.<---.>+.++++.  Print out the rest, "(:)-", noting how close each character is to either 60 or 40.

vsz

Posted 2016-07-31T20:01:07.933

Reputation: 7 963

1

x86 COM, 29 bytes

8A 0E 82 00 80 E9 30 B4
02 B2 3D CD 21 49 75 FB
BA 18 01 B4 09 CD 21 C3
28 3A 29 2D 24

aka

16DA:0100 8A0E8200      MOV     CL,[0082]
16DA:0104 80E930        SUB     CL,30
16DA:0107 B402          MOV     AH,02
16DA:0109 B23D          MOV     DL,3D
16DA:010B CD21          INT     21
16DA:010D 49            DEC     CX
16DA:010E 75FB          JNZ     010B
16DA:0110 BA1801        MOV     DX,0118
16DA:0113 B409          MOV     AH,09
16DA:0115 CD21          INT     21
16DA:0117 C3            RET
16DA:0118 283A          SUB     [BP+SI],BH // Snake Head
16DA:011A 292D          SUB     [DI],BP    // Snake Head
16DA:011C 2400          AND     AL,00      // Snake Head

Name it "snake.com"

Call it from cmd (tested with Win XP), parameters 1 to 9 supported:

snake 5

results in

=====(:)-

pjp

Posted 2016-07-31T20:01:07.933

Reputation: 11

1

C#, 42 bytes

string w(int n)=>new string('=',n)+"(:)-";

Try it here!

downrep_nation

Posted 2016-07-31T20:01:07.933

Reputation: 1 152

Does this code print the string? – Jordan – 2016-08-01T20:49:15.650

@Jordan: No, this is a function that returns the string. – raznagul – 2016-08-02T15:02:07.133

The requirements say solutions should "print a snake." I'm not sure if just returning a string is kosher. – Jordan – 2016-08-02T15:13:52.430

1

Lua, 28 Bytes

Takes input in the form of a command-line argument and outputs via its return value. Even if it uses a return statement, it is still a full program :).

return("="):rep(...).."(:)-"

Katenkyo

Posted 2016-07-31T20:01:07.933

Reputation: 2 857

1

LiveScript, 15 bytes

->'='*it+'(:)-'

Gustavo Rodrigues

Posted 2016-07-31T20:01:07.933

Reputation: 261

1

BASIC, 30 bytes

Let's keep it simple in BASIC. QB64 and TRS-80 Extended Color BASIC both tie, since TRS-80 needs a line number but doesn't need the space after INPUT, and it won't let you use INPUT in immediate mode. One liners are rare on a 32x16 screen.

TRS-80: (should also work on Microsoft GW-BASIC/BASICA)

0INPUTL:?STRING$(L,"=");"(:)-"

enter image description here

QB64:

INPUT L:?STRING$(L,"=");"(:)-"

GuitarPicker

Posted 2016-07-31T20:01:07.933

Reputation: 1 101

1

Java 1.7, 46 bytes

A recursive (and shorter) alternative to the iterative and 'replace' versions.

String f(int n){return n>0?"="+f(--n):"(:)-";}

Run it here.

This function first builds the n tail elements recursively, then appends the head.

MH.

Posted 2016-07-31T20:01:07.933

Reputation: 261

1

Brainfuck, 47 46 44 bytes

+[+<-[<]>->>++]<<++<,[->.<]<.>>---.<<+.++++.

Try it online! Takes input as a single byte value (e.g. space is 32). The program requires an interpreter with 8-bit cells and a tape not bounded on the left.

+[+<-[<]>->>++] (found by a bit of brute forcing) initialises the tape to

[253, 0, 0, 232, 0, 0, 40, 0, 59, 86, 0]
                                      ^

We then increment the 59 to 61 with <<++ and read the input char with <,. [->.<] then outputs the required number of =, and <.>>---.<<+.++++. outputs the (:)- portion, making use of the aforementioned 61 as well as the 40 two cells away.

Sp3000

Posted 2016-07-31T20:01:07.933

Reputation: 58 729

1

Fish (><>), 33 bytes:

An upside down F cause ><>. Takes integer as command line argument by running ./fish.py snake.fish -v [number].

\
"
-
)
:
(!;!?lo<
=
"
>{1-:?!^}:

Try it here! The best explanation is to watch it with animation on the website.

gowrath

Posted 2016-07-31T20:01:07.933

Reputation: 885

0

Scala, 30 Bytes

(x:Int)=>print(("="*x)+"(:)-")

Soapy

Posted 2016-07-31T20:01:07.933

Reputation: 251

0

Microscript, 14 bytes

ic'=P]"-):("ah

SuperJedi224

Posted 2016-07-31T20:01:07.933

Reputation: 11 342

0

Microscript II, 13 bytes

"="sN*p"(:)-"

SuperJedi224

Posted 2016-07-31T20:01:07.933

Reputation: 11 342

0

SNOBOL4 (CSNOBOL4), 39 bytes

 OUTPUT =LPAD('(:)-',INPUT + 4,'=')
END

Try it online!

Giuseppe

Posted 2016-07-31T20:01:07.933

Reputation: 21 077

0

Charcoal, 7 bytes

×=N(:)-

Try it online!

This is equivalent to the following code:

Print(Times("=", InputNumber()));
Print("(:)-");

Charlie

Posted 2016-07-31T20:01:07.933

Reputation: 11 448

0

Excel VBA, 20 Bytes

Anonymous VBE immediate window function that takes input, a, from range [A1] and outputs a snake of the length a to the VBE immediate window.

?[Rept("=",A1)]"(:)-

Taylor Scott

Posted 2016-07-31T20:01:07.933

Reputation: 6 709

0

Sinclair ZX80 (4K ROM) ~39 bytes

 1 INPUT L
 2 FOR I=1 TO L
 3 PRINT "=";
 4 NEXT I
 5 PRINT "(:)-"

No explanation needed really.

Shaun Bebbers

Posted 2016-07-31T20:01:07.933

Reputation: 1 814

0

JavaScript 6, input unary '=', 11B

x=>x+'(:)-'

Standard rule allowes it.

Fix it if that's not what you want.

l4m2

Posted 2016-07-31T20:01:07.933

Reputation: 5 985

Meta seems to say for such language unary is not allowed but also say allowed. Not sure which – l4m2 – 2017-12-18T19:29:23.560

0

Pyth, 10 bytes

+*\=Q"(:)-

Test suite.

+*\=Q"(:)-
    Q        get input
 *\=         repeat "=" that many times
+    "(:)-   append "(:)-"

Leaky Nun

Posted 2016-07-31T20:01:07.933

Reputation: 45 011

0

C++, 106 103 bytes

#include <iostream>
void s(int n){for(int i=0;i<n;i++){std::cout<<"=";}std::cout<<"(:)-"<<std::endl;}

Usage

#include <string>
int main(int argc, char *argv[]) {
    int length = std::stoi(argv[1]);
    s(length);
    return 0;
}

Ungolfed

#include <iostream> // for printing
void outputSnake(int length) {
    for (int i = 0; i < length; i++) { // iterates over length of snake
        std::cout << "="; // prints an equals sign
    }
    std::cout << "(:)-" << std::endl; // prints rest of snake
}

Thanks @LeakyNun for shaving off 3 bytes.

InitializeSahib

Posted 2016-07-31T20:01:07.933

Reputation: 491

Replace i!=n+1 with i<n, and I am not sure if std:: is needed. – Leaky Nun – 2016-07-31T20:15:36.887

@LeakyNun I would have to add "using namespace std;" to remove the std portion, but i'll replace that not equal sign – InitializeSahib – 2016-07-31T20:16:18.187

Couldn't you replace the for-loop with ´while(n-->0)´? – Doddy – 2016-08-02T06:10:12.973

0

><>, 26 23 bytes

:?!v"="o1-
oo;>"-):("oo

-3 bytes thanks to Leaky Nun

Test it here. (you have to input the code manually)

acrolith

Posted 2016-07-31T20:01:07.933

Reputation: 3 728

0

Java, 133 bytes

public class Snake {
    public void s(int n){
        for(int i=0;i<n;i++){
            System.out.print("=");
        }
        System.out.println("(:)-");
    }
}

InitializeSahib

Posted 2016-07-31T20:01:07.933

Reputation: 491

1This site says that your code is 165 bytes. – acrolith – 2016-07-31T20:39:11.173

1

If you prefer to just share the method then don't include the class at all, otherwise provide a main in order to provide a complete program. Anyway, class don't need to be public. Class name could be shorter. Also, you could save some bytes by concatenating the "=" and printing them all at the end.Don't include {} or unnecessary spaces. Here are these suggestions: http://pastebin.com/8jLNL65V

– Master_ex – 2016-07-31T20:51:09.493

@daHugLenny I used wc -c on GNU/Linux to count the bytes. – InitializeSahib – 2016-07-31T21:08:14.757

Assuming tabs (because SE sites convert them to spaces) the count actually turns out to 132 bytes. (And sorry @Master_ex, it seems like LeakyNun has already used what you said in their answer so it can't be used here.) – Value Ink – 2016-08-01T05:24:50.407

@ValueInk, if you referring to LeakyNun's java answer (the traditional one) it seems that it was posted after my comment and in my pastebin link I save some more bytes in the for loop, but no problem, I just wanted to help :-) – Master_ex – 2016-08-01T06:58:05.107

0

Bash, 52 36 bytes

while [[ ${#a} -lt $1 ]];do a=$a=;done;echo "$a(:)~"

printf -vs %$1s;echo "${s// /=}(:)~"

Excellent suggestion by @manatwork.

If one would like to use external programs (sed) this could be a bit smaller(31 bytes):

printf "%$1s(:)~"|sed 's/ /=/g'

Run like that:

$ bash snake.sh 1
=(:)~
$ bash snake.sh 3
===(:)~

Master_ex

Posted 2016-07-31T20:01:07.933

Reputation: 526

1I would go without while: printf -vs %$1s;echo "${s// /=}(:)~". – manatwork – 2016-08-01T12:50:39.990

0

Javascript (using external library) (39 bytes)

n=>_.Range(0,n).Write("=",x=>"")+"(:)-"

Link to lib: https://github.com/mvegh1/Enumerable

Code explanation: Create an array of n elements, and Write a concatenated string with "=" as the delimiter and an empty string as the current element. Then concatenate the head of the snake

enter image description here

applejacks01

Posted 2016-07-31T20:01:07.933

Reputation: 989

0

Matricks, 24 bytes

Very simple, just wanted to show off the new matrix definition feature.

m61:1:n;;a<40,58,41,45>;

Run with python matricks.py snek.txt [[]] <input> --asciiprint

Blue

Posted 2016-07-31T20:01:07.933

Reputation: 1 986

0

MATL, 12 bytes

61iY"'(:)-'h

Try it on MATL Online.

Explanation

61       % ASCII value for '='
i        % Take input number, n
Y"       % Row vector with 61 repeated n times
'(:)-'   % Push this string
h        % Concatenate horizontally. This converts to string. Implicitly display

Luis Mendo

Posted 2016-07-31T20:01:07.933

Reputation: 87 464

0

Nim, 46 bytes

import strutils,future
n=>repeat("=",n)&"(:)-"

This is an anonymous function that must be passed to a procedure to be used. To test, use the testing program below:

import strutils,future
proc test(f: int -> string) = echo f(#[Your input here]#)
test(n=>repeat("=",n)&"(:)-")

Copper

Posted 2016-07-31T20:01:07.933

Reputation: 3 684

0

Clojure, 35 bytes

#(str(apply str(repeat %\=))"(:)-")

This is an anonymous/unnamed function that takes a single integer as an argument and returns a string.

Doorknob

Posted 2016-07-31T20:01:07.933

Reputation: 68 138

0

ECMAScript 6, 37 44 37 bytes

Try it here!

Golfed

alert("=".repeat(prompt())+"(:)-");

Ungolfed

alert("=".repeat(prompt()) + "(:)-");

Revisions

  • Original revision.
  • Added alert(); because it used to save to a variable, n. Try it here!
  • Used ECMAScript 6's String.prototype.repeat method instead of the previous method that didn't even work and took more bytes. Try it here!

ECMAScript 6, 53 bytes

This is a version of the above code that is a function, not a full program. Try it here!

Golfed

function makeSnakeArt(n){return"=".repeat(n)+"(:)-")}

Ungolfed

function makeSnakeArt(n) {
    return "=".repeat(n) + "(:)-"
}

alert(makeSnakeArt(5));

Revisions

  • Original revision, stemming off the 3rd revision of the full program. Try it here!
  • Returns the snake instead of alerting it. Try it here!

haykam

Posted 2016-07-31T20:01:07.933

Reputation: 784

@WallyWest I'll add that into the answer. – haykam – 2016-08-09T00:49:53.587

@WallyWest Why'd you delete your comment? – haykam – 2016-08-09T00:50:40.993

0

PL/SQL: 28 bytes

rpad('=','&n','=')||'(:)-');

aravind reddy kaithy

Posted 2016-07-31T20:01:07.933

Reputation: 11

>

  • Tried it? There is no change to work due to the extra ). 2) No idea about the dialect, but wouldn't lpad('(:)~',&n+4,'='); work? 3) This is just a snippet, not a valid solution.
  • < – manatwork – 2016-08-01T13:40:56.807

    0

    VBA, 37 bytes

    Function S(L):S=String(L,"=")&"(:)-"
    

    and enter gives the End Function. Immediate window of VBA editor:

    ?S(41)
    =========================================(:)-
    

    Joffan

    Posted 2016-07-31T20:01:07.933

    Reputation: 832

    0

    Brainfuck, 67 bytes

    Code:

    >++++++[>++++++++++<-]>+<,[->.<]<++++[>++++++++++<-]>.>---.<+.++++.
    

    Explanation:

    > At #1
    ++++++[>++++++++++<-]>+< Put '=' in #2
    , Input into #1
    [->.<] Output '=' #1 times thereby emptying #1
    <++++[>++++++++++<-]>. Put '(' in #1 and output
    
    > At #2
    --- Change #2 to ':'
    . Output
    
    < At #1
    +.++++. Change to '(' and 'minus' and output
    

    Try it online!

    Input works using the corresponding ASCII characters - that is, to input 33 you would type !. For convenience, you can use \d as the input in the interpreter linked, where d is any non-negative integer.

    Vincent

    Posted 2016-07-31T20:01:07.933

    Reputation: 131

    0

    Julia, 15 bytes

    x->"="^x*"(:)-"
    

    Wow, this is short.

    Mama Fun Roll

    Posted 2016-07-31T20:01:07.933

    Reputation: 7 234

    0

    Racket, 46 bytes

    (λ(n)(string-append(make-string n#\=)"(:)-"))
    

    Pretty terrible golfing language :)))

    Winny

    Posted 2016-07-31T20:01:07.933

    Reputation: 1 120

    0

    , 10 chars / 13 bytes

    ⍘=Ċï+'(:)-
    

    Try it here (ES6 browsers only).

    Should be rather self-explanatory.

    Mama Fun Roll

    Posted 2016-07-31T20:01:07.933

    Reputation: 7 234

    0

    Silicon, 12 bytes

    I"="*"(:)-"+
    

    Explanation:

    I"="*"(:)-"+
    
        *   Times
    I       Get an integer as input
    "="     Push "="
    
    "(:)-"  Push "(:)-"
    +       Append
    

    Alternatively, 11 bytes (but non-competing):

    "="ã"(:)-"+
    

    m654

    Posted 2016-07-31T20:01:07.933

    Reputation: 765

    0

    PHP, 27 bytes

    Not the most wonderfull creation, but with register_globals on you can set it via url.

    <?=str_repeat('=',$n)?>(:)-
    

    Martijn

    Posted 2016-07-31T20:01:07.933

    Reputation: 713

    0

    Ruby, 20 bytes

    ->n{puts?=*n+"(:)-"}
    

    Jordan

    Posted 2016-07-31T20:01:07.933

    Reputation: 5 001

    0

    Ruby, 27 bytes

    puts"="*($*[0].to_i)+"(:)-"
    

    jose_castro_arnaud

    Posted 2016-07-31T20:01:07.933

    Reputation: 229

    No need for the parenthesis. – manatwork – 2016-08-09T12:35:15.977

    0

    J, 14 bytes

    '(:)-',~'='#~]
    

    Very simple. This is a five train:

    '(:)-' ,~ '=' #~ ]
    

    Both strings, in this case, become constant verbs. #~ essentially repeats its left argument the amount of times as its right argument. ,~ then appends its left argument to the end of its right argument.

    Test cases:

       f 2
    ==(:)-
       f 7
    =======(:)-
       f 10
    ==========(:)-
       f 0
    (:)-
    

    I think that it would look more snake-like with a tilde at the end, but eh.

    Conor O'Brien

    Posted 2016-07-31T20:01:07.933

    Reputation: 36 228

    0

    Kotlin, 34 bytes

    {String(ByteArray(it){61})+"(:)-"}
    

    This assumes that a function of (Int)->String type is requested (in such case, it is assumed to be an Int). Version with explicit typing (40 bytes):

    {n:Int->String(ByteArray(n){61})+"(:)-"}
    

    Try it out on the JVM target:

    fun main(args: Array<String>) {
        val f: (Int)->String = {String(ByteArray(it){61})+"(:)-"}
        println(f(5))
    }
    

    JustACluelessNewbie

    Posted 2016-07-31T20:01:07.933

    Reputation: 161

    0

    R, 39 bytes

    Mildly different than others above

    for(i in 1:scan())cat("=");cat("(:)-")
    

    or, if we can precede the line with the input

    ->n;for(i in 1:n)cat("=");cat("(:)-")
    

    user5957401

    Posted 2016-07-31T20:01:07.933

    Reputation: 699

    0

    Perl, 16 bytes

    say'='x<>.'(:)-'
    

    Takes input from stdin

    theLambGoat

    Posted 2016-07-31T20:01:07.933

    Reputation: 119

    0

    Pyke, 10 bytes

    \=*"(:)-"+
    

    Try it here!

    Or 8 bytes (noncompeting)

    \=*"(:)-
    

    Try it here!

    Blue

    Posted 2016-07-31T20:01:07.933

    Reputation: 26 661

    0

    Python, 23 bytes

    print'='*input()+'(:)-'
    

    vikarjramun

    Posted 2016-07-31T20:01:07.933

    Reputation: 794

    Unless it's Python 3, you can remove the int() part. – acrolith – 2016-08-08T21:33:42.120

    @daHugLenny thanks, -5 bytes – vikarjramun – 2016-08-09T01:06:23.520

    0

    Ruby, 24 bytes

    def f(l) ?=*l+"(:)-";end
    

    dkudriavtsev

    Posted 2016-07-31T20:01:07.933

    Reputation: 5 781

    0

    PyMin, 15 bytes

    »"="*¬+"(:)-"
    

    acrolith

    Posted 2016-07-31T20:01:07.933

    Reputation: 3 728

    0

    GameMaker Language, 49 bytes

    s="(:)-"for(a=0;a<argument0;a++){s="="+s}return s
    

    Timtech

    Posted 2016-07-31T20:01:07.933

    Reputation: 12 038

    0

    Factor 34

    [ [ "="write ] times "(:>~"print ]
    

    Quotation (anonymous function) that takes a number and prints a snake.

    Alternative (also 34):

    [ 61 <string> ":>~""("glue print ]
    

    Sorry, but i made some aesthetic changes...

    10 [ [ "="write ] times "(:>~"print ] call
    ==========(:>~
    

    fede s.

    Posted 2016-07-31T20:01:07.933

    Reputation: 945

    0

    Neoscript, 15 bytes

    {n|'=*n+"(:)-"}
    

    TuxCrafting

    Posted 2016-07-31T20:01:07.933

    Reputation: 4 547

    0

    Fourier, 28 bytes

    I(61ai^~i)40a-8a58a32a41a+4a
    

    Try it online

    Beta Decay

    Posted 2016-07-31T20:01:07.933

    Reputation: 21 478

    0

    GNU sed, 16 bytes

    y/0/=/;s/$/(:)-/
    

    Since sed has no native support for numbers, the input is given in unary based on this consensus.

    Run:

    sed -f snake.sed <<< "000"
    

    Output:

    ===(:)-
    

    seshoumara

    Posted 2016-07-31T20:01:07.933

    Reputation: 2 878

    -1

    JavaScript, 53 Bytes

    function a(b){return "=".repeat(parseInt(2))+"(:)-"}
    

    Winnie The Pooh

    Posted 2016-07-31T20:01:07.933

    Reputation: 111