In the example, "12." is converted to "twelve."
Does this hold true for all punctuation marks? And does it apply to punctuation both before and after a number?
For example, should "%8(" become "%eight("? Or does starting with punctuation mean it's not considered a number?
Are there always spaces between numbers? It is true in the sample input, but not stated as rule in the conditions.
For example, should "2#8" become "two#eight"? Or is this not considered a number (or only the 2 considered a number)?
Finally, what are we supposed to do with numbers starting with 0? For example, are we meant to interpret "09" the same as "9"?
clarification on punctuation, spacing rules
Under discussion
Comments (14)
- Popular
- New
- Old
You must be signed in to leave a comment
Gellert Varga
10 November 2021, 22:54
I just tried this input with my recently accepted code:
!!4 4! 4!! az 1 a12 12. 12? 11- 11) -9 2#8 %8 09 %8( a%5
01 002 00000003 00012 0.8 707
Output:
!!4 four! 4!! az one a12 twelve. twelve? eleven- eleven) -9 2#8 %8 nine %8( a%5
one two three twelve 0.8 707
Summary:
If there is a single non-space character (any type char) immediately following the numbers 0-12, it is considered a match:
5! = five!
7* = seven*
2b = twob
8% = eight%
but:
%8 = %8
I did not use regex for this task either.
(At this moment I don't like regex yet...)
I solved it with these String methods:
substring(), split(), and Integer.parseInt().
But if you want to learn regex, for me these four links were very-very useful:
https://docs.oracle.com/javase/tutorial/essential/regex/pre_char_classes.html
https://regexone.com/
https://en.wikipedia.org/wiki/Regular_expression
https://www.regular-expressions.info/wordboundaries.html
0
Guadalupe Gagnon
10 November 2021, 14:44
"%8(" does become "%eight("
"2#8" become "two#eight"
"09" stays "09"
+1
Justin Smith
10 November 2021, 14:47
Well damn, all of those answers make it harder, haha.
0
Guadalupe Gagnon
10 November 2021, 14:51
Take a look at Attila's comment on the task page. Word boundaries is what will make this task manageable. Just remember that the number in the String needs to MATCH EXACTLY the number surrounded by word boundaries. So, like you asked, the String "09" does not equal the String "9" in this task.
0
Guadalupe Gagnon
10 November 2021, 14:55
A general strategy:
you can test each sub-String between word boundaries.
- If the String is length 1 and contains any digit then it can be replaced
- if the String is length 2 then it must start with a 1 and then have either 0, 1, or 2 for the second digit to qualify for a change
- all others that don't meet the above requirements don't change
If it passes those requirements to change then you can use parseInt() and the Map to make the change.
0
Justin Smith
10 November 2021, 15:18
If I had input like:
would the correct output be:
or:
I'm not sure if have the letter 'a' there should prevent the 5 from parsing or not.
I took a look at the word boundaries page... can't really make much sense of it, unfortunately. Honestly, I wish CodeGym had an entire chapter devoted to regex,since it is used so much in the tasks. 0
Guadalupe Gagnon
10 November 2021, 15:26
That is the same situation as "2#8". Both the '2' and '8' are separate entities, such as the 'a' and '5' in your example.
0
Guadalupe Gagnon
10 November 2021, 15:28
A word boundary, for the purpose of this task, is anything that is not a letter (anything NOT a-z or A-Z). Be sure to compensate for the end of the line.
0
Justin Smith
10 November 2021, 15:52
I think at this point my foundation of understanding of regex is not sufficient. I understand how to use it to ask "does this string contain one of these characters" or "does this string contain this exact sequence of characters" but I am struggling with anything beyond that. I understand very little on the page Atilla posted. I feel like this is an area of programming where CodeGym has not provided sufficient instruction.
Since I was having difficulty with the regex approach, I tried to do this by iterating through the string and testing each character against the characters that come before and after it, but there proved to be so many exceptions to rules I had to account for that it was going to be a ridiculous number of nested conditional statements.
I think at this point my best option for trying to understand this task is to study the solution. Thank you again for the assistance.
0
Justin Smith
10 November 2021, 15:58
Well, I looked at the solution and honestly I still don't understand, haha.
If I have an input
and I search by regex
Why does this replace %12 but not a12? I don't understand where they have defined that % is okay but a letter is not. 0
Guadalupe Gagnon
10 November 2021, 16:03
because 'a' is not a word boundary
0
Justin Smith
10 November 2021, 16:06
But where does it define what the word boundaries are? Or do you mean that it's something already defined at system level? Maybe that's why I've been confused, I've been looking for where the regex defines what word boundaries are. I thought I would have to tell it that punctuation marks are word boundaries.
0
Justin Smith
10 November 2021, 16:15
Yeah, I think that's it. I was trying to find where in the regex something was defined, not realizing that it's a system-level definition already. A word character is either a letter or a numeric digit. That's not something I have to define in the regex, the system already knows that. My point of confusion was kind of similar to asking "but where do I define what 'int' means?"
0
Guadalupe Gagnon
10 November 2021, 16:37
When I completed this task I didn't know how to use regular expressions either, my solve didn't include them. My code basically did exactly what I explained to you as the general strategy. It iterated each character in the String looking for a digit, when one was found it determined if it started with a word boundary; then it found the next word boundary past the digit(s) it then determined if the String in between the word boundaries needed to be changed (it matches 0-12). If so then changed it and continued. A word boundary for me was either the start or end of the String, then anything NOT a letter.
0