Unfair Districts

Unfair Districts


Gerrymandering is a practice intended to establish a political advantage for a particular party or group by manipulating district boundaries. The resulting district is known as a gerrymander (/ˈdʒɛriˌmændər/); however, that word can also refer to the process. The term gerrymandering has negative connotations. Two principal tactics are used in gerrymandering: "cracking" (i.e. diluting the voting power of the opposing party's supporters across many districts) and "packing" (concentrating the opposing party's voting power in one district to reduce their voting power in other districts).

In addition to its use achieving desired electoral results for a particular party, gerrymandering may be used to help or hinder a particular demographic, such as a political, ethnic, racial, linguistic, religious, or class group, such as in U.S. federal voting district boundaries that produce a majority of constituents representative of African-American or other racial minorities, known as "majority-minority districts". Gerrymandering can also be used to protect incumbents.

Read more about it on wikipedia or watch this episode Gerrymandering: Last Week Tonight with John Oliver (HBO)

You have a map of units. Every unit has two elements (amount of people voted for candidate Yellow and amount of people voted for candidate Blue). Your mission is to split the given area on X amount of districts in such a way so candidate Yellow gets more districts voted for him than candidate Blue.

There are two main rules of building districts:

  • all districts should have same amount of people;
  • all units should be connected inside of one district.

The output is essentially labelling each unit with a letter, units with the same letter would become a post-gerrymander district. The population of the original units with the same label would be merged into the new post-gerrymander district. The goal of the gerrymandering is to make sure that if the election were to be held on these new districts, Yellow candidate would win.

Input: Two arguments. Amount of people for each district (as integer), grid of all electoral zone as list of lists of units (each unit is [Yellow-voters, Blue-voters])).

Output: A list of strings. The same letters of string represents units of the same district. If candidate Yellow can not win, return empty list [].


unfair_districts(5, [[[2, 1], [1, 1], [1, 2]],
                     [[2, 1], [1, 1], [0, 2]]]) == ['AAC',

unfair_districts(9, [[[0, 3], [3, 3], [1, 1]],
                     [[1, 2], [1, 0], [1, 1]],
                     [[0, 3], [2, 1], [2, 2]]]) == ['ABB',

Here is the explanation of the first example. "A", "B", "C" are districts names (you may use any: 'a', 'b', 'c'... or '1', '2', '3'... - the checker just regards the same letter as one district). We have 3 united districts, each of them has 2 units inside with following coordinates (row, column): "A" = 0,0 and 0,1; "B" = 1,0 and 1,1; "C" = 0,2 and 1,2. Every district has the same total amount of people - 5 and all units belonging to the same district are connected. Districts "A" and "B" are yellow, since there are more people in each district voted for Yellow candidate (3:2). The same is with district "C" (more for Blue candidate - 1:4). So there are two districts for Yellow and one for Blue - this splitting variant is a valid solution.


  • 2 ≤ grid_rows, grid_columns ≤ 6;
  • 4 ≤ grid_rows * grid_columns ≤ 25;
  • Total_population % amount_of_people == 0;
  • Total_population == sum(sum(unit) for unit in chain(*grid));
  • 0 ≤ each_votes ≤ 9.
Invalid hot key. Each hot key should be unique and valid
Hot keys:
CheckiO Extensions

CheckiO Extensions allow you to use local files to solve missions. More info in a blog post.

In order to install CheckiO client you'll need installed Python (version at least 3.8)

Install CheckiO Client first:

pip3 install checkio_client

Configure your tool

checkio --domain=py config --key=

Sync solutions into your local folder

checkio sync

(in beta testing) Launch local server so your browser can use it and sync solution between local file end extension on the fly. (doesn't work for safari)

checkio serv -d

Alternatevly, you can install Chrome extension or FF addon

checkio install-plugin
checkio install-plugin --ff
checkio install-plugin --chromium

Read more here about other functionality that the checkio client provides. Feel free to submit an issue in case of any difficulties.

Pair Programming (Beta-version)

Welcome to Pair Programming! Engage in real-time collaboration on coding projects by starting a session and sharing the provided unique URL with friends or colleagues. This feature is perfect for joint project development, debugging, or learning new skills together. Simply click 'Start Session' to begin your collaborative coding journey!

Waiting for Pair Programming to start...

You are trying to join a pair programming session that has not started yet.

Please wait for the session creator to join.

Waiting for Pair Programming to reconnect...

It looks like the creator of the pair programming session closed the editor window.

It might happen accidentally, so that you can wait for reconnection.

<< <
> >>
exec show

Whats Next?

Free accounts will see Best CheckiO solutions with some delay.
Best Solutions will be opened in a moment
Become Awesome and Don't wait
The next stage is ""
Will be activated in
View More Solutions Random Review Solutions Go to the next mission