Last active
March 16, 2017 08:29
-
-
Save tushar-mittal/064db1a2dfe1e6b4312a1f12b7dd22c5 to your computer and use it in GitHub Desktop.
checking for lottery tickets using the specified constraints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_lottery_suggestions_from_numbers(numbers, combinations_to_find): | |
#iterate over the numbers list | |
#find the eligible combinations | |
#print out the answer | |
print "Checking in input set :", numbers | |
for number in numbers: | |
#print "checking for ", number | |
combos = check_for_unique_combinations(int(number), combinations_to_find) | |
if combos: | |
print "FOUND Eligible Lottery Candidate >>>>>>>>>>", number, combos | |
def check_for_unique_combinations(input_number,combinations_to_find): | |
start_range = 1 | |
end_range = 59 | |
valid_combinations = [] | |
if combinations_to_find == 1: #break condition | |
if input_number >= start_range and input_number <=end_range:#imposing constraint | |
return [[input_number]] | |
else: | |
return [] #unique combinations don't exist.. | |
#empty array is indication that unique combinations not found that satisfy the constraint | |
else: | |
#break the digits one at a time OR two at a time and check in the reduced set recursively to find out the combinations | |
#try selecting the single digit first and breaking the remaining digits to check remaining combos | |
first_num = int(str(input_number)[0:1]) | |
if first_num >= start_range and first_num <=end_range and len(str(input_number)) > 1:##check for constraint and splittability | |
remaining_num = int(str(input_number)[1:]) | |
remaining_combinations = check_for_unique_combinations( remaining_num, combinations_to_find - 1) | |
for combination in remaining_combinations: | |
if combination and first_num not in combination: | |
combination.insert(0,first_num) | |
valid_combinations.append(combination) | |
#try now by selecting the first two digits as first combination | |
if len(str(input_number)) > 2: | |
first_num = int(str(input_number)[0:2]) | |
if first_num >= start_range and first_num <=end_range:#check for constraint and splittability | |
remaining_num = int(str(input_number)[2:]) | |
remaining_combinations_2 = check_for_unique_combinations( remaining_num, combinations_to_find - 1) | |
for combination in remaining_combinations_2: | |
if combination and first_num not in combination: | |
combination.insert(0,first_num) | |
valid_combinations.append(combination) | |
return valid_combinations | |
numbers = [ '5698157156','1', '42', '4938532894754', '1234567', '472844278465445', '76543212345'] | |
combinations_to_find = 7 | |
find_lottery_suggestions_from_numbers(numbers, combinations_to_find) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
change input at line 44 to play with a new input set