Created
November 13, 2017 05:46
-
-
Save jianminchen/9676949588f4eb4cef790a23f582012e to your computer and use it in GitHub Desktop.
Morgan stanley order strings - second submission - change numeric type from int to long, score from 8 lower to 5.33
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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace OrderStrings | |
{ | |
class Key | |
{ | |
public string name { get; set; } | |
public Key(string name) | |
{ | |
this.name = name; | |
} | |
} | |
class KeyValue | |
{ | |
public long value { get; set; } | |
public KeyValue(string valueString) | |
{ | |
value = Convert.ToInt64(valueString); | |
} | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
public class KeySorterLexicographic : IComparer | |
{ | |
public int Compare(object o1, object o2) | |
{ | |
Key p1 = o1 as Key; | |
Key p2 = o2 as Key; | |
int compare = p1.name.ToLower().CompareTo(p2.name.ToLower()); | |
return compare; | |
} | |
} | |
public class KeySorterNumeric : IComparer | |
{ | |
public int Compare(object o1, object o2) | |
{ | |
KeyValue p1 = o1 as KeyValue; | |
KeyValue p2 = o2 as KeyValue; | |
int compare = p1.value < p2.value ? -1: 1; | |
return compare; | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int n = Convert.ToInt32(Console.ReadLine().ToString()); | |
var numbers = new string[n]; | |
for(int i = 0; i < n; i++) | |
{ | |
numbers[i] = Console.ReadLine(); | |
} | |
var configurations = Console.ReadLine(); | |
var reverse = SortByInstruction(numbers, configurations); | |
if(!reverse) | |
{ | |
foreach(var item in numbers) | |
{ | |
Console.WriteLine(item); | |
} | |
} | |
else | |
{ | |
var length = numbers.Length; | |
for(int i = length - 1; i >= 0; i--) | |
{ | |
Console.WriteLine(numbers[i]); | |
} | |
} | |
} | |
/// <summary> | |
/// key - 2 | |
/// reverse - true, false | |
/// sort format - numeric, lexicographic | |
/// </summary> | |
/// <param name="numbers"></param> | |
/// <param name="configurations"></param> | |
/// <returns></returns> | |
public static bool SortByInstruction(string[] numbers, string configurations) | |
{ | |
var parameters = configurations.Split(' '); | |
int key = Convert.ToInt32(parameters[0]); | |
bool reverseCheck = Convert.ToBoolean(parameters[1]); | |
string order = parameters[2]; | |
var isNumeric = order.Contains("numeric"); | |
var isLexicographical = order.Contains("lexicographical"); | |
int length = numbers.Length; | |
if (isNumeric) | |
{ | |
var comparer = new KeySorterNumeric(); | |
KeyValue[] values = getKthValueNumeric(numbers, key); | |
Array.Sort(values, numbers, comparer); | |
} | |
else | |
{ | |
var comparer = new KeySorterLexicographic(); | |
Key[] keys = getKthValueLexicographical(numbers, key); | |
Array.Sort(keys, numbers, comparer); | |
} | |
return reverseCheck; | |
} | |
private static KeyValue[] getKthValueNumeric(string[] numbers, int key) | |
{ | |
int length = numbers.Length; | |
var kthValues = new KeyValue[length]; | |
for(int i = 0; i < length; i++) | |
{ | |
kthValues[i] = new KeyValue(numbers[i].Split(' ')[key - 1]); | |
} | |
return kthValues; | |
} | |
private static Key[] getKthValueLexicographical(string[] numbers, int key) | |
{ | |
int length = numbers.Length; | |
var kthKeys = new Key[length]; | |
for (int i = 0; i < length; i++) | |
{ | |
kthKeys[i] = new Key(numbers[i].Split(' ')[key - 1]); | |
} | |
return kthKeys; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment