7-9 string into a decimal integer (15 minutes)

7-9 string into a decimal integer (15 minutes)

After entering a string of # end, this question requires filter out all non-hexadecimal characters (case insensitive), to form a new hexadecimal digit string, and then convert it to a decimal number output. If there is a character before the first hexadecimal character "-", it means the number is negative.

Input formats:

It is given a non-empty string input by # in a row.

Output formats:

Outputting the converted decimal number in a row. Title ensure that the output in the Long Range.

Sample input:

+-P-xf4+-1!#

Sample output:

-3905
s = input()
number = [i for i in s if i.isdigit() or i in "abcdefABCDEF"]
if len(number) > 0:
    if s.find('-') < s.find(number[0]):
        number.insert(0, '-')
    print(int(''.join(number), 16))
else:
    print(0)

Guess you like

Origin www.cnblogs.com/fnmain/p/11753384.html