Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hackerrank NCR codesprint - Spiral message - code review on March 20, 2018
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpiralMessage
{
class Program
{
/// <summary>
/// https://www.hackerrank.com/contests/ncr-codesprint/challenges/spiral-message
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
//RunSampleTestCase();
SpiralMessage();
}
private static void RunTestcases()
{
TestOneChar();
TestOneRow();
TestingOneColumn();
RunSampleTestCase();
}
private static void RunSampleTestCase()
{
var input = new List<string>();
input.Add("a##ar");
input.Add("a#aa#");
input.Add("xxwsr");
string spiralMessage = SpiralMessageFromLowerLeftClockWise(input);
Debug.Assert(spiralMessage.CompareTo("xaa##ar#rswx#aa") == 0);
var result = spiralMessage.Split('#').ToList();
result.RemoveAll(str => string.IsNullOrEmpty(str));
Debug.Assert(result.Count == 4);
}
/// <summary>
/// one column test
/// </summary>
private static void TestingOneColumn()
{
var input = new List<string>();
input.Add("a");
input.Add("#");
input.Add("#");
input.Add("a");
input.Add("#");
Debug.Assert(SpiralMessageFromLowerLeftClockWise(input).CompareTo("#a##a") == 0);
}
private static void TestOneRow()
{
var input = new List<string>();
input.Add("a##a#");
Debug.Assert(SpiralMessageFromLowerLeftClockWise(input).CompareTo("a##a#") == 0);
}
private static void TestOneChar()
{
var input = new List<string>();
input.Add("a");
Debug.Assert(SpiralMessageFromLowerLeftClockWise(input).CompareTo("a") == 0);
}
private static void SpiralMessage()
{
var split = Console.ReadLine().Split(' ');
var arr = Array.ConvertAll(split, int.Parse);
var rows = arr[0];
var cols = arr[1];
var input = new List<string>();
for (int i = 0; i < rows; i++)
{
input.Add(Console.ReadLine().Trim());
}
var result = SpiralMessageFromLowerLeftClockWise(input).Split('#').ToList();
result.RemoveAll(str => string.IsNullOrEmpty(str));
Console.WriteLine(result.Count());
}
/// <summary>
/// March 20, 2018
/// I clean up code and make it more readable.
///
/// Dec. 8, 2016
/// Function spec:
/// 1. Clockwise direction
/// 2. Start from lower left corner
/// 3. String can be counted using the hash mark (#)
/// Return:
/// Spiral message instead of count of words
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
private static string SpiralMessageFromLowerLeftClockWise(IList<string> data)
{
var rows = data.Count;
var cols = data[0].Length;
int rowStart = 0;
int rowEnd = rows - 1;
int colStart = 0;
int colEnd = cols - 1;
var spiral = new StringBuilder();
while (rowStart <= rowEnd &&
colStart <= colEnd
)
{
int rowsLeft = rowEnd - rowStart + 1;
int colsLeft = colEnd - colStart + 1;
bool isOneNode = rowsLeft == 1 && colsLeft == 1;
bool isOneRow = rowsLeft == 1;
bool isOneCol = colsLeft == 1;
// starting from left-bottom corner, go upward first
for (int row = rowEnd; row >= rowStart; row--)
{
spiral.Append(data[row][colStart]);
}
// base case 1:
if (isOneNode || isOneCol)
{
break;
}
// go from left to right on top row
for (int col = colStart + 1; col <= colEnd; col++)
{
spiral.Append(data[rowStart][col]);
}
// base case 2:
if (isOneRow)
{
break;
}
// go from top to down on last column
for (int row = rowStart + 1; row <= rowEnd; row++)
{
spiral.Append(data[row][colEnd]);
}
// go from right to left on last row
for (int col = colEnd - 1; col > colStart; col--)
{
spiral.Append(data[rowEnd][col]);
}
rowStart++;
rowEnd--;
colStart++;
colEnd--;
}
return spiral.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.