8
\$\begingroup\$

This problem is inspired from shinh's problem.

Given an input of positive integer \$n\$, output a smiley triangle (see below) with \$n\$ rows.

Detailed rules

  • Can be either a function or a full program.
  • Standard I/O rules applied.
    • RULE CLARIFICATION: Output as exactly a string, rather than an array of strings whose item represent a line.
  • Standard loopholes are forbitten.
  • This is code-golf. Shortest code wins.

Valid output syntax

Assuming ASCII encoding. A smiley triangle with \$n\$ rows triangle(n), where n>=1, should be output. <0xXX> is a character with such codepoint, in hexadecimal.

triangle(n) = triangle(n) <0x0a> | triangle_(n)
triangle_(1) = <0x3a> spaces
triangle_(n) = triangle_(n-1) <0x0a> row(n) spaces
row(2) = <0x3a> <0x2d>
row(n) = row(n-1) <0x29>
spaces = <0x09> spaces | <0x20> spaces | *empty*

Example outputs

If n is 1, then

:

If n is 2, then

:
:-

If n=5, then

:
:-
:-)
:-))
:-)))
\$\endgroup\$
2
  • 1
    \$\begingroup\$ is returning an array of lines valid output? \$\endgroup\$
    – Razetime
    Aug 5, 2021 at 13:11
  • \$\begingroup\$ @Razetime, I don't think so, as I clarified to use LFs as in "Valid output syntax". \$\endgroup\$
    – user100411
    Aug 5, 2021 at 23:48

33 Answers 33

10
\$\begingroup\$

Python 2, 41 bytes

s=':-'
exec"print s[:-1];s+=')';"*input()

Try it online!

42 bytes

s,c=':-'
exec"print s;s+=c;c=')';"*input()

Try it online!

43 bytes

s=':'
exec"print s;s+='-)'[s>':'];"*input()

Try it online!

\$\endgroup\$
9
\$\begingroup\$

Jelly, 10 bytes

“:-)”ṁ«\)Y

Try it online!

        )     Map over (the range from 1 to) the input:
“:-)”ṁ        Mold ":-)" to the shape of the argument, cycling its contents.
      «\      Scan by minimum: replace anything to the right of ) with ).
         Y    Join on newlines.

Thanks to Jonathan Allan for salvaging my first attempt, ”)xⱮa⁾:-Y, for another 10-byter:

Jelly, 10 bytes

”)xa⁾:-ṁ)Y

Try it online!

        )     Map over (the range from 1 to) the input:
”)x           Repeat ")" by the argument.
   a⁾:-       Zipwith-vectorize logical AND with ":-":
              overwrite the first two characters with ":-".
       ṁ      Mold to the shape of the argument, trimming the extra "-" for 1.
         Y    Join on newlines.
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Your pretty close code can also be made into this working 9 byter: ”)xa⁾:-ṁ) TIO (or ”)ṁa⁾:-ṁ)). Good spot on the order of the characters by the way! \$\endgroup\$ Aug 4, 2021 at 12:51
  • \$\begingroup\$ @JonathanAllan Thanks! Checking the order of the characters was actually the first thing I thought to do, even though my initial approach didn't use it. \$\endgroup\$ Aug 4, 2021 at 23:27
  • \$\begingroup\$ newlines are required in the output. \$\endgroup\$
    – Razetime
    Aug 6, 2021 at 2:39
7
\$\begingroup\$

Excel, 37 49 bytes

+12 bytes to accommodate clarification that the output should be one string

=CONCAT(LEFT(":-"&REPT(")",A1),SEQUENCE(A1))&"
")

This returns one string as opposed to the earlier version which returned multiple cells each containing a string. Due to the Excel string limit, this only works for n <= 254.

Link to Spreadsheet

=LEFT(":-"&REPT(")",A1),SEQUENCE(A1))

Previous version that works for n <= 32767.

\$\endgroup\$
2
  • \$\begingroup\$ Why would the code fail after n=32767? \$\endgroup\$
    – Mohammad
    Aug 4, 2021 at 17:31
  • 1
    \$\begingroup\$ @Mohammad, it fails because of the string limit in Excel. \$\endgroup\$
    – Axuary
    Aug 4, 2021 at 21:16
5
\$\begingroup\$

05AB1E, 11 bytes

…:-).ÞI£Jη»

Try it online!

Or, for the same length:

')ׄ:-쨨η»

Try it online!

…:-)         # string literal ":-)"
    .Þ       # cycle last character to create infinite list
      I£     # take the first input characters
        J    # join into a string
         η   # take all prefixes
          »  # join by newlines

')×          # repeat ")" input times
   „:-ì      # prepend ":-"
       ¨¨    # remove the last two characters
         η»  # join the prefixes by newlines
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can combine both of your answers in the legacy version of 05AB1E for -1 byte: ')ׄ:-ì£η» (the £ will implicitly use the input, even though it's the first argument instead of second). \$\endgroup\$ Aug 30, 2021 at 13:23
5
\$\begingroup\$

Stax, 8 bytes

╞■¼╤╝<Ñ┬

Run and debug it

outputs with newlines.

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

Haskell, 32 bytes

(`take`(":":iterate(++")")":-"))

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Japt, 11 10 bytes

Outputs an array of lines.

õî":-"ú')U

Try it

õî":-"ú')U     :Implicit input of integer U
õ              :Range [1,U]
 î             :For each slice the following to that length
  ":-"         :  Literal string
      ú')U     :  Right padded with ")" to length U
\$\endgroup\$
3
\$\begingroup\$

J, 17 15 bytes

-1 thanks to Jonah!

[:]\$!.'('&':-'

Try it online!

