Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 14, 2017 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/c8e08e1c7cd9aa54c74c42eb5be63f42 to your computer and use it in GitHub Desktop.
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
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