I will continue in this article solving the first set of C tasks from CS50, while listening to Savant's new album, Jester, which is by the way freaking awesome!
So the task is to accept an integer and than draw two half-pyramids (rectangles if you wish so).
Example:
Input: 3
Output:
# #
## ##
### ###
We could ask all the same questions, as we done it in the previous task, so I will not bore you with that. I will jump into thinking about the solution!
So, if we call the input as N, we can observe, that the output will be a matrix, and the height of the matrix now is N, and the width of the matrix this time is 2N+1, meaning, that in the last row we will draw twice the times the hashtags as the input number plus one tiny separator white space.
Plan to solve the problem:
As we are again going through a matrix (or table if it fits your mind better), we need to touch every single cell, so after all we will touch N*(2N+1) cells, and after we drop the constants, we can see, that the big O of the algorithm is N2. One optimization could be, that after we wrote down the second set of the hashtags for the second rectangle, we skip to the next line instead of writing down white spaces, but if we need to replace the spaces with a different character, then we will need to modify our program.
The code itself:
I need to confess: I has to sit down twice to solve this problem without any outside help. For me it was quite mind blending to figure out in which cases do I need to put down a # or a space. I had the initial idea, but I needed to formalize it on paper for about half an hour, until I was ready to code it down. I've coded it, but still there was a minor error (forgot to increment correctly), so I needed to debug my program to make it perfect. Here you can find how to debug C programs with gcc and gdb.
After coding it, now it feels more or less trivial, but if it was me I would not give this to anyone at a coding interview. Seemingly this task tests the algorithmic capabilities to a great extreme, and to understand based on half an hour board coding whether the candidate is capable or not would be either a success or fail experience. So if the candidate does not figures out quite soon that she needs to draw a sign based on the states of three values, and starts to formalize the rules, it is definitely a fail, because she would waste a lot of time from the half-an-hour long coding session to just came up with the basic idea, and would not let time to code it down.
I honestly do think that I would have easily fail at a solving this task in half an hour if I would see it for the first time. What do you guys think? Is that fair to give a task like this to a candidate at a coding interview?
So the task is to accept an integer and than draw two half-pyramids (rectangles if you wish so).
Example:
Input: 3
Output:
# #
## ##
### ###
We could ask all the same questions, as we done it in the previous task, so I will not bore you with that. I will jump into thinking about the solution!
So, if we call the input as N, we can observe, that the output will be a matrix, and the height of the matrix now is N, and the width of the matrix this time is 2N+1, meaning, that in the last row we will draw twice the times the hashtags as the input number plus one tiny separator white space.
Plan to solve the problem:
- Get an integer from the user
- Validate if the integer is bigger than zero.
- If not, then terminate.
- Else draw N-(current row number) spaces, N times the hashtags, a space, N times the hashtags and N-(current row number) the spaces and then a break line character
- Iterate with variable I from 1 until it is still less than or equal with N (we are now iterating in the rows)
- Iterate with variable J from 1 until it is less than or equal with 2*N+1
- Inside the second iteration we need to have a decision tree based on the values of I, J and N to decide what character to draw into each separate spaces.
- First we need to draw N - I spaces (if N is 3, than we need to draw 2 spaces in the first row, where I is 1). So if J is less or equal with N - I , than print a space
- Else, then we need to draw I times the hashtags, so if J i greater than N-I, and J is less than or equals to N, then we need to draw a #
- Else, if J equals to N + 1, then we need to print a separator space
- Else if J is greater than N+1 and less than or equals to N+1+I (remember, we need to draw I times the hashtags), we draw a hashtag
- Else if J is greater than N+1+I we draw spaces
- Every time we increment I we need to break the line, so print out a break line sign
As we are again going through a matrix (or table if it fits your mind better), we need to touch every single cell, so after all we will touch N*(2N+1) cells, and after we drop the constants, we can see, that the big O of the algorithm is N2. One optimization could be, that after we wrote down the second set of the hashtags for the second rectangle, we skip to the next line instead of writing down white spaces, but if we need to replace the spaces with a different character, then we will need to modify our program.
The code itself:
#include <stdio.h> int main(void) { printf("Please provide a valid positive integer!\n"); int n; scanf("%d", &n); if (!(n > 0)) { printf("Please provide a number, which is bigger, than zero! Exiting program..\n"); return -1; } else { int i; i = 1; int j; while (i <= n) { j = 1; while (j <= 2 * n + 1) { if ((j <= n-i) || (j == n+1) || (n+1+i < j)) { printf(" "); } else if ((n-i < j && j <= n) || (n+1 < j && j <= n+1+i)) { printf("#"); } j++; } printf("\n"); i++; } } }
I need to confess: I has to sit down twice to solve this problem without any outside help. For me it was quite mind blending to figure out in which cases do I need to put down a # or a space. I had the initial idea, but I needed to formalize it on paper for about half an hour, until I was ready to code it down. I've coded it, but still there was a minor error (forgot to increment correctly), so I needed to debug my program to make it perfect. Here you can find how to debug C programs with gcc and gdb.
After coding it, now it feels more or less trivial, but if it was me I would not give this to anyone at a coding interview. Seemingly this task tests the algorithmic capabilities to a great extreme, and to understand based on half an hour board coding whether the candidate is capable or not would be either a success or fail experience. So if the candidate does not figures out quite soon that she needs to draw a sign based on the states of three values, and starts to formalize the rules, it is definitely a fail, because she would waste a lot of time from the half-an-hour long coding session to just came up with the basic idea, and would not let time to code it down.
I honestly do think that I would have easily fail at a solving this task in half an hour if I would see it for the first time. What do you guys think? Is that fair to give a task like this to a candidate at a coding interview?
Megjegyzések
Megjegyzés küldése