Your task is to, given a map as input, zoom it out or in, depending on the scale. Note that the scale given is the scale by which to zoom out, so a scale between 0 and 1 will actually zoom in.

For example, given the following (badly made) map:


And a scale factor of 2, you should first separate it into 2x2 sections:

.. | __ | __ | .. | ..
.. | /O | OO | \. | ..
.. | \O | O/ | \. | ..
.. | .. | .. | .. | ..

And in each section find the most common character:


Note that there was an ambiguous section:


I chose to use _ for this section, but using O would have been perfectly acceptable too.

If, for example, you were given the scale factor of 4, you would split it into 4x4 sections, like so:

..__ | __.. | ..
../O | OO\. | ..
..\O | O/\. | ..
.... | .... | ..

As you can tell, the map doesn't perfectly fit into 4x4 sections, but that's fine, as we can just lower the size of the section at the side.

Also, whenever we need to cut off our maps, we cut off at the bottom or on the right side.

The resulting map would look like so:


What an interesting map!

For scale factors below 1, such as 0.5, the process is simpler as we zoom in instead. Take this map:


Zooming with a scale of 0.5:


Note that whenever your zoom factor is less than 1, the following will always be true: 1/(zoom factor) % 2 == 0. When it is above 1, the only guarantee you have is that it will be a whole number. When it is 1, the map should stay the same.






My zoom
not change

My zoom
not change

You may also take the map as a newline-separated array.

  • 2
    \$\begingroup\$ I think you should've waited some more in the Sandbox. \$\endgroup\$ Commented May 9, 2017 at 15:11
  • \$\begingroup\$ @JonathonAllan No it wouldn't, on that section there are more . than O. We cut off on the right and bottom side. \$\endgroup\$
    – Okx
    Commented May 9, 2017 at 15:11
  • \$\begingroup\$ Ah, by "we cut off at the bottom or on the right side" you mean that the top-left of the map is always the top-left of a section? \$\endgroup\$ Commented May 9, 2017 at 15:17
  • \$\begingroup\$ @JonathanAllan Yes. \$\endgroup\$
    – Okx
    Commented May 9, 2017 at 15:23
  • \$\begingroup\$ OK, the word "or" is misleading :) \$\endgroup\$ Commented May 9, 2017 at 15:30

2 Answers 2


Mathematica, 105 bytes


The input is (scale, array of characters). The scale must be an integer or an exact fraction.


If[#<1, ..., ... ]

If the first input is less than 1...


Replace all Strings in the second input into a square array, with length 1/(first input)

ArrayFlatten[ ... ]

Flatten the result into a 2D array.

If[#<1, ..., ... ]

If the first input is not less than 1...


Partition the (second input) into partitions whose width/length are at most (first input).

Map[ ..., ... ,{2,3}]

Map onto level 2 and level 3...


The composition of the Commonest function (finds the commonest element in a list) and First (take the first element; in case there are multiple commonest elements).


Python, 191 182 180 bytes

lambda m,s,j=''.join,i=int:[j((lambda p:max(p,key=p.count))(j(g[i(x*s):-i(~x*s//1)]for g in m[i(y*s):-i(~y*s//1)]))for x in range(-i(-len(m[0])//s)))for y in range(-i(-len(m)//s))]

Call _(map, scale_factor), where map is an array of lines, and it returns an array of lines.

Though this answer has already been beaten, I want to explain it, as it doesn't special case where the scale factor is less than one.

It makes a h by w matrix, where h = ceiling(map height / scale factor), and w = ceiling(map width / scale factor).

For every index (x, y) in the matrix, do:

  • Take a submatrix from the coordinates int(x * scale factor), int(y * scale factor) to ceil((x + 1) * scale factor), ceil((y + 1) * scale factor).
  • Put the most common character in that submatrix at (x, y).

Try it online!

Try test cases


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.