Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A suggestion on the mandelbrot one-liner in the docs FAQ #97709

Closed
matheusja opened this issue Oct 1, 2022 · 3 comments
Closed

A suggestion on the mandelbrot one-liner in the docs FAQ #97709

matheusja opened this issue Oct 1, 2022 · 3 comments
Labels
docs Documentation in the Doc dir sprint

Comments

@matheusja
Copy link
Contributor

matheusja commented Oct 1, 2022

Documentation

On the docs FAQ there's a about obfuscated one-liners. The third one is a about the mandelbrot fractal, using the code:

print((lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
i=i,Sx=Sx,F=lambda xc,yc,x,y,k,f=lambda xc,yc,x,y,k,f:(k<=0)or (x*x+y*y
>=4.0) or 1+f(xc,yc,x*x-y*y+xc,2.0*x*y+yc,k-1,f):f(xc,yc,x,y,k,f):chr(
64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24))

I suggest 2 changes:

  1. Replace reduce(lambda x, y: x+y, <...>) with ''.join(<...>)

  2. Make the outer join intersperse the lines with a newline character instead of relying on line wrapping. This will turn the outer ''.join(<...>) into '\n'.join(<...>)

This results on the following code:

print((lambda Ru,Ro,Iu,Io,IM,Sx,Sy:'\n'.join(map(lambda y,
Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
Sx=Sx,Sy=Sy:''.join(map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
i=i,Sx=Sx,F=lambda xc,yc,x,y,k,f=lambda xc,yc,x,y,k,f:(k<=0)or (x*x+y*y
>=4.0) or 1+f(xc,yc,x*x-y*y+xc,2.0*x*y+yc,k-1,f):f(xc,yc,x,y,k,f):chr(
64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24))

You can see the result and compare both on the following colab link: https://colab.research.google.com/drive/1NWfwNH_o5GPRTWwCdS-pVQLwEOSgstnz?usp=sharing

@matheusja matheusja added the docs Documentation in the Doc dir label Oct 1, 2022
matheusja added a commit to matheusja/cpython that referenced this issue Oct 2, 2022
@serhiy-storchaka
Copy link
Member

I think that it goes against the initial purpose of these examples: showing an obfuscated code with lambdas.

The first example

print(list(filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,1000)))))

could be rewritten in shorter and clearer form which does not use explicit lambdas at all:

print([y for y in range(2,1000) if all(y%x for x in range(2,int(y**0.5+1)))])

But then it would not be a good example for this question.

@matheusja
Copy link
Contributor Author

Fair enough. Even then, I'm annoyed at the fact that the code doesn't explicitly insert newlines, is it possible to send another pull request, except changing only the part which concatenates lines?

The code would be:

print((lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+'\n'+y,map(lambda y,
Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
i=i,Sx=Sx,F=lambda xc,yc,x,y,k,f=lambda xc,yc,x,y,k,f:(k<=0)or (x*x+y*y
>=4.0) or 1+f(xc,yc,x*x-y*y+xc,2.0*x*y+yc,k-1,f):f(xc,yc,x,y,k,f):chr(
64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24))

The results may be seen on the collab link:

https://colab.research.google.com/drive/1RHtPuTYqwLiRh7S8SckI-cZj50fgvzGt#scrollTo=VJaN5YFEedqe

gpshead pushed a commit that referenced this issue Oct 4, 2022
Included newline separator in Mandelbrot set

Now the Mandelbrot set one-liner example on separates the lines with a '\n' character.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 4, 2022
…H-97737)

Included newline separator in Mandelbrot set

Now the Mandelbrot set one-liner example on separates the lines with a '\n' character.
(cherry picked from commit 4980260)

Co-authored-by: matheusja <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 4, 2022
…H-97737)

Included newline separator in Mandelbrot set

Now the Mandelbrot set one-liner example on separates the lines with a '\n' character.
(cherry picked from commit 4980260)

Co-authored-by: matheusja <[email protected]>
@gpshead
Copy link
Member

gpshead commented Oct 4, 2022

thanks!

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
BBBBBBBBBBBBBBBCCCCCCCCCCCCCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDCCCCCCCCCCCCCCC
BBBBBBBBBBBBBCCCCCCCCCDDDDDDDDDDDDDDDDDDDDDDDDEEEEEEFGJJHFFFEEEEEDDDDDCCCCCCCCCC
BBBBBBBBBBBCCCCCCCDDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEFFFGHJPJKL_FEEEEEEDDDDDDDCCCCC
BBBBBBBBBCCCCCCDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEEFFFFGHIJZR_QJIGFFFEEEEEEDDDDDDDCC
BBBBBBBBCCCCDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEEFFFGGGHIKP______SKHGFFFFFEEEEDDDDDDD
BBBBBBBCCCDDDDDDDDDDDDDDDDDDDDEEEEEEEEEFFFHHIXIIIIJKLO______NKJJIHGGGHSGFEEDDDDD
BBBBBBCCDDDDDDDDDDDDDDDDDDDEEEEEFFFFFFFGGHIM___R_________________PLN[SQXIFEEDDDD
BBBBBCDDDDDDDDDDDDDDDDEEEFFFFFFFFFFFGGGHIKMNS_________________________UIGGFEEDDD
BBBBBDDDDDDDDDDEEEEEFFGHOIHHHHHHHHHHHHIJLP____________________________RKJPGFEEDD
BBBBDDDDEEEEEEEEEFFFFGGHJM___LNW_ONKJJKNY______________________________SKHGFEEED
BBBBDEEEEEEEEEFFFFFFGHHJLOT__________QPZ_________________________________HFFEEEE
BBBDEEEEEEEFGGGGHHHJM_QNS______________________________________________[HGFFEEEE
BBB_________________________________________________________________QLIHGFFFEEEE
BBBDEEEEEEEFGGGGHHHJM_QNS______________________________________________[HGFFEEEE
BBBBDEEEEEEEEEFFFFFFGHHJLOT__________QPZ_________________________________HFFEEEE
BBBBDDDDEEEEEEEEEFFFFGGHJM___LNW_ONKJJKNY______________________________SKHGFEEED
BBBBBDDDDDDDDDDEEEEEFFGHOIHHHHHHHHHHHHIJLP____________________________RKJPGFEEDD
BBBBBCDDDDDDDDDDDDDDDDEEEFFFFFFFFFFFGGGHIKMNS_________________________UIGGFEEDDD
BBBBBBCCDDDDDDDDDDDDDDDDDDDEEEEEFFFFFFFGGHIM___R_________________PLN[SQXIFEEDDDD
BBBBBBBCCCDDDDDDDDDDDDDDDDDDDDEEEEEEEEEFFFHHIXIIIIJKLO______NKJJIHGGGHSGFEEDDDDD
BBBBBBBBCCCCDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEEFFFGGGHIKP______SKHGFFFFFEEEEDDDDDDD
BBBBBBBBBCCCCCCDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEEFFFFGHIJZR_QJIGFFFEEEEEEDDDDDDDCC
BBBBBBBBBBBCCCCCCCDDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEFFFGHJPJKL_FEEEEEEDDDDDDDCCCCC
BBBBBBBBBBBBBCCCCCCCCCDDDDDDDDDDDDDDDDDDDDDDDDEEEEEEFGJJHFFFEEEEEDDDDDCCCCCCCCCC

@gpshead gpshead closed this as completed Oct 4, 2022
@gpshead gpshead added the sprint label Oct 4, 2022
@ezio-melotti ezio-melotti moved this to Todo in Sprint 2024 Oct 4, 2022
miss-islington added a commit that referenced this issue Oct 4, 2022
Included newline separator in Mandelbrot set

Now the Mandelbrot set one-liner example on separates the lines with a '\n' character.
(cherry picked from commit 4980260)

Co-authored-by: matheusja <[email protected]>
JelleZijlstra pushed a commit that referenced this issue Oct 4, 2022
…) (#97823)

Included newline separator in Mandelbrot set

Now the Mandelbrot set one-liner example on separates the lines with a '\n' character.
(cherry picked from commit 4980260)

Co-authored-by: matheusja <[email protected]>
pablogsal pushed a commit that referenced this issue Oct 22, 2022
Included newline separator in Mandelbrot set

Now the Mandelbrot set one-liner example on separates the lines with a '\n' character.
(cherry picked from commit 4980260)

Co-authored-by: matheusja <[email protected]>
@terryjreedy terryjreedy moved this from Todo to Done in Sprint 2024 Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir sprint
Projects
Archived in project
Development

No branches or pull requests

3 participants