Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ericandrewlewis/59ee632d94ebd4ab50f0ba4641f061c5 to your computer and use it in GitHub Desktop.
Save ericandrewlewis/59ee632d94ebd4ab50f0ba4641f061c5 to your computer and use it in GitHub Desktop.
Annual global population growth for different periods
const parseInput = input => {
const lines = input.split('\n');
const output = [];
let periodStartDate = null;
let periodStartPopulation = null;
for (let i = 0; i < lines.length; i++) {
const lineBits = lines[i].split(' ').filter(lineBit => lineBit !== '');
if (i === 0) {
periodStartDate = parseInt(lineBits[0]);
periodStartPopulation = parseInt(lineBits[1]);
continue;
}
const period = {
startDate: periodStartDate,
startPopulation: periodStartPopulation,
endDate: parseInt(lineBits[0]),
endPopulation: parseInt(lineBits[1]),
};
period.annualGrowthRate =
(
Math.round(
( (period.endPopulation / period.startPopulation) - 1 )
/ (period.endDate - period.startDate) * 10000
) / 100
) + '%';
output.push(period);
if (i === lines.length - 1) {
continue;
}
periodStartDate = parseInt(lineBits[0]);
periodStartPopulation = parseInt(lineBits[1]);
}
return output;
};
const input = `-10000 4
-8000 5
-7000 5
-6000 5
-5000 5
-4000 7
-3000 14
-2000 27
-1000 50
-750 60
-500 100
-400 160
-200 150
1 170
200 190
400 190
500 190
600 200
700 210
800 220
900 226
1000 310
1100 301
1200 360
1250 400
1300 360
1340 443
1400 350
1500 425
1600 545
1650 470
1700 600
1750 790
1800 980
1850 1260
1900 1650
1910 1750
1920 1860
1930 2070
1940 2300
1950 2400
1960 3020
1970 3700
1974 4000
1980 4430
1987 5000
1990 5260
2000 6070
2017 7500`;
const output = parseInput(input);
console.log(output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment