Sha256: 24874aead3249eeff2ea9a4d14d47b3a0ecb87510018bd13f13826fd212a6352
Contents?: true
Size: 1.68 KB
Versions: 193
Compression:
Stored size: 1.68 KB
Contents
using System; using System.Collections.Generic; using System.Linq; public static class BookStore { public static double Total(IEnumerable<int> books) { return Total(books, 0); } private static double Total(IEnumerable<int> books, double priceSoFar) { if (!books.Any()) { return priceSoFar; } var groups = books .GroupBy(b => b) .Select(g => g.Key) .ToList(); var minPrice = double.MaxValue; for (int i = groups.Count; i >= 1; i--) { var itemsToRemove = groups.Take(i).ToList(); var remaining = books.ToList(); foreach (var item in itemsToRemove) { remaining.Remove(item); } var price = Total(remaining.ToList(), priceSoFar + CostPerGroup(i)); minPrice = Math.Min(minPrice, price); } return minPrice; } private static double CostPerGroup(int groupSize) { double discountPercentage; switch (groupSize) { case 1: discountPercentage = 0; break; case 2: discountPercentage = 5; break; case 3: discountPercentage = 10; break; case 4: discountPercentage = 20; break; case 5: discountPercentage = 25; break; default: throw new InvalidOperationException($"Invalid group size: {groupSize}"); } return 8 * groupSize * (100 - discountPercentage) / 100; } }
Version data entries
193 entries across 193 versions & 1 rubygems