Python Forum
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
desired patterns
#1
my question here

my code here
How is to use desired patterns programming ? Such as :

case:
....int 20 + int 1 : print('twenty') + print('one')
....int 20 + int 7 : print('twenty') + print('seven')

tysm in advance.
Reply
#2
I don't understand your question. Do you want to print 'twenty-one' when you get the number 21?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Yes Sir.
Reply
#4
You will need a couple of dictionaries - one for values, one - for powers of 10, like

{1: 'one',
2: 'two',
....
11: 'eleven'
...
90: 'ninety'}
and

{10: 'ten',
100: 'hundred'
1000: 'thousand'
}
And then you'll have to parse digits in the reversed order with some convoluted logic thrown into the fray At
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
It's okay to write for a limited range.
To make it work for big range,it's a lot of work.

There are libraries for this like inflect.
Test:
pip install inflect
>>> import inflect
>>> p = inflect.engine()

>>> p.number_to_words(21)
'twenty-one'
>>> p.number_to_words(27)
'twenty-seven'

# Big number
>>> p.number_to_words(12345)
'twelve thousand, three hundred and forty-five'
>>> p.number_to_words(956748)
'nine hundred and fifty-six thousand, seven hundred and forty-eight'

# Group
>>> p.number_to_words(21, group=1)
'two, one'
>>> p.number_to_words(965495, group=1)
'nine, six, five, four, nine, five'
>>> p.number_to_words(965495, group=2)
'ninety-six, fifty-four, ninety-five'
Reply
#6
Up to 99, you can do it with two lists:
under_twenty = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', ..., 'nineteen']
twenty_up = ['twenty', 'thirty', 'forty', ..., 'ninety']
Then you can use numeric indexing (divided by ten for twenty_up, hint: use //), and only have three cases to worry about: 0, < 20, >= 20.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Regex Include and Exclude patterns in Same Expression starzar 2 735 May-23-2023, 09:12 AM
Last Post: Gribouillis
  The included URLconf 'scribimus.urls' does not appear to have any patterns in it. nengkya 0 1,037 Mar-03-2023, 08:29 PM
Last Post: nengkya
  Json filter is not capturing desired key/element mrapple2020 1 1,073 Nov-24-2022, 09:22 AM
Last Post: ibreeden
  How to remove patterns of characters from text aaander 4 1,068 Nov-19-2022, 03:34 PM
Last Post: snippsat
  how can I display only desired items? 3lnyn0 5 1,973 Dec-25-2021, 06:49 PM
Last Post: menator01
  ERROR: importing desired module mbgamer28 0 1,648 Apr-05-2021, 07:46 PM
Last Post: mbgamer28
  Not rounding to desired decimal places? pprod 2 2,505 Mar-05-2021, 11:11 AM
Last Post: pprod
  Why this code not getting desired output ? MDRI 2 2,485 Sep-18-2020, 02:11 AM
Last Post: MDRI
  Extracting data based on specific patterns in a text file K11 1 2,180 Aug-28-2020, 09:00 AM
Last Post: Gribouillis
  showing only desired part of a plot grknkilicaslan 1 2,291 Jul-10-2020, 03:51 PM
Last Post: Marbelous

Forum Jump:

User Panel Messages

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