yomama Posted September 16, 2016 Report Share Posted September 16, 2016 Here we have a dictionary of type [String: String] that contains a three letter country code as a key and that country's capital city as the associated value. We also have three empty arrays, europeanCapitals, asianCapitals, and otherCapitals. The goal is to iterate through the dictionary and end up with just the names of the capital cities in the relevant array. For example, after you execute the code you write, europeanCapitals will have the values ["Vaduz", "Brussels", "Sofia"] (not necessarily in that order). To do this you're going to use a switch statement and switch on the key. For cases where the key is a European country, append the value (not the key!) to the europeanCapitals array. For keys that are Asian countries, append the value to asianCapitals and finally for the default case, append the values tootherCapitals. I've already set up the for in loop for you so jump right in! var europeanCapitals: [String] = [] var asianCapitals: [String] = [] var otherCapitals: [String] = [] let world = [ "BEL": "Brussels", "LIE": "Vaduz", "BGR": "Sofia", "USA": "Washington D.C.", "MEX": "Mexico City", "BRA": "Brasilia", "IND": "New Delhi", "VNM": "Hanoi"] for (key, value) in world { // Enter your code below // End code } Quote Link to comment Share on other sites More sharing options...
loveindia Posted September 16, 2016 Report Share Posted September 16, 2016 let use chestunnav antey ios programming nerchukuntunnavaa man?? idi easy ne ga man... switch(key) { case BEL, LIE, BGR : europeanCapitals.add(value) break case IND, VNM : asianCapitals.add(value) break default : otherCapitals.add(value) break } test cheyaledu, but mac nunchi aitey playgrounds lo test chesko... Quote Link to comment Share on other sites More sharing options...
Suhaas Posted September 16, 2016 Report Share Posted September 16, 2016 foreach(var item in world.Keys) { int continent = GetContinentIdFromKey(item.Key); switch (continent) { case 1: europeanCapitals.Add(item.value) break; case 2: asianCaptials.Add(item.value) break; default: otherCapitals.Add(item.value); break; } } Using C#. Note: Use List<String> instead of array. Quote Link to comment Share on other sites More sharing options...
loveindia Posted September 16, 2016 Report Share Posted September 16, 2016 1 minute ago, Suhaas said: foreach(var item in myDictionary.Keys) { int continent = GetContinentIdFromKey(item.Key); switch (continent) { case 1: europeanCapitals.Add(item.value) break; case 2: asianCaptials.Add(item.value) break; break; default: otherCapitals.Add(item.value); break; } } how come you are getting a number man? there is no integer value for the key right?? Key need not be an integer in dictionaries... Quote Link to comment Share on other sites More sharing options...
Suhaas Posted September 16, 2016 Report Share Posted September 16, 2016 2 minutes ago, loveindia said: how come you are getting a number man? there is no integer value for the key right?? Key need not be an integer in dictionaries... He did not specify they way to get Continent from the key Using a function to calc that logic whatever that logic may be int continent = GetContinentIdFromKey(item.Key); Quote Link to comment Share on other sites More sharing options...
loveindia Posted September 16, 2016 Report Share Posted September 16, 2016 1 minute ago, Suhaas said: He did not specify they way to get Continent from the key Using a function to calc that logic whatever that logic may be int continent = GetContinentIdFromKey(item.Key); why that much kastaalu man... there is not a logic unless you have specified data for continents... so you will be hardcoding there also... in such a case, hard code the values in the first place only... less code the better kada man... Quote Link to comment Share on other sites More sharing options...
Suhaas Posted September 16, 2016 Report Share Posted September 16, 2016 1 minute ago, loveindia said: why that much kastaalu man... there is not a logic unless you have specified data for continents... so you will be hardcoding there also... in such a case, hard code the values in the first place only... less code the better kada man... Sure bhayya. I usually a separate the logic for unit testing or whatever reasons. PS: I am not an expert. Just trying. Quote Link to comment Share on other sites More sharing options...
loveindia Posted September 16, 2016 Report Share Posted September 16, 2016 2 minutes ago, Suhaas said: Sure bhayya. I usually a separate the logic for unit testing or whatever reasons. PS: I am not an expert. Just trying. I am not criticizing your code man... if you thought so... just productive ways laaga discussing with you.. so you will have one idea, I will also have one because of the discussion... so ala... there are always 100's of ways you can approach... Quote Link to comment Share on other sites More sharing options...
loveindia Posted September 16, 2016 Report Share Posted September 16, 2016 challenge esi answer eyyakunda poyadu @yomama man Quote Link to comment Share on other sites More sharing options...
Suhaas Posted September 16, 2016 Report Share Posted September 16, 2016 Just now, loveindia said: I am not criticizing your code man... if you thought so... just productive ways laaga discussing with you.. so you will have one idea, I will also have one because of the discussion... so ala... there are always 100's of ways you can approach... bhayya deniki. I know you man. Criticize chesinattu nakem anpinchale. Just general ga cheppina neenu kuda pedha expert em kadu ani. No hard feelings. BTW busy ayinattu unnav. Kothaga em start chesthaleva training sessions. If so, let me know. Quote Link to comment Share on other sites More sharing options...
yomama Posted September 16, 2016 Author Report Share Posted September 16, 2016 15 minutes ago, loveindia said: challenge esi answer eyyakunda poyadu @yomama man man pani mida poya. 1 min am looking at answers now. Test chesta undu. Xcode ye using and swift. Quote Link to comment Share on other sites More sharing options...
Sambadu Posted September 16, 2016 Report Share Posted September 16, 2016 em language edi? VB ah? Quote Link to comment Share on other sites More sharing options...
yomama Posted September 16, 2016 Author Report Share Posted September 16, 2016 43 minutes ago, loveindia said: let use chestunnav antey ios programming nerchukuntunnavaa man?? idi easy ne ga man... switch(key) { case BEL, LIE, BGR : europeanCapitals.add(value) break case IND, VNM : asianCapitals.add(value) break default : otherCapitals.add(value) break } test cheyaledu, but mac nunchi aitey playgrounds lo test chesko... Quote Link to comment Share on other sites More sharing options...
yomama Posted September 16, 2016 Author Report Share Posted September 16, 2016 41 minutes ago, Suhaas said: foreach(var item in world.Keys) { int continent = GetContinentIdFromKey(item.Key); switch (continent) { case 1: europeanCapitals.Add(item.value) break; case 2: asianCaptials.Add(item.value) break; default: otherCapitals.Add(item.value); break; } } Using C#. Note: Use List<String> instead of array. c# aa swift or obj c using man. But ikada key value rendu strings and you are trying to assign int to string. Quote Link to comment Share on other sites More sharing options...
yomama Posted September 16, 2016 Author Report Share Posted September 16, 2016 6 minutes ago, Sambadu said: em language edi? VB ah? swift man. ios dev language Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.