With a challenging problem, what is often hardest is
getting started. Where to begin?
Let's start by examining the output more closely:
Here's what we notice:
At this point, it's important NOT to think about coding details.
This is a critically useful strategy that deserves some explanation:
So now, let's just think high level but a little code-like.
We call this algorithmic thinking.
Since there are 10 lines, we could say "we're doing something 10
times" (each time slightly differently).
In code-like structure, called pseudcode:
It's worth staring at the above for a few minutes. We'll point out
that:
Next, if we could somehow figure out the last letter in each
line, we'd be in good shape:
We need to figure out the last letter in the i-th line:
At this point, let's step back and reflect on problem-solving:
zyxwvutsrq
zyxwvutsr
zyxwvuts
zyxwvut
zyxwvu
zyxwv
zyxw
zyx
zy
z
for i=1 to 10 {
figure out and print the i-th line
}
for i=1 to 10 {
figure out lastLetter in line i
// Print the line
for c='z' to lastletter {
print c
}
}
char ch = 'z';
int k = (char) ch;
int k = 100;
char ch = (char) k;
char ch = 'z';
int k = (char) ch;
then consider:
char ch = 'z';
int k = (char) ch;
int j = k - (10 - i);
char ch = 'z';
int k = (char) ch;
int j = k - (10 - i);
char lastLetter = (char) j;
q r s t u v w x y z
113 114 115 116 117 118 119 120 121 122
public class TestCharIdea {
public static void main (String[] argv)
{
int i = 1;
int k = (int) 'z';
int j = k - (10 - i);
char lastLetter = (char) j;
System.out.println (lastLetter);
}
}
One can then try different values of i to see if this works.
for (char c='z'; c>=lastLetter; c--) {
System.out.print (c);
}
for i=1 to 10 {
figure out lastLetter
// Print the line
for c='z' to lastletter {
print c
}
}
for i=1 to 10 {
// Figure out lastLetter in line i
char ch = 'z';
int k = (char) ch;
int j = k - (10 - i);
char lastLetter = (char) j;
// Print the line
for c='z' to lastletter {
print c
}
}
for i=1 to 10 {
// Figure out lastLetter in line i
char ch = 'z';
int k = (char) ch;
int j = k - (10 - i);
char lastLetter = (char) j;
// Print the line
for (char c='z'; c>=lastLetter; c--) {
System.out.print (c);
}
}
for (int i=1; i<=10; i++) {
// Figure out lastLetter in line i
char ch = 'z';
int k = (char) ch;
int j = k - (10 - i);
char lastLetter = (char) j;
// Print the line
for (char c='z'; c>=lastLetter; c--) {
System.out.print (c);
}
}
public class CharProblem {
public static void main (String[] argv)
{
for (int i=1; i<=10; i++) {
// Figure out lastLetter in line i
char ch = 'z';
int k = (char) ch;
int j = k - (10 - i);
char lastLetter = (char) j;
// Print the line
for (char c='z'; c>=lastLetter; c--) {
System.out.print (c);
}
}
}
}
Back to the assignment