Class 2. Challenge problems 1. Leap years: Given a year, determine ...

Class 2. Challenge problems

1. Leap years:

Given a year, determine whether or not it is a leap year

? A good way to tell if a year is a leap year is if it¡¯s divisible by 4 (e.g. 2004), however if a year is

divisible by 100 then it is not a leap year (e.g. 1900). Years that are divisible by 400 however (e.g.

2000) are leap years.

? Prompt the user for year and determine whether or not it¡¯s a leap year.

2. Roman numbers:

Write a program that converts a positive integer into the Roman number system. The Roman

number system has digits:

I

1

V

5

X

10

L

50

C

100

D

500

M

1000

Numbers are formed according to the following rules:

a. Only numbers up to 3999 are represented

b. As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately.

c. The numbers 1 to 9 are represented as:

I

1

VI

6

II

2

VII

7

III

3

VIII

8

IV

4

IX

9

V

5

As you can see, an I preceding an V or X is subtracted from the value and you can never have

more than three I's in a row.

d. Tens and hundreds are done the same way, except that the letters X, L, C and C, D, M are

used instead of I, V, X, respectively.

Your program should take an input from the user, such as 1978, and convert it to Roman numerals,

MCMLXXVIII.

(From Horstmann, Big Java)

3. Prompt the user for two words. If they are the same, print "Great - the same". If they are the same

except for the case OR same except for one letter substitution, print "Okay - almost the same". If they are

the same length, print "At least the same length." A large part of this task is to find to right functions

available for use with strings. Consult the Python documentation linked on the course website!

4. Let's recommend the number of calories a person should eat each day. As a gross

approximation, we will base our recommendation on the person's weight and whether the person

has an active or sedentary lifestyle. If the person is sedentary, that person's activity factor is 13. If

the person is active, that person's activity factor is 15. If the person has a moderate level of

activity, the factor is 14. The recommended number of calories is the product of the person's

weight (lb) and the activity factor. Start your program by:

?

?

?

Take in user's weight (floating point number) as input

Take in lifestyle mode ('A' for active, 'M' for moderate, and 'S' for sedentary) as input

Print out the results (calorie recommendation)

(From fac.ksu.edu.sa, )

5. We will practice applying if statements for input validation, i.e. making sure that the user input is

acceptable for your program before proceeding with your program, in Class 4. This problem will help

you think about the algorithm for such a process in advance:

Write a program that simulates an elevator panel that skips the 13th floor, checking for input errors. For

example, if the input is not an integer, let the user know that instead of trying to move to another floor.

Or decide on how high or high deep (B1, B2, etc) your building is, and let the user know they're trying to

go to a non-existing floor. Or if they enter '13', let them know your building does not have a 13th floor.

The main goal is to only alert the user they've moved to their desired floor if it's a valid input for your

desired building.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download