Dictionaries: Gold Challenge Solutions

Just contributing to this forum. First time! Below is my code for the Gold solution:

var blueDevilsPlayers = [“Matt”, “John”, “Cuda”, “Reba”, “Alex”]
var cavaliersPlayers = [“John1”, “Maria”, “Ari”, “Carmelo”, “Betsy”]
var crusadersPlayers = [“Ramona”, “Frank”, “Ore”, “Sean”, “Zach”]

var blueDevilsList = blueDevilsPlayers.joined(separator: “\n”)
var cavaliersList = cavaliersPlayers.joined(separator: “\n”)
var crusadersList = crusadersPlayers.joined(separator: “\n”)

var dciLeague = [“BlueDevils”: blueDevilsPlayers, “Cavaliers”: cavaliersPlayers, “Crusaders”: crusadersPlayers]

var dciLeagueList = [“BlueDevils”: blueDevilsList, “Cavaliers”: cavaliersList, “Crusaders”: crusadersList]

print(“The dci league has the following players: (dciLeague.values)\n”)

for (key, value) in dciLeagueList{
print("(key):", “(value)”, separator: “\n”, terminator: “\n\n”)
}

I could not resist beautifying your code, with minor corrections in the print statements, by including it between a pair of three back-tick characters like this :slight_smile:

```
Your Code
```

var blueDevilsPlayers = ["Matt", "John", "Cuda", "Reba", "Alex"]
var cavaliersPlayers  = ["John1", "Maria", "Ari", "Carmelo", "Betsy"]
var crusadersPlayers  = "“Ramona", "Frank", "Ore", "Sean", "Zach"]

var blueDevilsList = blueDevilsPlayers.joined (separator: "\n")
var cavaliersList  = cavaliersPlayers.joined (separator: "\n")
var crusadersList  = crusadersPlayers.joined (separator: "\n")

var dciLeague = ["BlueDevils": blueDevilsPlayers, "Cavaliers": cavaliersPlayers, "Crusaders": crusadersPlayers]

var dciLeagueList = ["BlueDevils": blueDevilsList, "Cavaliers": cavaliersList, "Crusaders": crusadersList]

print ("The dci league has the following players: \(dciLeague.values)\n")

for (key, value) in dciLeagueList {
     print ("\(key): \(value)", separator: "\n", terminator: "\n\n")
}

Let’s farewell the year 2020, and welcome the 2021!

Wishing everyone a safe and happy new year…

1 Like

Thank you very much Ibex! Have a happy new year!

Hello,
Here is my solution:
let footballTeam = [“Man U” : [“Ik”, “Karl”, “Dozie”, “Kachi”],
“Arsenal” : [“Swarez”, “Neymar”, “Messi”, “Pogba”],
“Man City” : [“Scholes”, “Rooney”, “Rashford”, “Benzema”]
]
for (key, value) in footballTeam {
print("(key): “)
for v in value {
print(v)
}
print(”")
}

Hello,
Here is my solution:
let footballTeam = [“Man U” : [“Ik”, “Karl”, “Dozie”, “Kachi”],
“Arsenal” : [“Swarez”, “Neymar”, “Messi”, “Pogba”],
“Man City” : [“Scholes”, “Rooney”, “Rashford”, “Benzema”]
]
for (key, value) in footballTeam {
print("(key): “)
for v in value {
print(v)
}
print(”")
}

I wound up with 2 carriage returns instead of one. What could I have done differently?

Code:

for roster in rosters {
    var team = ["\(roster.key) roster:"]
    
    let teamPlayers = rosters[roster.key] ?? ["No players"]
    
    for teamPlayer in teamPlayers {
        team.append(teamPlayer)
    }
    
    team.insert("\n", at: team.count)
    
    for member in team {
        print(member)
    }
}

Output:

New York roster:
Jane
Michaela
Rachel
Allysha
Janine


LA roster:
Kailen
McKenzie
Thaisa
Shea
Jen


Boston roster:
Sydney
Toni
Shelina
Emily
Chioma

team is an array of strings. So when you insert the new line character, you’re really inserting a string consisting of a single new line character. team winds up looking like (for the first team)

["New York Roster:", "Jane", "Michaela", "Rachel", "Allysha", "Janine", "\n"]

When you print out each string in the array, it automatically adds a new line character at the end of each printed string, including the string that contains the new line character, resulting in two blank lines.

Change that line to

team.insert("", at: team.count)

and you’ll get just one blank line in between the team rosters.

let Ligue_1 = [
                "PSG" : ["Messi", "Mbappé", "Neymar", "Ramos", "Icardi", "Navas"],
                "Marseille": ["Gerson", "Ünder", "Payet", "Harit", "Milik"],
                "Lille" : ["Yilmaz", "Sanches", "David", "Gomes", "Fonte"]
]

for (team, players) in Ligue_1 {
    print("\(team) members:",
          "\n\(players.joined(separator: "\n"))",
          terminator: "\n\n")
}