Генерация простой гистограммы целочисленных данных в C #

У меня было то же сообщение об ошибке после добавления версии фреймворка Swift 3.

Моя цель framework search path все еще была настроена на поиск версий swift 2 и swift 3 одной и той же платформы, поэтому мой проект застрял на версии swift 2.3.

Для меня решение состояло в том, чтобы удалить старую платформу из каталога моего проекта и удалить ссылку на ее папку из framework search path.

8
задан Jon Cage 29 May 2009 в 14:12
поделиться

2 ответа

Вы можете использовать SortedDictionary

uint[] items = new uint[] {5, 6, 1, 2, 3, 1, 5, 2}; // sample data
SortedDictionary<uint, int> histogram = new SortedDictionary<uint, int>();
foreach (uint item in items) {
    if (histogram.ContainsKey(item)) {
        histogram[item]++;
    } else {
        histogram[item] = 1;
    }
}
foreach (KeyValuePair<uint, int> pair in histogram) {
    Console.WriteLine("{0} occurred {1} times", pair.Key, pair.Value);
}

Это оставит пустые ячейки, хотя

17
ответ дан 5 December 2019 в 06:10
поделиться

Основываясь на предложении BastardSaint, я придумал аккуратную и довольно общую оболочку:

public class Histogram<TVal> : SortedDictionary<TVal, uint>
{
    public void IncrementCount(TVal binToIncrement)
    {
        if (ContainsKey(binToIncrement))
        {
            this[binToIncrement]++;
        }
        else
        {
            Add(binToIncrement, 1);
        }
    }
}

Итак, теперь я могу:

const uint numOfInputDataPoints = 5;
Histogram<uint> hist = new Histogram<uint>();

// Fill the histogram with data
for (uint i = 0; i < numOfInputDataPoints; i++)
{
    // Grab a result from my algorithm
    uint numOfIterationsForSolution = MyAlorithm.Run();

    // Add the number to the histogram
    hist.IncrementCount( numOfIterationsForSolution );
}

// Report the results
foreach (KeyValuePair<uint, uint> histEntry in hist.AsEnumerable())
{
    Console.WriteLine("{0} occurred {1} times", histEntry.Key, histEntry.Value);
}

Мне потребовалось время, чтобы придумать, как сделать ее универсальной ( для начала я просто переопределил конструктор SortedDictionary , что означало, что вы могли использовать его только для ключей uint ).

6
ответ дан 5 December 2019 в 06:10
поделиться
Другие вопросы по тегам:

Похожие вопросы: