- 1

Can you solve this?

Return the sum of the numbers in the array, returning 0 for an empty array. Except the number 13 is very unlucky, so it does not count and numbers that come immediately after a 13 also do not count. sum13([1, 2, 2, 1]) → 6 sum13([1, 1]) → 2 sum13([1, 2, 2, 1, 13]) → 6

1st Nov 2020, 5:58 AM
roshan roy
1 Antwort
0
Here is a solution in Python that implements the given requirements: def sum13(nums): sum = 0 skip = False for i in range(len(nums)): if nums[i] == 13: skip = True elif not skip: sum += nums[i] else: skip = False return sum This solution uses a loop to iterate through the nums array. If the current number is 13, a flag skip is set to True to indicate that the next number should be skipped. If skip is False, the current number is added to the sum. After processing each number, skip is reset to False. The function returns the sum as the result.
6th Feb 2023, 12:02 PM
Manhar Hadiyal
Manhar Hadiyal - avatar