Sha256: 595d1ecab60040d0bf9d147cffe19a467962cbda5054c922e8cb82af70b14aba

Contents?: true

Size: 1.71 KB

Versions: 70

Compression:

Stored size: 1.71 KB

Contents

using System;
using System.Collections.Generic;
using System.Linq;

public class BookStore
{
    public static double CalculateTotalCost(List<int> books)
    {
        return CalculateTotalCost(books, 0);
    }

    private static double CalculateTotalCost(List<int> books, double priceSoFar)
    {
        if (books.Count == 0)
        {
            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 = CalculateTotalCost(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

70 entries across 70 versions & 1 rubygems

Version Path
trackler-2.0.8.14 tracks/csharp/exercises/book-store/Example.cs
trackler-2.0.8.13 tracks/csharp/exercises/book-store/Example.cs
trackler-2.0.8.12 tracks/csharp/exercises/book-store/Example.cs
trackler-2.0.8.11 tracks/csharp/exercises/book-store/Example.cs
trackler-2.0.8.10 tracks/csharp/exercises/book-store/Example.cs
trackler-2.0.8.9 tracks/csharp/exercises/book-store/Example.cs
trackler-2.0.8.8 tracks/csharp/exercises/book-store/Example.cs
trackler-2.0.8.7 tracks/csharp/exercises/book-store/Example.cs
trackler-2.0.8.6 tracks/csharp/exercises/book-store/Example.cs
trackler-2.0.8.5 tracks/csharp/exercises/book-store/Example.cs
trackler-2.0.8.4 tracks/csharp/exercises/book-store/Example.cs
trackler-2.0.8.3 tracks/csharp/exercises/book-store/Example.cs
trackler-2.0.8.2 tracks/csharp/exercises/book-store/Example.cs
trackler-2.0.8.1 tracks/csharp/exercises/book-store/Example.cs
trackler-2.0.7.0 tracks/csharp/exercises/book-store/Example.cs
trackler-2.0.6.44 tracks/csharp/exercises/book-store/Example.cs
trackler-2.0.6.43 tracks/csharp/exercises/book-store/Example.cs
trackler-2.0.6.42 tracks/csharp/exercises/book-store/Example.cs
trackler-2.0.6.41 tracks/csharp/exercises/book-store/Example.cs
trackler-2.0.6.40 tracks/csharp/exercises/book-store/Example.cs