Created
November 9, 2016 19:59
-
-
Save jianminchen/d92eb90a6fd4c9a678f0462e354fbb4d to your computer and use it in GitHub Desktop.
HackerRAnk NCR codesprint - spiral message - submission #3 - score 6.94
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
code review after the contest: