Looking at the templates we need to parse for...
<tag>text1</tag>
<tag text2>text1</tag>
<tag
text2>text1</tag>
The first and second lines make sense. But looking around the rest of the exercise description and examples, I still can't decipher what's exactly being illustrated in the last two lines.
Mr. or Ms. Klesk's expansion in the Discussion section doesn't help at all. I'd like to hang in here and get just the first part of this figured out (i.e. Understanding the test conditions!)
Input:
<tag>text1</tag>
<tag text2><tag>text3<tag>text4</tag></tag>text1</tag>
<tag
text2>text1</tag>
Output:
<tag>text1</tag>
<tag text2><tag>text3<tag>text4</tag></tag>text1</tag>
<tag>text3<tag>text4</tag></tag>
<tag>text4</tag>
<tagtext2>text1</tag>
Clarify the Conditions
Under discussion
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
17 March 2023, 15:24
Line 1 makes sense. Line 2 makes sense. Lines 3 and 4 are just an example of line 2 that has a line break in it. So:
<tag text2>text1</tag>
equals
<tag
text2>text1</tag>
0
Guadalupe Gagnon
17 March 2023, 16:12
I recommend to not over complicated this task. Forget about the templates, they really don't help. Think only as open tags and closing tags. An open tag will specifically have a "less than" plus the tag, so if the tag is "span" then all the open tags will be exactly <span. An optional "greater than" (>) may exist at the end, but you don't need to test for it because the output is from the start of the tag to the end of the closing tag, not everything in between the tags. So the output would be:
<tag>text1</tag>
and not
text1
The closing tag is going to be a < and forward slash, the tag, and then a "greater than". So using "span" as the tag then it would be </span>.
Your algorithm only needs to find an each open tag in order and then determine the closing tag that specifically goes with that open tag. When the proper closing tag is found then output everything in between.
0
Guadalupe Gagnon
17 March 2023, 16:59
Here is an example input with the tag name of "O":
Everything before the first open tag is completely irrelevant <O something something<O more something <O>something more</O></O><O>Also Something More</O></O>
With the expected output:
<O something something<O more something <O>something more</O></O><O>Also Something More</O></O>
<O more something <O>something more</O></O>
<O>something more</O>
<O>Also Something More</O>
0
Guadalupe Gagnon
17 March 2023, 17:15
I'll explain an algorithm to solve it, use any of this that you like:
- get a list of all the open tag indices (where they are in the String)
- get a list of all the close tag indices
now, for each open tag in order you need to find the correct closing tag. The way I solved this is by counting each tag in order, no matter if open or close, preceding the tag you are trying to find the correct close for. An open tag is a +1, a closing tag is a -1. Additionally, if you are counting a closing tag you first check if count is 0 before adjusting the count by -1. If it is 0 then you can output the results and start the algorithm over by getting the next open tag. So all of that in steps:
LOOP1:
- get the next open tag
- set count to 0
LOOP2 (inside LOOP1):
- get next tag
- if its an open tag then add 1 to count
- if its a close tag then
- check if count is 0, if it is then output from the open tag in LOOP1 to this close tag and end LOOP2
- if not then subtract 1 from count
- repeat LOOP2 until correct close tag is found
- repeat LOOP1 until all open tags have been matched
This is generic and even if you code EXACTLY what i say there would be bugs, but if you decide to use some it could help solve this task
0