Better Hex Color Codes for Your Internet



Hex triplets such as #ffffff (white) or #3764ef (blueish) are often used to denote RGB colors. They consist of # followed by six hexadecimal digits (0-f), or sometimes three digits where the real color is obtained by doubling each digit. For example, #fff is #ffffff and #1a8 is #11aa88.

Sadly, that three digit shorthand was the golfiest the internet had to offer, until now.

Write a program or function that takes in a string of 1 to 7 characters:

  • The first character will always be #.
  • The other characters will always be hexadecimal digits: 0123456789abcdef.

The input is a shorthand form of a hex triplet (or the full form if 7 characters are given). You need to output a full hex triplet that expands the input shorthand based on these patterns:

Input   -> Output
#       -> #000000    (black)
#U      -> #UUUUUU
#UV     -> #UVUVUV
#UVW    -> #UUVVWW    (usual 3-digit shorthand)
#UVWXYZ -> #UVWXYZ    (not shorthand)

Each of U, V, W, X, Y, and Z may be any hexadecimal digit. The output is always 7 characters.

For example:

Input -> Output
# -> #000000
#0 -> #000000
#4 -> #444444
#f -> #ffffff
#a1 -> #a1a1a1
#0f -> #0f0f0f
#99 -> #999999
#1a8 -> #11aa88
#223 -> #222233
#fff -> #ffffff
#1230 -> #102030
#d767 -> #d77767
#bbb5 -> #b5b5b5
#aabbc -> #aabbcc
#00000 -> #000000
#3764e -> #3764ee
#3764ef -> #3764ef
#123456 -> #123456
#f8f8f8 -> #f8f8f8


  • The input will always start with # and so must the output.

  • You may assume all input letters are lowercase (abcdef) or uppercase (ABCDEF) as you prefer.

  • Letters in the output may be in either case as you prefer. You can even mix cases.

  • Alpha/transparency is not dealt with here (though there are hex versions of RGBA colors).

The shortest code in bytes wins.

Calvin's Hobbies

Posted 2017-05-30T02:26:17.527

Reputation: 84 000


"Sadly, that three digit shorthand was the golfiest the internet had to offer, until now." - Uh, not exactly. HTML, 0 bytes - it works out of the box

– Bergi – 2017-05-30T08:47:54.030

11The reverse of this would be a cool challenge too – Beta Decay – 2017-05-30T09:20:12.377

9I am unsatisfied with the #UVWXY -> #UVWXYY entry because it allows for a single-value representation for the Blue channel, but there's no similar expression for Red and Green (e.g. if I wanted #889071 I can't abbreviate, but #907188 can #90718) the others all work great. – Draco18s no longer trusts SE – 2017-05-30T19:07:33.720

3@Draco18s I love it. That one and the #UVWX -> #UXVXWX. It's such an inconsistent and arbitrary behaviour that it's hard to believe there isn't a couple of browser that actually currently implement it. – xDaizu – 2017-05-31T07:37:18.163

1@xDaizu CSS specification has #RGBA and #RRGGBBAA, so #1234 should be read as rgba(17, 34, 51, 0.25) – tsh – 2017-05-31T07:40:18.837

@tsh Boy oh boy, conflicting interpretations? In HTML/CSS? Loving it more and more! – xDaizu – 2017-05-31T08:01:30.220

@xDaizu it's hard to believe there isn't a couple of browsers that actually currently implement it Ha! Point to you sir. (I do like #UVWX -> #UXVXWX though, that's clever). – Draco18s no longer trusts SE – 2017-05-31T13:42:35.623

@Draco18s The 5-digit one is weird but I'm not sure there's a nice way to make it symmetric, except for awkward stuff like #U(X+Y)V(X+Y)W(X+Y). – Calvin's Hobbies – 2017-05-31T16:40:41.767

Oh agreed, I was just voicing my displeasure. More at the fact that there isn't a good solution rather than your particular one. – Draco18s no longer trusts SE – 2017-05-31T16:44:06.573

#UVWXYY makes sense because blue is the color that looks the darkest, and has the least effect on the output. It's also pretty common to store colors in 16 bits with 6 bits for green and 5 each for red and blue. – 12Me21 – 2019-06-26T19:44:11.190



JavaScript, 86 82 77 bytes


just find out that remove recursive save 4 bytes...

idea from @Arnauld save 4 bytes, +1 more bytes


Posted 2017-05-30T02:26:17.527

Reputation: 13 072

([s,a=0,b=a,c,d,e,f]=x)=>f?x:e?x+e:d?s+a+d+b+d+c+d:c?s+a+a+b+b+c+c:s+a+a+a+b+b+b for 80 bytes – Luke – 2017-05-30T08:45:03.473

@Luke I just got ReferenceError: x is not defined – tsh – 2017-05-30T08:47:12.340


Jelly, 24 bytes



A full program (the empty lines are actually empty lines).

Try it online! or see a test suite*


     - Link 1 (0 bytes), returns its input (e.g. "U" -> "U")
     - Link 2 (0 bytes), returns its input (e.g. "UV" -> "UV")
x2   - Link 3, doubles up (e.g. "UVW" -> "UUVVWW")
j0ị$ - Link 4, joins with final element (e.g. "UVWX" -> "UXVXWXX")
m0   - Link 5, reflects its input (e.g. "UVWXY" -> "UVWXYYXWVU")
     - Link 6 (0 bytes), returns its input (e.g. "UVWXYZ" -> "UVWXYX")
0    - Link 7, returns zero (link 7 is also link 0 since there are 7 links)
Ḣ;LĿṁ6$$ - Main link: string
Ḣ        - head (get the '#')
       $ - last two links as a monad:
   Ŀ     -   call link at index:
  L      -     length
      $  -   last two links as a monad:
    ṁ6   -     mould like 6 (e.g. "UVWXYYXWVU" -> "UVWXYY"
         -                    or  "UV" -> "UVUVUV")
 ;       - concatenate (prepend the '#' again)
         - implicit print

* the test-suite program had to be altered by swapping the order of what were the Main link and Link 7, while the footer became the Main Link. Furthermore the # had to be manually replaced, since the program as-is heads it.

Jonathan Allan

Posted 2017-05-30T02:26:17.527

Reputation: 67 804


CJam, 45  44  42 40 36 35 bytes

q(\0se|_,("6* 3* 2e* )f+ _W=+ "S/=~

Runs various code snippets based on the length of the input.

Esolanging Fruit

Posted 2017-05-30T02:26:17.527

Reputation: 13 542

1Some formatting <s>not</s> required.... <s></s> – stevefestl – 2017-05-30T06:30:43.827


@SteveFest But crossed out 44 is still regular 44...

– Esolanging Fruit – 2017-05-30T06:44:25.940

1Aw! You got me! I forgot that! – stevefestl – 2017-05-30T06:45:43.050


PHP 7.1, 88 bytes


PHP 5, 90 88 bytes



Posted 2017-05-30T02:26:17.527

Reputation: 1 571

I have no Idea how you get this idea but it works. Work _21422112233122444113355123456[6*$i+++strlen($argn|aa)-8] ? – Jörg Hülsermann – 2017-05-30T13:55:24.140

1could you please explain how this works? – Brian H. – 2017-05-30T14:10:25.900

This one is nice ! It stores the offset in $argn in 21422112233122444113355123456and selects the correct one based on strlen. aa pads the string to at least 2 chars. On input # there is no $argn[1] so ?:0 outputs a 0. This also works for on 0 in the string. One of the best answers I've seen! Sadly it doesn't pay of too much (got Jörg's answer golfed down to 95). – Christoph – 2017-05-30T14:26:10.530

1Haha, this is the best abuse of PHP's automatic strings I've seen in a while. +1 – ETHproductions – 2017-05-31T02:12:17.730

@JörgHülsermann It works, but the digits have to be decreased by 1 and then changed back. Otherwise _ would lead to zero index. – user63956 – 2017-05-31T05:47:40.620

Sadly your latest version (while having an awesome trick again!) results in a warning which does get printed by PHP (and therefore results in the wrong output). Usally we need surpress it with @. Your second version sadly fails for everything I tested. – Christoph – 2017-05-31T06:33:51.577

@Christoph The second version seems to work (PHP 5.6.29). As far as I understand, STDERR is allowed to be ignored.

– user63956 – 2017-05-31T08:01:25.350

PHP doesn't print errors to stderr on default but rather to stdout. ini_set('display_errors','stderr'); can be used to change this since PHP 5.2.4. The second version not working for me might of course be a simple PHP version problem :) – Christoph – 2017-05-31T09:47:59.017

1@Christoph The Second version needs a PHP Version under 7.1 and a PHP Version over 5.6 I think add this should make this clearer – Jörg Hülsermann – 2017-05-31T09:58:24.880


Java 10, 228 227 224 182 bytes

s->{var x="$1$1";int l=s.length();return l>6?s:l>5?s+s.charAt(5):l<2?"#000000":s.replaceAll(l>4?"(.)(.)(.)(.)$":l==3?"([^#]{2})":"([^#])",l>4?"$1$4$2$4$3$4":l>3?x:l>2?x+"$1":x+x+x);}

Try it online.


s->{                      // Method with String as both parameter and return-type
  var x="$1$1";         //  Create a temp String to repeat a match
  int l=s.length();     //  Length of the String
  return l>6?           //  If the length is 7:
    s                   //   Return the input-String as is
   :l>5?                //  Else-if the length is 6:
    s+s.charAt(5)       //   Return the input-String with the last character repeated
   :l<2?                //  Else-if the length is 1:
    "#000000";          //   Simply return the literal String #000000
   :                    //  Else (the length is 2, 3, 4, or 5):
    s.replaceAll(       //   Return the input-String after a regex replace:
                        //    With as match:
     l>4?               //     If the length is 5:
      "(.)(.)(.)(.)$",  //      Use a match for pattern #(A)(B)(C)(D)
     :l==3?             //     Else-if the length is 3:
      "([^#]{2})"       //      Use a match for pattern #(AB)
     :                  //     Else (the length is 2 or 4):
      "([^#])",         //      Use a match for pattern #(A) or #(A)(B)(C)
                        //    And as replacement: 
     l>4?               //     If the length is 5:
      "$1$4$2$4$3$4"    //      Change #ABCD to #ADBDCD
     :l>3?              //     Else-if the length is 4:
      x                 //      Change #ABC to #AABBCC
     :l>2?              //     Else-if the length is 3:
      x+"$1"            //      Change #AB to #ABABAB
     :                  //     Else (the length is 2):
      x+x+x);}          //      Change #A to #AAAAAA

Kevin Cruijssen

Posted 2017-05-30T02:26:17.527

Reputation: 67 575


Python 3, 166 162 160 152 bytes

import re
lambda x,d='(.)$',b=r'\1':re.sub(*[('$','0'*6),(d,b*6),('(..)$',b*3),('(\w)',b*2),('.'+'(.)'*4,r'#\1\4\2\4\3\4'),(d,b*2),('','')][len(x)-1],x)

I construct a list of regex replacement tuples for each pattern, and then extract the tuple at index len(x)-1, finally splatting (*) it into the arguments of re.sub:

lambda x, d='(.)$', b=r'\1':   # lambda expression, save often used strings
  re.sub(   # regex replacement of:
         *  # expand what follows into arguments, i.e. f(*(1,2)) -> f(1,2)
         [  # list of replacement patterns:
            # 1 character: replace the end with 6 zeroes
            ('$', '0'*6),
            # 2 chars: repeat the last character 6 times
            (d, b*6),
            # 3 chars: repeat the two non-#s 3 times.
            ('(..)$', b*3),
            # 4 chars: replace every non-# with twice itself
            ('(\w)', b*2),
            # 5 chars: has to be somewhat verbose..
            ('.'+'(.)'*4, r'#\1\4\2\4\3\4'), 
            # 6 chars: repeat the last character
            (d, b*2),
            # 7 chars: complete already, replace nothing with nothing
            ('', '')
         ][len(x)-1], # select the element from the list that has the right length
        x)  # replace in argument x

saved 8 bytes by memorizing r'\1' (thanks, Gábor Fekete)


Posted 2017-05-30T02:26:17.527

Reputation: 3 151

1Wouldn't using r'\1' as a named parameter save some bytes? – Gábor Fekete – 2017-05-30T11:49:58.457

1You wrote o=r'\1' but use b in your code :D – Gábor Fekete – 2017-05-31T08:08:12.143

1@GáborFekete Whoops :D – L3viathan – 2017-05-31T08:11:28.813


PHP, 95 93 89 87


Basically @JörgHülsermann's answer but greatly golfed down so I decided to post it as a seperate answer. I'd count this answer as an collective effort of me and Jörg.

-4 bytes thanks to @JörgHülsermann
-1 bytes thanks to @JörgHülsermann's base 33 numbers


Posted 2017-05-30T02:26:17.527

Reputation: 1 489


APL (Dyalog), 43 bytes

Requires ⎕IO←0 which is default on many systems.

'#',{6⍴(≢⍵)⊃'0'⍵ ⍵(2/⍵)(∊⍵,¨⊃⌽⍵)(⍵,⌽⍵)⍵}1↓⍞

Try it online!

1↓⍞ drop the first character (the hash)

{ apply the following anonymous function

(≢⍵)⊃ use the length of the argument to pick one of the following seven values:
  '0' a zero
   the argument
   the argument
  2/⍵ two (2) of each (/) of the argument ()
  ∊⍵,¨⊃⌽⍵ the flattened () argument () followed each () by the first () of the reversed () argument ()
  ⍵,⌽⍵ the argument () prepended (,) to the reversed () argument ()
   the argument

6⍴ repeat elements from that until a length of six is achieved

} end of anonymous function

'#', prepend a hash to that


Posted 2017-05-30T02:26:17.527

Reputation: 37 779


Python 2, 167 165 bytes

-2 bytes thanks to Trelzevir

lambda s:'#'+''.join([reduce(lambda x,y:x+y,c)for c in['0'*6,s[1:2]*6,z(s[1:2],s[2:3])*3,z(*z(s[1:2],s[2:3],s[3:4]))*2,z(s[1:4],s[-1]*3),s+s[-1],s][len(s)-1]])

It creates a list of strings and chooses based on the length of the string.

Gábor Fekete

Posted 2017-05-30T02:26:17.527

Reputation: 2 809

1You can save 2 bytes using z=zip. – Trelzevir – 2017-05-30T10:37:55.763


Retina, 90 bytes


Try it online! Includes test cases.

Explanation: The first translation handles two digits, the second one three, the third one four, and the fourth one zero. However, neither the second and fourth translations repeat the (last) digit, as that is done at the end anyway to cover all the remaining cases.


Posted 2017-05-30T02:26:17.527

Reputation: 95 035


Haskell, 130 127 122 118 109 95 bytes (by user1472751)

y a|l<-[last a]=[y"0",y$a++a,a++a++a,do c<-a;[c,c],(:l)=<<init a,a++l,a]!!length a
f(h:r)=h:y r

Try it online!


Posted 2017-05-30T02:26:17.527

Reputation: 1 261

There is a superfluous space behind g . – Laikoni – 2017-06-01T15:13:38.240

1Also (x:r)!(y:t)=x:y:r!t;e!_=e is shorter than a!b=id=<<[[x,y]|(x,y)<-zip a b]. – Laikoni – 2017-06-01T15:15:14.783

As the first char is always # you can do g(a:t)|l<-last t=a:[ ... – Laikoni – 2017-06-01T15:18:49.170

@Laikoni indeed, those are great improvements! – bartavelle – 2017-06-01T15:52:13.360

I found a 95 byte solution that uses a similar approach to yours (great minds think alike, huh?). You can use it or I can post a separate answer.

– user1472751 – 2018-02-13T21:52:35.047

I replaced my solution with yours. It is much nicer! – bartavelle – 2018-02-15T08:43:20.620


PHP, 87 Bytes

use Base 35 Numbers


Try it online!

or use Base 33 Numbers


Try it online!

PHP, 89 Bytes


Try it online!

intval(["8kn",gd8,"9ft",wqq,i3j,i3k][strlen($argn|aa)-2],36) + 3 Bytes using a 36 Base

PHP, 102 Bytes


Try it online!

PHP, 180 Bytes


Try it online!

Jörg Hülsermann

Posted 2017-05-30T02:26:17.527

Reputation: 13 026


I golfed down this version to 95 bytes but I thought it was to different so I posted it as an own answer. Hope you like it :)

– Christoph – 2017-05-30T14:29:02.060


@Christoph In the moment I am here with my version Try it online!

– Jörg Hülsermann – 2017-05-30T14:41:27.253

2Base 33 is an awesome idea! I've been sitting here a while but didn't come up with it damn. – Christoph – 2017-05-30T16:30:21.580

1@Christoph it is very similar with your golfing of my first version. It was not easy to golf my approch under your approach – Jörg Hülsermann – 2017-05-30T16:47:05.110

Saved another byte: $argn."0" to $argn.=0. – Christoph – 2017-05-31T06:54:08.130

1@Christoph Thanks and the base 35 Number system is my approach in our Teamwork – Jörg Hülsermann – 2017-05-31T09:49:37.470


Sed, 119 (118 Bytes + -E)


Straightforward text substitution.


Posted 2017-05-30T02:26:17.527

Reputation: 501


Powershell, 113 111 bytes


Explaned test script:

$f = {

param($s)           # parameter string
$s+='0'*($s-eq'#')  # append '0' if $s equal to '#'
$i=(                # get indexes from array
    (,1*5),         # $i = 1,1,1,1,1 if $s.length-2 = 0
    (2,1*2+2),      # $i = 2,1,2,1,2 if $s.length-2 = 1
    (1,2,2,3,3),    # $i = 1,2,2,3,3 if $s.length-2 = 2
    (4,2,4,3,4),    # $i = 4,2,4,3,4 if $s.length-2 = 3
    (2..5+5),       # $i = 2,3,4,5,5 if $s.length-2 = 4
    (2..6)          # $i = 2,3,4,5,6 if $s.length-2 = 5
-join$s[0,1+$i]     # join chars from $s by indexes 0, 1 and $i


    , ("#", "#000000")
    , ("#0", "#000000")
    , ("#4", "#444444")
    , ("#f", "#ffffff")
    , ("#a1", "#a1a1a1")
    , ("#0f", "#0f0f0f")
    , ("#99", "#999999")
    , ("#1a8", "#11aa88")
    , ("#223", "#222233")
    , ("#fff", "#ffffff")
    , ("#1230", "#102030")
    , ("#d767", "#d77767")
    , ("#bbb5", "#b5b5b5")
    , ("#aabbc", "#aabbcc")
    , ("#00000", "#000000")
    , ("#3764e", "#3764ee")
    , ("#3764ef", "#3764ef")
    , ("#123456", "#123456")
    , ("#f8f8f8", "#f8f8f8")
) |% {
    $s, $e = $_
    $r = &$f $s
    "$($e-eq$r): $r"


True: #000000
True: #000000
True: #444444
True: #ffffff
True: #a1a1a1
True: #0f0f0f
True: #999999
True: #11aa88
True: #222233
True: #ffffff
True: #102030
True: #d77767
True: #b5b5b5
True: #aabbcc
True: #000000
True: #3764ee
True: #3764ef
True: #123456
True: #f8f8f8


Posted 2017-05-30T02:26:17.527

Reputation: 4 832


JavaScript (ES6), 96 bytes





Posted 2017-05-30T02:26:17.527

Reputation: 6 214


Windows batch, 389 372 362 349 231 bytes

I totally copied @Neil code...

@call:c %s:~1,1% %s:~2,1% %s:~3,1% %s:~4,1% %s:~5,1% %s:~6,1%
@for %%r in (#%1%2%3%4%5%6.%6 #%1%2%3%4%5%5.%5 #%1%4%2%4%3%4.%4 %s%%1%2%3.%3 
%s%%1%2%1%2.%2 %s%%1%1%1%1%1.%1 #000000.0)do @if not %%~xr.==. @echo %%~nr&exit/b


Posted 2017-05-30T02:26:17.527

Reputation: 539

1replacing %s% with %1 should save you a few bytes. – satibel – 2017-05-30T06:53:51.160

2%s:~3,1%%s:~4,1% can be replaced with %s:~3,2%. Also I'm not sure that this works for an input of #. – Neil – 2017-05-30T08:56:47.337

2By the way, I tried a different algorithm, and it came out at 243 bytes. – Neil – 2017-05-30T09:31:16.487

1May I know what's the algorithm? – stevefestl – 2017-05-30T09:55:02.967

1(Sorry, I didn't see your comment due to a lack of @Neil.) There's some boilerplate but the two lines of interest are call:c %s:~1,1% %s:~2,1% %s:~3,1% %s:~4,1% %s:~5,1% %s:~6,1% and for %%r in (#%1%2%3%4%5%6.%6 #%1%2%3%4%5%5.%5 #%1%4%2%4%3%4.%4 %s%%1%2%3.%3 %s%%1%2%1%2.%2 %s%%1%1%1%1%1.%1 #000000.0)do if not %%~xr.==. echo %%~nr&exit/b. – Neil – 2017-05-31T11:12:49.423

@Neil Can the first one be golfed down to call:c %s~1,6%? – stevefestl – 2017-05-31T11:41:00.157

1@SteveFest No, I want the 6 characters as separate %N variables. – Neil – 2017-05-31T11:46:44.583

@Neil Can I use exit instead of exit /b? – stevefestl – 2017-05-31T11:51:06.657

1It will close your command prompt... – Neil – 2017-05-31T12:03:02.493

Does that matter? I thought we only needed the code working. – stevefestl – 2017-05-31T12:03:41.260


Perl, 61 bytes

say+(/./g,0)[0,1,(unpack+S7,"g+g+ÜRÉ/Â¥[ [")[y/#//c]=~/./g]

Run with perl -nE. Assumes that the input is exactly as described (gives incorrect results if the input has a trailing newline).

The string "g+g+ÜRÉ/Â¥[ [" encodes the 7 16-bit numbers 11111,11111,21212,12233,42434,23455,23456 as 14 latin1 characters. Here’s a hexdump for clarity:

0000001d: 672b 672b dc52 c92f c2a5 9f5b a05b       g+g+.R./...[.[


Posted 2017-05-30T02:26:17.527

Reputation: 12 521

I replaced the Latin-1 string with a call to pack(), and got: perl -nE 'say+(/./g,0)[0,1,(unpack+S7,pack "H*","672b672bdc52c92fc2a59f5ba05b")[y/#//c]=~/./g]' . But when I type "#a", I get "#a0a0a0" which I think is wrong. It should be "#aaaaaa". (Maybe I made a mistake in the pack() call.) – J-L – 2018-09-26T02:27:04.300

This time I replaced the unpack() & pack() calls with the literal shorts, and got: perl -nE 'say+(/./g,0)[0,1,(11111,11111,21212,12233,42434,23455,23456)[y/#//c]=~/./g]'. It still appears to be wrong, as "#a" still yields the incorrect answer of "#a0a0a0" (instead of "#aaaaaa"). – J-L – 2018-09-26T16:04:03.197

Ah! I figured it out! I needed to use the -l switch (that's "ell" as in the "letter L") with the -nE switch, like this: perl -lnE 'say+(/./g,0)[0,1,(11111,11111,21212,12233,42434,23455,23456)[y/#//c]=~/./g]' . Now it works correctly. – J-L – 2018-09-26T16:27:27.743

The warning that says "(gives incorrect results if the input has a trailing newline)" can be eliminated by changing "Run with perl -nE" to "Run with perl -lnE". (The -l part of the switch gets rid of the trailing newline for you.) – J-L – 2018-09-26T16:33:47.510

Using -F on the command line lets you change this to say+(@F,0)[0,1,(unpack+S7,"g+g+ÜRÉ/Â¥[ [")[$#F]=~/./g] saving 5 bytes in the code. – Xcali – 2019-06-28T04:27:18.400


Pyth, 35 bytes


Try it online here, or verify all test cases here.

+\#@<R6[J|tQ\0K*6JKKs*R2JjeJJ+JeJ)lQ   Implicit: Q=eval(input())
                                       Trailing Q inferred
          tQ                           Remove first char of input
         |  \0                         The above, or "0" if empty
        J                             *Store in J (also yields stored value)
              K*6J                    *Repeat J 6 times, store in K
                  KK                  *2 more copies of the above
                    s*R2J             *Duplicate each char of J in place
                         jeJJ         *Join chars of J on last char of J
                             +JeJ     *Append last char of J to J
       [                         )     Wrap the 5 starred results in an array
    <R6                                Trim each to length 6
   @                              lQ   Choose result at index of length of input
                                       (Modular indexing, so length 7 selects 0th element)
+\#                                    Prepend #, implicit print


Posted 2017-05-30T02:26:17.527

Reputation: 5 592


Python 2, 99 bytes

def a(s):s=s[1:]or'0';l=len(s);print('#'+(l/4*s[-1]).join(i+i*(l==3)for i in(l<5)*6*s)+s+s[-1])[:7]

Try it online!


Posted 2017-05-30T02:26:17.527

Reputation: 3 566

Nice first post. As an aside, Try it online! is an optional but recommended site that runs code to include in your answer. It can generate CG&CC posts and gives an accurate byte count to boot.

– Veskah – 2019-06-27T12:43:23.320

Very cool, thanks! – Jitse – 2019-06-27T12:44:53.953


Python 2 - 179 bytes

n=raw_input()                                #prompts for string
t=len(n)                                     #the length of the string is stored to 't'
if t==1:n+="0"*6                             #if t is only one char long, it needs to be black, so n is assigned 6 zeroes
if t==2:n+=n[1]*5                            #if t is two chars long, it adds the last character times 5 at the end
if t==3:n+=n[1:3]*2                          #if t is 3 chars, it multiplies the last two digits times 3
if t==4:n="#"+n[1]*2+n[2]*2+n[3]*2           #if t is 4 chars, it multiplies each char by two
if t==5:n=n[:2]+n[4]+n[2]+n[4]+n[3]+n[4]     #if t is 5 chars, it makes it work
if t==6:n+=n[t-1]                            #if t is 6 chars, it adds the last character to the end
print n                                      #it prints out n

Can anyone help me save some bytes? All of the if statements seem like they could be shortened into something shorter, I just don't know what.


Posted 2017-05-30T02:26:17.527

Reputation: 1 425

1Try putting each snippet in a list and indexing. Also, switching to Python 3 will most likely save bytes and list[len(list)-x] is the same as list[-x]. – CalculatorFeline – 2017-05-31T01:49:08.223

If you want to squeeze out some bytes, consider converting if t==1: to if t<2: (and if t==2: to if t<3:, etc.). It's less readable, to be sure, but more code-golf-able! – J-L – 2018-09-26T19:51:27.683


TXR Lisp: 171 bytes


(do let ((s (cdr @1)))
  (caseql (length s)
    (0 "#000000") 
    (1 `#@s@s@s@s@s@s`)
    (2 `#@s@s@s`)
    (3 `#@[mappend list s s]`)
    (4 `#@[s 0]@[s 3]@[s 1]@[s 3]@[s 2]@[s 3]`)
    (5 `#@s@[s 4]`)
    (6 `#@s`))))

This is an anonymous function: the do macro generates a (lambda ...) form.

It is idiomatic coding style, suitable for production; the only golfing is squashing whitespace:

(do let((s(cdr @1)))(caseql(length s)(0"#000000")(1`#@s@s@s@s@s@s`)(2`#@s@s@s`)(3`#@[mappend list s s]`)(4`#@[s 0]@[s 3]@[s 1]@[s 3]@[s 2]@[s 3]`)(5`#@s@[s 4]`)(6`#@s`))))


Posted 2017-05-30T02:26:17.527

Reputation: 372


Braingolf, 95 bytes


Try it online!

This is effectively Braingolf's equivalent of a switch-case on the amount of characters after the # in the input.


Stuff that always runs:

l1-.1  Implicit input to stack
l      Push length of stack
 1-    Decrement last item in stack
   .   Duplicate last item in stack
    1  Push 1

If #X:

e              If last 2 items (input length - 1 and 1) are equal..
 $_            ..Pop last item silently
   !&@         ..Print entire stack as chars without popping
      4>       ..Push 4 and move it to start of stack
        [..]   ..While loop, decrements first item in stack when it reaches ]
               ..If first item in stack is 0 when reaching ], exit loop
               ..This loop will run 5 times
         !@    ....Print last char without popping
            |  endif

If #XX

This one may be golfable a little, I might look at it when I get home

.2               Duplicate last item and push 2
  e              If last 2 items (input length - 1 and 2) are equal..
   $_            ..Pop last item silently
     !&@         ..Print entire stack as chars without popping
        !@2      ..Print last 2 items as chars without popping
           !@2   ..Print last 2 items as chars without popping
              |  Endif


.3                   Duplicate last item and push 3
  e                  If last 2 items (input length - 1 and 3) are equal..
   $_                ..Pop last item silently
     <@              ..Move first item in stack to the end, then pop and print
       V2            ..Create new stack and push 2 to it
         [.......]   ..While loop, see above for explanation
          R<         ....Switch to stack1 and move first item to end of stack
            !@!@     ....Print last item on stack twice without popping
                v    ....Move to stack2 for loop counting
                  |  Endif

You get the idea


Posted 2017-05-30T02:26:17.527

Reputation: 9 656


Ruby, 127 bytes


Try it online!


Posted 2017-05-30T02:26:17.527

Reputation: 101


Ruby, 118 bytes

def f c

Try it online!


Posted 2017-05-30T02:26:17.527

Reputation: 101


05AB1E, 24 bytes


Try it online or verify all test cases.


ć           # Extract the head of the (implicit) input-string;
            # pop and push remainder and head
 U          # Pop and store the head in variable `X`
  ©         # Store the remainder in variable `®` (without popping)
  0         # Push a 0
  ®Ð        # Push `®` three times
    €D      # Duplicate each character in the last copy (which becomes a character-list)
  ®S        # Push variable `®` again, converted to a character-list
    ¤       # Get its last character (without popping the list itself)
     ý      # Join the list by this character
  ®         # Push variable `®` once again
   ¤        # Get its last character (without popping the string itself)
    «       # Append it to the string
  )         # Wrap all values into a list
   J        # Join the inner list from `€D` together to a list,
            # or in case of input `#`, join everything together to string "0"
            #  i.e. "#" → (["","0","","","",""] → ) "0"
            #  i.e. "#4" → ["4","0","4","4","44","4","44"]
            #  i.e. "#a1" → ["a1","0","a1","a1","aa11","a11","a11"]
            #  i.e. "#1a8" → ["1a8","0","1a8","1a8","11aa88","18a88","1a88"]
            #  i.e. "#abcd" → ["abcd","0","abcd","abcd","aabbccdd","adbdcdd","abcdd"]
            #  i.e. "#3764e" → ["3764e","0","3764e","3764e","33776644ee","3e7e6e4ee","3764ee"]
            #  i.e. #123456 → ["123456","0","123456","123456","112233445566","16263646566","1234566"]
    Ig      # Push the length of the input
      è     # Index (0-based) this into the list (with automatic wraparound for length=7)
       6∍   # Extend/shorten the string to length 6
         Xì # And prepend variable `X` (the "#")
            # (after which the result is output implicitly)

Kevin Cruijssen

Posted 2017-05-30T02:26:17.527

Reputation: 67 575