Here is the code which I want to shorten.

while n:
 x,r,g,b=(int(x) for x in s.split())
 if r==0:e="black"
 elif r==100:e="red"
 elif r==1:e="blue"
 elif r==10:e="green"
 elif r==101:e="magenta"
 elif r==11:e="cyan"
 elif r==110:e="yellow"

Input: 3

4643 5913 4827 9752
5583 5357 5120 9400
2025 5475 4339 8392


  • 2
    \$\begingroup\$ Mind explaining what exactly what this is for, and what you're doing? \$\endgroup\$ – Okx Oct 15 '17 at 18:47
  • \$\begingroup\$ This is off topic imo. There is no "winner" and tips are more general. \$\endgroup\$ – Christopher Oct 15 '17 at 19:04
  • 8
    \$\begingroup\$ @Christopher2EZ4RTZ we had many questions like that one before. they are not off topic, this had been discussed for a long time \$\endgroup\$ – Uriel Oct 15 '17 at 19:05
  • 13
    \$\begingroup\$ For the close voters, golfing tips questions are perfectly on-topic here, as decided on meta \$\endgroup\$ – caird coinheringaahing Oct 15 '17 at 19:07
  • 1
    \$\begingroup\$ Can you give us the problem statement? \$\endgroup\$ – xnor Oct 15 '17 at 19:12

Instead of ((a*10+c)*10)+d we can use ((a*2+c)*2)+d to distinguish the colors.

 if r==0:e="black"
 elif r==4:e="red"
 elif r==1:e="blue"
 elif r==2:e="green"
 elif r==5:e="magenta"
 elif r==3:e="cyan"
 elif r==6:e="yellow"

Ah, but now we're just distinguishing between values from 0 to 7, so we can index into an array instead!

 # or even shorter:
 e="black blue green cyan red magenta yellow white".split()[r]

Combining with Uriel's changes we get down to 136 bytes (164 bytes saved).

exec'x,r,g,b=map(int,raw_input().split());print"black blue green cyan red magenta yellow white".split()[x/r%2*4+x/g%2*2+x/b%2];'*input()

Try it online!

| improve this answer | |
  • \$\begingroup\$ @RanvijaySingh If you don't mind putting , (commas) between the integers, then you can reach 115 bytes \$\endgroup\$ – Mr. Xcoder Oct 15 '17 at 19:51

For the repetition use an exec statement,

map(int, for the conversion of string input into numerals,

shorten calculating r with r=a*100+c*10+d, then put the calculations of each variable (a, c, d) instead of the variable,

and for the conditions use a dictionary with a get query.

Finally, mash everything into one line.

Final result (updating):


Bytes saved: 121.

| improve this answer | |
  • \$\begingroup\$ Can you please add a tio link? Thanks! \$\endgroup\$ – Mr. Xcoder Oct 15 '17 at 18:57
  • 1
    \$\begingroup\$ @Mr.Xcoder I don't know what type of input OP is expecting, so I can't give an example until he does \$\endgroup\$ – Uriel Oct 15 '17 at 18:58
  • 1
    \$\begingroup\$ Btw you can save quite a few bytes using exec"..."*input() \$\endgroup\$ – Mr. Xcoder Oct 15 '17 at 18:59
  • 1
    \$\begingroup\$ You have some redundant parentheses in there (around print, and (x/r%2)*100x/r%2*100 etc.) \$\endgroup\$ – Lynn Oct 15 '17 at 19:11
  • 2
    \$\begingroup\$ @Lynn yea, you covered it better. I just applied golfing techniques - and I'm used to python 3 \$\endgroup\$ – Uriel Oct 15 '17 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.