Avatar of scottw

scottw's solution

to Kindergarten Garden in the Swift Track

Published at Apr 26 2020 · 1 comment
Test suite

Given a diagram, determine which plants each child in the kindergarten class is responsible for.

The kindergarten class is learning about growing plants. The teacher thought it would be a good idea to give them actual seeds, plant them in actual dirt, and grow actual plants.

They've chosen to grow grass, clover, radishes, and violets.

To this end, the children have put little cups along the window sills, and planted one type of plant in each cup, choosing randomly from the available types of seeds.

........................ # each dot represents a cup

There are 12 children in the class:

  • Alice, Bob, Charlie, David,
  • Eve, Fred, Ginny, Harriet,
  • Ileana, Joseph, Kincaid, and Larry.

Each child gets 4 cups, two on each row. Their teacher assigns cups to the children alphabetically by their names.

The following diagram represents Alice's plants:


In the first row, nearest the windows, she has a violet and a radish. In the second row she has a radish and some grass.

Your program will be given the plants from left-to-right starting with the row nearest the windows. From this, it should be able to determine which plants belong to each student.

For example, if it's told that the garden looks like so:


Then if asked for Alice's plants, it should provide:

  • Violets, radishes, violets, radishes

While asking for Bob's plants would yield:

  • Clover, grass, clover, clover


Go through the project setup instructions for Xcode using Swift:


Notably from the source directory:

swift test runs tests
swift package generate-xcodeproj creates an Xcode project


Random musings during airplane trip. http://jumpstartlab.com

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


import XCTest
@testable import KindergartenGarden

class KindergartenGardenTests: XCTestCase {
    private let disorderedGarden = Garden("VCRRGVRG\nRVGCCGCV", children: ["Samantha", "Patricia", "Xander", "Roger"])
    private static let diagram = "VCRRGVRG\nRVGCCGCV"
    private let garden1 = Garden(diagram, children: ["Alice", "Bob", "Charlie", "Dan"])
    private let garden2 = Garden(diagram, children: ["Bob", "Charlie", "Dan", "Erin"])

    func testAlicesGarden() {
        let garden = Garden("RC\nGG")
        XCTAssertEqual([.radishes, .clover, .grass, .grass], garden.plantsForChild("Alice"))

    func testDifferentGardenForAlice() {
        let garden = Garden("VC\nRC")
        XCTAssertEqual([.violets, .clover, .radishes, .clover], garden.plantsForChild("Alice"))

    func testBobsGarden() {
        let garden = Garden("VVCG\nVVRC")
        XCTAssertEqual([.clover, .grass, .radishes, .clover], garden.plantsForChild("Bob"))

    func testBobAndCharliesGardens() {
        let garden = Garden("VVCCGG\nVVCCGG")
        XCTAssertEqual([.clover, .clover, .clover, .clover], garden.plantsForChild("Bob"))
        XCTAssertEqual([.grass, .grass, .grass, .grass], garden.plantsForChild("Charlie"))

    // MARK: - Test full garden

    func testAlice() {
        XCTAssertEqual([.violets, .radishes, .violets, .radishes], fullGarden.plantsForChild("Alice"))

    func testBob() {
        XCTAssertEqual([.clover, .grass, .clover, .clover], fullGarden.plantsForChild("Bob"))

    func testCharlie() {
        XCTAssertEqual([.violets, .violets, .clover, .grass], fullGarden.plantsForChild("Charlie"))

    func testDavid() {
        XCTAssertEqual([.radishes, .violets, .clover, .radishes], fullGarden.plantsForChild("David"))

    func testEve() {
        XCTAssertEqual([.clover, .grass, .radishes, .grass], fullGarden.plantsForChild("Eve"))

    func testFred() {
        XCTAssertEqual([.grass, .clover, .violets, .clover], fullGarden.plantsForChild("Fred"))

    func testGinny() {
        XCTAssertEqual([.clover, .grass, .grass, .clover], fullGarden.plantsForChild("Ginny"))

    func testHarriet() {
        XCTAssertEqual([.violets, .radishes, .radishes, .violets], fullGarden.plantsForChild("Harriet"))

    func testIleana() {
        XCTAssertEqual([.grass, .clover, .violets, .clover], fullGarden.plantsForChild("Ileana"))

    func testJoseph() {
        XCTAssertEqual([.violets, .clover, .violets, .grass], fullGarden.plantsForChild("Joseph"))

    func testKincaid() {
        XCTAssertEqual([.grass, .clover, .clover, .grass], fullGarden.plantsForChild("Kincaid"))

    func testLarry() {
        XCTAssertEqual([.grass, .violets, .clover, .violets], fullGarden.plantsForChild("Larry"))

    // MARK: - Test disordered garden

    func testPatricia() {
        XCTAssertEqual([.violets, .clover, .radishes, .violets], disorderedGarden.plantsForChild("Patricia"))

    func testRoger() {
        XCTAssertEqual([.radishes, .radishes, .grass, .clover], disorderedGarden.plantsForChild("Roger"))

    func testSamantha() {
        XCTAssertEqual([.grass, .violets, .clover, .grass], disorderedGarden.plantsForChild("Samantha"))

    func testXander() {
        XCTAssertEqual([.radishes, .grass, .clover, .violets], disorderedGarden.plantsForChild("Xander"))

    // MARK: - Test two gardens, different students

    func testBobAndCharliePerGarden() {
        XCTAssertEqual([.radishes, .radishes, .grass, .clover], garden1.plantsForChild("Bob"))
        XCTAssertEqual([.violets, .clover, .radishes, .violets], garden2.plantsForChild("Bob"))
        XCTAssertEqual([.grass, .violets, .clover, .grass], garden1.plantsForChild("Charlie"))
        XCTAssertEqual([.radishes, .radishes, .grass, .clover], garden2.plantsForChild("Charlie"))

    static var allTests: [(String, (KindergartenGardenTests) -> () throws -> Void)] {
        return [
            ("testAlicesGarden", testAlicesGarden),
            ("testDifferentGardenForAlice", testDifferentGardenForAlice),
            ("testBobsGarden", testBobsGarden),
            ("testBobAndCharliesGardens", testBobAndCharliesGardens),
            ("testAlice", testAlice),
            ("testBob", testBob),
            ("testCharlie", testCharlie),
            ("testDavid", testDavid),
            ("testEve", testEve),
            ("testFred", testFred),
            ("testGinny", testGinny),
            ("testHarriet", testHarriet),
            ("testIleana", testIleana),
            ("testJoseph", testJoseph),
            ("testKincaid", testKincaid),
            ("testLarry", testLarry),
            ("testPatricia", testPatricia),
            ("testRoger", testRoger),
            ("testSamantha", testSamantha),
            ("testXander", testXander),
            ("testBobAndCharliePerGarden", testBobAndCharliePerGarden),


import XCTest
@testable import KindergartenGardenTests

struct Garden {
    enum Plants: CaseIterable {
        case clover, grass, radishes, violets

    static let plantTypes: [Character: Plants] = [
      "C": .clover,
      "G": .grass,
      "R": .radishes,
      "V": .violets,

    static let defaultChildren = ["Alice", "Bob", "Charlie", "David",
                                  "Eve", "Fred", "Ginny", "Harriet",
                                  "Ileana", "Joseph", "Kincaid", "Larry"]

    private var childPlot: [String: [Plants]] = [:]

    let plotWidth: Int = 2

    init(_ diagram: String, children: [String] = defaultChildren) {

        let allChildren = children.sorted()
        childPlot = allChildren.reduce(into: [:]) { plot, name in
            plot[name] = Array<Plants>()

        let gardenPlots = parseDiagram(diagram)

        for row in gardenPlots {
            var childIndex = 0

            for (n, plant) in row.enumerated() {  // plant is a char
                let currentChild = allChildren[childIndex]

                guard let plantType = Garden.plantTypes[plant] else {
                    print("Unknown plant type '\(plant)'")

                guard childPlot[currentChild] != nil else {
                    print("No plot for \(currentChild)")

                if n % plotWidth == plotWidth - 1 {
                    childIndex += 1

    func plantsForChild(_ child: String) -> [Plants] {
        guard let plot = childPlot[child] else {
            print("Unable to find plot for \(child)")
            return []
        return plot

    private func parseDiagram(_ diagram: String) -> [[Character]] {
        let splitRows = diagram.split(separator: "\n").map { String($0) } // ["AC","GV"]
        return splitRows.map { $0.map { $0 } } // [["A", "C"], ["G", "V"]]

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of scottw
Solution Author
commented 100 days ago

This took a lot more time than I thought it would.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?