Created
September 25, 2021 04:14
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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