I was asked to write code for the below structure:
1 3 2 4 5 6 10 9 8 7 11 12 13 14 15
And I tried the below code, which works, yet I'm wondering if there is any better approach than this:
public static void main(String[] args) {
int n=5; // no of rows
int c=0;
for(int i=1;i<=n;i++){
for(int k=i;k<n;k++){
System.out.print(" ");
}
if(i%2!=0){
for(int j=0;j<i;j++){
c++;
System.out.print(c +" ");
}
}
else{
int a[] = new int [i+1];
for(int j=0;j<i;j++){
c++;
a[j]=c;
}
for(int j=i-1;j>=0;j--){
System.out.print(a[j]+" ");
}
}
System.out.println("");
}
}
Is there any better implementation or am I doing it right?
-
3\$\begingroup\$ oh hell please, first and foremost: properly indent your code. especially for interviews... \$\endgroup\$Joshua– Joshua2014年10月13日 12:49:44 +00:00Commented Oct 13, 2014 at 12:49
-
\$\begingroup\$ i don't worry about indenting in here, but the implementation matters. Indenting is a matter of ctrl + keys.. \$\endgroup\$Vignesh Paramasivam– Vignesh Paramasivam2014年10月13日 14:32:24 +00:00Commented Oct 13, 2014 at 14:32
-
2\$\begingroup\$ "i don't worry about indenting in here", but you should as we, who review your code, should be able to do this as best as possible. \$\endgroup\$Heslacher– Heslacher2014年10月14日 07:42:46 +00:00Commented Oct 14, 2014 at 7:42
-
\$\begingroup\$ @Heslacher agreed \$\endgroup\$Vignesh Paramasivam– Vignesh Paramasivam2014年10月14日 08:35:54 +00:00Commented Oct 14, 2014 at 8:35
3 Answers 3
you have done it like this
else{
int a[] = new int [i+1];
for(int j=0;j<i;j++){
c++;
a[j]=c;
}
for(int j=i-1;j>=0;j--){
System.out.print(a[j]+" ");
}
}
analyzing your code:
int a[] = new int [i+1];
for(int j=0;j<i;j++){
c++;
a[j]=c;
}
in the above statements you are using an array to store the values to be displayed and then iterating over the array below
for(int j=i-1;j>=0;j--){
System.out.print(a[j]+" ");
}
so in all looping over twice for a single line first generating the values and then printing the values, and using extra memory for storing the values.
instead you can implement it like this
else
{
c+=i; // find the number to start from
for (int j = 0; j < i; j++){
System.out.print(c+" "); // print the number
c--; // decrement the number
}
c+=i; // reset the number to maintain count.
}
Explanation
c+=i; // find the number to start from
just calculate the number from where to print then use a single loop
for (int j = 0; j < i; j++){
System.out.print(c+" "); // print the number
c--; // decrement the number
}
to print the number by just decrementing the counter, and the last step would be to just restore the counter to proceed with normal execution.
c+=i; // reset the number to maintain count.
This will have following advantages
- No use of extra memory.
- No use of extra iteration.
-
\$\begingroup\$ Welcome to Code Review! Although this answer provides an alternative way of implementing what the OP wants, commenting on what things you do differently, why you are doing it that way and pointing out the flaws in the OP's original code would make this a better answer. Copy-pasting code does not always provide a good learning experience. \$\endgroup\$Simon Forsberg– Simon Forsberg2014年10月13日 10:50:58 +00:00Commented Oct 13, 2014 at 10:50
Naming
int n=5; // no of rows
If you need a comment to explain the intent of an variable, then something went wrong. You should just rename the variable to numberOfRows
.
Also variable names which aren't loop iteration variables shouldn't have single letter names.
E.g c
should better be currentNumber
.
Space
for(int i=1;i<=n;i++){
Help you code to breath and add some space
for(int i = 1; i <= n; i++){
is easier to read.
Indention
if(i%2!=0){ for(int j=0;j<i;j++){ c++; System.out.print(c +" "); } }
wouldn't the above be easier to read if coded like
if(i%2 != 0){
for(int j = 0; j < i; j++){
c++;
System.out.print(c +" ");
}
}
Any better implementation or am i doing it right?
As soon as you print double digits, your pyramid doesn't look quite right anymore. This will only get worse with three digits, etc. But if this wasn't a concern in the question, then your implementation looks fine.
Naming
@Heslacher already mentioned n
, but all your other variable names are also not expressive: c
, i
, k
, j
, a
. You should avoid using one-letter variables except in a very limited number of situations (i
and j
for loops in case there is no better name, x
and y
for coordinates).
Better names would be:
i
:row
k
: I don't know. But I would just move this code to its own function (getSpaces(int amountSpaces)
) anyways.j
:column
c
:value
ornumber
oroutput
a
:rowContent
These are just suggestions, you can improve on them.
Comments and extracting Functions
If you look at this code in a month, will you still understand it directly, without thinking about it? I don't think so. But there are ways to improve on this (in addition to better naming):
I would either extract some code to its own functions (eg getSpaces(int)
, getRow(int)
, getReversedRow(int)
), or at least add some comments to the loops to make your code a lot more readable.