# HanaisMe's solution

## to Kindergarten Garden in the Swift Track

Published at Jun 28 2020 · 0 comments
Instructions
Test suite
Solution

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.

``````[window][window][window]
........................ # 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:

``````[window][window][window]
VR......................
RG......................
``````

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:

``````[window][window][window]
VRCGVVRVCGGCCGVRGCVCGCGV
VRCCCGCRRGVCGCRVVCVGCGCV
``````

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

While asking for Bob's plants would yield:

• Clover, grass, clover, clover

## Setup

Go through the project setup instructions for Xcode using Swift:

http://exercism.io/languages/swift
http://exercism.io/languages/swift/tests

Notably from the source directory:

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

## Source

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.

### KindergartenGardenTests.swift

``````import XCTest
@testable import KindergartenGarden

class KindergartenGardenTests: XCTestCase {
private let fullGarden = Garden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV")
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")
}

func testDifferentGardenForAlice() {
let garden = Garden("VC\nRC")
}

func testBobsGarden() {
let garden = Garden("VVCG\nVVRC")
}

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() {
}

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

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

func testDavid() {
}

func testEve() {
}

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

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

func testHarriet() {
}

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() {
}

func testRoger() {
}

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

func testXander() {
}

// MARK: - Test two gardens, different students

func testBobAndCharliePerGarden() {
XCTAssertEqual([.grass, .violets, .clover, .grass], garden1.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),
]
}
}``````

### LinuxMain.swift

``````import XCTest
@testable import KindergartenGardenTests

XCTMain([
testCase(KindergartenGardenTests.allTests),
])``````
``````// MARK: - Plant

enum Plant: String {
case clover   = "C"
case grass    = "G"
case violets  = "V"
}

// MARK: - Children

enum Children: String, CaseIterable {
case alice   = "Alice"
case bob     = "Bob"
case charlie = "Charlie"
case david   = "David"
case eve     = "Eve"
case fred    = "Fred"
case ginny   = "Ginny"
case harriet = "Harriet"
case ileana  = "Ileana"
case joseph  = "Joseph"
case kincaid = "Kincaid"
case larry   = "Larry"
}

extension Children {

static func getAll() -> [String] {
return Children.allCases.map{ \$0.rawValue }
}
}

// MARK: - Garden

struct Garden {

let gardens: [[Plant]]
let children: [String]

init(_ diagram: String, children: [String] = Children.getAll()) {
self.gardens = diagram.split{ \$0 == "\n" }.map{
Array(\$0).compactMap{ Plant.init(rawValue:String(\$0)) }
}
self.children = children.sorted()
}

func plantsForChild(_ name: String) -> [Plant] {
if let index = self.children.index(of: name) {
let lowerBound = index * 2
let upperBound = index * 2 + 2
let plants = gardens[0][lowerBound..<upperBound] + gardens[1][lowerBound..<upperBound]
return Array(plants)
}
return [Plant]()
}
}``````