Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Random Pixels Image Compression

Challenge:

Given an integer \$n\$, guaranteed to be \$\geq2\$ and a power of 2, we are going to draw multiply boards either side-by-side or below one another, where the first has dimensions of \$n\$ by \$n\$; the second \$\frac{n}{2}\$ by \$\frac{n}{2}\$; the third \$\frac{n}{4}\$ by \$\frac{n}{4}\$; etc.; until the final \1ドル\$ by \1ドル\$ pixel.

The first \$n\$ by \$n\$ board will have its pixels uniformly randomly generated with black (rgb(0,0,0)/#000000) and white (rgb(255,255,255)/#FFFFFF) pixels. The next \$\frac{n}{2}\$ by \$\frac{n}{2}\$ compressed image will have each 2x2 block of pixels reduced to the color with the most pixels in that block (thus if a 2x2 block has four or three pixels of the same color, it becomes that color; and if both colors have two pixels each, we again choose one uniformly randomly).

It must be clear how the boards of each iteration are separated.

Challenge rules:

  • Output is flexible, but must be a . So it can be drawn and shown in a frame/canvas; output to an image-file on disk; drawn in a graph; generated as black/white Minecraft Wool blocks; generated as colored MS Excel cells; etc.
  • As mentioned, it should be clear how the boards of each iteration are separated. This can for example be done by using a non-white and non-black color: setting a background of your canvas with a single pixel delimiter between boards or surrounding each board with a border. Alternatively you can output each board separately, in its own image-file or own canvas tab/window perhaps.
  • The iterations can be drawn both horizontally or vertically, and alignment is irrelevant (e.g. vertical output with right aligned blocks is fine).
  • The size of the individual blocks doesn't necessarily have to be a single pixel, as long as they are all the same size and squares.

Example:

Input: \8ドル\$
Potential output - horizontal; bottom-aligned; red background as separator:

enter image description here

Same example output, but vertical, right-aligned, with a blue border as separator instead:

enter image description here

General rules:

  • This is , so the shortest answer in bytes wins.
    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
  • Standard rules apply for your answer with default input rules and graphical output rules.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code, and if not, please include an example screenshot of the output.
  • Also, adding an explanation for your answer is highly recommended.

Answer*

Draft saved
Draft discarded
Cancel
1
  • \$\begingroup\$ The u+m+1 can be u-~m to save a byte (relevant tip). \$\endgroup\$ Commented Sep 23, 2024 at 10:00

AltStyle によって変換されたページ (->オリジナル) /