fork(1) download
  1. import re
  2.  
  3. def foo(s):
  4. nums = [int(x) for x in re.split(r'(\d)', s) if x.isdigit()]
  5. return nums[0]*10 + nums[-1]
  6.  
  7. def bar(s):
  8. words = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
  9. pattern = r'(\d|' + '|'.join(words) + ')'
  10. nums = [int(x) if x.isdigit() else words.index(x)
  11. for x in re.split(pattern, s) if x.isdigit() or x in words]
  12. return nums[0]*10 + nums[-1]
  13.  
  14. if __name__ == '__main__':
  15. print(foo('pqr3stu8vwx'))
  16. print(foo('8vwx'))
  17. print(bar('two1nine'))
  18. print(bar('alskdfsdlakjtwoalsdkjf'))
Success #stdin #stdout 0.03s 9920KB
stdin
Standard input is empty
stdout
38
88
29
22