In: Computer Science
Write a program that checks if all the input numbers cover 1 to 99. Each ticket for the Pick-10 lotto has 10 unique numbers ranging from 1 to 99. Suppose you buy a lot of tickets and like to have them cover all numbers from 1 to 99. Write a program that reads the ticket numbers from a file and checks whether all numbers are covered. Assume the last ending number in the file is 0. Suppose the file contains the numbers
80 3 87 62 30 90 10 21 46 27
12 40 83 9 39 88 95 59 20 37
80 40 87 67 31 90 11 24 56 77
11 48 51 42 8 74 1 41 36 53
52 82 16 72 19 70 44 56 29 33
54 64 99 14 23 22 94 79 55 2
60 86 34 4 31 63 84 89 7 78
43 93 97 45 25 38 28 26 85 49
47 65 57 67 73 69 32 71 24 66
92 98 96 77 6 75 17 61 58 13
35 81 18 15 5 68 91 50 76 0
Your program should display
The tickets cover all numbers
Suppose the file contains the numbers
11 48 51 42 8 74 1 41 36 53
52 82 16 72 19 70 44 56 29 33
0
Your program should display
The tickets don't cover all numbers
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;
public class Main {
public static void main(String args[]) throws Exception {
// scanner class to read file
Scanner sc = new Scanner(new BufferedReader(new FileReader("lotto.txt")));
// declare a hash array which track all the numbers from 1 to 99
// declare pther variables
int h[]=new int[99],sum=0;
int t=0,i;
// populate h with 1 to 99
for(i=0;i<99;i++)
h[i]=i+1;
//read each line
while(sc.hasNextLine()){
// store each word of a line
String[] line = sc.nextLine().trim().split(" ");
// check each word
for(i=0; i<line.length; i++){
//convert to int
t=Integer.parseInt(line[i]);
// if 0 is detected then end of numbers in file
if(t==0)
break;
// discard number
h[t-1]=0;
}
if(t==0)
break;
}
// check if all the element has been discarded or not
t=0;
for(i=0;i<99;i++){
if(h[i]>0){
System.out.print("The tickets don't cover all numbers");
t=1;
break;
}
}
if(t==0)
System.out.print("The tickets cover all numbers");
}
}
___________________________________________________________________
# function to if file contains 1 to 99
def check_numbers(file):
# declare the variables
Line = []
# open the file
f=open(file,"r")
# create a hash array to track numbers from 1 to 99
h=[]
for i in range(1, 100):
h.append(i)
# iterate over each line of file
flag=0
for line in f:
# copy current line
Line=line
# extract numbers from Line
numbers = [int(num) for num in Line.split() if num.isdigit()]
# discard all detected numbers from hash array
# make them 0
for i in range(len(numbers)):
# if 0 is detected then end of numbers in file
if numbers[i]==0:
break
h[numbers[i]-1]=0
# if all num bers from 1 to 99 detected
if sum(h)==0:
return True
else:
return False
# main function
def main():
# declare filename
filename="lotto.txt"
# call function and get return value
t=check_numbers(filename)
# conclusion
if t==True:
print("The tickets cover all numbers")
else:
print("The tickets don't cover all numbers")
# driver code
if __name__ == "__main__":
main()
___________________________________________________________________
80 3 87 62 30 90 10 21 46 27
12 40 83 9 39 88 95 59 20 37
80 40 87 67 31 90 11 24 56 77
11 48 51 42 8 74 1 41 36 53
52 82 16 72 19 70 44 56 29 33
54 64 99 14 23 22 94 79 55 2
60 86 34 4 31 63 84 89 7 78
43 93 97 45 25 38 28 26 85 49
47 65 57 67 73 69 32 71 24 66
92 98 96 77 6 75 17 61 58 13
35 81 18 15 5 68 91 50 76 0
___________________________________________________________________
The tickets cover all numbers
___________________________________________________________________
Note: If you have
queries or confusion regarding this question, please leave a
comment. I would be happy to help you. If you find it to be useful,
please upvote.