Created
November 14, 2017 20:11
-
-
Save jianminchen/fc63769b2e5f6bc88d783bbe8bb5cdc6 to your computer and use it in GitHub Desktop.
Order strings - study code, and code review. A few things to learn from, the design of structure, and Sort function.
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; | |
using System.Linq; | |
class Solution | |
{ | |
/// <summary> | |
/// study code: order string | |
/// https://www.hackerrank.com/rest/contests/morgan-stanley-codeathon-2017/challenges/shell-sort-command/hackers/kilicars/download_solution | |
/// Look into keyword: delegate | |
/// String.TrimStart | |
/// | |
/// </summary> | |
struct OrderString | |
{ | |
public int Index; | |
public string Lexicographic; | |
} | |
static void Main(String[] args) | |
{ | |
int n = Convert.ToInt32(Console.ReadLine()); | |
var table = new string[n][]; | |
for (int t = 0; t < n; t++) | |
{ | |
table[t] = Console.ReadLine().Split(' ').ToArray(); | |
} | |
var orderInstructions = Console.ReadLine().Split(' ').ToArray(); | |
int key = Convert.ToInt32(orderInstructions[0]) - 1; | |
var reverse = orderInstructions[1]; | |
var type = orderInstructions[2]; | |
var isNumeric = type == "numeric"; | |
if (isNumeric) | |
{ | |
var orderLevel = SortByNumeric(n, table, key); | |
if (reverse == "true") | |
{ | |
for (int i = n - 1; i >= 0; i--) | |
{ | |
Console.WriteLine(String.Join(" ", table[orderLevel[i].Index])); | |
} | |
} | |
else | |
{ | |
for (int i = 0; i < n; i++) | |
{ | |
Console.WriteLine(String.Join(" ", table[orderLevel[i].Index])); | |
} | |
} | |
} | |
else | |
{ | |
var isReverse = reverse == "true"; | |
var orderLevel = SortByLexicographic(n, table, key, isReverse); | |
for (int i = 0; i < n; i++) | |
{ | |
Console.WriteLine(String.Join(" ", table[orderLevel[i].Index])); | |
} | |
} | |
} | |
/// <summary> | |
/// code review on Nov. 14, 2017 | |
/// study delegate keyword in C# first time | |
/// TrimStart('0') - using string API TrimStart | |
/// Study struct OrderString design, two things: | |
/// save the order of string, and content of the string. | |
/// | |
/// Pitfall of my code in the contest: | |
/// I did not save the index of string in contest | |
/// code of my contest is here: | |
/// https://gist.github.com/jianminchen/a2cbe655fd9010ec33d1517fc4efa893 | |
/// </summary> | |
/// <param name="n"></param> | |
/// <param name="table"></param> | |
/// <param name="key"></param> | |
/// <returns></returns> | |
static OrderString[] SortByNumeric(int n, string[][] table, int key) | |
{ | |
var orderLevel = new OrderString[n]; | |
for (int i = 0; i < n; i++) | |
{ | |
orderLevel[i].Index = i; // save the index of string for comparison | |
orderLevel[i].Lexicographic = table[i][key].TrimStart('0'); ; | |
} | |
Array.Sort(orderLevel, (delegate(OrderString e1, OrderString e2) | |
{ | |
var lexicographic1 = e1.Lexicographic; | |
var lexicographic2 = e2.Lexicographic; | |
var index1 = e1.Index; | |
var index2 = e2.Index; | |
if (lexicographic1 == lexicographic2) // ? | |
{ | |
return e1.Index.CompareTo(e2.Index); // compare the order in the strings | |
} | |
else if (lexicographic1 == null) | |
{ | |
return -1; //empty string before non-empty string | |
} | |
else if (lexicographic2 == null) | |
{ | |
return 1; //non-empty string after empty string | |
} | |
else | |
{ | |
var length1 = lexicographic1.Length; | |
var length2 = lexicographic2.Length; | |
if (length1 < length2) | |
{ | |
return -1; //shorter string before longer string | |
} | |
else if (length1 > length2) | |
{ | |
return 1; //longer string after shorter string | |
} | |
else | |
{ | |
return lexicographic1.CompareTo(lexicographic2); //alphabetical order | |
} | |
} | |
})); | |
return orderLevel; | |
} | |
static OrderString[] SortByLexicographic(int n, string[][] table, int key, bool reverse) | |
{ | |
var orderLevel = new OrderString[n]; | |
for (int i = 0; i < n; i++) | |
{ | |
orderLevel[i].Index = i; | |
orderLevel[i].Lexicographic = table[i][key]; | |
} | |
if (reverse) | |
{ | |
Array.Sort(orderLevel, delegate(OrderString e1, OrderString e2) | |
{ | |
return (e2.Lexicographic.CompareTo(e1.Lexicographic)); | |
}); | |
} | |
else | |
{ | |
Array.Sort(orderLevel, delegate(OrderString e1, OrderString e2) | |
{ | |
return (e1.Lexicographic.CompareTo(e2.Lexicographic)); | |
}); | |
} | |
return orderLevel; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment