Created
November 14, 2017 23:02
-
-
Save jianminchen/1310f42d3dae9cb50258aabb48c74768 to your computer and use it in GitHub Desktop.
Order string - review code using IComparable, String.TrimStart, the code is reviewed and line 72 to line 74 are added.
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.IO; | |
/// <summary> | |
/// code study: | |
/// https://www.hackerrank.com/rest/contests/morgan-stanley-codeathon-2017/challenges/shell-sort-command/hackers/ggarvin0/download_solution | |
/// | |
/// code review: | |
/// Nov. 14, 2017 | |
/// Use IComparable class | |
/// | |
/// </summary> | |
class Solution | |
{ | |
public class Node : IComparable | |
{ | |
public string[] OrderStrings; | |
public string Lexicographic; | |
public bool Numerical; | |
public int Index; | |
public Node(string line, int index) | |
{ | |
OrderStrings = line.Split(new char[0]); | |
Index = index; | |
} | |
public void Set(int index, bool number) | |
{ | |
Numerical = number; | |
Lexicographic = OrderStrings[index]; | |
} | |
public int CompareTo(Object item) | |
{ | |
var node = (Node)item; | |
if (!Numerical) | |
{ | |
return Lexicographic.CompareTo(node.Lexicographic); | |
} | |
return NumericComparer(node); | |
} | |
/// <summary> | |
/// code review on Nov. 14, 2017 | |
/// Make the code short and easy to read using String.TrimStart API | |
/// </summary> | |
/// <param name="node"></param> | |
/// <returns></returns> | |
public int NumericComparer(Node node) | |
{ | |
var orderString1 = Lexicographic.TrimStart('0'); | |
var orderString2 = node.Lexicographic.TrimStart('0'); | |
int length1 = orderString1.Length; | |
int length2 = orderString2.Length; | |
if (length1 < length2) | |
{ | |
return -1; | |
} | |
if (length2 < length1) | |
{ | |
return 1; | |
} | |
if (orderString1 == orderString2) // add one more case | |
{ | |
return Index.CompareTo(node.Index); | |
} | |
return orderString1.CompareTo(orderString2); | |
} | |
public override string ToString() | |
{ | |
return string.Join(" ", OrderStrings); | |
} | |
} | |
static void Main(String[] args) | |
{ | |
int N = Int32.Parse(Console.ReadLine()); | |
var nodes = new Node[N]; | |
for (int i = 0; i < N; i++) | |
{ | |
nodes[i] = new Node(Console.ReadLine(), i); | |
} | |
var commands = Console.ReadLine().Split(new char[0]); | |
int column = Int32.Parse(commands[0]) - 1; | |
bool reverse = Boolean.Parse(commands[1]); | |
bool numerical = commands[2].Equals("numeric"); | |
for (int i = 0; i < N; i++) | |
{ | |
nodes[i].Set(column, numerical); | |
} | |
Array.Sort(nodes); | |
if (reverse) | |
{ | |
for (int n = N - 1; n >= 0; n--) | |
{ | |
Console.WriteLine(nodes[n]); | |
} | |
} | |
else | |
{ | |
for (int n = 0; n < N; n++) | |
{ | |
Console.WriteLine(nodes[n]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment