Simple Script to Compute All Possible Ways to score a given total in football
Possible Football Scoring Scenarios
var SCORES = [2, 3, 6, 7, 8]; | |
var SCORE_MAPPING = { | |
2: 'Safety', | |
3: 'Field Goal', | |
6: 'TD with missed PAT', | |
7: 'TD', | |
8: 'TD with 2P conversion', | |
}; | |
function getCombos(total, vals) { | |
if (vals.length === 0) { | |
if (total === 0) { | |
return [[]]; | |
} else { | |
return []; | |
} | |
} | |
var result = []; | |
var currVal = vals[0]; | |
var remainingVals = vals.slice(1); | |
var currMultiple = 0; | |
var currMultiplier = 0; | |
var subResult, i, subVal; | |
while (currMultiple <= total) { | |
subResult = getCombos(total - currMultiple, remainingVals); | |
for (i = 0; i < subResult.length; i++) { | |
subVal = subResult[i]; | |
result.push([currMultiplier].concat(subVal)); | |
} | |
currMultiple += currVal; | |
currMultiplier += 1; | |
} | |
return result; | |
} | |
function getScoringPlays(score) { | |
var result = []; | |
var allCombos = getCombos(score, SCORES); | |
var i, currInfo, j, numScores, scoringPlay, scoreDescription; | |
for (i = 0; i < allCombos.length; i++) { | |
currInfo = []; | |
for (j = 0; j < SCORES.length; j++) { | |
numScores = allCombos[i][j]; | |
scoringPlay = SCORES[j]; | |
scoreDescription = SCORE_MAPPING[scoringPlay]; | |
if (numScores !== 0) { | |
currInfo.push(numScores.toString() + ' x ' + scoreDescription); | |
} | |
} | |
result.push(currInfo.join(', ')); | |
} | |
return result; | |
} |
#!/usr/bin/env python | |
import argparse | |
SCORES = (2, 3, 6, 7, 8) | |
SCORE_MAPPING = { | |
2: 'Safety', | |
3: 'Field Goal', | |
6: 'TD with missed PAT', | |
7: 'TD', | |
8: 'TD with 2P conversion', | |
} | |
def get_combos(total, vals): | |
if len(vals) == 0: | |
if total == 0: | |
return [[]] | |
else: | |
return [] | |
result = [] | |
curr_val = vals[0] | |
remaining_vals = vals[1:] | |
curr_multiple = 0 | |
curr_multiplier = 0 | |
while curr_multiple <= total: | |
for sub_val in get_combos(total - curr_multiple, remaining_vals): | |
result.append([curr_multiplier] + sub_val) | |
curr_multiple += curr_val | |
curr_multiplier += 1 | |
return result | |
def get_scoring_plays(score): | |
for combo in get_combos(score, SCORES): | |
curr_info = [] | |
for num_scores, scoring_play in zip(combo, SCORES): | |
score_description = SCORE_MAPPING[scoring_play] | |
if num_scores != 0: | |
curr_info.append('%d x %s' % (num_scores, | |
score_description)) | |
yield ', '.join(curr_info) | |
def main(score): | |
for play in get_scoring_plays(score): | |
print '- %s' % (play,) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser( | |
description='Get ways to attain a given score in football.') | |
parser.add_argument('--score', type=int, required=True, | |
help='Score for a single team') | |
args = parser.parse_args() | |
main(args.score) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment