I found this assignment on C++ forum as was browsing to see little where I am in my mostly C little C++ thrown in understanding of C++ and if I should try to increase my knowledge in that language.
I could not believe my eyes! The OP is completely lost what to do and what approach to take and do not finish in time. Ok, we must consider that I have read this post for main points before but I do one experiment.
- I paste the description on this post and log the time
- I write the function for the checking part but by my way
- I adapt solution to do two other ways and menu for choices
Let's see how I do now is 21:50 GMT+3. I start. Description of assignment:
A lottery ticket buyer purchases 10 tickets a week. always playing the same 10 5-digit "lucky" combinations. Write a program that reads the numbers from an input file, initializes an array with these numbers and then lets the player enter this week's winning 5-digit numbers (Use random number generator to generate 5 numbers in the range 1 through 49 for the winning combination rather than letting the user enter the winning number combination).
The program should perform both, linear and binary search though the list of the player's numbers and report whether or not one of the tickets is a winner this week. You should present the user with the menu to find out if he/she is a winner through linear or binary search.
Here are the numbers:
1. I do not want to do the stupid way they suggest so I do it first the easy way.
- Preparing file
- read it in by splitting as list of sets
- generate correct row
- compare to the lucky numbers with set intersection
I'll keep the numbers as strings, just to do it 'difficult way' according to C++ thread.
My coding took 1,5 hours to do (with linear search working after 20 minutes), including cleaning up the code and basic testing of the final code (and getting up the children to school :) )
from __future__ import print_function try: input = raw_input except: pass # LOG: #start time 21:51 #22:11, finished for the day #restart time 6:11 #Finished 7:03 (including waking children) #TOTAL: 21:51 TO 22:11 = 20 MIN + 6:11 TO 7:03 = 52 MIN, final testing and clean up 18 MIN => 90 MINUTES # IT TOOK 1,5 HOURS TO PROGRAM import random import bisect FILE = 'lucky.txt' MAX_NUMBER, HOW_MANY = 49, 5 lottery = 1 def choose_winner(lottery): winner = random.sample(list(range(1, MAX_NUMBER + 1)), HOW_MANY) print('Week %i winner row is: %s' % (lottery, sorted(winner, key=int))) return winner binary_lucky = None with open(FILE) as number_file: lucky_numbers = [set(line.strip().split('-')) for line in number_file] print('Your lucky numbers:') for lucky in lucky_numbers: print(lucky) print() while True: search_type = input(''' Type B for binary search check, Q to quit, anything else gives you linear search.\n\t''').upper() if search_type and search_type in 'BQ': if search_type == 'Q': raise SystemExit('Bye, bye!') # binary search print('Binary search selected') if not binary_lucky: binary_lucky = sorted(sorted(int(number) for number in numbers) for numbers in lucky_numbers) print('Numbers as integers, sorted, for binary search.') for lucky in binary_lucky: print(lucky) print() winner = choose_winner(lottery) # winner = sorted(map(int,'1-15-17-23-32'.split('-'))) # debug: check that the winner finding works print('You got winner'.center(40,'*') if binary_lucky[bisect.bisect(binary_lucky, winner) - 1] == winner else 'No luck this time') else: winner = set(map(str, choose_winner(lottery))) #winner = set('1-15-17-23-32'.split('-')) # debug: check that the winner finding works, worked first time print('Best try had these correct:', max((box.intersection(winner) for box in lucky_numbers), key = len)) print('You got winner'.center(40,'*') if winner in lucky_numbers else 'No luck this time')