Skip to content

Instantly share code, notes, and snippets.

@yixizhang
Last active October 12, 2015 20:48
Show Gist options
  • Save yixizhang/4084787 to your computer and use it in GitHub Desktop.
Save yixizhang/4084787 to your computer and use it in GitHub Desktop.
sort strings by numeric char number
#!/usr/bin/env python -*- coding: utf-8 -*-
import re
import unittest
def least_numeric(strings):
return min(strings, key=lambda s: len(re.findall(r'\d', s)))
class TestLeastNumeric(unittest.TestCase):
def test1(self):
l1 = ['123', 'hello1234world', 'bang1']
self.assertEqual(least_numeric(l1), 'bang1')
def test2(self):
l2 = ['1', 'bang!', 'helloworld33']
self.assertEqual(least_numeric(l2), 'bang!')
def test3(self):
l3 = ['zang1', 'o 2', 'woo3']
self.assertEqual(least_numeric(l3), 'zang1')
def test4(self):
l4 = ['fo-o', 'b', 'woolala']
self.assertEqual(least_numeric(l4), 'fo-o')
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment