Lottery Scheduling is a probabilistic scheduling algorithm for processes in an operating system. Processes are each assigned some number of lottery tickets, and the scheduler draws a random ticket to select the next process. Contribute to TRiahi/AP-Computer-Science development by creating an account on GitHub. * A program to determine if a user has correctly chosen a randomly selected: three digit number. Println(' Winning Lottery Number: ' + randomLotteryNum). Lottery Scheduling is a probabilistic scheduling algorithm for processes in an operating system. Processes are each assigned some number of lottery tickets, and the scheduler draws a random ticket to select the next process.
$begingroup$I was trying to solve one of the hackerrank practice questions. Could anyone please let me know how can I make this code much effective, in terms of time and space complexity?
The Super Bowl Lottery is about to commence, and there are several lottery tickets being sold, and each ticket is identified with a ticket ID. In one of the many winning scenarios in the Superbowl lottery, a winning pair of tickets is:
- Concatenation of the two ticket IDs in the pair, in any order, contains each digit from 0 to 9 at least once.
For example, if there are 2 distinct tickets with ticket ID 12930455 and 56789, (129300455, 56789) is a winning pair.
NOTE: The ticket IDs can be concantenated in any order. Digits in the ticket ID can occur in any order.
Your task is to find the number of winning pairs of distinct tickets, such that concatenation of their ticket IDs (in any order) makes for a winning scenario. Complete the function winningLotteryTicket
which takes a string array of ticket IDs as input, and return the number of winning pairs.
Input Format
The first line contains n denoting the total number of lottery tickets in the Super Bowl. Each of the next n lines contains a string, where string on a ith line denotes the ticket id of the ith ticket.
Constraints
- [1 ≤ pretty much everything input ≤ 10⁶]
- Each ticket id consists of digits from [0, 9]
Output Format
Print the number of pairs in a new line.
Sample Input
Sample Output
greybeard2 Answers
$begingroup$Java Lottery Program Challenge
try-with-resources
Since Java 7, you can use try-with-resources
for safe and efficient handling of the underlying I/O resource.
return boolean
This kind of code can be simplified as return condition
.
Method names
Your naming can be better refined to reflect what they are doing. For example, getStatus
can be renamed as hasUniqueNumerals
, following the standard is/has
prefix for methods returning a boolean
. winningLotteryTicket
can be renamed as countWinningPairs
.
for-each
loop
Your loop on c.toCharArray()
can also be written as:
What's nice
You checked if the concatenation of the two inputs will give you 10 or more digits, returning
false
first if not.You declared
charSet
as aSet
rather than aHashSet
and relied on generic type inference.
Listless additions to h.j.k.'s answer:
Java Program For Matrix Multiplication
- no doc comments
- the name
getStatus()
is not hinting in a useful direction - the number of winning pairs may easily exceed
Integer.MAX_VALUE
(there may have been a reason forLong
)
code alternative for getStatus()
without concatenation, with one more early out:
In each iteration of winningLotteryTicket()
's outer loop, tickets[i]
stays the same for all the iterations of the inner loop, as does its contribution to the set.
If you didn't literally concatenate strings, it was apparent that the same sets were created for the 2nd ticket's digits time and again - and the sets 'checked for completeness' were the unions of the digit/char sets of tickets i and j:
It looks advantageous to create each ticket's set once and for all and think hard about what can be done to reduce the number of set unions to evaluate - if ticket i consists of 2 distinct digits, only, there's no need to pair with any ticket consisting of no more than 7 distinct digits.
If one ticket contains every digit, it is a winner paired with every other ticket …
If and when coding that proves not fast enough, note that the digit sets are quite small and reconsider their representation.
greybeardgreybeardNot the answer you're looking for? Browse other questions tagged javaalgorithmstatistics or ask your own question.
I'm in the process of writing a program that generates six random numbers. If I run the program I have so far, there's a bit of a problem. It is possible for this code to generate the same random number more than once. Generally speaking, lottery tickets require the numbers to be unique.
I know I need to modify Part1.java so that the generate method does not create duplicate numbers. I'm guessing it is easiest to get this right by adding additional static methods, rather than
putting all the code inside the existing loop? I'm really not so sure how I should go about doing this though. I've also attached the program for you guys to take a look at. I'd really appreciate any suggestions or feedback. Thank you!
- 3 Contributors
- forum2 Replies
- 225 Views
- 18 Hours Discussion Span
- commentLatest Postby javaAddict
As each number is generated, check the current numbers array to make sure it does not already contain that number, if it does, just generate another.