Просмотр исходного кода

new two-bucket exercise (#1315)

* new two-bucket exercise

* new two-bucket exercise

* update two-bucket exercise
jssander 8 лет назад
Родитель
Сommit
5fb5cedc8d

+ 13
- 0
config.json Просмотреть файл

614
     },
614
     },
615
     {
615
     {
616
       "core": false,
616
       "core": false,
617
+      "difficulty": 5,
618
+      "slug": "two-bucket",
619
+      "topics": [
620
+        "algorithms",
621
+        "loops",
622
+        "conditionals",
623
+        "mathematics"
624
+      ],
625
+      "unlocked_by": "triangle",
626
+      "uuid": "210bf628-b385-443b-8329-3483cc6e8d7e"
627
+    },
628
+    {
629
+      "core": false,
617
       "difficulty": 6,
630
       "difficulty": 6,
618
       "slug": "alphametics",
631
       "slug": "alphametics",
619
       "topics": [
632
       "topics": [

+ 1
- 0
exercises/settings.gradle Просмотреть файл

90
 include 'triangle'
90
 include 'triangle'
91
 include 'trinary'
91
 include 'trinary'
92
 include 'twelve-days'
92
 include 'twelve-days'
93
+include 'two-bucket'
93
 include 'two-fer'
94
 include 'two-fer'
94
 include 'word-count'
95
 include 'word-count'
95
 include 'word-search'
96
 include 'word-search'

+ 145
- 0
exercises/two-bucket/.meta/src/reference/java/TwoBucket.java Просмотреть файл

1
+import java.util.ArrayList;
2
+import java.util.Objects;
3
+
4
+class TwoBucket {
5
+	
6
+    private class State {
7
+    	int moves;
8
+    	int bucketOne;
9
+    	int bucketTwo;
10
+    	
11
+    	State (int moves, int bucketOne, int bucketTwo) {
12
+    		this.moves = moves;
13
+    		this.bucketOne = bucketOne;
14
+    		this.bucketTwo = bucketTwo;
15
+    	}
16
+    	
17
+    	@Override
18
+    	public boolean equals(Object o) {
19
+    		State otherState = (State) o;
20
+    		return this.moves == otherState.moves &&
21
+    				this.bucketOne == otherState.bucketOne &&
22
+    				this.bucketTwo == otherState.bucketTwo;
23
+    	}
24
+    	
25
+    	@Override
26
+    	public int hashCode() {
27
+    		return Objects.hash(moves, bucketOne, bucketTwo);
28
+    	}
29
+    }
30
+    
31
+    private State finalState;
32
+    
33
+    private int bucketOneCap;
34
+    private int bucketTwoCap;
35
+    private int desiredLiters;
36
+    private String startBucket;
37
+    
38
+    TwoBucket(int bucketOneCap, int bucketTwoCap, int desiredLiters, String startBucket) {
39
+    	this.bucketOneCap = bucketOneCap;
40
+    	this.bucketTwoCap = bucketTwoCap;
41
+    	this.desiredLiters = desiredLiters;
42
+    	this.startBucket = startBucket;
43
+    	
44
+    	finalState = computeFinalState();
45
+    }
46
+    
47
+    private ArrayList<State> getAdjacentStates (State state) {
48
+    	ArrayList<State> adjacentStates = new ArrayList<State>();
49
+    	
50
+    	//Empty bucket one
51
+    	adjacentStates.add(new State(state.moves + 1, 0, state.bucketTwo));
52
+    	
53
+    	//Empty bucket two
54
+    	adjacentStates.add(new State(state.moves + 1, state.bucketOne, 0));
55
+    	
56
+    	//Fill bucket one
57
+    	adjacentStates.add(new State(state.moves + 1, bucketOneCap, state.bucketTwo));
58
+    	
59
+    	//Fill bucket two
60
+    	adjacentStates.add(new State(state.moves + 1, state.bucketOne, bucketTwoCap));
61
+    	
62
+    	//pour from bucket one to bucket two
63
+    	if (state.bucketOne + state.bucketTwo > bucketTwoCap) {
64
+    		adjacentStates.add(new State(state.moves + 1, state.bucketOne - (bucketTwoCap - state.bucketTwo), bucketTwoCap));
65
+    	} else {
66
+    		adjacentStates.add(new State(state.moves + 1, 0, state.bucketOne + state.bucketTwo));
67
+    	}
68
+    	
69
+    	//pour from bucket two to bucket one
70
+    	if (state.bucketTwo + state.bucketOne > bucketOneCap) {
71
+    		adjacentStates.add(new State(state.moves + 1, bucketOneCap, state.bucketTwo - (bucketOneCap - state.bucketOne)));
72
+    	} else {
73
+    		adjacentStates.add(new State(state.moves + 1, state.bucketTwo + state.bucketOne, 0));
74
+    	}
75
+    	
76
+    	return adjacentStates;
77
+    }
78
+    
79
+    private boolean isValid(State state) {
80
+    	if (state.bucketOne == bucketOneCap && state.bucketTwo == 0 && startBucket.equals("two")) {
81
+    		return false;
82
+    	} else if (state.bucketOne == 0 && state.bucketTwo == bucketTwoCap && startBucket.equals("two")) {
83
+    		return false;
84
+    	} else {
85
+    		return true;
86
+    	}
87
+    }
88
+    
89
+    private State computeFinalState() {
90
+    	ArrayList<State> paths = new ArrayList<State>();
91
+    	
92
+    	State initialState;
93
+    	if (startBucket.equals("one")) {
94
+    		initialState = new State(1, bucketOneCap, 0);
95
+    	} else {
96
+    		initialState = new State(1, 0, bucketTwoCap);
97
+    	}
98
+    	
99
+    	if (initialState.bucketOne == desiredLiters || initialState.bucketTwo == desiredLiters) {
100
+    		return initialState;
101
+    	}
102
+    	
103
+    	paths.add(initialState);
104
+    	
105
+    	for (int i = 0; i < 10000; i++) {
106
+    		State currentState = paths.remove(0);
107
+    		ArrayList<State> adjacentStates = getAdjacentStates(currentState);
108
+    		for (State state : adjacentStates) {
109
+    			if (state.bucketOne == desiredLiters || state.bucketTwo == desiredLiters) {
110
+    				return state;
111
+    			}
112
+    			
113
+    			if (!paths.contains(state) && isValid(state)) {
114
+    				paths.add(state);
115
+    			}
116
+    		}
117
+    	}
118
+    	
119
+    	return null;
120
+    }
121
+    
122
+    int getTotalMoves() {
123
+    	return finalState.moves;
124
+    }
125
+    
126
+    String getFinalBucket() {
127
+    	if (finalState.bucketOne == desiredLiters) {
128
+    		return "one";
129
+    	} else if(finalState.bucketTwo == desiredLiters) {
130
+    		return "two";
131
+    	} else {
132
+    		return "No solution found in " + finalState.moves + " iterations!";
133
+    	}
134
+    }
135
+    
136
+    int getOtherBucket() {
137
+    	if (getFinalBucket().equals("one")) {
138
+    		return finalState.bucketTwo;
139
+    	} else if(getFinalBucket().equals("two")) {
140
+    		return finalState.bucketOne;
141
+    	} else {
142
+    		return -1;
143
+    	}
144
+    }
145
+}

+ 1
- 0
exercises/two-bucket/.meta/version Просмотреть файл

1
+1.4.0

+ 48
- 0
exercises/two-bucket/README.md Просмотреть файл

1
+# Two Bucket
2
+
3
+Given two buckets of different size, demonstrate how to measure an exact number of liters by strategically transferring liters of fluid between the buckets.
4
+
5
+Since this mathematical problem is fairly subject to interpretation / individual approach, the tests have been written specifically to expect one overarching solution.
6
+
7
+To help, the tests provide you with which bucket to fill first. That means, when starting with the larger bucket full, you are NOT allowed at any point to have the smaller bucket full and the larger bucket empty (aka, the opposite starting point); that would defeat the purpose of comparing both approaches!
8
+
9
+Your program will take as input:
10
+- the size of bucket one
11
+- the size of bucket two
12
+- the desired number of liters to reach
13
+- which bucket to fill first, either bucket one or bucket two
14
+
15
+Your program should determine:
16
+- the total number of "moves" it should take to reach the desired number of liters, including the first fill
17
+- which bucket should end up with the desired number of liters (let's say this is bucket A) - either bucket one or bucket two
18
+- how many liters are left in the other bucket (bucket B)
19
+
20
+Note: any time a change is made to either or both buckets counts as one (1) move.
21
+
22
+Example:
23
+Bucket one can hold up to 7 liters, and bucket two can hold up to 11 liters. Let's say bucket one, at a given step, is holding 7 liters, and bucket two is holding 8 liters (7,8). If you empty bucket one and make no change to bucket two, leaving you with 0 liters and 8 liters respectively (0,8), that counts as one "move". Instead, if you had poured from bucket one into bucket two until bucket two was full, leaving you with 4 liters in bucket one and 11 liters in bucket two (4,11), that would count as only one "move" as well.
24
+
25
+To conclude, the only valid moves are:
26
+- pouring from one bucket to another
27
+- emptying one bucket and doing nothing to the other
28
+- filling one bucket and doing nothing to the other
29
+
30
+Written with <3 at [Fullstack Academy](http://www.fullstackacademy.com/) by [Lindsay](http://lindsaylevine.com).
31
+
32
+# Running the tests
33
+
34
+You can run all the tests for an exercise by entering
35
+
36
+```sh
37
+$ gradle test
38
+```
39
+
40
+in your terminal.
41
+
42
+## Source
43
+
44
+Water Pouring Problem [http://demonstrations.wolfram.com/WaterPouringProblem/](http://demonstrations.wolfram.com/WaterPouringProblem/)
45
+
46
+## Submitting Incomplete Solutions
47
+
48
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.

+ 18
- 0
exercises/two-bucket/build.gradle Просмотреть файл

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
+
13
+test {
14
+    testLogging {
15
+        exceptionFormat = 'full'
16
+        events = ["passed", "failed", "skipped"]
17
+    }
18
+}

+ 0
- 0
exercises/two-bucket/src/main/java/.keep Просмотреть файл


+ 77
- 0
exercises/two-bucket/src/test/java/TwoBucketTest.java Просмотреть файл

1
+import org.junit.Ignore;
2
+import org.junit.Test;
3
+import static org.junit.Assert.assertEquals;
4
+
5
+public class TwoBucketTest {
6
+	
7
+	@Test
8
+	public void testBucketOneSizeThreeBucketTwoSizeFiveStartWithOne() {
9
+		
10
+		TwoBucket twoBucket = new TwoBucket(3, 5, 1, "one");
11
+		
12
+		assertEquals(4, twoBucket.getTotalMoves());
13
+		assertEquals("one", twoBucket.getFinalBucket());
14
+		assertEquals(5, twoBucket.getOtherBucket());
15
+		
16
+	}
17
+	
18
+	@Ignore("Remove to run test")
19
+	@Test
20
+	public void testBucketOneSizeThreeBucketTwoSizeFiveStartWithTwo() {
21
+		
22
+		TwoBucket twoBucket = new TwoBucket(3, 5, 1, "two");
23
+		
24
+		assertEquals(8, twoBucket.getTotalMoves());
25
+		assertEquals("two", twoBucket.getFinalBucket());
26
+		assertEquals(3, twoBucket.getOtherBucket());
27
+		
28
+	}
29
+	
30
+	@Ignore("Remove to run test")
31
+	@Test
32
+	public void testBucketOneSizeSevenBucketTwoSizeElevenStartWithOne() {
33
+		
34
+		TwoBucket twoBucket = new TwoBucket(7, 11, 2, "one");
35
+		
36
+		assertEquals(14, twoBucket.getTotalMoves());
37
+		assertEquals("one", twoBucket.getFinalBucket());
38
+		assertEquals(11, twoBucket.getOtherBucket());
39
+		
40
+	}
41
+	
42
+	@Ignore("Remove to run test")
43
+	@Test
44
+	public void testBucketOneSizeSevenBucketTwoSizeElevenStartWithTwo() {
45
+		
46
+		TwoBucket twoBucket = new TwoBucket(7, 11, 2, "two");
47
+		
48
+		assertEquals(18, twoBucket.getTotalMoves());
49
+		assertEquals("two", twoBucket.getFinalBucket());
50
+		assertEquals(7, twoBucket.getOtherBucket());
51
+		
52
+	}
53
+	
54
+	@Ignore("Remove to run test")
55
+	@Test
56
+	public void testBucketOneSizeOneBucketTwoSizeThreeStartWithTwo() {
57
+		
58
+		TwoBucket twoBucket = new TwoBucket(1, 3, 3, "two");
59
+		
60
+		assertEquals(1, twoBucket.getTotalMoves());
61
+		assertEquals("two", twoBucket.getFinalBucket());
62
+		assertEquals(0, twoBucket.getOtherBucket());
63
+		
64
+	}
65
+	
66
+	@Ignore("Remove to run test")
67
+	@Test
68
+	public void testBucketOneSizeTwoBucketTwoSizeThreeStartWithOne() {
69
+		
70
+		TwoBucket twoBucket = new TwoBucket(2, 3, 3, "one");
71
+		
72
+		assertEquals(2, twoBucket.getTotalMoves());
73
+		assertEquals("two", twoBucket.getFinalBucket());
74
+		assertEquals(2, twoBucket.getOtherBucket());
75
+		
76
+	}
77
+}