How to add the rest of the division to the last Value of a dictionary?

Asked

Viewed 60 times

0

I have the total of a purchase list and need to divide this total by the amount of emails which I have on my second list and return a dictionary. However, not all division is exact, so I need to add the rest of divisions not exact to the last Value dictionary.

Is there any method to take the last value and make the change?

using System;
using System.Collections.Generic;

namespace listas {
  class Program {
    static void Main(string[] args) {
        List<Itens> itens = new List<Itens>();
        itens.Add(new Itens() {
            Item = "Pendrive", Quantidade = 2, ValorUND = 20
        });
        itens.Add(new Itens() {
            Item = "Fones de ouvido", Quantidade = 2, ValorUND = 20
        });
        itens.Add(new Itens() {
            Item = "SSD 10GB", Quantidade = 1, ValorUND = 20
        });

        List<string> emails = new List<string>();
        emails.Add("[email protected]");
        emails.Add("[email protected]");
        emails.Add("[email protected]");

        Calcula(itens, emails);
    }

    static Dictionary<string,int> Calcula(List<Itens> lista1, List<string> lista2) 
    {
        int total = 0;
        int resto;
        Dictionary<string, int> dict = new Dictionary<string, int>();
        
        for (int i = 0; i < lista1.Count; i++) {
            total += (lista1[i].Quantidade * lista1[i].ValorUND);
        }

        total = total / lista2.Count;
        resto = total % lista2.Count;

        lista2.ForEach((string email) => {
            dict.Add(email, total);
        });

        if (resto != 0) {

        }

        foreach(KeyValuePair < string, int > d in dict) {
        Console.WriteLine($"{d.Key}: {d.Value.ToString("
          C ")}");
        }

        return dict;

    }
  }
}

Neste caso, eu teria que somar o resto(1) + 33 no último valor pra soma ser 100 igual ao total

  • There’s no way to know the last value of a dictionary, it’s a cluttered collection. Source: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2 "For purposes of enumeration, each item in the Dictionary is treated as a Keyvaluepair<Tkey,Tvalue> Structure Representing a value and its key. The order in which the items are returned is Undefined."

2 answers

0

As I had no way of knowing the last value of the dictionary, I created a new list to store the values of the dictionary, according to the size of the list that is received as argument of the function:

List<int> valores = new List<int>();
for (int i = 0; i < lista2.Count; i++)
        {
            valores.Add(total);
        }

Then I did a Do While to distribute the rest of the division among the list elements:

int n = 0;
        do
        {
            valores[n] = valores[n] + 1;
            n++;
        } while (n < resto);

At the end, I made a loop to add to the dictionary the elements of Lista2 as Keys, and the elements of the list values as values:

for (int i = 0; i < lista2.Count; i++)
        {
            dict.Add(lista2[i], valores[i]);
        }

0

Buddy, I don’t really understand Dictionary, but have you ever tried to create a variable to know what the last value is and then just call it? Like this:

int x = dict.count; 

// depois chamar, caso queira exibir
Console.WriteLine(Convert.ToString(lista[x].ValorUND));

I don’t know if this is exactly the lineage of the code, but it was the logic I had.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.