Skip to main content
added 4 characters in body
Source Link
RomanPerekhrest
  • 30.9k
  • 5
  • 47
  • 68

Python 3.x approach (just the alternative solution):

sum_digits.py script:

import sys
with open(sys.argv[1], 'r') as f:
    for l in f:
        print("sum", sum(int(d) for d in l if d.isdigit()))

Usage:

python3 sum_digits.py yourfile

The output:

sum 8
sum 15
sum 13

Python 3.x approach (just alternative solution):

sum_digits.py script:

import sys
with open(sys.argv[1], 'r') as f:
    for l in f:
        print("sum", sum(int(d) for d in l if d.isdigit()))

Usage:

python3 sum_digits.py yourfile

The output:

sum 8
sum 15
sum 13

Python 3.x approach (just the alternative solution):

sum_digits.py script:

import sys
with open(sys.argv[1], 'r') as f:
    for l in f:
        print("sum", sum(int(d) for d in l if d.isdigit()))

Usage:

python3 sum_digits.py yourfile

The output:

sum 8
sum 15
sum 13
Source Link
RomanPerekhrest
  • 30.9k
  • 5
  • 47
  • 68

Python 3.x approach (just alternative solution):

sum_digits.py script:

import sys
with open(sys.argv[1], 'r') as f:
    for l in f:
        print("sum", sum(int(d) for d in l if d.isdigit()))

Usage:

python3 sum_digits.py yourfile

The output:

sum 8
sum 15
sum 13