Tax Brackets
January 26, 2021
My W-2 form came in the mail a few days ago, so a task about taxes is appropriate. Given the following tax brackets:
income cap tax rate
$ 10,000 0%
$ 30,000 10%
$100,000 25%
more 40%
Calculate the amount of tax given an amount of income. The tax brackets work like this: If your income is less than $10,000, your tax is $0. If your income is between $10,000 and $30,000, your tax is 10% of your income in excess of $10,000. If your income is between $30,000 and $100,000, your tax is 10% of the $20,000 of income between $10,000 and $30,000 (or $2,000) plus 25% of your income over $30,000. If your income is over $100,000, your tax is 10% of the $20,000 of income between $10,000 and $30,000 (or $2,000), plus 25% of your income between $30,000 and $100,000 (or $17,500), plus 40% of your income over $100,000 (ouch!).
For example, if your income is $123,456, your tax is $2,000 plus $17,500 plus 40% of $23,456 (or $9,382.40), a total tax of $28,882.40, or 23.4% of your income.
Your task is to write a program that calculates the amount of tax due for a given amount of income. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Stalin Sort
January 19, 2021
Stalin Sort is a single-pass sort that operates in O(1) space and O(n) time. Iterate down the list of elements checking if they are in order. Any element which is out of order is sent to the gulag (eliminated from the list). At the end you have a sorted list, though it may not be a permutation of the original list. As an example, the list (1 2 5 3 5 7) is Stalin-sorted as (1 2 5 5 7); the 3 is dropped because it is less than the 5 that it follows.
Your task is to write a program that implements Stalin sort. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Animal.txt
January 12, 2021
Today’s task is from a beginning programmer, who starts with an input file called animal.txt:
There once was a Dog
Wednesday he ate Apples
Thursday he ate Apples
Friday he ate Apples
Saturday he ate carrots
There once was a Bear
Tuesday he ate carrots
Wednesday he ate carrots
Thursday he ate chicken
He wants to create this output:
Food: Apples Animals who ate it: 1
=======
Dog
Food: Carrots Animals who ate it: 2
========
Bear
Dog
Food: Chicken Animals who ate it: 1
========
Bear
He gave a complicated awk solution that didn’t work; it produced duplicate lines of output in those cases
where the same animal ate the same food on multiple days.
Your task is to write a program to produces the desired transformation form input to output. When
you are finished, you are welcome to read or run a suggested solution, or to post your own solution or
discuss the exercise in the comments below.
Two Simple Tasks
January 5, 2021
Happy New Year! May 2021 be a better year than 2020.
We have two simple tasks today:
First: Write a program that prints the sequence 1, 5, 10, 50, 100, … up to a given limit.
Second: Write a program that finds all three-digit numbers n such that n/11 equals the sum of the squares of the three digits.
Your task is to write programs that solve the two tasks given above. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.