Skip to content

Instantly share code, notes, and snippets.

@tyler-austin
Created June 21, 2017 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyler-austin/07d841e076492592616022b25d96c677 to your computer and use it in GitHub Desktop.
Save tyler-austin/07d841e076492592616022b25d96c677 to your computer and use it in GitHub Desktop.
Code Fights - firstDigit

Find the leftmost digit that occurs in a given string.

Example

  • For inputString = "var_1__Int", the output should be
    firstDigit(inputString) = '1';
  • For inputString = "q2q-q", the output should be
    firstDigit(inputString) = '2';
  • For inputString = "0ss", the output should be
    firstDigit(inputString) = '0'.

Input/Output

  • [time limit] 4000ms (py3)

  • [input] string inputString

    A string containing at least one digit.

    Guaranteed constraints:

     3 ≤ inputString.length ≤ 10.
    
  • [output] char

import re
def first_digit(input_string: str):
return re.search(r'\d', input_string).group()
import unittest
from first_digit import first_digit
class TestFirstDigit(unittest.TestCase):
def test_1(self):
input_string = 'var_1__Int'
solution = '1'
result = first_digit(input_string)
self.assertEqual(result, solution)
def test_2(self):
input_string = 'q2q-q'
solution = '2'
result = first_digit(input_string)
self.assertEqual(result, solution)
def test_3(self):
input_string = '0ss'
solution = '0'
result = first_digit(input_string)
self.assertEqual(result, solution)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment