Last active
November 21, 2020 04:27
-
-
Save Gerst20051/cf4202ec13199bb02ce1361e4c29e06e to your computer and use it in GitHub Desktop.
Max Points
This file contains 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
#!/bin/python3 | |
# | |
# Complete the 'maxPoints' function below. | |
# | |
# The function is expected to return a LONG_INTEGER. | |
# The function accepts INTEGER_ARRAY elements as parameter. | |
# | |
def maxPoints(elements): | |
possiblePoints = [] | |
possiblePoints.append(pointsSorting(elements[:], False, False)) | |
possiblePoints.append(pointsSorting(elements[:], True, False)) | |
possiblePoints.append(pointsSorting(elements[:], True, True)) | |
possiblePoints.append(pointsSortingLoop(elements[:], True, True)) | |
return max(possiblePoints) | |
def pointsSorting(elements, sort, reverse): | |
points = 0 | |
if sort: | |
elements.sort(reverse=reverse) | |
while len(elements): | |
maxElement = elements[0] | |
points += sum([e for e in elements if e == maxElement]) | |
elements = [e for e in elements if e not in [maxElement, maxElement - 1, maxElement + 1]] | |
return points | |
def pointsSortingLoop(elements, sort, reverse): | |
points = 0 | |
if sort: | |
elements.sort(reverse=reverse) | |
previousMaxElement = elements[0] | |
for element in elements: | |
if element == previousMaxElement: | |
points += element | |
if element < previousMaxElement - 1: | |
points += element | |
previousMaxElement = element | |
return points | |
if __name__ == '__main__': | |
print(maxPoints([3, 4, 2])) # 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment