Bläddra i källkod

kindergarten-garden: add to track

smarticles101 9 år sedan
förälder
incheckning
6d292a0786

+ 5
- 0
config.json Visa fil

334
       "slug": "run-length-encoding",
334
       "slug": "run-length-encoding",
335
       "difficulty": 1,
335
       "difficulty": 1,
336
       "topics": []
336
       "topics": []
337
+    },
338
+    {
339
+      "slug": "kindergarten-garden",
340
+      "difficulty": 1,
341
+      "topics": []
337
     }
342
     }
338
   ],
343
   ],
339
   "deprecated": [
344
   "deprecated": [

+ 17
- 0
exercises/kindergarten-garden/build.gradle Visa fil

1
+apply plugin: "java"
2
+apply plugin: "eclipse"
3
+apply plugin: "idea"
4
+
5
+repositories {
6
+    mavenCentral()
7
+}
8
+
9
+dependencies {
10
+    testCompile "junit:junit:4.12"
11
+}
12
+test {
13
+    testLogging {
14
+        exceptionFormat = 'full'
15
+        events = ["passed", "failed", "skipped"]
16
+    }
17
+}

+ 54
- 0
exercises/kindergarten-garden/src/example/java/KindergartenGarden.java Visa fil

1
+import java.util.Arrays;
2
+
3
+public class KindergartenGarden {
4
+	private String[] students;
5
+	private String garden;
6
+	final int PARTITION_SIZE_WIDTH = 2;
7
+	final static String[] DEFAULT_STUDENTS = {
8
+			"Alice", "Bob", "Charlie", "David",
9
+        		"Eve", "Fred", "Ginny", "Harriet",
10
+        		"Ileana", "Joseph", "Kincaid", "Larry"
11
+		};
12
+
13
+	public KindergartenGarden(String garden, String[] students) {
14
+		this.garden = garden;
15
+		Arrays.sort(students);
16
+		this.students = students;
17
+	}
18
+
19
+	public KindergartenGarden(String garden) {
20
+		this(garden, DEFAULT_STUDENTS);
21
+	}
22
+
23
+	public String[] getPlantsOfStudent(String student) {
24
+		String[] plants = new String[PARTITION_SIZE_WIDTH * 2];
25
+		int studentPlantsIndex  = Arrays.binarySearch(students, student) * PARTITION_SIZE_WIDTH;
26
+		
27
+		for(int i = 0; i<PARTITION_SIZE_WIDTH; i++) {
28
+			plants[i] = getPlantName(garden.charAt(studentPlantsIndex + i));
29
+		}
30
+		
31
+		int newRowLocation = garden.indexOf('\n');
32
+		
33
+		for(int i = PARTITION_SIZE_WIDTH; i < PARTITION_SIZE_WIDTH * 2; i++) {
34
+			plants[i] = getPlantName(garden.charAt(newRowLocation - 1 + studentPlantsIndex + i));
35
+		}
36
+		
37
+		return plants;
38
+	}
39
+
40
+	public String getPlantName(char plantCode) {
41
+		switch(plantCode) {
42
+			case 'G':
43
+				return "grass";
44
+			case 'C':
45
+				return "clover";
46
+			case 'R':
47
+				return "radishes";
48
+			case 'V':
49
+				return "violets";
50
+		}
51
+		
52
+		return null;
53
+	}
54
+}

+ 13
- 0
exercises/kindergarten-garden/src/main/java/KindergartenGarden.java Visa fil

1
+public class KindergartenGarden {
2
+    public KindergartenGarden(String garden) {
3
+        throw new UnsupportedOperationException("Method has not been implemented yet.");
4
+    }
5
+    
6
+    public KindergartenGarden(String garden, String[] students) {
7
+        throw new UnsupportedOperationException("Method has not been implemented yet.");
8
+    }
9
+    
10
+    public String[] getStudentPlants(String student) {
11
+        throw new UnsupportedOperationException("Method has not been implemented yet.");
12
+    }
13
+}

+ 152
- 0
exercises/kindergarten-garden/src/test/java/KindergartenGardenTest.java Visa fil

1
+import org.junit.Test;
2
+import org.junit.Ignore;
3
+
4
+import static org.junit.Assert.assertEquals;
5
+
6
+public class KindergartenGardenTest {
7
+	@Test
8
+	public void singleStudent() {
9
+		String student = "Alice";
10
+		String plants = "RC\nGG";
11
+		String[] expected = {"radishes", "clover", "grass", "grass"};
12
+
13
+		assertEquals(expected, new KindergartenGarden(plants)
14
+								.getPlantsOfStudent(student));
15
+	}
16
+
17
+	@Ignore("Remove to run test")
18
+	@Test
19
+	public void singleStudent2() {
20
+		String student = "Alice";
21
+        String plants = "VC\nRC";
22
+        String[] expected = {"violets", "clover", "radishes", "clover"};
23
+
24
+        assertEquals(expected, new KindergartenGarden(plants)
25
+        						.getPlantsOfStudent(student));
26
+	}
27
+
28
+	@Ignore("Remove to run test")
29
+	@Test
30
+	public void twoStudents() {
31
+		String student = "Bob";
32
+        String plants = "VVCG\nVVRC";
33
+        String[] expected = {"clover", "grass", "radishes", "clover"};
34
+
35
+        assertEquals(expected, new KindergartenGarden(plants)
36
+        						.getPlantsOfStudent(student));
37
+	}
38
+
39
+	@Ignore("Remove to run test")
40
+	@Test
41
+	public void oneGardenSecondStudent() {
42
+		String student = "Bob";
43
+        String plants = "VVCCGG\nVVCCGG";
44
+        String[] expected = {"clover", "clover", "clover", "clover"};
45
+
46
+        assertEquals(expected, new KindergartenGarden(plants)
47
+        						.getPlantsOfStudent(student));
48
+	}
49
+
50
+	@Ignore("Remove to run test")
51
+	@Test
52
+	public void oneGardenThirdStudent() {
53
+		String student = "Charlie";
54
+        String plants = "VVCCGG\nVVCCGG";
55
+        String[] expected = {"grass", "grass", "grass", "grass"};
56
+
57
+        assertEquals(expected, new KindergartenGarden(plants)
58
+        						.getPlantsOfStudent(student));
59
+	}
60
+	
61
+	@Ignore("Remove to run test")
62
+	@Test
63
+	public void fullGardenFirstStudent() {
64
+		String student = "Alice";
65
+        String plants = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV";
66
+        String[] expected = {"violets", "radishes", "violets", "radishes"};
67
+
68
+        assertEquals(expected, new KindergartenGarden(plants)
69
+        						.getPlantsOfStudent(student));
70
+	}
71
+	
72
+	@Ignore("Remove to run test")
73
+	@Test
74
+	public void fullGardenSecondStudent() {
75
+		String student = "Bob";
76
+        String plants = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV";
77
+        String[] expected = {"clover", "grass", "clover", "clover"};
78
+
79
+        assertEquals(expected, new KindergartenGarden(plants)
80
+        						.getPlantsOfStudent(student));
81
+	}
82
+	
83
+	@Ignore("Remove to run test")
84
+	@Test
85
+	public void fullGardenSecondToLastStudent() {
86
+		String student = "Kincaid";
87
+        String plants = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV";
88
+        String[] expected = {"grass", "clover", "clover", "grass"};
89
+
90
+        assertEquals(expected, new KindergartenGarden(plants)
91
+        						.getPlantsOfStudent(student));
92
+	}
93
+	
94
+	@Ignore("Remove to run test")
95
+	@Test
96
+	public void fullGardenLastStudent() {
97
+		String student = "Larry";
98
+        String plants = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV";
99
+        String[] expected = {"grass", "violets", "clover", "violets"};
100
+
101
+        assertEquals(expected, new KindergartenGarden(plants)
102
+        						.getPlantsOfStudent(student));
103
+	}
104
+	
105
+	@Ignore("Remove to run test")
106
+	@Test
107
+	public void customStudentGardenFirstStudent() {
108
+		String[] studentArray = {"Samantha", "Patricia", "Xander", "Roger"};
109
+		String student = "Patricia";
110
+        String plants = "VCRRGVRG\nRVGCCGCV";
111
+        String[] expected = {"violets", "clover", "radishes", "violets"};
112
+
113
+        assertEquals(expected, new KindergartenGarden(plants, studentArray)
114
+        						.getPlantsOfStudent(student));
115
+	}
116
+	
117
+	@Ignore("Remove to run test")
118
+	@Test
119
+	public void customStudentGardenSecondStudent() {
120
+		String[] studentArray = {"Samantha", "Patricia", "Xander", "Roger"};
121
+		String student = "Roger";
122
+        String plants = "VCRRGVRG\nRVGCCGCV";
123
+        String[] expected = {"radishes", "radishes", "grass", "clover"};
124
+
125
+        assertEquals(expected, new KindergartenGarden(plants, studentArray)
126
+        						.getPlantsOfStudent(student));
127
+	}
128
+	
129
+	@Ignore("Remove to run test")
130
+	@Test
131
+	public void customStudentGardenThirdStudent() {
132
+		String[] studentArray = {"Samantha", "Patricia", "Xander", "Roger"};
133
+		String student = "Samantha";
134
+        String plants = "VCRRGVRG\nRVGCCGCV";
135
+        String[] expected = {"grass", "violets", "clover", "grass"};
136
+
137
+        assertEquals(expected, new KindergartenGarden(plants, studentArray)
138
+        						.getPlantsOfStudent(student));
139
+	}
140
+	
141
+	@Ignore("Remove to run test")
142
+	@Test
143
+	public void customStudentGardenFourthStudent() {
144
+		String[] studentArray = {"Samantha", "Patricia", "Xander", "Roger"};
145
+		String student = "Xander";
146
+        String plants = "VCRRGVRG\nRVGCCGCV";
147
+        String[] expected = {"radishes", "grass", "clover", "violets"};
148
+
149
+        assertEquals(expected, new KindergartenGarden(plants, studentArray)
150
+        						.getPlantsOfStudent(student));
151
+	}
152
+}