Data Manipulation practice 3 - populate dictionary

Hi,

I’m trying to understand the logic of the dictionary being populated in this practice.
When reading through the solution I cannot find where the new dictionary stating the winners and the amount of wins gets populated with the names.
Is the condition in the “If” activity also the place where the names get used as key values?
WinnerCounts.ContainsKey(WinnerName)

Below the practice

Calculate and print the number of victories of each Tour de France winner

Given an input dictionary containing a year and a name, please calculate the number of victories of each winner and print all the names of the winners with the number of corresponding victories.

Note: Initialize a dictionary of type (Int32, String) with the following value - New Dictionary(Of Int32,String) From {{2006,“Oscar Pereiro”},{2007,“Alberto Contador”}, {2008, “Carlos Sastre”}, {2009,“Alberto Contador”}, {2010, “Andy Schleck”}, {2011, “Cadel Evans”}, {2012,“Bradley Wiggins”}, {2013,“Chris Froome”}, {2014,“Vincenzo Nibali”},{2015,“Chris Froome”},{2016,“Chris Froome”},{2017,“Chris Froome”}, {2018,“Geraint Thomas”}}

Practice 3 Solution

  1. Start the project as a sequence and create a new Dictionary with types (String, Int32)
  2. Loop through the values in the first Dictionary with a ’ For Each ’ (“ForEach WinnerNames in FirstDictionaryName.Values”) and check if the value is already present as key in the new Dictionary using the ‘.ContainsKey’ method inside an ’ If ’ activity (“SecondDictionaryName.ContainsKey(WinnerName)”):
  3. if the condition is met, use an ’ Assign ’ activity to increase the value of the corresponding key: WinnerCounts(WinnerName) = WinnerCounts(WinnerName) + 1
  4. if the condition is not met, add the key to the second Dictionary with value “1” using an ‘Invoke Method’ activity with the TargetObject the second Dictionary name and ’ Add ’ as MethodName
  5. Loop through the second Dictionary using ’ For Each ’ (“ForEach winner in SecondDictionaryName”) and use an ’ If ’ to write a differentiated text for those with 1 win vs. those with more wins using ‘Write Line’.

Yes. While iterating first dictionary,
If SecondDictionary contains name already, assign its value = value + 1 (number of victories)
else(there is no name in SeoncdDictionary), add to dictionary by (name, 1)

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.