patternjavaMinor
Finding Lottery odds - Topcoder #268
Viewed 0 times
oddstopcoder268lotteryfinding
Problem
I have implemented the question #268 from Topcoder. The question is that there are 4 conditions for a lottery ticket:
The input is given as
For example, when the conditions are
one of the values possible is
Suppose these are the inputs: (in the format NAME: CHOICES BLANKS SORTED UNIQUE)
The number of ways in which these can happen is:
And the output is:
I would like to have some suggestions for:
Main Class
Lottery
```
public class Lottery {
public String[] sortByOdds(String[] rules) {
Rule[] rule = new Rule[rules.length];
for (int i = 0; i < rules.length; i++) {
rule[i] = parseStringAndCreateRule(rules[i]);
}
MergeSort.sort(rule);
// MergeSort is REQUIRED because we shouldn't change the order of Rules
// if 2 (or more) Rules have the same Odds. This is not guaranteed with
// Array.sort().
// Arrays.sort(rule);
String[] namesOfRules = new String[rule.length];
for (int i = 0; i < rule.length; i++) {
namesOfRules[i] = rule[i].name;
}
return namesOfRules;
}
private Rule parseStringAndCreateRule(String rule) {
StringTokenizer strTokenizer = new StringTokenizer(rul
- CHOICES - numbers available (10 ≤ CHOICES ≤ 100)
- BLANKS - number of numbers that can be chosen (1 ≤ BLANKS ≤ 8)
- SORTED - are the numbers sorted
- UNIQUE - are the numbers unique
The input is given as
(CHOICES, BLANKS, SORTED, UNIQUE)For example, when the conditions are
CHOICES = 15, BLANKS = 4, SORTED = FALSE and UNIQUE = TRUEone of the values possible is
{3, 7, 12, 14}Suppose these are the inputs: (in the format NAME: CHOICES BLANKS SORTED UNIQUE)
{"PICK ANY TWO: 10 2 F F",
"PICK TWO IN ORDER: 10 2 T F",
"PICK TWO DIFFERENT: 10 2 F T",
"PICK TWO LIMITED: 10 2 T T"}The number of ways in which these can happen is:
PICK ANY TWO - 100 i.e (10 * 10)
PICK TWO IN ORDER - 55
PICK TWO DIFFERENT - 90 i.e. (10 * 9) i.e. 10!/(10-2)!
PICK TWO LIMITED - 45 i.e. 10!/2!(10-2)!And the output is:
PICK TWO LIMITED
PICK TWO IN ORDER
PICK TWO DIFFERENT
PICK ANY TWOI would like to have some suggestions for:
- Code structure
- Test cases that could be added
- Errors that can be found
Main Class
Lottery
```
public class Lottery {
public String[] sortByOdds(String[] rules) {
Rule[] rule = new Rule[rules.length];
for (int i = 0; i < rules.length; i++) {
rule[i] = parseStringAndCreateRule(rules[i]);
}
MergeSort.sort(rule);
// MergeSort is REQUIRED because we shouldn't change the order of Rules
// if 2 (or more) Rules have the same Odds. This is not guaranteed with
// Array.sort().
// Arrays.sort(rule);
String[] namesOfRules = new String[rule.length];
for (int i = 0; i < rule.length; i++) {
namesOfRules[i] = rule[i].name;
}
return namesOfRules;
}
private Rule parseStringAndCreateRule(String rule) {
StringTokenizer strTokenizer = new StringTokenizer(rul
Solution
Your code is generally sound. I would make a few changes:
-
Oh, I guess only one change...
-
new BigInteger("1")BigInteger has a static final field , BigInteger.ONE, which represents the value one. I suggest you replace all your new BigInteger("1") with BigInteger.ONE as BigIntegers are immutable anyways and it saves you from creating another BigInteger.Oh, I guess only one change...
Context
StackExchange Code Review Q#78443, answer score: 2
Revisions (0)
No revisions yet.