Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created September 20, 2016 04:40
Show Gist options
  • Save jianminchen/ada496ab333e962988ca088a07deeac3 to your computer and use it in GitHub Desktop.
Save jianminchen/ada496ab333e962988ca088a07deeac3 to your computer and use it in GitHub Desktop.
HackerRank - Stryker world code sprint - study code - Array.sort call using compare function - how to define
using System;
using System.Collections.Generic;
using System.IO;
class PointF3D{
public int Id{get; set;}
public double X{get; set;}
public double Y{get; set;}
public double Z{get; set;}
public override string ToString(){
return string.Format("{0} = ({1:F3},{2:F3},{3:F3})", Id, X, Y, Z);
}
}
class Solution {
static void Main(String[] args) {
var nb = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
int n = nb[0];
int b = nb[1];
Dictionary<int, PointF3D> bucket = new Dictionary<int, PointF3D>(b);
PointF3D[] points = new PointF3D[n];
for(int i = 0; i < n; i++){
var line = Console.ReadLine().Split(' ');
points[i] = new PointF3D{
Id = int.Parse(line[0]),
X = double.Parse(line[1]),
Y = double.Parse(line[2]),
Z = double.Parse(line[3])
};
}
Array.Sort(points, CompareByZ);
int index = b;
for(int i = 0; i < b; i++){
bucket.Add(points[i].Id, points[i]);
}
while(true){
string line = Console.ReadLine();
if(string.IsNullOrWhiteSpace(line)){
break;
}
var query = line.Split(' ');
var k = int.Parse(query[1]);
if(query[0] == "f" || query[0] == "F"){
if(bucket.ContainsKey(k)){
Console.WriteLine(bucket[k]);
}
else{
Console.WriteLine("Point doesn't exist in the bucket.");
}
}
else{
if(bucket.ContainsKey(k)){
if(index >= n){
Console.WriteLine("No more points can be deleted.");
}
else{
bucket.Remove(k);
var p = points[index];
index++;
bucket.Add(p.Id, p);
Console.WriteLine("Point id {0} removed.", k);
}
}
else{
Console.WriteLine("Point doesn't exist in the bucket.");
}
}
}
}
static int CompareByZ(PointF3D p1, PointF3D p2){
return p2.Z.CompareTo(p1.Z);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment