String to Number in Python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: String to Number in Python (/thread-38725.html) |
String to Number in Python - Godjuned - Nov-16-2022 Number: positive, up to billion (10 digits) Example 1: Input : I bought twenty three packs of bread Output : I bought 23 packs of bread Example 2: Input : four billion five hundred sixty seven million eight hundred ninety thousand one hundred and twenty three Output: 4567890123 Do you have any solutions for this problem? I'm getting stuck. RE: String to Number in Python - Yoriz - Nov-16-2022 Thank you for posting your homework question. https://python-forum.io/misc.php?action=help&hid=52 Wrote:Homework and No Effort Questions Please read the full details from the link above, also see the links in my signature for details of what to post and how to post code. RE: String to Number in Python - deanhystad - Nov-17-2022 This is not that difficult a problem. Number words can be divided into categories: Number Names These are names that refer to a particular number: zero, one, two ... eighteen, nineteen, twenty, thirty ... eighty ninety Position Names These are names that scale a number: hundred, thousand, million, billion Converting a number string to a number will consist of splitting the number string into words, determining if the words are number names of position names, and keeping track of a total. For example: twelve = value(twelve) forty two = value(forty) + value(two) Things git a little tricky when you get into the hundreds: two hundred fifty three = value(two) * value(hundred) + value(fifty) + value(three) And trickier still when there are thousands, millions or billions; two hundred forty three thousand one hundred eleven = (value(two) * value(hundred) + value(forty) + value(three)) * value(thousand) + value(one) * value(hundred) + value(eleven) There is a pattern for knowing when you have to add values, when you have to multiply and when you have to group. Work through several examples using pencil and paper and you should see the pattern. This program gets a lot more fun if you also handle non-standard number strings like four one one, nine eleven, five o one, twenty twenty two etc... RE: String to Number in Python - Godjuned - Nov-17-2022 @Yoriz Thanks for the answer. I don't ask you to finish with full code script. I Also, this is not my homework, i just read some problem and coding and try to get enlightenment to resolve. |