Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 9, 2016 19:59
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/d92eb90a6fd4c9a678f0462e354fbb4d to your computer and use it in GitHub Desktop.
Save jianminchen/d92eb90a6fd4c9a678f0462e354fbb4d to your computer and use it in GitHub Desktop.
HackerRAnk NCR codesprint - spiral message - submission #3 - score 6.94
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
*
* 1:07pm
* 6.12/ 20
* test cases: 0 - 13
* pass:
*/
static void Main(string[] args)
{
process();
// testing the code
//testing();
//testing2();
}
/*
* 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));
}
/*
* Fail the test
*/
private static void testing2()
{
IList<string> input = new List<string>();
input.Add("a");
input.Add("#");
input.Add("#");
input.Add("a");
input.Add("#");
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;
Dictionary<string, int> table = new Dictionary<string, int> ();
//while (row < rows && col < cols)
while (startX <= endX &&
startY <= endY &&
startX <= (rows - 1) &&
startY <= (cols - 1)
)
{
// go over 4 direction
// to right, downward, to left, to up
// 1. to right
for (int j = startY; j <= endY; j++)
{
string key = row + "+" + j;
bool isIn = table.ContainsKey(key);
bool current = isAlphabetic(data[row][j]);
if(!isIn)
sb.Append(data[row][j]);
if (current && !previous && !isIn)
{
count++;
table.Add(key, 1);
}
previous = current;
}
// 2. downward
for (int i = startX + 1; i <= endX; i++)
{
string key = i + "+" + endY;
bool isIn = table.ContainsKey(key);
bool current = isAlphabetic(data[i][endY]);
if (!isIn)
sb.Append(data[i][endY]);
if (current && !previous && !isIn)
{
count++;
table.Add(key, 1);
}
previous = current;
}
// 3. to left
for (int j = endY - 1; j >= startY; j--)
{
string key = endX + "+" + j;
bool isIn = table.ContainsKey(key);
bool current = isAlphabetic(data[endX][j]);
if (!isIn)
sb.Append(data[endX][j]);
if (current && !previous && !isIn)
{
count++;
table.Add(key, 1);
}
previous = current;
}
// 4. to upward
for (int i = endX - 1; i > startX; i--)
{
string key = i + "+" + startY;
bool isIn = table.ContainsKey(key);
bool current = isAlphabetic(data[i][startY]);
if (!isIn)
sb.Append(data[i][startY]);
if (current && !previous && isIn)
{
count++;
table.Add(key, 1);
}
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

code review after the contest:

  1. The while loop - 115 - 119, but line 118, 119 redundant, part of first two lines.
  2. Base case - one row, one column do not work - count twice
  3. Add debug code - stringBuilder to tracker the string output - it is helpful, but unfortunately in the contest, Julia did not catch the error - starting point.
  4. Missing start time/ end time for submission above function comment, need to track time spent.

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