python/CodeChallenge_22.py

27 lines
739 B
Python

'''
Given a list of integers and a threshold integer, return a sum of numbers, where each number contributes 2 if it exceeds or equals
the threshold and 1 otherwise.
Example 1
Input:
[2, 8, 25, 18, 99, 11, 17, 16], 17
Output:
12
'''
from typing import List
class Solution(object):
def solve(self, nums:List[int], threshold:int) -> int:
print(f"nums: {nums}, threshold: {threshold}")
total = 0
for num in nums:
if num >= threshold:
total += 2
else:
total += 1
return total
s = Solution()
print(s.solve([2, 8, 25, 18, 99, 11, 17, 16], 17)) # Output: 12
print(s.solve([1, 2, 3, 4, 5], 3)) # Output: 8
print(s.solve([10, 20, 30], 15)) # Output: