Question

In: Computer Science

Write a program that checks if all the input numbers cover 1 to 99. Each ticket...

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

Solutions

Expert Solution

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.


Related Solutions

PYTHON Write a program that accepts a range of input from the user and checks whether...
PYTHON Write a program that accepts a range of input from the user and checks whether the input data is sorted or not. If the data series is already sorted your program should print “True” or should print “False” otherwise. You should not use any sort function for this program. Input: How many numbers you want to input: 3 # user input 3 Input the number: 5 Input the number: 2 Input the number: 7 Output: False
In Assembly Language Write a program that generates 10 random numbers (0~99). Save the numbers into...
In Assembly Language Write a program that generates 10 random numbers (0~99). Save the numbers into arrayInt and calculate the sum. .data arrayInt Byte 10 DUP(?) Displays the array and the sum as follows: The random numbers are: xx xx xx xx xx xx …. The sum is   xxxx
Write an application named OddNums that displays all the odd numbers from 1 through 99. All...
Write an application named OddNums that displays all the odd numbers from 1 through 99. All ODD numbers should show all together(Should not need to scroll down), do it in 10 rows. FOR C#
Write a program to convert the input numbers to another number system. 1. Decimal to Binary...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary 2. Binary to Decimal 3. Hexadecimal to Decimal 4. Decimal to Hexadecimal 5. Binary to Hexadecimal 6. Hexadecimal to Binary The user will type in the input number as following: Binary number : up to 8 bits Hexadecimal number: up to 2 bytes Decimal number: Less than 256 As a result, print out the output after the conversion with their input numbers. The program...
Write an X86-series assembly language program that checks whether input string is palindrome or not. A...
Write an X86-series assembly language program that checks whether input string is palindrome or not. A palindrome is a word, number, phrase or any other sequence which reads the same backward as forward e.g. madam, racecar. Sample Execution: Please enter a String: redivider The string is a palindrome Another Sample Execution: Please enter a String: abracadabra The string is not a palindrome
In Assembly Language MASM Write a program that generates 10 random numbers (0~99). Save the numbers...
In Assembly Language MASM Write a program that generates 10 random numbers (0~99). Save the numbers into arrayInt and calculate the sum. .data arrayInt Byte 10 DUP(?) Displays the array and the sum as follows: The random numbers are: xx xx xx xx xx xx …. The sum is   xxxx
Using the Python program: a/ Write a program that adds all numbers from 1 to 100...
Using the Python program: a/ Write a program that adds all numbers from 1 to 100 to a list (in order). Then remove the multiples of 3 from the list. Print the remaining values. b/ Write a program that initializes a list with ten random integers from 1 to 100. Then remove the multiples of 3 and 5 from the list. Print the remaining values. Please show your work and explain. Thanks
in Java, write a program that takes an input of 4 numbers and splits them into...
in Java, write a program that takes an input of 4 numbers and splits them into 4 separate lines. EXAMPLE: so if input is 1994 the output should be 1 9 9 4
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
I'm making a python program that checks the users input. If the users input does not...
I'm making a python program that checks the users input. If the users input does not match the rules then the program will output "No". If the users input does match the rules it will output "Yes". The rules are : at least 5 uppercase letters at least 5 lowercase letters at least 5 numbers No more than 20 characters in total I have managed to meet these conditions in individual python files but not in one. Ideally without importing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT