Peter H
Peter H

Reputation: 11

How can I split an array into specified lines when printing?

New to software development, I'm working through Big Nerd Ranch's book 'Swift Programming'. One of the challenges set by the book is to print an array within a dictionary that contains 15 zip codes and to have them split across three lines in groups of five. The formatting must exactly match how it appears in the book.

I have tried to solve the issue using the information taught in the book with assistance from the developer documentation.

No dice.

This is what I need to print:

Caterham has the following zip codes: [111, 111, 111, 111, 111,
                                       222, 222, 222, 222, 222,
                                       333, 333, 333, 333, 333]

However this is the closest I can get...

var areaCodes = ["Caterham": [111, 111, 111, 111, 111], "Coulsdon": [222, 222, 222, 222, 222], "Purley": [333, 333, 333, 333, 333]]

var caterhamCodes : [Int] = []

for (_, values) in areaCodes {
    caterhamCodes += values
}

print("Caterham has the following codes: \(caterhamCodes[0...4]),")
print("                                  \(caterhamCodes[5...9]),")
print("                                  \(caterhamCodes[10...14]).")

which prints:

Caterham has the following postcodes: [111, 111, 111, 111, 111],
                                      [222, 222, 222, 222, 222],
                                      [333, 333, 333, 333, 333].

Essentially, I need what I have above minus some square brackets and without the punctuation after each time a block of zip codes is printed. The book specifies that I might need to use a parameter called terminator. It also mentions I might need to learn how to represent special characters in string literals.

I appreciate that this is probably a high level question with a simple answer, but I'm only a few weeks into learning, so I hope I can be forgiven for asking at least a few silly questions!

Upvotes: 0

Views: 94

Answers (2)

ielyamani
ielyamani

Reputation: 18591

Here is a way of getting the desired output :

let intro = "Caterham has the following zip codes: ["
let spaces = ",\n" + String(repeating: " ", count: intro.count)

let areaCodes = ["Caterham": [111, 111, 111, 111, 111], "Coulsdon": [222, 222, 222, 222, 222], "Purley": [333, 333, 333, 333, 333]]

let codes = areaCodes.values.sorted { $0[0] < $1[0] }

var output = intro +
    codes.map { $0.map{ String($0) }.joined(separator: ", ") }
    .joined(separator: spaces)
    + "]"

print(output)

Which prints

Caterham has the following zip codes: [111, 111, 111, 111, 111,
                                       222, 222, 222, 222, 222,
                                       333, 333, 333, 333, 333]

Upvotes: 0

haste
haste

Reputation: 342

Welcome to the developer community and have a nice stay! I am trying to put answer as simple as possible, so here is my attempt:

var areaCodes = ["Caterham": [111, 111, 111, 111, 111],
                 "Coulsdon": [222, 222, 222, 222, 222],
                 "Purley": [333, 333, 333, 333, 333]]
let codesArray = areaCodes.values.sorted(by: { $0[0] < $1[0] })
let caterhamString = "Caterham has the following zip codes: ["
var printString = caterhamString

for (index, area) in codesArray.enumerated() {
   for code in area {
      printString += "\(code), "
   }

   if index != codesArray.count - 1 {
      printString += "\n" + String(repeating: " ", count: caterhamString.count)
   }
}

print(printString.dropLast(2).appending("]"))

Hope this helps! Good luck!

Upvotes: 1

Related Questions