Python Forum
String to Number in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String to Number in Python
#1
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.
Reply
#2
Thank you for posting your homework question.

https://python-forum.io/misc.php?action=help&hid=52 Wrote:Homework and No Effort Questions

This forum is focused on education. It exists to help people learn Python. We don’t exist to solve others’ problems, although that tends to be a happy byproduct of education. You should keep this in mind when writing replies.

Learning is most effective when the learner is making mental effort (see Make It Stick by Mark A. McDaniel and Peter C. Brown or this Youtube video for more about this). Since we are focused on education (learning) it is essential to make sure that the question asker has made some reasonable effort. This pertains to every question asked on the forum, not just homework questions. Ideally, they provide a “Minimal, Reproducible Example” to quote Stackoverflow. It’s reasonable to point them toward that or other links (e.g. Lifehack or catb) which talk about how to ask questions well.

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.
Reply
#3
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...
Reply
#4
@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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  number to string Ali_ 1 1,318 Mar-31-2022, 11:22 AM
Last Post: ndc85430
  Counting the number of occurrences of characters in a string nsadams87xx 1 1,970 Jun-16-2020, 07:22 PM
Last Post: bowlofred
  Convert string to a specific number of bytes artblinked 1 2,459 Mar-28-2019, 08:43 PM
Last Post: Larz60+
  Trying to get the week number from a string of numbers fad3r 2 3,272 Apr-15-2018, 06:52 PM
Last Post: ljmetzger

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020