Bird
0
0

Given a tuple array: [(name: String, score: Int)], how do you create a new array of just the scores using tuple access?

hard📝 Application Q9 of 15
Swift - Data Types
Given a tuple array: [(name: String, score: Int)], how do you create a new array of just the scores using tuple access?
Alet scores = players.map { $0[1] }
Blet scores = players.map { $0.score }
Clet scores = players.filter { $0.score }
Dlet scores = players.reduce(0) { $0.score + $1.score }
Step-by-Step Solution
Solution:
  1. Step 1: Understand tuple array and map

    players is an array of tuples with named elements. To get scores, map each tuple to its score.
  2. Step 2: Check each option

    let scores = players.map { $0.score } correctly maps to $0.score. let scores = players.map { $0[1] } tries index access (invalid for named tuples). let scores = players.filter { $0.score } uses filter incorrectly. let scores = players.reduce(0) { $0.score + $1.score } reduces to sum, not array.
  3. Final Answer:

    let scores = players.map { $0.score } -> Option B
  4. Quick Check:

    Use map with named tuple property to extract values [OK]
Quick Trick: Use map and dot notation to extract tuple elements [OK]
Common Mistakes:
  • Using index access on named tuples
  • Confusing filter with map
  • Using reduce when array needed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes