public int FurthestBuilding(int[] heights, int bricks, int ladders) { var pq = new MyPriorityQueue(); for (int i = 0; i < heights.Length - 1; i++) { int climb = heights[i + 1] - heights[i]; if (climb <= 0) { continue; } pq.add(climb); if (pq.size() > ladders) { var first = pq.poll(); bricks -= first; } if (bricks < 0) { return(i); } } return(heights.Length - 1); }
public void Test_constructor1() { Queue = new MyPriorityQueue <int>(); Assert.True(Queue.IsEmpty()); Assert.Equal(10, Queue.HeapCapacity); }
/// <summary> /// <para> /// Replacement for list.OrderBy(?).Take(n) /// </para> /// <para> /// 1- Low Memory usage (since this method requires only ~"2*n" elements be in memory) /// </para> /// 2- Speed up by an order of magnitude especially when the input is large. /// </summary> public static IEnumerable <T> TakeOrdered <T, TKey>(this IEnumerable <T> list, int n, Func <T, TKey> keySelector, bool ascending = true) where TKey : IComparable <TKey> { var pq = new MyPriorityQueue <T>(n, ascending ? (Func <T, T, bool>)((a, b) => keySelector(a).CompareTo(keySelector(b)) >= 0) : (Func <T, T, bool>)((a, b) => keySelector(a).CompareTo(keySelector(b)) < 0) ); Stack <T> stack = new Stack <T>(); int count = 0; foreach (T item in list) { pq.InsertWithOverflow(item); count++; } int min = Math.Min(count, n); for (int i = 0; i < min; i++) { stack.Push(pq.Pop()); } for (int i = 0; i < min; i++) { yield return(stack.Pop()); } }
public void FillRemoveMixTest() { var queue = new MyPriorityQueue(10); queue.Push("Max"); Assert.Equal(0.1, queue.PercentageFilled); queue.Push("Susi"); Assert.Equal(0.2, queue.PercentageFilled); Assert.Equal("Max", queue.Pop()); Assert.Equal(0.1, queue.PercentageFilled); queue.Push("Muster"); Assert.Equal(0.2, queue.PercentageFilled); Assert.Equal("Susi", queue.Pop()); Assert.Equal("Muster", queue.Pop()); // Assert.Equal(0.0, queue.PercentageFilled); Assert.Throws <InvalidOperationException>(() => queue.Pop()); queue.Push("Axl"); Assert.Equal(0.1, queue.PercentageFilled); for (int i = 1; i < queue.Size; i++) { queue.Push(i.ToString()); } Assert.Throws <InvalidOperationException>(() => queue.Push("Already full not possible")); Assert.Equal(1.0, queue.PercentageFilled); }
public void Test_constructor2() { Queue = new MyPriorityQueue <int>(RandArr); Assert.False(Queue.IsEmpty()); Assert.Equal(RandArr.Length, Queue.HeapCapacity); Assert.Equal(RandArr.Length, Queue.HeapSize); }
public void PropertiesInitTests() { int size = 5; var queue = new MyPriorityQueue(size); Assert.Equal(0.0, queue.PercentageFilled); Assert.Equal(size, queue.Size); }
public void Test_peek() { Queue = new MyPriorityQueue <int>(RandArr); for (int i = 0; i < RandArr.Length; i++) { Assert.Equal(Queue.Peek(), Queue.Poll()); } }
public void PushOnFullTest() { // Size of two but have 3 pushes var queue = new MyPriorityQueue(2); queue.Push("Me"); queue.Push("You"); Assert.Throws <InvalidOperationException>(() => queue.Push("Push On Full Outh")); }
public void AddLettersBigAndSmall() { MyPriorityQueue <string> sut = new MyPriorityQueue <string>(); sut.Add("A"); sut.Add("a"); string actual = sut.Peek(); Assert.AreEqual("a", actual); }
public void SwoopsDataTheRightWay() { MyPriorityQueue <int> sut = new MyPriorityQueue <int>(); sut.Add(8); sut.Add(2); int actual = sut.Peek(); Assert.AreEqual(2, actual); }
public void AddNegativeValues() { MyPriorityQueue <int> sut = new MyPriorityQueue <int>(); sut.Add(-8); sut.Add(-2); int actual = sut.Peek(); Assert.AreEqual(-8, actual); }
public void CountReturnsRightValue() { MyPriorityQueue <int> sut = new MyPriorityQueue <int>(); sut.Add(8); sut.Add(2); int actual = sut.Count(); Assert.AreEqual(2, actual); }
public void Test_remove() { Queue = new MyPriorityQueue <int>(RandArr); for (int i = 0; i < RandArr.Length; i++) { Assert.True(Queue.Remove(i)); } Assert.True(Queue.IsEmpty()); }
public void EmptyPopTest() { var queue = new MyPriorityQueue(2); // Can't pop on empty queue Assert.Throws <InvalidOperationException>(() => queue.Pop()); queue.Push("Wolfi"); Assert.Equal(0.5, queue.PercentageFilled); Assert.Equal("Wolfi", queue.Pop()); Assert.Throws <InvalidOperationException>(() => queue.Pop()); }
public void PopOneValue() { MyPriorityQueue <int> sut = new MyPriorityQueue <int>(); sut.Add(8); sut.Add(2); sut.Pop(); int actual = sut.Peek(); Assert.AreEqual(8, actual); }
public void AddloadsOfValues_IfCountIsRight() { MyPriorityQueue <int> sut = new MyPriorityQueue <int>(); for (int i = 0; i < 1000; i++) { sut.Add(i); } int actual = sut.Count(); Assert.AreEqual(1000, actual); }
public void PopTwoValues() { MyPriorityQueue <int> sut = new MyPriorityQueue <int>(); sut.Add(8); sut.Add(2); sut.Pop(); sut.Pop(); int actual = sut.Count(); Assert.AreEqual(0, actual); }
private static void Simulate(long threadsQuantity, long[] jobsDuration) { var threads = new MyPriorityQueue(threadsQuantity); var jobs = new Queue <long>(jobsDuration); while (jobs.Any()) { var newJobDuration = jobs.Dequeue(); var activeThread = threads.GetThread(); Console.WriteLine($"{activeThread.Index} {activeThread.ReleaseTime}"); activeThread.ReleaseTime += newJobDuration; threads.SiftDown(0); } }
static void Main() { var people = new MyPriorityQueue <Person>(); people.Enqueue(new Person("Doncho", 25)); people.Enqueue(new Person("Niki", 24)); people.Enqueue(new Person("Ivo", 25)); people.Enqueue(new Person("Asya", 22)); people.Enqueue(new Person("Pesho", int.MinValue)); while (people.Count > 0) { Console.WriteLine(people.Dequeue()); } }
public void Test_poll() { Queue = new MyPriorityQueue <int>(); foreach (var e in RandArr) { Queue.Add(e); } while (!Queue.IsEmpty()) { Queue.Poll(); } Assert.True(Queue.IsEmpty()); }
public void GivenAQueueWith1And3Enqueued_WhenEnqueue2_ShouldEnqueueThisNewValueOnMiddle() { // Arrange var queue = new MyPriorityQueue(3); queue.Enqueue(1); queue.Enqueue(3); // Act queue.Enqueue(2); // Assert queue.Dequeue().Should().Be(1); queue.Dequeue().Should().Be(2); queue.Dequeue().Should().Be(3); }
public void GivenAQueueWithSomeItems_WhenTryToEnqueueANewValueThatIsGreaterThanAll_ShouldEnqueueThisLast() { // Arrange var queue = new MyPriorityQueue(3); queue.Enqueue(1); queue.Enqueue(2); // Act queue.Enqueue(3); // Assert queue.Dequeue().Should().Be(1); queue.Dequeue().Should().Be(2); queue.Dequeue().Should().Be(3); }
public void Test_add() { Queue = new MyPriorityQueue <int>(); for (int i = RandArr.Length - 1; i >= 0; i--) { Queue.Add(RandArr[i]); } for (int i = 0; i < RandArr.Length; i++) { Queue.Add(RandArr[i]); } Assert.True(Queue.IsMinHeap(0)); Assert.False(Queue.IsEmpty()); Assert.Equal(2 * RandArr.Length, Queue.HeapSize); }
public void NormalQueueTest() { // First fill then remove string[] data = { "Max", "Muster", "Müller", "Susi", "Mosi", "Gucki", "Hucki" }; var queue = new MyPriorityQueue(data.Length); // Fill it foreach (var item in data) { queue.Push(item); } // Pop It for (int i = 0; i < data.Length; i++) { Assert.Equal(data[i], queue.Pop()); } }
public void Test_contains() { MyPriorityQueue <char> queue = new MyPriorityQueue <char>(); queue.Add('a'); queue.Add('d'); queue.Add('g'); queue.Add('x'); queue.Add('z'); Assert.True(queue.Contains('a')); Assert.True(queue.Contains('z')); Assert.True(queue.Contains('g')); Assert.False(queue.Contains('r')); Assert.False(queue.Contains('u')); Assert.False(queue.Contains('i')); }
public void AddLoadsOfLettersBigAndSmall() { MyPriorityQueue <string> sut = new MyPriorityQueue <string>(); sut.Add("y"); sut.Add("Y"); sut.Add("a"); sut.Add("A"); sut.Add("j"); sut.Add("L"); sut.Add("R"); sut.Add("r"); sut.Add("B"); string actual = sut.Peek(); Assert.AreEqual("a", actual); }
public static void Main() { var queue = new MyPriorityQueue<int>(); queue.Insert(10); queue.Insert(5); queue.Insert(7); queue.Insert(3); queue.Insert(15); queue.Insert(4); queue.Insert(13); queue.Insert(20); queue.Insert(5); queue.Insert(500); queue.Insert(12); queue.Insert(145); queue.Insert(10); queue.Insert(5123); queue.Insert(2); queue.Insert(513); queue.Insert(-20); queue.Insert(-20); queue.Insert(-20); queue.Insert(-20); queue.Insert(-20); queue.Insert(400); foreach (var item in queue) { Console.WriteLine(item); } Console.WriteLine(); Console.WriteLine("-----------"); Console.WriteLine("Max: {0}", queue.ExtractMax()); Console.WriteLine("-----------"); Console.WriteLine(); while (queue.Count > 0) { Console.WriteLine("Max: {0}", queue.ExtractMax()); } }
public void PriorityPushTests() { var queue = new MyPriorityQueue(10); queue.Push("Susi"); queue.Push("VIP MAX", PersonTypes.Vip); Assert.Equal("VIP MAX", queue.Pop()); Assert.Equal("Susi", queue.Pop()); queue.Push("Normal1"); queue.Push("Normal2"); queue.Push("VIP1", PersonTypes.Vip); queue.Push("VIP2", PersonTypes.Vip); queue.Push("Normal3"); queue.Push("VIP3", PersonTypes.Vip); Assert.Equal("VIP1", queue.Pop()); Assert.Equal("VIP2", queue.Pop()); Assert.Equal("VIP3", queue.Pop()); Assert.Equal("Normal1", queue.Pop()); Assert.Equal("Normal2", queue.Pop()); Assert.Equal("Normal3", queue.Pop()); }
static void Main() { /*Implement a class PriorityQueue<T> based on the data structure "binary heap". Write a program to read a large collection of products(name + price) and efficiently find the first 20 products in the price range[a…b]. Test for 500 000 products and 10 000 price searches. Hint: you may use OrderedBag<T> and sub-ranges. Write a program that finds a set of words (e.g. 1000 words) in a large text(e.g. 100 MB text file). Print how many times each word occurs in the text. Hint: you may find a C# trie in Internet.*/ var priorityQueue = new MyPriorityQueue<int>(); priorityQueue.Enqueue(1); priorityQueue.Enqueue(3); priorityQueue.Enqueue(18); priorityQueue.Enqueue(4); priorityQueue.Enqueue(105); priorityQueue.Enqueue(213); var last = priorityQueue.Dequeue(); Console.WriteLine("TASK 1:"); Console.WriteLine("Dequed: " + last); Console.WriteLine("Size of queue: " + priorityQueue.Count()); Console.WriteLine("All elements: \n" + String.Join(", ", priorityQueue.All())); Console.WriteLine(); /*Write a program to read a large collection of products (name + price) and efficiently find the first 20 products in the price range [a…b]. Test for 500 000 products and 10 000 price searches. Hint: you may use OrderedBag<T> and sub-ranges.*/ var db = new ProductsDb(); for (int i = 0; i < 500000; i++) { db.AddProduct(new Product(GetRandomName(), GetRandomPriceInRange(0, 100000))); } Console.WriteLine("TASK 2:"); Console.WriteLine("500 000 products added"); var stopWatch = new Stopwatch(); stopWatch.Start(); var productsInRange = db.GetProductsInPriceRange(100, 3000); stopWatch.Stop(); Console.WriteLine("Alll products in this range: " + productsInRange.Count); Console.WriteLine("Elapsed time to find all products: "+ stopWatch.Elapsed); Console.WriteLine(); /*Write a program that finds a set of words(e.g. 1000 words) in a large text(e.g. 100 MB text file). Print how many times each word occurs in the text. Hint: you may find a C# trie in Internet.*/ var sw = new Stopwatch(); var trie = new Trie(); for (int i = 0; i < wordsToAdd; i++) { string word = GetRandomName(); sw.Start(); trie.Add(word); sw.Stop(); } Console.WriteLine("TASK 3"); Console.WriteLine("Added {0} words for {1} time", wordsToAdd, sw.Elapsed); sw.Reset(); for (int i = 0; i < wordsToSearch; i++) { string word = GetRandomName(); sw.Start(); trie.GetWordOccurance(word); sw.Stop(); } Console.WriteLine("Found {0} words for {1} time", wordsToSearch, sw.Elapsed); Console.WriteLine("Most common word: {0}", trie.GetMostCommonWord()); }
static void Main(string[] args) { Console.BackgroundColor = ConsoleColor.White; Console.Clear(); Console.ForegroundColor = ConsoleColor.Black; string[] ilAdı = { "Edirne", "Istanbul", "Kırklareli", "Kocaeli", "Denizli", "Izmir", "Manisa", "Muğla", "Adana", "Antalya", "Mersin", "Ankara", "Bolu", "Trabzon", "Erzurum" }; int[] havaSıcaklığı = { 21, 22, 21, 25, 24, 21, 22, 18, 27, 23, 26, 18, 17, 20, 8 }; List <Il> GList = new List <Il>(); // il nesneleri içeren generic list List <List <Il> > GList_main = new List <List <Il> >(); // farklı sayıda il nesnesi içeren generic listleri kapsayan genercic list for (int i = 0; i < havaSıcaklığı.Length; i++) { GList.Add(new Il(ilAdı[i], havaSıcaklığı[i])); } int sayac = 0; int counter = 0; int son = GList.Count; Console.WriteLine("**İl nesnelerinin veri yapısından çıktıları**"); for (int m = 1; ; m++, m++) { for (int j = 1; j <= m; j++) { GList_main.Add(new List <Il>()); GList_main[counter].Add(GList[sayac]); Console.WriteLine(GList_main[counter][j - 1].ilAdi + " Slot:" + m); sayac++; if (son <= sayac) { break; } } counter++; if (son <= sayac) { break; } } Console.WriteLine(); MyStack iller_s = new MyStack(havaSıcaklığı.Length); Console.WriteLine("**Hava sıcaklığı verileri stack yapısına geçiriliyor..**"); for (int j = 0; j < havaSıcaklığı.Length; j++) { iller_s.push(havaSıcaklığı[j]); } Console.WriteLine("**Hava sıcaklığı verilerinin stack yapısından çıktıları**"); while (!iller_s.isEmpty()) { int donkey = iller_s.pop(); Console.WriteLine(donkey); } Console.WriteLine(); MyQueue iller_q = new MyQueue(havaSıcaklığı.Length); Console.WriteLine("**Hava sıcaklığı verileri queue yapısına geçiriliyor..**"); for (int j = 0; j < havaSıcaklığı.Length; j++) { iller_q.insert(havaSıcaklığı[j]); } Console.WriteLine("**Hava sıcaklığı verilerinin queue yapısından çıktıları**"); while (!iller_q.isEmpty()) { int donkey = iller_q.remove(); Console.WriteLine(donkey); } Console.WriteLine(); MyPriorityQueue iller_pq = new MyPriorityQueue(GList.Count); Console.WriteLine("**Priority Queue il nesneleri alfabetik azalan sırada ekleme işlemi yapılıyor..**"); for (int j = 0; j < GList.Count; j++) { iller_pq.insert(GList[j]); } Console.WriteLine("**Priority Queue il nesneleri silme işlemi çıktıları**"); while (!iller_pq.isEmpty()) { Console.WriteLine(iller_pq.remove().ilAdi); } Console.WriteLine(); Random r = new Random(); double toplam = 0; double[] numList = new double[25]; MyPriorityQueue bee = new MyPriorityQueue(25); Console.WriteLine("**Priority Queue double veri olan müşteri işlem sürelerinin artan sırada ekleme işlemi yapılıyor..**"); for (int i = 0; i < 25; i++) { numList[i] = r.NextDouble() * (125.00 - 15.00) + 15.00; if (i == 0) { bee.insert(numList[i]); toplam = toplam + numList[i]; continue; } for (int j = i - 1; j >= 0; j--) { numList[i] = numList[i] + numList[j]; } bee.insert(numList[i]); toplam = toplam + numList[i]; } Console.WriteLine("**Priority Queue double veri olan müşteri süreleri çıktıları**"); while (!bee.isEmpty()) { Console.WriteLine(bee.Remove()); } Console.WriteLine("**Priority Queue double veri olan müşteri süreleri ortalaması**"); Console.WriteLine(toplam / numList.Length); Console.WriteLine(); Random r_ = new Random(); double toplam_ = 0; double[] numList_ = new double[25]; MyQueue bee_ = new MyQueue(25); Console.WriteLine("**Queue double veri olan müşteri işlem sürelerinin artan sırada ekleme işlemi yapılıyor..**"); for (int i = 0; i < 25; i++) { numList_[i] = r_.NextDouble() * (125.00 - 15.00) + 15.00; if (i == 0) { bee_.insert(numList_[i]); toplam_ = toplam_ + numList_[i]; continue; } for (int j = i - 1; j >= 0; j--) { numList_[i] = numList_[i] + numList_[j]; } bee_.insert(numList_[i]); toplam_ = toplam_ + numList_[i]; } Console.WriteLine("**Queue double veri olan müşteri süreleri çıktıları**"); while (!bee_.isEmpty()) { Console.WriteLine(bee_.Remove()); } Console.WriteLine("**Queue double veri olan müşteri süreleri ortalaması**"); Console.WriteLine(toplam_ / numList_.Length); Console.WriteLine(); }
public void Test_remove_from_empty() { Queue = new MyPriorityQueue <int>(); Assert.Throws <Exception>(() => Queue.Remove(1)); }
public void Test_add_null() { MyPriorityQueue <string> queue = new MyPriorityQueue <string>(); Assert.Throws <ArgumentNullException>(() => queue.Add(null)); }