12 Parties


A logic test from a job selection process:

A nation’s people can vote for members of parliament from 12 parties. One voter must cast only one vote for one representative. If a party doesn’t get more than 5% of votes, then it won’t get any chairs in the parliament. How many chairs can get (at most) the party which collects 25% of votes?

Solution

  1. If V1 == 25%, then Sum(Vi, i=2..12) == 75%

  2. The best for P1 would be that the other parties get 5% of votes, so that they lower the 75% but don’t get any chairs.

    • 5% * 11 = 55% < 75%
      11 other parties cannot get 5% each because there are no parties left to absorb the remaining 20% of votes.

    • 5% * 10 = 50% -> V2 = 75% – 50% == 25%
      10 other parties can get 5% each instead because the remaining party can get the remaining 25% of votes.

  3. Only P1 and P2 will share the parliament, and each of them with 50% of the chairs.

About uniformity

Use uniform schemes, at least project wide, if not company wide.

When an engineer is a world apart, allowed to work alone on a big chunk of functionality, during a few weeks in a row, they produce lots of code which is seldom harmonized with the rest of existing functionalities.

Why does this happen? There are many reasons. Clearly there is little to no interest in improving code quality at a management level, because they think that technical debt can always be fixed later by adding more cheap laborers, when it’s probably the exact opposite of that, i.e. you can always add more cheap laborers to produce more code if the technical debt is kept at bay along time.

Another reason is that the size and number of code changes in a single merge request makes it nearly impossible to perform meaningful code reviews, and in fact seasoned reviewers skim through them very fast and accept true atrocities.

For example, in the same API

  • they allow the same properties to have different names, having to use one or the other depending on the state of the object, like
    • activeGroup.name
    • pendingGroup.groupName
  • they allow the same concepts to have different values, having to use one or the other depending on the endpoint you use, like
    • operations = ['CREATE', 'UPDATE', 'DELETE'];
    • operations = ['C', 'U', 'D']
  • they allow the same operations to have different results’ structures, like
    • `POST {name: ‘John’, age: 32}
      • success response: {name: ‘John’, age: 32, created: …}
      • error response: {error: ‘Age should be less than 30’}`
    • `POST {city: ‘Barcelona’, temperature: ’18 C’}
      • success response: ‘OK’
      • error response: {msg: ‘Italian cities only, please.’}`

Of course there are many more typologies of incoherences in some code bases.

It could seem that they are just little inconveniences but they contribute lots of lines of code to the app. For example, a thing so simple like comparing two instances of the same document in different states, becomes a mess (i.e. more intricacies and bugs) when the names of the properties change according to the state.

How to add a link from a featured image to any URL

I want to provide a link to the source of a featured image, that I use to draw attention to a post of mine, to help catalog that as a “fair use”.

Unfortunately, the standard Twentyseventeen theme of WordPress doesn’t offer any way of doing such a simple thing out of the box.

But with the help of my Custom Stuff plugin

<?php
/*
Plugin Name: Custom Stuff
Plugin URI: http://andowebsit.es/
Description: Custom stuff for my blog.
Author: Andrea Ercolino
Author URI: http://andowebsit.es/about
Version: 1.1
*/

function custom_stuff_header() {
    require 'header.php';
}
add_action( 'wp_head',  'custom_stuff_header', 10, 0 );


function custom_stuff_footer() {
    require 'footer.php';
}
add_action( 'wp_footer',  'custom_stuff_footer', 10, 0 );

require 'no-richedit.php';

(file wordpress/wp-content/plugins/custom-stuff/custom-stuff.php)

that’s only a few lines away

<?php

function custom_stuff_featured_image_link($html, $post_id, $post_thumbnail_id) {
    $post_thumbnail = get_post( $post_thumbnail_id );
    if ( ! $post_thumbnail ) {
        return $html;
    }
    $href = $post_thumbnail->post_title;
    if ( ! $href ) {
        return $html;
    }
    return "<a href='$href' target='_blank'>$html</a>";
}

add_filter( 'post_thumbnail_html', 'custom_stuff_featured_image_link', 10, 3 );

(file wordpress/wp-content/plugins/custom-stuff/featured-image-link.php)

<?php
/*
Plugin Name: Custom Stuff
Plugin URI: http://andowebsit.es/
Description: Custom stuff for my blog.
Author: Andrea Ercolino
Author URI: http://andowebsit.es/about
Version: 1.2
*/

function custom_stuff_header() {
    require 'header.php';
}
add_action( 'wp_head',  'custom_stuff_header', 10, 0 );


function custom_stuff_footer() {
    require 'footer.php';
}
add_action( 'wp_footer',  'custom_stuff_footer', 10, 0 );

require 'no-richedit.php';

require 'featured-image-link.php';

(file wordpress/wp-content/plugins/custom-stuff/custom-stuff.php)