Created
September 20, 2016 05:01
-
-
Save jianminchen/e9ddff599b7ec3cfe53dd7d5c20f66bd to your computer and use it in GitHub Desktop.
HackerRank Stryker code sprint - study code #7
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.Globalization; | |
using System.IO; | |
using System.Linq; | |
class Solution { | |
internal interface IProblem | |
{ | |
IEnumerable<string> GetResults(); | |
} | |
public abstract class ProblemBase: IProblem | |
{ | |
/// <summary> | |
/// Pring array of <T> | |
/// </summary> | |
/// <typeparam name="T">Generic paramter.</typeparam> | |
/// <param name="items">The items.</param> | |
/// <param name="prefixMessage">The message shown first.</param> | |
public void PrintArray<T>(IEnumerable<T> items, string prefixMessage) | |
{ | |
Console.WriteLine("{0}: {1}", prefixMessage, string.Join(",",items)); | |
} | |
public string GetBitArrayString(System.Collections.BitArray bits) | |
{ | |
if (bits == null) | |
{ | |
throw new ArgumentNullException(nameof(bits)); | |
} | |
var builder = new System.Text.StringBuilder(); | |
for (int i = bits.Length-1; i >=0 ; i--) | |
{ | |
builder.Append(bits[i] ? "1" : "0"); | |
} | |
return builder.ToString(); | |
} | |
public void WriteDiagnostics(string message, params object[] objects) | |
{ | |
Console.WriteLine(message, objects); | |
} | |
public virtual IEnumerable<string> GetResults() | |
{ | |
yield return "No result"; | |
} | |
} | |
internal class ScsTask2 : ProblemBase | |
{ | |
internal class Point | |
{ | |
public int K { get; set; } | |
public double X { get; set; } | |
public double Y { get; set; } | |
public double Z { get; set; } | |
} | |
public override IEnumerable<string> GetResults() | |
{ | |
var firstLine = Console.ReadLine().Split(' '); | |
var N = Convert.ToInt32(firstLine[0]); | |
var B = Convert.ToInt32(firstLine[1]); | |
var bucket = new Dictionary<int, Point>(); | |
var points = new Point[N]; | |
for (int i = 0; i < N; i++) | |
{ | |
var line = Console.ReadLine(); | |
//Console.WriteLine("Parsing {0}", line); | |
var tokens = line.Split(' '); | |
var k = Convert.ToInt32(tokens[0]); | |
var x = Convert.ToDouble(tokens[1], NumberFormatInfo.InvariantInfo); | |
var y = Convert.ToDouble(tokens[2], NumberFormatInfo.InvariantInfo); | |
var z = Convert.ToDouble(tokens[3], NumberFormatInfo.InvariantInfo); | |
var point = new Point | |
{ | |
K = k, | |
X = x, | |
Y = y, | |
Z = z | |
}; | |
points[i] = point; | |
} | |
//var pointsList = Enumerable.Reverse(Enumerable.OrderBy(points, item => item.Z)).ToList(); | |
var pointsStack = new Stack<Point>(); | |
foreach (var point in Enumerable.OrderBy(points, item => item.Z)) | |
{ | |
pointsStack.Push(point); | |
} | |
for (int i = 0; i < B; i++) | |
{ | |
var point = pointsStack.Pop(); | |
bucket.Add(point.K, point); | |
} | |
while (true) | |
{ | |
var command = Console.ReadLine(); | |
if (command == null) | |
{ | |
break; | |
} | |
var tokens = command.Split(' '); | |
var action = tokens[0]; | |
var k = Convert.ToInt32(tokens[1]); | |
var pointDoesnTExistInTheBucket = "Point doesn't exist in the bucket."; | |
if (action.Equals("F", StringComparison.InvariantCultureIgnoreCase)) | |
{ | |
if (bucket.ContainsKey(k)) | |
{ | |
var point = bucket[k]; | |
yield return string.Format( | |
CultureInfo.InvariantCulture, | |
"{0} = ({1:F3},{2:F3},{3:F3})", | |
point.K, | |
point.X, | |
point.Y, | |
point.Z); | |
} | |
else | |
{ | |
yield return pointDoesnTExistInTheBucket; | |
} | |
} | |
else if (action.Equals("R", StringComparison.InvariantCultureIgnoreCase)) | |
{ | |
if (bucket.ContainsKey(k)) | |
{ | |
if (pointsStack.Count > 0) | |
{ | |
bucket.Remove(k); | |
var newPoint = pointsStack.Pop(); | |
bucket.Add(newPoint.K, newPoint); | |
yield return string.Format("Point id {0} removed.", k); | |
} | |
else | |
{ | |
yield return "No more points can be deleted."; | |
//break; | |
} | |
} | |
else | |
{ | |
yield return pointDoesnTExistInTheBucket; | |
} | |
} | |
else | |
{ | |
throw new InvalidOperationException(string.Format("Cannot parse commnd {0}", command)); | |
} | |
} | |
} | |
} | |
static void Main(String[] args) { | |
const string filePath = "Data\\ScsTask2.txt"; | |
if (System.IO.File.Exists(filePath)) | |
{ | |
Console.SetIn(new System.IO.StreamReader(filePath)); | |
} | |
IProblem counter = new ScsTask2(); | |
foreach (var result in counter.GetResults()) | |
{ | |
Console.WriteLine(result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment