Skip to content

Commit 2d6ee02

Browse files
p178
1 parent 8bdfb39 commit 2d6ee02

File tree

5 files changed

+68
-7
lines changed

5 files changed

+68
-7
lines changed

AllQuestions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2827,7 +2827,7 @@ For example, given the linked list 7 -> 7 -> 3 -> 5 and k = 2, it should become
28272827
Given the linked list 1 -> 2 -> 3 -> 4 -> 5 and k = 3, it should become 3 -> 4
28282828
-> 5 -> 1 -> 2.
28292829

2830-
## Problem-178:waxing_crescent_moon:
2830+
## [Problem-178](src/main/java/in/ashwanik/dcp/problems/p151_180/p178):sunny:
28312831

28322832

28332833
> This problem was asked by Two Sigma.

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ Solutions to the coding problems from [Daily coding problem](https://dailycoding
8888
|[P28](src/main/java/in/ashwanik/dcp/problems/p1_30/p28)|[P95](src/main/java/in/ashwanik/dcp/problems/p91_120/p95)|
8989

9090

91+
## **Two Sigma (2)**
92+
| | |
93+
|--|--|
94+
|[P45](src/main/java/in/ashwanik/dcp/problems/p31_60/p45)|[P178](src/main/java/in/ashwanik/dcp/problems/p151_180/p178)|
95+
96+
9197
## **Square (2)**
9298
| | |
9399
|--|--|
@@ -112,12 +118,6 @@ Solutions to the coding problems from [Daily coding problem](https://dailycoding
112118
|[P34](src/main/java/in/ashwanik/dcp/problems/p31_60/p34)|
113119

114120

115-
## **Two Sigma (1)**
116-
| |
117-
|--|
118-
|[P45](src/main/java/in/ashwanik/dcp/problems/p31_60/p45)|
119-
120-
121121
## **Yelp (1)**
122122
| |
123123
|--|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Alice wants to join her school's Probability Student Club. Membership dues are
2+
computed via one of two simple probabilistic games.
3+
4+
The first game: roll a die repeatedly. Stop rolling once you get a five followed
5+
by a six. Your number of rolls is the amount you pay, in dollars.
6+
7+
The second game: same, except that the stopping condition is a five followed by
8+
a five.
9+
10+
Which of the two games should Alice elect to play? Does it even matter? Write a
11+
program to simulate the two games and calculate their expected value.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package in.ashwanik.dcp.problems.p151_180.p178;
2+
3+
import java.util.Random;
4+
5+
class Solution {
6+
private Random random;
7+
8+
Solution() {
9+
random = new Random();
10+
}
11+
12+
private int dice() {
13+
return random.nextInt(6) + 1;
14+
}
15+
16+
int numberOfDiceToWin(int a, int b) {
17+
int diceThrown = 1;
18+
int current = dice();
19+
while (true) {
20+
int next = dice();
21+
diceThrown++;
22+
if (current == a && next == b) {
23+
return diceThrown;
24+
}
25+
current = next;
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package in.ashwanik.dcp.problems.p151_180.p178;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
public class SolutionTest {
8+
private Solution solution = new Solution();
9+
10+
@Test
11+
void testDice() {
12+
assertTrue(count(5, 6) < count(5, 5));
13+
}
14+
15+
private int count(int a, int b) {
16+
int count = 0;
17+
for (int index = 0; index < 10000; index++) {
18+
count = solution.numberOfDiceToWin(a, b);
19+
}
20+
return count;
21+
}
22+
}

0 commit comments

Comments
 (0)