We’ve so far gone over a lot of concepts but I’d like to take this opportunity to offer you some programming challenges so that you can put some of what you’ve learned up to this point to use, both so that you can retain some of the things that you’ve learned through application and to build your own confidence in solving problems through technology.
Feel free to do as many or as few as you want, but I do encourage you to open up your IDE and do at least three of them.
Fibonacci
The “Fibonacci Sequence” is a sequence of numbers in which each number is the sum of the two numbers before it.
We will start at 1, so the beginning of the sequence would look like this:
1, 1, 2, 3, 5, […]
Write a program to print all numbers in this Fibonacci sequence that are below the number 1,000.
Palindromes
A “palindrome” is a word or phrase that is the same when it is read from the front or the back.
Write a program that will print “true” if a given word is a palindrome, and print “false” if it is not.
Some test cases:
Should return “true”:
- level
- racecar
- stanley yelnats
Should return “false”:
- football
- honeybee
- microwave
Temperature Conversion
Write a program that can convert temperatures between Fahrenheit and Celsius.
For the sake of simplicity we will represent our temperatures as a number, followed by either a F or a C to denote Fahrenheit or Celsius.
To convert from Fahrenheit to Celsius: subtract 32 and then multiply by 5/9.
To convert from Celsius to Fahrenheit: multiply by 9/5 and then add 32.
You do not need to perform any rounding on the result.
Some test cases:
15C should return 59.0F
90F should return 32.22222222222222C
20F should return -6.666666666666667C
FizzBuzz
This programming challenge may be of particular interest to some of you interested in pursuing a career in development. FizzBuzz or some variation of it is frequently used in technical interviews as a baseline filter for candidates.
The problem:
Write a program to print the numbers 1 through 100.
- If a number is divisible by 3 print “Fizz” instead of the number.
- If a number is divisible by 5 print “Buzz” instead of the number.
- If a number is divisible by both 3 and 5 print “FizzBuzz” instead of the number.
Constructing Chords
Musical notes can be thought of as twelve notes, each of which is a “semi-tone” above or below the note next to it:
C – C♯/D♭ – D – D♯/E♭ – E – F – F♯/G♭ – G – G♯/A♭ – A – A♯/B♭ – B
For our purposes we can use enharmonic sharps and flats interchangeably. (e.g. C♯ is equal to D♭, etc) so do not worry about differentiating them.
“Chords” in music are sequences of notes which are played together to create some sort of harmony. These notes can be categorized by the intervals in which they appear.
A “Major” Chord consists of a root note, a major third (four semitones above the root), and a perfect fifth (seven semitones above the root.)
Write a program which, given the root note, can print the accompanying “Major” chord. The exact formatting is not important here, just that you are outputting the correct notes.
Some test cases would be:
- C should return C E G
- C# should return C# F G#
- A should return A C# E
Extra Credit
There are many other types of chords than simply major chords. Add support for the following additional chord types:
Minor Chords still consist of a root note and a perfect fifth, but instead of a major third we will use a minor third (three semitones above the root.) We will differentiate minor chords with a “m” suffix.
Some test cases would be:
- Cm should return C D# G
- C#m should return C# E G#
- Am should return A C E
Dominant Seventh Chords consist of a root note, major third, and a perfect fifth similar to a major chord, but also add in a minor seventh (ten semitones above the root.) We will differentiate dominant seventh chords with a “7” suffix.
Some test cases would be:
- C7 should return C E G A#
- C#7 should return C# F G# B
- A7 should return A C# E G