Skip to content

Instantly share code, notes, and snippets.

@Valindo
Last active February 24, 2019 11:13
Show Gist options
  • Save Valindo/5b7a4ad84b6ce57c19afaf29f0ea2161 to your computer and use it in GitHub Desktop.
Save Valindo/5b7a4ad84b6ce57c19afaf29f0ea2161 to your computer and use it in GitHub Desktop.
Score items in an array based on presence in other arrays
#Declare Constants For Score
X_SCORE = 3
Y_SCORE = 2
Z_SCORE = 1
compare_list=[0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 14, 16, 17, 18, 19, 20, 21, 22]
x=[0, 1, 2, 3, 4, 18, 19, 20, 21, 22]
y=[2, 3, 4, 8, 14, 17, 20, 21]
z=[5, 1, 6, 3, 7, 10, 16, 21, 22, 2, 8]
#Function calculateScore accepts an item from the array for each iteration of the array
def calculateScore(item):
score = 0
if item in x:
score = X_SCORE
if item in y:
score = Y_SCORE
if item in z:
score = Z_SCORE
if item in x and item in y:
score = X_SCORE + Y_SCORE
if item in x and item in z:
score = X_SCORE + Z_SCORE
if item in y and item in z:
score = Y_SCORE + Z_SCORE
if item in y and item in x and item in z:
score = X_SCORE + Y_SCORE + Z_SCORE
return score
# map(functionToPerformAction, ArrayToPerformActionOn)
# This will return an array
final_score = map(calculateScore, compare_list)
print(list(final_score))
# Output
# [3, 4, 6, 6, 5, 1, 1, 1, 3, 1, 2, 1, 2, 3, 3, 5, 6, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment