Programming Practice Problems
Programming Practice Problems
Easy Problems
Moderate Problems
Easy Problems
+-------------------------+
34 21 32 41 25
+----+----+----+----+-----
14 42 43 14 31
+----+----+----+----+-----
54 45 52 42 23
+----+----+----+----+-----
33 15 51 31 35
+----+----+----+----+-----
21 52 33 13 23
+-------------------------+
1. Do you like treasure hunts? In this problem you are to write a program
to explore the above array for a treasure. The values in the array are
clues. Each cell contains an integer between 11 and 55; for each value the
ten's digit represents the row number and the unit's digit represents the
column number of the cell containing the next clue. Starting in the upper
left corner (at 1,1), use the clues to guide your search of the array. (The
first three clues are 11, 34, 42). The treasure is a cell whose value is the
same as its coordinates. Your program must first read in the treasure map
data into a 5 by 5 array. Your program should output the cells it visits
during its search, and a message indicating where you found the treasure.
Output
hae and via ecy
fto ehg ee dd
clu hlt io
5. The results from the mayor's race have been reported by each precinct
as follows:
Candidate
Precinct
A
1
192
2
147
3
186
4
5
114
267
21
13
408
382
39
29
Moderate Problems
Write a program to find all the sentences, or consecutive sequence of
sentences, in a text file where: min <= length <= max.
Assume that a sentence ends in a period, question mark, or exclamation
point.
In the special case of quoted sentences (that begin with quotes or an
apostrophe), include the terminating quote mark or apostrophe.
Count all blanks and punctuation, but assume only one blank between
sentences.
(All EOL characters should be converted to blanks).
Precondition: Min and Max will be positive integers less than 1000, and Min
<= Max.
10
100
1000
50
500
Convert the roman numeral to arabic processing the symbols from left to
right according to the following rules:
1. A symbol following one of greater or equal value adds to its value.
(E.g., XII = 12)
2. A symbol preceding one of greater value subtracts its value.(E.g., IV =
4; XL = 40)
ERROR HANDLING REQUIREMENTS
In each of the error conditions below, display the given message and skip
the numeral and continue processing the next line.
"Invalid character in input. Valid characters are I,V,X,L,C,D,M."
Only the listed characters are valid.
"Invalid numeral: can't subtract auxiliary symbol."
It is not permitted to subtract an "auxiliary" symbol. (CML, not LM = 950;
XLV not VL, = 45).
"Invalid numeral: two consecutive subtractions."
Can't do two subtractions in a row, thus LIVX is illegal.
"Invalid numeral: additions don't decrease."
Additions must decrease, as you go from left to right. Thus, each symbol
added must have a value equal or less than the last symbol which was
added. Thus, LIIX is wrong, cause we added L, added I, subtracted I, then
try to add X.
Phone "words"
Each number on the telephone dial (except 0 and 1) corresponds to three
alphabetic characters. Those correspondences are:
2
3
4
5
6
7
8
9
ABC
DEF
GHI
JKL
MNO
PRS
TUV
WXY
Given a seven digit telephone number, print all 2187 possible "words" that
number spells. (They may not be real english words, but just some
sequence of characters). Since the digits 0 and 1 have no alphabetic
equivalent, an input number which contains those digits should be
rejected. The input will be one or more seven digit integers from standard
input.
The problem can be made a bit more challenging by outputting more than
one word per line. Usually about seven words will fit on a line.
echo the input line to standard output, followed by the list of subwords,
one word per line, left justified. Each set of output words should be
followed by one blank line. The program should respond to invalid input by
echoing the input line to the output, and then issuing a one line message
indicating the nature of the error:
"Illegal character in word"
"Illegal character in number"
"Number greater than length of word"
"Word longer than 20 characters"
After issuing the message, processing should resume with the next input
line. When all input has been processed, issue a message indicating the
number of input words that were processed (including word with errors)
e.g., "End of input encountered, 4 words processed."
8
9
10
139
148
168
Write a program to accept from standard input the scores for a sequence
of balls and output the scores for the ten frames. There may be multiple
lines of input, where each input line will be the scores for one player. The
scores will be separated by one or more blanks. You may assume that the
number of scores on the line is valid.
Lotto Guesser
Cobbletown has a lottery (a small version of California's Lotto) in which
players guess four number between 1 and 9. Larry likes to play and thinks
he has a scheme to pick winning numbers. He keeps a history of past
winning numbers in a text data file. Larry thinks that if a number hasn't
occurred recently then it is more likely to show up as a winner. (Obviously
Larry isn't familiar with the statistical fact that each number has an equal
likelihood of being picked, since each week's drawing is an independent
event). Larry wants us to write a program to assist him in his wacky
scheme.
INPUT REQUIREMENTS
The lotto history data: a file of unknown length, with four integers per line.
Each integer is in the range 1 to 9.
OUTPUT REQUIREMENTS
1. The four least frequently occurring integers in the history data. (That is,
counting all the weeks, the four numbers that appeared fewer times than
any others).
2. The one integer with longest time since last occurrence. (That is, if you
count backward from now, the number for which you count the most
weeks since it occurred).
FUNCTIONAL REQUIREMENTS
(You may assume that no data validity checking is required).
1. Determine and display the four integers which occur least often in the
history data.
Note: To make the problem easier, if there is a tie between two or more
numbers for fourth place, it doesn't matter which one is printed.
2. Determine and display the integer which has gone the longest without
appearing in a winning sequence.
At a minimum your program must work correctly for the following sample
data.
Test Data
1234
5678
8765
1567
4321
Sudoku Solver
Sudoku Solver assignment