-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexercises_d1.tex
270 lines (211 loc) · 7.75 KB
/
exercises_d1.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
\documentclass{article}
\usepackage[margin=2cm]{geometry}
\begin{document}
\section{Ternary operator}
\label{sec:ternary-operator}
Read the following code and, without executing it first, explain what
it does.
\begin{verbatim}
int currentYear = 2012
print "When were you born (year)?"
String str = System.console().readLine();
int birthYear = Integer.parseInt(str);
int ageAprox = currentYear - birthYear;
String result = (ageAprox > 17) ? "" : "not "
println "It seems you are " + result + "an adult."
\end{verbatim}
Hint: look at section ``The Ternary Operator''.
\section{Ende homage}
\label{sec:ende-homage}
Is there anything wrong with the following piece of code (hint: yes)?
What does it do?
\begin{verbatim}
int i = 10
while (i < 5) {
i++;
println i
}
\end{verbatim}
\section{Yet another loop}
\label{sec:yet-another-loop}
What does the following piece of code do?
\begin{verbatim}
String str = System.console().readLine()
int i = Integer.parseInt(str)
while (i < 10) {
i++;
str = System.console().readLine()
int j = Integer.parseInt(str)
if (j == 0) {
break;
} else if (j != 1) {
println j;
}
}
println "finished"
\end{verbatim}
Hint: the reserved word \verb+break+ exits the current loop.
\section{Prime numbers}
\label{sec:prime-numbers}
Write a program that asks a number from the user, then says whether
the number is prime or not. Remember that a prime number is a number
that is divisible only by 1 and itself. You can use the modulo
operator (if \verb+a % b + is zero, then $a$ is divisible by $b$).
\section{Multiplication}
\label{sec:multiplication}
Write a program that requests two numbers from the user and then
outputs its product. You cannot use the ``*'' operator.
\section{Division}
\label{sec:division}
Write a program that requests two numbers from the user and then
outputs the quotient and the remainder, e.g. if the user enters 7 and
3, your program should ouput something like ``7 divided by 3 is 2,
remainder 1''. You cannot use the ``/'' or ``\%'' operators.
\section{Naive sorting}
\label{sec:naive-sorting}
Write a program that reads three numbers and prints them in order, from
lowest to highest.
\section{Maximising}
\label{sec:maximising}
Write a program that reads a (arbitrarily long) sequence of positive
numbers. The sequence is ended when the users enters ``-1''. At that
point, the program must output the highest number in the sequence.
\section{Going up!}
\label{sec:going-up}
Read an arbitrarily long sequence of positive numbers from the user,
until -1 is entered. At that point, print "Yes" if the numbers were
consecutive and increasing and "No" otherwise. Sequences "1,2,3,4,-1"
and "5,6,7,8,9,10,11,-1" should output "Yes", but "2,3,5,6,7,-1",
"10,9,8,7,-1", and "1,1,2,3,4,5,-1" should output "No".
\section{You said high, I said low\ldots}
\label{sec:you-said-high}
Modify your former program so that it outputs "Yes" when the numbers
are consecutive, regardless of whether they go up or down. For
example, both "2,3,4,5,6,-1" and "10,9,8,7,-1" should now result in
"Yes".
\section{Poker hands (*)}
\label{sec:poker-hands}
Read five cards from the user. For each card, read the rank
(1,2,3,4,5,6,7,8,9, 10,J,Q,K) and the suit ("spades", "hearts",
"diamonds", "clubs"). Each of the five cards must be valid before
accepting the next one. Once the program has the five cards, it
should tell the user what is the best hand she has got, as per the
following list (from best to worst):
\begin{description}
\item[Straigh flush: ] all cards are of the same suite and
their ranks are consecutive. Note that they are probably
not ordered as they were entered.
\item[Poker: ] four of the five cards have the same
rank.
\item[Full House: ] three of a kind plus two of a kind.
\item[Flush: ] all cards share the same suit, but are not
consecutive.
\item[Straight: ] all cards are consecutive, but not of the same
suit.
\item[Three of a kind: ] three of the five cards have the same
rank.
\item[Two pairs: ] two pairs (see below).
\item[Pair:] two of the five cards have the same rank.
\item[Nothing: ] any other situation.
\end{description}
If you ever launch an online poker business, this could be one (very
small) piece of it.
\section{Number pyramids}
\label{sec:number-pyramids}
\subsection*{a)}
\label{sec:number-pyramidsa}
Write a program that outputs a number pyramid like the one below,
going on forever (until you press Ctrl-C).
\begin{verbatim}
1
22
333
4444
55555
666666
7777777
...
\end{verbatim}
(The formatting of the pyramid will mess up after a few numbers but
that is OK for this exercise. If that bothers you, move on to the
second and harder part of this exercise).
\subsection*{b) (*)}
\label{sec:number-pyramidsb}
Write a program that reads a number between 1 and 25, and then outputs
a number pyramid like the one below (the example is for number 8) with
that number of levels. Notice that you must write the right number of
spaces at each level so that the pyramid is properly aligned to the
right.
\begin{verbatim}
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
\end{verbatim}
\section{All the primes up to 1,000 (*)}
\label{sec:all-primes-up}
Write a program that prints all on screen all prime numbers up
to~1,000.
\section{Up to 1,000 primes (*)}
\label{sec:all-primes-uprr}
Modify the program that you wrote for the former exercise so that it
writes on screen the first 1,000 primes.
\section{Guess my number (*)}
\label{sec:guess-my-number}
Write a program that thinks of a random number between 0 and 1000, and
then lets the user try to guess it. For every guess, the computer says
whether the guess is correct, or too low, or too high. When the user
finds the number, the computer will tell how many guesses were
needed. The output could be similar to the following example:
\begin{verbatim}
Try to guess my number!
Tell me a number: 2
No! My number is higher.
Tell me a number: 800
No! My number is lower.
Tell me a number: 500
No! My number is lower.
Tell me a number: 350
No! My number is higher.
Tell me a number: 376
CORRECT!
You needed 5 guesses.
\end{verbatim}
(Hint: to get a random number between 0 and 1000, use the following
line:)
\begin{verbatim}
int numberToGuess = Math.abs(1000 * Math.random())
\end{verbatim}
\section{Rock, Paper, Scissors (*)}
\label{sec:rock-paper-scissors}
Write a program that reads 2 characters from either the keyboard or a
file. The characters are either PP, PR, PS, RP, RR, RS, SP, SR,
or~SS. They correspond to the selections made by 2 players playing the
game of rock-paper-scissors.
Make the program accept inputs until one player's score is more than 3
points ahead of the other.
Hint: remember that you can use \verb+.substring()+ to get the
elements of a String.
\section{Optimus Prime (**)}
\label{sec:optimus-prime}
Write a program that reads an integer number from the user, and then
outputs the closest prime number. If there are two prime numbers at
the same distance, it should output both. For instance, if the user
enters 5116, the output should be 5113 \emph{and} 5119.
\section{$\pi$ (**)}
\label{sec:pi-}
Pi ($\pi$), the ratio of a circle's circumference to its diameter,
can be computed by adding the following terms:
$$ \pi = 4 \sum^{\infty}_{k=0} {(-1)^k \over 2k+1} =
{4 \over 1} - {4 \over 3} + {4 \over 5} - {4 \over 7}
+ {4 \over 9} - {4 \over 11} + \cdots $$
Create a program that asks the user for a number $n$ and then
calculates the partial addition of $n$ terms of this infinite sum.
How many terms do you need to get
the first three digits right (3.14)? How many for the first 10 digits
(3.14159265358\ldots)?
\end{document}