patternpythonMinor
Character to decimal conversion
Viewed 0 times
conversiondecimalcharacter
Problem
This converts from character representation of integers to their decimal value:
For example: "123" -> 123
def chrtodec(str):
dec = 0
base = ord('0')
for chr in str:
dec = dec * 10 + ord(chr) - base
return decFor example: "123" -> 123
Solution
You shouldn't use
You should also add comments and a docstring. Docstrings are basically comments that are programmatically accessible so that other users can understand how to use your function.
I think you have another big problem though, which is invalid input handling. Take a look at these:
Clearly there are problems here when you have characters other than numbers, so I think what you should do is raise a
You could also just use
Also you could add support for negative numbers by checking at the start if the first character is
This evaluates the expression
Then just return with a ternary that checks
chr or str as names because they shadow the builtin chr and str methods. If you then wanted to use str() you'd be out of luck because str is now a string instead of a function. Given that your function deals with both strings and character ordinals it is not unlikely that these could be used. string is a potential improvement, it can occasionally cause trouble if you're trying to use the string module of the same name.You should also add comments and a docstring. Docstrings are basically comments that are programmatically accessible so that other users can understand how to use your function.
def chrtodec(str):
"""Converts a string to a float using character ordinal positions."""I think you have another big problem though, which is invalid input handling. Take a look at these:
>>> chrtodec("hello")
619663
>>> chrtodec("12.21")
11821
>>> chrtodec("523.32f")
5228374Clearly there are problems here when you have characters other than numbers, so I think what you should do is raise a
ValueError when an invalid string is passed. You already have the tools to do this figured out of course if you just check that a character's ordinal fits in the right range.if not (ord('0') <= ord(chr) <= ord('9')):
raise ValueError("Invalid character {} in string {},"
"only digits are parseable.".format(chr, str))You could also just use
chr.isdigit() as @Barry pointed out.Also you could add support for negative numbers by checking at the start if the first character is
ord('-').negative = ord(str[0]) == ord('-')This evaluates the expression
str[0] == ord('-') and sets negative as the boolean result. Note that to make this compatible with the error handling I suggested, you should then remove the first character from str. And probably update the error message and docstring too.if negative:
str = str[1:]Then just return with a ternary that checks
negative.return dec if not negative else -decCode Snippets
def chrtodec(str):
"""Converts a string to a float using character ordinal positions.""">>> chrtodec("hello")
619663
>>> chrtodec("12.21")
11821
>>> chrtodec("523.32f")
5228374if not (ord('0') <= ord(chr) <= ord('9')):
raise ValueError("Invalid character {} in string {},"
"only digits are parseable.".format(chr, str))negative = ord(str[0]) == ord('-')if negative:
str = str[1:]Context
StackExchange Code Review Q#104289, answer score: 6
Revisions (0)
No revisions yet.