Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 14, 2017 20:51
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/40aa86b94d7b2c2b8989062517e2fe67 to your computer and use it in GitHub Desktop.
Save jianminchen/40aa86b94d7b2c2b8989062517e2fe67 to your computer and use it in GitHub Desktop.
Order string - study code - using SortedDictionary
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
/// <summary>
/// study code:
/// https://www.hackerrank.com/rest/contests/morgan-stanley-codeathon-2017/challenges/shell-sort-command/hackers/jusizela/download_solution
///
/// code review:
/// Spent 10 - 20 minutes to read the code and then figure out how the thought process is.
/// It is time to review SortedDictionary and how to define one quickly.
/// Change coding style, and then review the code.
/// </summary>
class Solution
{
/// <summary>
/// handle reverse order
/// </summary>
public class DescendingComparer : IComparer<String>
{
public int Compare(string x, string y)
{
return string.Compare(x, y) * -1;
}
}
public class DescendingComparerI : IComparer<BigInteger>
{
public int Compare(BigInteger x, BigInteger y)
{
return BigInteger.Compare(x, y) * -1;
}
}
static void Main(String[] args)
{
int n;
n = int.Parse(Console.ReadLine());
var orderStrings = new string[n];
for (int i = 0; i < n; ++i)
{
orderStrings[i] = Console.ReadLine();
}
var tmp = Console.ReadLine().Split(' ');
int column = int.Parse(tmp[0]) - 1;
bool reverseOrder = tmp[1] == "true";
bool isNumeric = tmp[2] == "numeric";
if (isNumeric)
{
var dictionary = SortByNumeric(reverseOrder, orderStrings, column);
foreach (var v in dictionary)
{
foreach (var v1 in v.Value)
{
Console.WriteLine(v1);
}
}
}
else
{
SortedDictionary<string, string> dictionary = SortLexicographic(reverseOrder, orderStrings, column);
foreach (var v in dictionary)
{
Console.WriteLine(v.Value);
}
}
}
/// <summary>
/// code review on Nov. 14, 2017
///
/// </summary>
/// <param name="reverseOrder"></param>
/// <param name="orderStrings"></param>
/// <param name="column"></param>
/// <returns></returns>
public static IDictionary<BigInteger, LinkedList<string>> SortByNumeric(bool reverseOrder, string[] orderStrings, int column)
{
SortedDictionary<BigInteger, LinkedList<string>> dictionary;
var comparer = new DescendingComparerI();
if (reverseOrder)
{
dictionary = new SortedDictionary<BigInteger, LinkedList<string>>(comparer);
}
else
{
dictionary = new SortedDictionary<BigInteger, LinkedList<string>>();
}
foreach (var c in orderStrings)
{
var k = BigInteger.Parse(c.Split(' ')[column]);
LinkedList<String> linkedList;
if (dictionary.TryGetValue(k, out linkedList) == false)
{
linkedList = new LinkedList<string>();
dictionary.Add(k, linkedList);
}
if (!reverseOrder)
{
linkedList.AddLast(c);
}
else
{
linkedList.AddFirst(c);
}
}
return dictionary;
}
/// <summary>
/// code review on Nov. 14, 2017
///
/// </summary>
/// <param name="reverseOrder"></param>
/// <param name="orderStrings"></param>
/// <param name="column"></param>
/// <returns></returns>
public static SortedDictionary<string, string> SortLexicographic(bool reverseOrder, string[] orderStrings, int column)
{
var comparer = new DescendingComparer();
SortedDictionary<string, string> dictionary;
if (reverseOrder)
{
dictionary = new SortedDictionary<string, string>(comparer);
}
else
{
dictionary = new SortedDictionary<string, string>();
}
foreach (var valueString in orderStrings)
{
string key = (valueString.Split(' ')[column]);
dictionary.Add(key, valueString);
}
return dictionary;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment