Created
November 14, 2017 22:22
-
-
Save jianminchen/c8e08e1c7cd9aa54c74c42eb5be63f42 to your computer and use it in GitHub Desktop.
Order string - study code, most simple code I have seen so far. Numeric sort using conversion: (BigInteger.Parse(item) << 32) + index
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; | |
using System.Numerics; | |
/// <summary> | |
/// code study: | |
/// https://www.hackerrank.com/rest/contests/morgan-stanley-codeathon-2017/challenges/shell-sort-command/hackers/outvoider/download_solution | |
/// | |
/// code review: | |
/// The tip to do numerica comparison: | |
/// BigInteger.Parse(item) << 32) + index | |
/// Left shift 32 bits and then increment the value of index variable. | |
/// This code is most simple code I have seen so far using C# programming language | |
/// </summary> | |
class Solution | |
{ | |
static void Main(String[] args) | |
{ | |
new OrderStrings().Solve(); | |
} | |
} | |
class OrderStrings | |
{ | |
public void Solve() | |
{ | |
var number = int.Parse(Console.ReadLine()); | |
var stringsToOrder = new string[number]; | |
for (var i = 0; i < number; ++i) | |
{ | |
stringsToOrder[i] = Console.ReadLine(); | |
} | |
var commands = Console.ReadLine().Split(' '); | |
var column = int.Parse(commands[0]) - 1; | |
var key = stringsToOrder.Select(v => v.Split(' ')[column]); | |
if (commands[2] == "numeric") | |
{ | |
var keyArray = key.Select((item, index) => (BigInteger.Parse(item) << 32) + index).ToArray(); | |
Array.Sort(keyArray, stringsToOrder); // Array.Sort two arrays, sort the first array with big integer | |
} | |
else | |
{ | |
var keyArray = key.ToArray(); | |
Array.Sort(keyArray, stringsToOrder); | |
} | |
if (commands[1] == "true") | |
{ | |
Array.Reverse(stringsToOrder); | |
} | |
foreach (var v in stringsToOrder) | |
{ | |
Console.WriteLine(v); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment