Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 8, 2016 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/127e84bace7cbcd6dedfc006188ca2f7 to your computer and use it in GitHub Desktop.
Save jianminchen/127e84bace7cbcd6dedfc006188ca2f7 to your computer and use it in GitHub Desktop.
HackerRank NCR codesprint - code is no good in structure, repetition code. Counting mixes with string construction. Counting code duplicates 4 time.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpiralMessage
{
class Program
{
/*
* https://www.hackerrank.com/contests/ncr-codesprint/challenges/spiral-message
* 11:42am start to read problem statement
* Write a code to work on the sample test case first, we do not need to store
* result of string, just keep counting.
* 3 5
* a##ar
* a#aa#
* xxwsr
*
* 12:21pm
* 4.49/ 20
* test cases: 0 - 13
* pass: 0, 11, 12, 13
*/
static void Main(string[] args)
{
//process();
// testing the code
testing();
}
/*
* test case:
* 3 5
a##ar
a#aa#
xxwsr
*/
private static void testing()
{
IList<string> input = new List<string>();
input.Add("a##ar");
input.Add("a#aa#");
input.Add("xxwsr");
Console.WriteLine(calculate(input));
}
private static void process()
{
int[] arr = ToInt(Console.ReadLine().Split(' '));
int rows = arr[0], cols = arr[1];
IList<string> input = new List<string>();
for (int i = 0; i < rows; i++)
{
input.Add(Console.ReadLine().Trim());
}
Console.WriteLine(calculate(input));
}
/*
* start: 11:53am
* exit: 12:15
* static analysis the code
*/
private static int calculate(IList<string> data)
{
int rows = data.Count;
int cols = data[0].Length;
int row = 0;
int col = 0;
int count = 0;
StringBuilder sb = new StringBuilder();
bool previous = false; // alpahbetic a-z
int startX = row, endX = rows - 1 - row;
int startY = col, endY = cols - 1 - col;
//while (row < rows && col < cols)
while(startX <= endX && startY <= endY)
{
// go over 4 direction
// to right, downward, to left, to up
// 1. to right
for (int j = startY; j <= endY; j++)
{
bool current = isAlphabetic(data[row][j]);
sb.Append(data[row][j]);
if (current && !previous)
{
count++;
}
previous = current;
}
// 2. downward
for (int i = startX + 1; i <= endX; i++)
{
bool current = isAlphabetic(data[i][endY]);
sb.Append(data[i][endY]);
if (current && !previous)
{
count++;
}
previous = current;
}
// 3. to left
for (int j = endY - 1; j >= startY; j--)
{
bool current = isAlphabetic(data[endX][j]);
sb.Append(data[endX][j]);
if (current && !previous)
{
count++;
}
previous = current;
}
// 4. to upward
for (int i = endX - 1; i > startX; i--)
{
bool current = isAlphabetic(data[i][startY]);
sb.Append(data[i][startY]);
if (current && !previous)
{
count++;
}
previous = current;
}
row++;
col++;
startX = row;
endX = rows - 1 - row;
startY = col;
endY = cols - 1 - col;
}
string res = sb.ToString();
return count;
}
private static bool isAlphabetic(char c)
{
if ((c - 'a') >= 0 && ('z' - c) >= 0)
return true;
else
return false;
}
private static int[] ToInt(string[] arr)
{
int len = arr.Length;
int[] res = new int[len];
for (int i = 0; i < len; i++)
{
res[i] = Convert.ToInt32(arr[i]);
}
return res;
}
}
}
@jianminchen
Copy link
Author

Submission #2 issues:

  1. code is no good in structure, repetition code.
  2. Counting mixes with string construction. Counting code duplicates 4 time. line 96 - 100
  3. base case is not handled properly, one row, one column; one row will be counted twice.
  4. string split using # is avoided, just use count directly.

@jianminchen
Copy link
Author

Score is 0.82 (full score is 20), pass 2 cases of 14 test cases.

@jianminchen
Copy link
Author

row, col two variable can be avoided, using startX, startY, endX, endY to calculate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment