Skip to content

Instantly share code, notes, and snippets.

@sharmaeklavya2
Created September 25, 2021 04:14
Show Gist options
  • Save sharmaeklavya2/2fa65ea7141c19d3aecbd078a28468ed to your computer and use it in GitHub Desktop.
Save sharmaeklavya2/2fa65ea7141c19d3aecbd078a28468ed to your computer and use it in GitHub Desktop.
Extract the longest numeric suffix from each line in stdin and output the sum.
#!/usr/bin/env python
"""Extract the longest numeric suffix from each line in stdin and output the sum."""
from __future__ import print_function
import sys
import argparse
numstr = '.0123456789'
def numSuffix(s):
for i in range(len(s) - 1, -1, -1):
if s[i] not in numstr:
return float(s[i + 1:] or 0.0)
return 0.0
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.parse_args()
lines = sys.stdin.readlines()
s = 0
for line in lines:
line = line.strip()
s += numSuffix(line)
print()
print(s)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment