• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

pattern matching issue when validating String input

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that Corona the lemonade of Corona the beer? Or is it “hIC! Haec! Hoc!” like a platoon of drunken Roman solders in an Asterix book?
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Actually @Carey, I'm still confused.  "MMMDCCCLXXVI" shows as valid according to my code, but you seem to think my code shows it as invalid?


Apologies Mike, I put a '!' in where I shouldn't have, but when I fixed it I now get this which I did check against your last snippet.

 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is all of them up to 4000. Without looking at it with a microscope it apears that these patterns are rejected: LXL, DCD.

***EDIT*** These are all bogus so I removed the bulk of them. My intToRoman() method had a cut and paste error. Humble apologies to Mike.
 
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tested Mikes pattern and found it was correct in all cases (well done Mike!)

In this

MMMDCDLXLIII is indeed invalid, since it contains two D's


Addition: see rule 3 of that PE document.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Mike, you are the King of regex's.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Piet, for the validation.  And Carey, whose last post I saw just after I wrote the rest of this.

Piet Souris wrote:MMMDCDLXLIII is indeed invalid, since it contains two D's



Also because it contains LXL.  I think all of the "failed" patterns contain DCD and or LXL.  Surprisingly, no VIV, which also would have been invalid for similar reasons.

Going back...

Piet Souris wrote:As I said, it is easy to get the preferred Roman form of any number 0...999. When you have these, it is a little more work to expand these Romans according to what my linked article says. For instance, IV can be expanded to IIII (but not to IIIIII). The longest series I found was 499:

CDXCIX
CCCCXCIX
CDXCVIIII
CCCCXCVIIII
CDLXXXXVIIII
CDXCIIIIIIIII
CCCCLXXXXVIIII
CCCCXCIIIIIIIII
CDLXXXXIIIIIIIII
CCCCLXXXXIIIIIIIII
CDXXXXXXXXXIIIIIIIII
CCCCXXXXXXXXXIIIIIIIII



What about these others for 499?

CDLXXXXIX
CDXXXXXXXXIX
CDXXXXXXXXVIIII
CCCCLXXXXIX
CCCCXXXXXXXXIX
CCCCXXXXXXXXVIIII

Also, I think maybe you didn't include 999 in your tests.  By my count there are three ways to write 900 (CL, DCCC, CCCCCCCCC), three ways to write 90 (XC, LXXXX, XXXXXXXXX), and three ways to write 9 (IX, VIII, IIIIIIIII).  So 27 ways to write 999.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you perhaps insinuating there is a bug in my code??    
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Non dicere aliquid.
 
Piet Souris
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Found the bug...  

From the preferred Roman I determine all the possibilities, and put these in a TreeSet. The Comparator I use for this TreeSet is based on String::length, to get them neatly ordered from small to long.....

Never too old to screw up, I guess. Thanks for noticing!
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With all this discussion, it seems like it would be simpler to take a roman number text (lets call it X), validate for all characters, simply do a raw conversion to a number. Then take that number and convert it back to roman using all the rules strictly etc... and compare it with X. if they match, voila !!
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if we start with x="IIII", we get the number 4, which can be converted to Roman text 'IV'.  Which does not match the original x.  That may tell us that IIII was not the preferrd way to write 4.  But it's still a valid representation, no?
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but it would be a good user experience to get a suggestion instead merely true/false response.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another approach is to start with a number first, range 1-3999, convert it to Roman and then convert Roman back to an int and compare the before and after int values.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
reply
    Bookmark Topic Watch Topic
  • New Topic