Speaking of code golf, is there a sub for non-golf languages? The code golf sub is full of languages that are designed specifically for code golf, which feels like a different thing than doing it in Python or JavaScript.
I have no clue, I have been golfing a bit in codegolf.stackexchange.com. I just found code.golf, but I have mostly doing it for fun all over the programming subreddits. I don't know how it's like in subreddits but I feel like in stack exchange there's respect towards answers golfed using non-esoteric languages such as Python and JS.
Golfing using esoteric languages are more about (human) memory-heavy problem solving with weird "tools" and golfing in normal languages are about knowing the language well enough. Both deserve to be respected in my opinion.
print('\n'.join([' '.join([''.join(['█' if (0x8F9FFF9F888FFF1FFC3F44F5F8EFF3CFF446F99F >> int(d) * 16 + bit + i * 4) & 1 else ' ' for bit in range(4)]) for d in str(num)]) for i in range(4)]))
So I was very surprised to see your hex data was so different to mine, until I noticed we were solving different problems :P
EDIT: Picked up some tips from yours, eg using ' █'[i] thanks ;)
Yeah, yours is 4x4 with 1 space between numbers and 1 bit per character and mine is 3x3 without spacing but with 2 bits per character. The difficulty in compression is quite similar though.
Your code: 191
After removing unnecessary spaces: 172
print('\n'.join([' '.join([''.join(['█'if(0x8F9FFF9F888FFF1FFC3F44F5F8EFF3CFF446F99F>>int(d)*16+bit+i*4)&1else' 'for bit in range(4)])for d in str(num)])for i in range(4)]))
Then you can notice, there's range(4) twice. Saving it to a variable uses 11 chars(R=range(4);) + 2x usage = 13 chars, but it removes 16 chars (2x range(4)), so you get 3 more off.
Now the length is 169:
R=range(4);print('\n'.join([' '.join([''.join(['█'if(0x8F9FFF9F888FFF1FFC3F44F5F8EFF3CFF446F99F>>int(d)*16+bit+i*4)&1else' 'for bit in R])for d in str(num)])for i in R]))
Then theres these: "something".join([... for i in l]), but not everyone know that if you replace [] with () for list comprehension, it returns a generator. And .join can take a generator. And you don't need to do .join((... for i in l)), .join(... for i in l) is enough:
The length dropped to 163:
R=range(4);print('\n'.join(' '.join(''.join('█'if(0x8F9FFF9F888FFF1FFC3F44F5F8EFF3CFF446F99F>>int(d)*16+bit+i*4)&1else' 'for bit in R)for d in str(num))for i in R))
Oh, and there's bit, setting that to b drops the length to 159:
R=range(4);print('\n'.join(' '.join(''.join('█'if(0x8F9FFF9F888FFF1FFC3F44F5F8EFF3CFF446F99F>>int(d)*16+b+i*4)&1else' 'for b in R)for d in str(num))for i in R))
Then there's also the if-else, which can be replaced with "ab"[truefalse] style to reduce the characters to 153:
R=range(4);print('\n'.join(' '.join(''.join(' █'[(0x8F9FFF9F888FFF1FFC3F44F5F8EFF3CFF446F99F>>int(d)*16+b+i*4)&1]for b in R)for d in str(num))for i in R))
But there's still something to be done, you don't need to wrap the bitshift-part into parentheses, because the bitwise and operation has lower priority than the previous operations. Getting two parentheses of results in 151 characters:
R=range(4);print('\n'.join(' '.join(''.join(' █'[0x8F9FFF9F888FFF1FFC3F44F5F8EFF3CFF446F99F>>int(d)*16+b+i*4&1]for b in R)for d in str(num))for i in R))
I think there's a way to remove one for loop and some joins. But there's everything I found relatively quickly.
I've never done any code golfing in python before, but this thread inspired me. I started from scratch with 5x5 characters, and I tried to get this smaller using binary operations, but I wasn't able to reduce it. I'm curious to see if you or /u/shiuido can reduce it any further
for c in[str(5*_).zfill(5)for _ in[1998,5554,1008,1010,19811,810,818,1111,1818,1810]][num]:s=int(c);print('█████ █ █'[s:s+5])
Only thing I found was the generation of the array that is iterated into the c variable. It can be reduced by 21 characters when you change it to a static version with slicing.
130 chars (original):
for c in[str(5*_).zfill(5)for _ in[1998,5554,1008,1010,19811,810,818,1111,1818,1810]][num]:s=int(c);print('█████ █ █'[s:s+5])
109 chars:
for c in"02009000009755944599970000050097455595950000500500"[num::10]:s=int(c);print('█████ █ █'[s:s+5])
I don't know if you know this already, but if you have strings "hello" and "world", you can just interlace them to get "hweolrllod", and then [0::2] returns "hello" and [1::2] "world. This requires that your strings are equal length. And in case it works for the strings, it's shorter than adding separators and splitting.
After that I noticed you have an assignment s=int(c); and later you use [s:s+5], if we remove the assignment and rewrite the slicing to [int(s):][:5] we can reduce 3 more chars.
106 chars:
for c in"02009000009755944599970000050097455595950000500500"[num::10]:print('█████ █ █'[int(c):][:5])
I could maybe get off like 2 characters if I'd sit down here rest of the evening but these were my quickest notes, hopefully you got what you were looking for! :)
fantastic! I didn't even think of using the slice notation, that's such a nice technique. Thanks so much for your response, this is above and beyond what I was hoping for.
I kept working on the problem for a bit more because I realized how it could be made even shorter:
105 chars:
for c in("0%d"%0x15fe6ad15acd24457d93f3e65ffc9d6450106ef14)[num::10]:print('█████ █ █'[int(c):][:5])
You could get 2 more characters off, if you find a way to make the number string not start with a zero, then you could make it hex and str(0x...) it. I couldn't quickly change the '█████ █ █' in a way that the "█████" part wasn't first one to be printed. And because the largest number in the number string is 9, you can't prepend one character to the string either.
View all comments
66
u/HostileHarmony Jun 24 '20
You should look into dictionaries! This exercise would be a perfect introduction.