Working solution for problem no 21, but the one liners might require a few comments to be understandable