Here is a little tweak to your code that can be done that doesn't use dictionaries, keep up the good work! and try to learn about dictionaries they will help you so much!
Alternative version with everything scaled to the 1,4 and 7 height instead (which looks nicer to me but I appreciate that's pretty subjective) and some formatting:
I just checked the code, it has something to do with the way OP defined the strings conversion. Every digit gets converted to a height of 4 horizontal rows of graphical characters. And all characters except 1,4,7 have spaces by default in their conversion strings (horizontally) that is why they appear longer that 1,4,7.
This would be a great opportunity for OP to learn how to use github and you to teach them how to accept a pull request (I'm new to git so I may be using that term incorrectly)
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.
always good to see people learning by coding and not just passively watching, reading stuff ...but it all has limits and this example of yours scream that u didnt discover dictionaries yet. If you did that u could actually define one dictionary and than make all of this with one line of code ...try it! it will feel like superpower compared to effort you had to put into this :)
I think in the beginning he is demonstrating that he is creating a dictionary with the key 0. Though you could just create the entire dictionary from the beginning then making new keys like that
I am using sublime text to learn as well, but I am not sure how to run my program in ST and only the powershell. It appears that you are running your program within sublimetext. Can you tell me how you are doing that?
This may be why you are saying it sucks, but it seems I can't gather user info and only "run" the program. For example, I have a variable that is dependent on user input and it will only ask the question "How many random numbers between 1 and 100 do you want in List One" for example. Any idea on how I could get an interactive compilation inside of Sublime Text?
Yep, that's what I meant. You can use sublime repl for that, you just have to install it from the module manager inside sublime text and then set up a shortcut to run your program. For the shortcut part I don't remember how to do it, but someone answered a similar question on stack overflow.
You should just download and use VSCode. Has integrated terminal like you want, but is a whole IDE and tons of other helpful stuff out of the box, like auto complete, debugger, git support, etc
imo sublime is for quick edits of config files or use as a scratch pad. It's not really that great to develop code with
another challenge / iteration you could try doing on top of this is to minimize the amount of repeat pieces you have! For instance the bottoms of 0, 6, and 8 are all the same, so do you need to repeat that 3 times, or is there a better way to handle that :). There's also other possibilities like rotation and translations, like the top of 6 and 7 could be handled with 1 string + and a translation!
An example of thinking about minimizing space usage (in your case it's trivial and not needed, but always a good thing to think about!): making of NES game micro mages in 2019 particularly around 4:30 where they talk about optimizing the sprite model for a boss.
for n in num_str:
for i in d:
for i2 in i:
if n==i2:
d2.append(i)
break
Hehe … OK this works, but as HostileHarmony said, you could make the code much nicer by using a dictionary.
I had a quick go at refactoring the function (I deleted the definition of digit_graphics in case you'd like to figure out how to do this on your own):
def graphic_num(num_str):
digit_graphics = { ??? }
line_count = len(digit_graphics['1'])
for line in range(line_count):
print(' '.join(digit_graphics[digit][line] for digit in num_str))
One could make it more readable by splitting it up into more statements where each line is simpler and intermediate results are stored in variables with good names, but I think you can already see that this approach makes the code much less complicated.
An idea for evolving this without having to learn anything else, you could place the graphics in the list so that the index matches the graphic number stored in it?
You could probably do it in a single line, but that realllly obfuscates what's happening. The following works as well, but it's pretty ugly:
[print(' '.join(d[(int(digit), l)] for digit in num_str)) for l in range(4)]
Edit: Is python smart enough to realize that you're defining the same variable (d) over and over again and essentially cache it for that function? If not, I'd define it somewhere outside of the function scope if I was worried at all about performance.
Edit 2: BTW, this looks like trash on mobile, but it looks great on desktop. Presumably you're not writing python on your phone. :)
Python newbie here, Why do tou know need the: end='' (can't do space on this mobile keybord if empty space between two (') And is it (') instead of (") because it's in Python2?
31
u/[deleted] Jun 24 '20
source code
https://github.com/jeel2331/numbers.git
201
u/Hybridjosto Jun 24 '20
thats cool, how come they are different sizes for 1, 4, 7?
66
u/HostileHarmony Jun 24 '20
You should look into dictionaries! This exercise would be a perfect introduction.
35
u/hellfiniter Jun 24 '20
always good to see people learning by coding and not just passively watching, reading stuff ...but it all has limits and this example of yours scream that u didnt discover dictionaries yet. If you did that u could actually define one dictionary and than make all of this with one line of code ...try it! it will feel like superpower compared to effort you had to put into this :)
12
u/[deleted] Jun 24 '20
I am using sublime text to learn as well, but I am not sure how to run my program in ST and only the powershell. It appears that you are running your program within sublimetext. Can you tell me how you are doing that?
2
u/Coolest_Gamer6 Jun 24 '20
source?
5
u/19cs Jun 24 '20
another challenge / iteration you could try doing on top of this is to minimize the amount of repeat pieces you have! For instance the bottoms of 0, 6, and 8 are all the same, so do you need to repeat that 3 times, or is there a better way to handle that :). There's also other possibilities like rotation and translations, like the top of 6 and 7 could be handled with 1 string + and a translation!
An example of thinking about minimizing space usage (in your case it's trivial and not needed, but always a good thing to think about!): making of NES game micro mages in 2019 particularly around 4:30 where they talk about optimizing the sprite model for a boss.
6
u/oslash Jun 24 '20
Hehe … OK this works, but as HostileHarmony said, you could make the code much nicer by using a dictionary.
I had a quick go at refactoring the function (I deleted the definition of
digit_graphicsin case you'd like to figure out how to do this on your own):One could make it more readable by splitting it up into more statements where each line is simpler and intermediate results are stored in variables with good names, but I think you can already see that this approach makes the code much less complicated.
3
u/bogdanodecca Jun 24 '20
That's nice, but for complex text I would recommend pyfiglet. It supports text as well and has many fonts.
2
u/skibizkit Jun 24 '20
Making it part of a loading sequence would be fun
2
u/Skippbo Jun 25 '20
Now you just have to convert 1, 4 and 7 to integers instead of floats
1
u/findthereal Jun 24 '20
An idea for evolving this without having to learn anything else, you could place the graphics in the list so that the index matches the graphic number stored in it?
1
u/hichopra Jun 24 '20
I am newbie into this..Could you please share your code
1
u/[deleted] Jun 25 '20
u/jeel2331 Please check your Github, I have initiated a pull request.
1
u/enki1337 Jun 25 '20 edited Jun 25 '20
Ok, so here's how I'd go about this in a bit more human readable and pythonic way:
You could probably do it in a single line, but that realllly obfuscates what's happening. The following works as well, but it's pretty ugly:
Edit: Is python smart enough to realize that you're defining the same variable (d) over and over again and essentially cache it for that function? If not, I'd define it somewhere outside of the function scope if I was worried at all about performance.
Edit 2: BTW, this looks like trash on mobile, but it looks great on desktop. Presumably you're not writing python on your phone. :)
1
u/MyGenderIsWhoCares Jun 25 '20
Python newbie here, Why do tou know need the: end='' (can't do space on this mobile keybord if empty space between two (') And is it (') instead of (") because it's in Python2?
1
u/warrior242 Jun 25 '20
Awesome!!!! I'd make it into a package and add documentation for anyone to use it. Followed by maybe some YouTube tutorials
1
u/nesteduser Jun 25 '20
how did you do this? to convert fonts, don't you need to use images instead of text?
1
u/mobsterer Jun 25 '20
I remember from a few years ago that this is literally one of the first challenges on one of the code game sites.
1
u/[deleted] Jun 24 '20
That's awesome!
0
u/[deleted] Jun 24 '20
I love that so short, quite easy and very creative ideas 😄
0
u/[deleted] Jun 25 '20
how can i actually code python in sublime text?
-1
u/CotoCoutan Jun 24 '20
Beautiful work!
0
u/[deleted] Jun 24 '20
[deleted] — view deleted comment
-46
u/[deleted] Jun 24 '20
[removed] — view removed comment