P
P
PRIYD2020-05-04 17:55:16
Algorithms
PRIYD, 2020-05-04 17:55:16

How to fill in the missing elements of the dictionary?

Hello, there is a certain ordered dictionary SortedDictionary, with a dimension of 360 elements, which receives data {int, float}, where int є [0; 360] (you can think that int is something like id), float is any. But, the catch is that in the n-th number of elements there is no value for float.

The task is to fill each empty element of the dictionary with the help of two neighboring elements (the float variable is equal to their arithmetic mean), provided:
1. If the unknown float is the very first in the dictionary, then it is simply assigned the value of the second element.
2. If an unknown float is in last place, then it is assigned the value of the penultimate element.
3. If several unknown elements are in a row, then the float value of the first such element is the arithmetic mean of the first known element before this row and the first known element after this row.

Here is a good example (let "?" be an unknown float):
{{1, ?}, {2, 20}, {3, 18}, {4, ?}, {5, ?}, {6, ?}, {7, 13}, ..., {359, 8}, {360, ?}}

PS The algorithm seems to be the simplest, but, apparently, I didn't get enough sleep today and I don't understand what to do here at all.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
#
#, 2020-05-04
@PRIYD

1 - I think the Unity tag is superfluous here))
2 -

3. If several unknown elements are in a row, then the float value of the first such element is the arithmetic mean of the first known element before this row and the first known element after this row.
Well, here something falls out of the series .. why not build a discrete series of averages between the initial and final ones? .. but these are also thoughts out loud. if your conditions are set from the outside or you rigidly insist that this is the only way , then the master
's 3 - all three (your. According to the text of the question) points are very simple. the question(s) is only:
- when do you find the lack of values?
When should the correction be done?
- .. somehow reduce these events to a solution ;)))
4 - and here, the only point that, in my opinion, is most important for you to learn in the context of the question
- the key in the c# dictionary is unique
- you limit the values ​​to the range [0-360] (by the way, why not [0-359] or [1-360]? whatever you want)
- it's definitely easier for you to use an array float[360]. do not thank the lie, I love marks. and this point, at the moment, is the most valuable for you ;))
ps something was thought.. you probably need to somehow distinguish values ​​that have not yet been initialized? .. maybe float?[360]( Nullable ) is a good way to distinguish a value that has not yet taken on a numerical value, without creating any other abstruse abstractions ...
and even indicate the case when a previously defined value must be assigned a value that clearly says and now we again do not know the number, and insist on the uncertainty of the value

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question