r/Python • u/[deleted] • Jun 24 '20
I Made This i made a python function that turn numbers into graphical format.
201
u/Hybridjosto Jun 24 '20
thats cool, how come they are different sizes for 1, 4, 7?
74
u/ashishkhulbey Jun 24 '20 edited Jun 24 '20
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.
Edit : Added Link.
60
u/SecretAtLarge Jun 24 '20 edited Jun 24 '20
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)
8
-56
66
u/HostileHarmony Jun 24 '20
You should look into dictionaries! This exercise would be a perfect introduction.
15
u/b10011 Jun 24 '20
Or binary operations if he's interested in code golfing. I just golfed a similar task like 2 hours ago:
3
u/ffollett Jun 24 '20
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.
4
u/b10011 Jun 24 '20
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.
2
1
u/Ash01Blitz Jun 25 '20
Wow that editor looks really nice. What is that?
2
u/b10011 Jun 25 '20
Neovim in a terminal
0
Jun 25 '20
[deleted]
1
u/b10011 Jun 25 '20
nnoremap <silent> <leader>sh :tabnew<CR>:terminal<CR>a nnoremap <silent> <leader>ss :split<CR>:terminal<CR>aSo basically I just run a zsh inside nvim.
To help navigation between windows, I have these binds:
nnoremap <C-J> <C-W><C-J> nnoremap <C-K> <C-W><C-K> nnoremap <C-L> <C-W><C-L> nnoremap <C-H> <C-W><C-H>To help exiting the terminal, I have this bind:
tnoremap <Esc> <C-\><C-n>This way if I have terminal at the bottom and code on top, I can just:
<Esc><C-j><C-p><Enter><Esc><C-k>aBreakdown:
<Esc>exit any non-normal mode
<C-j>bound to<C-w><C-j>, goes to window below
<C-p>(same as arrow up if using emacs bindings in bash/zsh)
<Enter>execute the previous command
<Esc>exit terminal mode
<C-k>bound to<C-w><C-k>, goes to window above
aappend mode, now your cursor is where it was before going to the terminal (with some exceptions)0
Jun 25 '20
[deleted]
1
u/b10011 Jun 25 '20
For newer versions of Vim it should open terminal in a split window too. It didn't change long ago (and I run Arch btw).
I know because I use Vim on servers.
0
Jun 25 '20
[deleted]
1
u/b10011 Jun 25 '20
I just type `:terminal` and it splits the window into terminal-panel and file-panel
→ More replies (0)-1
u/userjrd Jun 25 '20
Sublime Text I think
2
u/b10011 Jun 25 '20
Haha, no, it's terminal based neovim
1
1
u/shiuido Jun 25 '20
After messing around in python I came up with :
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 ;)1
u/b10011 Jun 25 '20
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 (2xrange(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.joincan 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 tobdrops 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.
1
u/travisdoesmath Jun 28 '20
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])1
u/b10011 Jun 28 '20
That's really beautiful and efficient approach!
Only thing I found was the generation of the array that is iterated into the
cvariable. 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! :)
1
u/travisdoesmath Jun 28 '20
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.
1
u/b10011 Jun 28 '20
Hey,
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.
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 :)
18
u/hellfiniter Jun 24 '20
https://pastebin.com/raw/85rWqZd9 kinda like this, didnt define those images of yours
9
u/TriggerCape Jun 24 '20
No, you are treating a dictionary like a list.
4
u/druman22 Jun 24 '20
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
1
u/hellfiniter Jun 24 '20
quick pastebin demonstration during commute on phone ...exactly as u said :)
1
u/hellfiniter Jun 24 '20
not sure what u mean but made this on phone on bus without running it so u might find some issues there
12
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?
13
u/lvettore02 Jun 24 '20
I think the shortcut is ctrl b by default, but you can change it in the settings.
8
Jun 24 '20 edited Jun 24 '20
I am a moron. I got all the way to dictionaries before I knew this. THANK YOU so much.
2
u/lvettore02 Jun 24 '20
Also, look into the sublime repl module because the default interpreter kinda sucks (or it did in the old version)
1
Jun 24 '20
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?
1
u/lvettore02 Jun 24 '20
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.
1
Jun 24 '20
Fantastic. I will look into this. Thank you so so much!
0
u/raycus92 Jun 25 '20
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
1
1
Jun 25 '20
I will certainly check this out. Thanks very much!
I worked with VS a long, long time ago and it seemed very bulky. From what I am reading quite a bit, that has changed.
2
u/raycus92 Jun 25 '20
So there is Visual Studio (old clunky thing) and then VSCode, which are different products
→ More replies (0)
2
u/Coolest_Gamer6 Jun 24 '20
source?
3
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
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.
1
u/enki1337 Jun 25 '20
Seems we both came up with the same solution. :) This is really more in line with the "pythonic way", I think.
Also, if you use a tuple as the dict index, you can really beautify the
digit_graphicsvariable.
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
2
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
1
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:
def graphic_num(num_str):
d = {
(1, 0): '▀▀█ ', (2, 0): '▀▀▀█', (3, 0): '▀▀▀█', (4, 0): '█ █ ',
(1, 1): ' █ ', (2, 1): '▄▄▄█', (3, 1): '▄▄▄█', (4, 1): '█▄█▄',
(1, 2): ' █ ', (2, 2): '█ ', (3, 2): ' █', (4, 2): ' █ ',
(1, 3): '▀▀▀▀', (2, 3): '█▄▄▄', (3, 3): '▄▄▄█', (4, 3): ' ▀ ',
(5, 0): '█▀▀▀', (6, 0): '█▀▀▀', (7, 0): '▀▀▀█', (8, 0): '█▀▀█',
(5, 1): '█▄▄▄', (6, 1): '█▄▄▄', (7, 1): ' █', (8, 1): '█▄▄█',
(5, 2): ' █', (6, 2): '█ █', (7, 2): ' █', (8, 2): '█ █',
(5, 3): '▄▄▄█', (6, 3): '█▄▄█', (7, 3): ' ▀', (8, 3): '█▄▄█',
(9, 0): '█▀▀█', (0, 0): '█▀▀█',
(9, 1): '█▄▄█', (0, 1): '█ █',
(9, 2): ' █', (0, 2): '█ █',
(9, 3): '▄▄▄█', (0, 3): '█▄▄█',
}
for l in range(4):
print(" ".join(d[(int(digit), l)] for digit in num_str))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. :)
2
u/GeorgeDaNub Jun 25 '20 edited Jun 25 '20
Wow that’s so understandable. Basically the second number in the tuple detonates what part of the number to print right?
Awesome solution imho
1
u/enki1337 Jun 25 '20
You got it! Glad it helped! :)
I really like to make my code as "obvious" as I can.
1
u/shiuido Jun 25 '20
I think you are better off just using
digits = [ ['█▀▀█', '█ █', '█ █', '█▄▄█', ],to save yourself some trouble, then join with nested list comprehensions:
print('\n'.join([' '.join([digits[int(d)][i] for d in str(number)]) for i in range(len(digits[0]))]))
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?
2
u/enki1337 Jun 25 '20 edited Jun 25 '20
Because by default print() will append end to whatever string you print, and by default end is set to '\n', the newline char.
It's basically the difference between print() and println() in java, if that helps.
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
0
0
-1
0
-46
Jun 24 '20
[removed] — view removed comment
7
8
Jun 24 '20
Yep, cause a lot of people are beginner here, it's good for them to look up to something simple.
2
u/b10011 Jun 25 '20
To be honest, I'd say these belong to /r/learnpython.
But I don't care that much.
31
u/[deleted] Jun 24 '20
source code
https://github.com/jeel2331/numbers.git