w3resource

Python: Find the sum of the numbers among the first k with more than 2 digits


Sum of Numbers with 2+ Digits

Write a Python program to find the sum of the numbers in a given list among the first k with more than 2 digits.

Input:  
[4, 5, 17, 9, 14, 108, -9, 12, 76]
Value of K: 4
Output:
0

Input:  
[4, 5, 17, 9, 14, 108, -9, 12, 76]
Value of K: 6
Output:
108

Input:  
[114, 215, -117, 119, 14, 108, -9, 12, 76]
Value of K: 5
Output:
331

Input:  
[114, 215, -117, 119, 14, 108, -9, 12, 76]
Value of K: 1
Output:
114

Visual Presentation:

Python: Find the sum of the numbers among the first k with more than 2 digits.
Python: Find the sum of the numbers among the first k with more than 2 digits.

Sample Solution-1:

Python Code:

# Define a function named 'test' that takes a list of numbers 'nums' and an integer 'k' as input
def test(nums, k):
    # Initialize a variable 's' to store the sum of numbers meeting the specified conditions
    s = 0
    # Iterate through the first 'k' elements in 'nums'
    for i in range(len(nums))[:k]:
        # Check if the absolute value of the current number has more than 2 digits
        if len(str(abs(nums[i]))) > 2:
            # Add the current number to the sum 's'
            s = s + nums[i]
    # Return the final sum 's'
    return s

# Assign a specific list of numbers 'nums' to the variable
nums = [4, 5, 17, 9, 14, 108, -9, 12, 76]

# Print a message indicating the operation to be performed
print("Original list:", nums)

# Assign a specific value 'K' to the variable
K = 4

# Print a message indicating the value of 'K'
print("Value of K:", K)

# Print a message indicating the operation to be performed
print("Sum of the numbers among the first k with more than 2 digits:")
# Print the result of the test function applied to 'nums' and 'K'
print(test(nums, K))

# Assign another specific value 'K' to the variable
K = 6

# Print a message indicating the value of 'K'
print("\nOriginal list:", nums)
print("Value of K:", K)

# Print a message indicating the operation to be performed
print("Sum of the numbers among the first k with more than 2 digits:")
# Print the result of the test function applied to 'nums' and 'K'
print(test(nums, K))

# Assign another specific list of numbers 'nums' to the variable
nums = [114, 215, -117, 119, 14, 108, -9, 12, 76]

# Print a message indicating the operation to be performed
print("\nOriginal list:", nums)

# Assign another specific value 'K' to the variable
K = 5

# Print a message indicating the value of 'K'
print("Value of K:", K)

# Print a message indicating the operation to be performed
print("Sum of the numbers among the first k with more than 2 digits:")
# Print the result of the test function applied to 'nums' and 'K'
print(test(nums, K))

# Print an additional message indicating the original list
print("\nOriginal list:", nums)

# Assign another specific value 'K' to the variable
K = 1

# Print a message indicating the value of 'K'
print("Value of K:", K)

# Print a message indicating the operation to be performed
print("Sum of the numbers among the first k with more than 2 digits:")
# Print the result of the test function applied to 'nums' and 'K'
print(test(nums, K))

Sample Output:

Original list: [4, 5, 17, 9, 14, 108, -9, 12, 76]
Value of K: 4
sum of the numbers among the first k with more than 2 digits
0

Original list: [4, 5, 17, 9, 14, 108, -9, 12, 76]
Value of K: 6
sum of the numbers among the first k with more than 2 digits
108

Original list: [114, 215, -117, 119, 14, 108, -9, 12, 76]
Value of K: 5
sum of the numbers among the first k with more than 2 digits
331

Original list: [114, 215, -117, 119, 14, 108, -9, 12, 76]
Value of K: 1
sum of the numbers among the first k with more than 2 digits
114

Flowchart:

Flowchart: Python - Find the sum of the numbers among the first k with more than 2 digits.

Sample Solution-2:

Python Code:

#License: https://bit.ly/3oLErEI

# Define a function named 'test' that takes a list of numbers 'nums' and an integer 'k' as input
def test(nums, k):
    # Use a generator expression to sum numbers meeting the specified conditions in the first 'k' elements of 'nums'
    return sum(n for n in nums[:k] if len(str(abs(n))) > 2)

# Assign a specific list of numbers 'nums' to the variable
nums = [4, 5, 17, 9, 14, 108, -9, 12, 76]

# Print a message indicating the operation to be performed
print("Original list:", nums)

# Assign a specific value 'K' to the variable
K = 4

# Print a message indicating the value of 'K'
print("Value of K:", K)

# Print a message indicating the operation to be performed
print("Sum of the numbers among the first k with more than 2 digits:")
# Print the result of the test function applied to 'nums' and 'K'
print(test(nums, K))

# Assign another specific value 'K' to the variable
K = 6

# Print a message indicating the value of 'K'
print("\nOriginal list:", nums)
print("Value of K:", K)

# Print a message indicating the operation to be performed
print("Sum of the numbers among the first k with more than 2 digits:")
# Print the result of the test function applied to 'nums' and 'K'
print(test(nums, K))

# Assign another specific list of numbers 'nums' to the variable
nums = [114, 215, -117, 119, 14, 108, -9, 12, 76]

# Print a message indicating the operation to be performed
print("\nOriginal list:", nums)

# Assign another specific value 'K' to the variable
K = 5

# Print a message indicating the value of 'K'
print("Value of K:", K)

# Print a message indicating the operation to be performed
print("Sum of the numbers among the first k with more than 2 digits:")
# Print the result of the test function applied to 'nums' and 'K'
print(test(nums, K))

# Print an additional message indicating the original list
print("\nOriginal list:", nums)

# Assign another specific value 'K' to the variable
K = 1

# Print a message indicating the value of 'K'
print("Value of K:", K)

# Print a message indicating the operation to be performed
print("Sum of the numbers among the first k with more than 2 digits:")
# Print the result of the test function applied to 'nums' and 'K'
print(test(nums, K))

Sample Output:

Original list: [4, 5, 17, 9, 14, 108, -9, 12, 76]
Value of K: 4
sum of the numbers among the first k with more than 2 digits
0

Original list: [4, 5, 17, 9, 14, 108, -9, 12, 76]
Value of K: 6
sum of the numbers among the first k with more than 2 digits
108

Original list: [114, 215, -117, 119, 14, 108, -9, 12, 76]
Value of K: 5
sum of the numbers among the first k with more than 2 digits
331

Original list: [114, 215, -117, 119, 14, 108, -9, 12, 76]
Value of K: 1
sum of the numbers among the first k with more than 2 digits
114

Flowchart:

Flowchart: Python - Find the sum of the numbers among the first k with more than 2 digits.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Find the positions of all uppercase vowels (not counting Y) in even indices.
Next: Product of the odd digits in n, or 0 if there aren't any.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.