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

Version Path
trackler-2.2.1.139 tracks/csharp/exercises/book-store/Example.cs
trackler-2.2.1.138 tracks/csharp/exercises/book-store/Example.cs
trackler-2.2.1.137 tracks/csharp/exercises/book-store/Example.cs
trackler-2.2.1.136 tracks/csharp/exercises/book-store/Example.cs
trackler-2.2.1.135 tracks/csharp/exercises/book-store/Example.cs
trackler-2.2.1.134 tracks/csharp/exercises/book-store/Example.cs
trackler-2.2.1.133 tracks/csharp/exercises/book-store/Example.cs
trackler-2.2.1.132 tracks/csharp/exercises/book-store/Example.cs
trackler-2.2.1.131 tracks/csharp/exercises/book-store/Example.cs
trackler-2.2.1.130 tracks/csharp/exercises/book-store/Example.cs
trackler-2.2.1.129 tracks/csharp/exercises/book-store/Example.cs
trackler-2.2.1.128 tracks/csharp/exercises/book-store/Example.cs
trackler-2.2.1.127 tracks/csharp/exercises/book-store/Example.cs
trackler-2.2.1.126 tracks/csharp/exercises/book-store/Example.cs
trackler-2.2.1.125 tracks/csharp/exercises/book-store/Example.cs
trackler-2.2.1.124 tracks/csharp/exercises/book-store/Example.cs
trackler-2.2.1.123 tracks/csharp/exercises/book-store/Example.cs
trackler-2.2.1.122 tracks/csharp/exercises/book-store/Example.cs
trackler-2.2.1.121 tracks/csharp/exercises/book-store/Example.cs
trackler-2.2.1.120 tracks/csharp/exercises/book-store/Example.cs