Ugrás a fő tartalomra

CS50 - week 1 - Problem Set 2/4

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:
  1. Get an integer from the user
  2. Validate if the integer is bigger than zero.
  3. If not, then terminate.
  4. 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
 To elaborate the fourth point:
  1. Iterate with variable I from 1 until it is still less than or equal with N (we are now iterating in the rows)
  2.  Iterate with variable J from 1 until it is less than or equal with 2*N+1
  3. 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.
    1. 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
    2. 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 #
    3. Else, if J equals to N + 1, then we need to print a separator space
    4. 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
    5. Else if J is greater than N+1+I we draw spaces
  4. Every time we increment I we need to break the line, so print out a break line sign 
Optimization:
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

Népszerű bejegyzések ezen a blogon

Introduction

So , finally I thought, I would need to develop some kind of mechanism to force myself to achieve more. I think, this blog will do it, as it will obligate me to log my progress towards a more successful me. How rude I am... let me introduce myself! I am a software developer, who works in the capital of Hungary, in Budapest. I am currently working for a huge international outsourcing software development company, as a senior .Net based web engineer. My main focus of work as of now is back-end services, from the dark ages (WCF) to newer beauties (.NET core self hosted stuff), and of course everything between them.  From a personal point of view, I am 27 years old,  married and a proud father of a beautiful and insanely clever one-and-a-half year old little girl. I'm in the software development industry since the September of 2012, and from that point I am in the .NET stack. As I wrote in the first paragraph, mainly the purpose of creating this blog is to enhance myself, and t

CS50 - week 1 - Problem Set 1/4

Harvard runs an introductory computer science course, called CS50. The purpose of the said course is to give a taste of programming to the students. During the 12 lessons there are multiple programming languages one get exposed to upon completing the series, so far I've got introduced to Scratch and the very basics of C. Last year I already tried to get into the course, but as usual I only made the first lesson. Now I'm jumping straight into the C parts to avoid getting bored with the repeated lesson. In the CS50 series each lesson has problem sets, homework assignments which are testing the level of understanding of the given subject. In Lesson 1 there are four tasks to complete, and during this week I will complete them and write here how I've tackled them, primary because I think these tasks could be great candidates for software engineer coding interviews.  Task 1: Mario level 1 (PSET 1 - 1/4) The task is to create a software, which fulfills