Week 3: Exercises

1. Sum of Even Fibonacci Numbers

(Project Euler, problem 2)

Determine the sum of all even Fibonacci numbers that are less than 4,000,000.

2. AEIOU

Write a program that reads a string on a single line and prints "vowels" if the string contains any vowels, otherwise "no vowels".

3. Reverse It

Read a string and print it backwards.

Enter string: zyzzyva
avyzzyz

4. Spaces In Between

Read a string and write it out with a space between each adjacent pair of characters.

Enter string: waffle
w a f f l e

5. Double Or Nothing

Read a string and print 'double' if any two adjacent characters are the same, or 'nothing' otherwise.

Enter string: abacuses
nothing
===
Enter string: lionesses
double

6. Capitalizing Words

Write a program that reads a string and capitalizes all words in the string. For this exercise, words are sequences of characters separated by one or more spaces. Do not use the built-in method title(), which does the same thing.

Enter string:    one    fine     day

 One    Fine     Day

7. Reversing the Input

Write a program that reads a series of lines, terminated by ctrl+D or ctrl+Z. The program should print the lines out in reverse order.

8. Random Card

Each card in a standard deck has a rank (2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, or Ace) and a suit (clubs, diamonds, hearts, or spades).

Write a program that prints a random card such as "9 of hearts" or "king of spades".

9. Password Generator

Write a program that generates a random password with 10 lowercase letters. The password should contain alternating consonants and vowels.

Sample outputs:

  kimolonapo
  ritilenora

10. String Comparison

Write a program that reads two string S and T, each on its own line. The program should print the string that is lexicographically greater, i.e. the string that appears later in dictionary order. Do not use the built-in string comparison operator '>'.

Enter S: limerick
Enter T: limeade
limerick

11. All Upper

Write a program that reads a string and makes all lowercase letters uppercase without using the library function upper.

Sample input:

100 Spires and Towers

Sample output:

100 SPIRES AND TOWERS

12. Month and Day

Write a program that reads a month and day, and prints a number from 1 to 365 representing a day of the year. Assume that the current year is not a leap year. For example:

Enter month: 3

Enter day: 7

Day number: 62

Explanation: March 7th is the 66th day of the year, since there are 31 days in January and 28 days in February, and 31 + 28 + 7 = 66.