Jump to content

small code challenge


yomama

Recommended Posts

 

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
}

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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);
Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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... sorry 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...

Link to comment
Share on other sites

Just now, loveindia said:

I am not criticizing your code man... sorry 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 sorry 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. 

Link to comment
Share on other sites

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...

loveindia_code.png

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...