Reshape :- with fill ( to length n. [:[\ return each prefix.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ [:]\{.!.'('&':-' for 16. \$\endgroup\$
    – Jonah
    Aug 4, 2021 at 13:53
  • 1
    \$\begingroup\$ @Jonah ah, with that $ works, too! Thanks! \$\endgroup\$
    – xash
    Aug 4, 2021 at 16:27
  • \$\begingroup\$ Cool, didn't realize you could use fit with $. One thing I don't understand is why you can't ]\@$!.'('&':-'? I mean, I get that ]\@$ is a new verb, which has no definition for !., but why wouldn't J just check the rightmost verb in the list of conjunctions, and if it's defined there, use that one? \$\endgroup\$
    – Jonah
    Aug 4, 2021 at 17:07
  • 1
    \$\begingroup\$ @Jonah even better !. would just set the fill for the context, e.g. (… $ … {. … $)!._ would all use _. But I guess the cost of implementing it outweighs the seldom usage. \$\endgroup\$
    – xash
    Aug 4, 2021 at 18:59
  • \$\begingroup\$ Why a sad triangle instead of a smiley triangle? Isn't ) the correct fill char? \$\endgroup\$
    – Razetime
    Aug 5, 2021 at 13:13
3
\$\begingroup\$

K (oK), 37 bytes

{({(":-",(x-2)#")")[!x]}'!x+1)[1+!x]}

Try it online!

My first non trivial K answer, a lot of room for inprovement.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Do this to surpress printing \$\endgroup\$
    – emanresu A
    Aug 4, 2021 at 20:18
  • 1
    \$\begingroup\$ And the f: doesn't count as part of your bytecount - it's like lambda a,b: a+b but the only difference is newlines are unescapable delimiters, so the f: has to be on the same line. \$\endgroup\$
    – emanresu A
    Aug 4, 2021 at 20:20
  • \$\begingroup\$ you can use # to save a lot of bytes instead of indexing with [] \$\endgroup\$
    – Razetime
    Aug 5, 2021 at 8:16
3
\$\begingroup\$

Vyxal j, 9 bytes

⇩\)*‛:-p¦

Try it Online!

Wasif said I should post my 9-byter as my own answer, but I thought it was too similar to his, so I got rid of the map lambda and the slice, just because why not. :P

Explanation:

⇩          # x = input - 2
 \)*       # Push a 'x' long string of ')'
    ‛:-p   # Prepend the string ':-'
        ¦  # Prefixes
           # 'j' flag - Join top of stack on newlines and print
\$\endgroup\$
2
\$\begingroup\$

Husk, 11 10 9 8 bytes

Edit: -1 byte simultaneously spotted by Razetime

↑ḣ¡→":-)

Try it online!

    ":-)    # the string ":-)"
  ¡         # construct infinite list by repeatedly appending
   →        # last element of list so far;
 ḣ          # now make list of all prefixes,  
↑           # and take number of elements equal to input
\$\endgroup\$
2
  • 1
    \$\begingroup\$ remove for -1 \$\endgroup\$
    – Razetime
    Aug 4, 2021 at 10:04
  • \$\begingroup\$ @Razetime - Thanks: I was just doing it while you commented! \$\endgroup\$ Aug 4, 2021 at 10:14
2
\$\begingroup\$

JavaScript, 37 bytes

f=n=>--n?f(n)+(s=n>1?s+')':`
:-`):':'

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Vyxal jr, 10 bytes

ƛ?\)*‛:-+Ẏ

Try it Online!

Thanks to @emanresuA for -2 bytes

Vyxal j, 12 bytes

‛:-?⇩\)*+¦?Ẏ

Try it Online!

\$\endgroup\$
7
2
\$\begingroup\$

Python 3, 52 bytes

lambda n:[print((':-'+')'*i)[:-1])for i in range(n)]

Try it online!

-1 byte thanks to Mohammad

\$\endgroup\$
1
  • 1
    \$\begingroup\$ -1 instead of i+1 will save 1 character \$\endgroup\$
    – Mohammad
    Aug 4, 2021 at 21:51
2
\$\begingroup\$

APL(Dyalog Unicode), 20 bytes SBCS

{,\⍵⍴':-',')'⍴⍨|⍵-2}

Try it on APLgolf!

Monadic function

{...} monadic function, takes n on right

⍵-2 n minus 2

| abs (No. of braces, call it x)

swap x with

')' brace for

dyadic reshape to get x braces

, concat with

':-' smiley head

⍵⍴ take first n (monadic reshape)

,\ prefixes (ravel scan)

\$\endgroup\$
2
\$\begingroup\$

TI-Basic, 40 35 27 bytes

Input N
Disp ":
":-
For(I,2,N
Disp Ans
Ans+")
End

-5 bytes thanks to MarcMush

-8 bytes thanks to MarcMush

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

PHP -F, 59 bytes

for(;$i<$argn;)echo$i++?":-".str_repeat(")",$i-2)."
":":
";

Try it online!

Straightforward stuff.. Probably golfable a bit more, I'll try other things later

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

M4, 88 bytes

Just a port of the syntax in OP.

define(g,`ifelse($1,2,,`)g(decr($1))')')define(f,`ifelse($1,1,:,`f(decr($1))
:-g($1)')')

Try it online!

Usage

f(number here)

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

Charcoal, 12 bytes

NθG↓→θ⁺:-×)θ

Try it online! Link is to verbose version of code. Explanation:

Nθ

Input n.

G↓→θ

Draw a triangle of size n.

⁺:-×)θ

Paint the triangle using a smiley with n chins.

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

Retina 0.8.2, 25 bytes

.+
$*)
^.
:
:.
:-
.
$`$&¶

Try it online! Explanation:

.+
$*)

Start with n chins.

^.
:

Change the first chin into eyes.

:.
:-

Change the second chin, if any, into a nose.

.
$`$&¶

Output all of the nontrivial prefixes.

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

C (gcc), 63 bytes

f(x){x&&f(x-1)+g(x);}g(x){x&&g(x-1);putchar("\n:-)"[x<3?x:3]);}

Try it online!

  • double recursion
\$\endgroup\$
1
\$\begingroup\$

Haskell, 37 35 bytes

f x=($":-"++cycle")").take<$>[1..x]

Try it online!

  • Thanks to @Unrelated String for saving 2!
\$\endgroup\$
2
1
\$\begingroup\$

brainfuck, 57 bytes

This assumes the initial memory location contains n in binary (0x05 == 5). The TIO link allows a single ASCII character as input and converts it to binary.

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

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Is it outputting a line rather than a triangle? \$\endgroup\$
    – user100411
    Aug 23, 2021 at 14:10
  • \$\begingroup\$ Ah… I misunderstood that bit. \$\endgroup\$ Aug 24, 2021 at 22:20
1
\$\begingroup\$

Ruby, 40 38 bytes

->n{n.times{|i|puts":-#{?)*n}"[0..i]}}

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 1.upto(n) saves 1 byte \$\endgroup\$
    – G B
    Aug 4, 2021 at 20:38
  • \$\begingroup\$ @GB Thanks, but I found an even better way: n.times saves 2 :) \$\endgroup\$
    – Kirill L.
    Aug 5, 2021 at 6:50
1
\$\begingroup\$

PowerShell Core, 43 33 28 bytes

':-'+')'*"$args"|% *ve @args

Try it online!

Saved 10 bytes thanks to mazzy!
Saved 5 bytes by:

  • removing gratuitous parenthesis
  • using Remove instead of Substring
  • using splatting
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Try it online? \$\endgroup\$
    – mazzy
    Aug 5, 2021 at 6:09
  • 1
    \$\begingroup\$ awesome! ٩(^‿^)۶ \$\endgroup\$
    – mazzy
    Aug 6, 2021 at 4:52
1
\$\begingroup\$

Python 2, 56 bytes

i=input()
o=':-'+')'*i
for x in range(1,i+1):print o[:x]

Try it online!

Without the leading newline

Python 2, 54 bytes

i=input()
o=':-'+')'*i
for x in range(i+1):print o[:x]

Try it online!

Never going to be a winner. Just an alternative approach.

55 bytes

lambda x:'\n'.join((':-'+')'*x)[:x]for x in range(x+1))

Try it online!

Another alternative using a lambda, surprisingly longer than using input(). It's that join() that's expensive.

\$\endgroup\$
3
  • \$\begingroup\$ These produce leading newlines \$\endgroup\$
    – Jitse
    Aug 5, 2021 at 12:48
  • \$\begingroup\$ @jitse Having read the question again several times, I don't see where it says leading newlines are disallowed. Maybe I am missing something?? \$\endgroup\$
    – ElPedro
    Aug 6, 2021 at 21:01
  • \$\begingroup\$ Allowed formatting is specified in the Valid output syntax block in the question. Leading spaces are specifically not included there, so I don't think they are valid. \$\endgroup\$
    – Jitse
    Aug 9, 2021 at 7:20
1
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 109 95 bytes

 n =input
b s =gt(n,1) char(10) ':-' dupl(')',n - 2) s :f(c)
 n =n - 1 :(b)
c output =':' s
end

Try it online!

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

Java (JDK), 82 80 bytes

String a(int n){return(n>1?a(n-1):"")+(":-"+")".repeat(n)).substring(0,n)+"\n";}

Try it online!

Recursively append longer smileys. A different approach might be shorter...

-2 thanks to ceilingcat!

\$\endgroup\$
1
  • \$\begingroup\$ I always miss the simplest things, thanks! \$\endgroup\$ Sep 8, 2021 at 12:23
1
\$\begingroup\$

Zsh, 53 39 37 bytes

a=:-;for n ({1..$1})a+=\)&&<<<$a[1,n]

Try it online!  39 bytes  53 bytes

Realised I was wasting bytes by doing 2 loops when one would suffice.

\$\endgroup\$
0
\$\begingroup\$

C (gcc), 68 bytes

i;f(n){char*s=calloc(n,4);for(s[i=0]=58;n--;s[i]=i++?41:45)puts(s);}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.