Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.2 kB
2
Indexable
Never
#part 1
def get_cal_value(seq):
    #remove letters
    clean = re.sub('\D', '', seq)
    return (int(clean[0]) * 10) + int(clean[-1])

sum([get_cal_value(i) for i in inputs])

#part 2
number_words = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]

valid_digits = [str(i) for i in list(range(1,10))] + number_words)

outputs = []

for starting_word in inputs:
    #find first digit
    for i in range(1, len(starting_word)+1):
        string_to_check = starting_word[:i]
        if any(digit in string_to_check for digit in valid_digits):
            for word, number in number_dict.items():
                string_to_check = string_to_check.replace(word, str(number))
            first_digit = re.sub('\D', '', string_to_check)
            break
    #find second digit
    for i in range(1, len(starting_word)+1):
        string_to_check = starting_word[-i:]
        if any(digit in string_to_check for digit in valid_digits):
            for word, number in number_dict.items():
                string_to_check = string_to_check.replace(word, str(number))
            second_digit = re.sub('\D', '', string_to_check)
            break
    outputs.append(int(first_digit) * 10 + int(second_digit))
Leave a Comment