algorithm search close tag - algorithm

Image http://img534.imageshack.us/img534/8542/aaagla.jpg
It is good to give me a example (any language)

The traditional way to do this is with a stack. Push when you get an open tag, and pull when you get a close tag. Pulling from an empty stack or having a non-zero stack at the end indicates mismatched tags.

Pseudocode
input: lineopen (line having opening tag)
input: text (test as array of lines)
output: lineclose (line having closing tag)
line := lineopen
counter := 1
do
line := line + 1
if text[line] contains opening tag
then counter := counter + 1
if text[line] contains closing tag
then counter := counter - 1
while counter > 0
lineclose := line

Related

How to add a newline into tabwriter in go?

i want to print a newline but if i add a newline it changes the format, here is the code.
q := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', tabwriter.AlignRight|tabwriter.Debug)
fmt.Fprintf(q, "Replica\tStatus\tDataUpdateIndex\t\n")
fmt.Fprintf(q, "\n")
for i := 0; i < 4; i += 3 {
fmt.Fprintf(q, "%s\t%s\t%s\t\n", statusArray[i], statusArray[i+1], statusArray[i+2])
}
How to add newline without affecting the format?
As stated in the docs (emphasis is mine):
Tab-terminated cells in contiguous lines constitute a column.
https://golang.org/pkg/text/tabwriter/#Writer
When you insert the new line in your code, you are separating the header and content lines so they are not treated as "columns".
In order to fix that, make your newline insert an empty row with the same number of columns (but blank).
fmt.Fprintf(q, "Replica\tStatus\tDataUpdateIndex\t\n")
fmt.Fprintf(q, "\t\t\t\n") // blank line
for i := 0; i < 4; i += 3 {
fmt.Fprintf(q, "%s\t%s\t%s\t\n", statusArray[i], statusArray[i+1], statusArray[i+2])
}

String Comparison inside loop Maple

I am new to maple. I am having a problem with a small program that will read data from file, perform some calculations and then write back to file. Data being read from the file is required to be compared in order to extract x,y and z data. I can read properly from the file and do the comparison but when I read inside a loop the comparison doesnot work, It seems the compare function is not working at all inside the loop.
Can anyone please tell me whats wrong with my code?
with(StringTools):
file:=fopen("C:\\Fileread.txt",READ,TEXT);
cryptf:=fopen("C:\\Filewrite.txt",WRITE,TEXT);
line :=readline(file);
while (line <> 0) do
if type(Compare(line, "x"),boolean) then
line := readline(file);
x[1] = convert(line, decimal, hex);
elif Compare(line, "y") then
line := readline(file);
Y[1]= convert(line, decimal, hex);
elif Compare(line, "Z") then
line := readline(file);
Z[1] = convert(line, decimal, hex);
else
d=1;
end if;
line :=readline(file);
writeline(cryptf,X[1]);
od;
close(file);
close(cryptf);

Accepting only alphanumerics in Golang and ncurses

So, I'm teaching myself some Golang by making a simple resource management game with ncurses. I'm using this library to connect Golang to ncurses.
I've made a simple text input panel that takes in one character at a time, displays it, and then adds it to a string composing the user's response. Here's what it looks like:
// Accept characters, printing them until end
ch := window.GetChar()
kstr := gc.KeyString(ch)
response := ""
cur := 0
for kstr != "enter" {
// Diagnostic print to get key code of current character
window.Move(0,0)
window.ClearToEOL()
window.MovePrint(0, 0, ch)
// If its a backspace or delete, remove a character
// Otherwise as long as its a regular character add it
if ((ch == 127 || ch == 8) && cur != 0){
cur--
response = response[:len(response)-1]
window.MovePrint(y, (x + cur), " ")
} else if (ch >= 33 && ch <= 122 && cur <= 52) {
window.MovePrint(y, (x + cur), kstr)
response = response + kstr
cur++
}
// Get next character
ch = window.GetChar()
kstr = gc.KeyString(ch)
}
However, the arrow and function keys seem to be coming up as keycodes already associated with the normal a-zA-Z characters. For example, right-arrow comes up as 67 and F1 as 80. Any ideas what I'm doing wrong here, or if there's a better approach to taking in alphanumerics through ncurses? I'd like to avoid ncurses fields and classes as much as possible, because the point here is to learn Golang, not ncurses. Thanks!
If you do not enable the keypad mode, (n)curses will return the individual bytes which make up a special key.
To fix, add this to your program's initialization:
stdscr.Keypad(true) // allow keypad input
which will return special keys such as right-arrow as values above 255. goncurses has symbols defined for those, e.g., KEY_RIGHT.

Parsing file, ignoring comments and blank lines

As the title says, I am trying to parse a file but ignore comments (started with #) or blank lines. I have tried to make a system for this, yet it always seems to ignore that it should be ignoring comments and/or blank lines.
lines := strings.Split(d, "\n")
var output map[string]bool = make(map[string]bool)
for _, line := range lines {
if strings.HasPrefix(line, "#") != true {
output[line] = true
} else if len(line) > 0 {
output[line] = true
}
}
When run (this is part of a function), it outputs the following
This is the input ('d' variable):
Minecraft
Zerg Rush
Pokemon
# Hello
This is the output when printed ('output' variable):
map[Minecraft:true Zerg Rush:true Pokemon:true :true # Hello:true]
My issue here is that it still keeps the "" and "# Hello" values, meaning that something failed, something I haven't been able to figure out.
So, what am I doing wrong that this keeps the improper values?
len(line) > 0 will be true for the "# Hello" line, so it will get added to output.
Currently, you are adding lines that either don't start with a # or are not empty. You need to only add lines that satisfy both conditions:
if !strings.HasPrefix(line, "#") && len(line) > 0 {
output[line] = true
}

Usage of # in Pascal

Q1: What does this mean: WriteLn (#$0b)?
$0b should hexadecimal like 0x0b, but what about the # sign?
Q2:
x:=readkey;
if ( x = #5) do...
Does #5 mean five? Then what is the# sign for?
Many thanks.
The # in front of a number represents a character with the indicated value (both decimal, and hex numbers preceded by a $, are accepted). So #5 is the same as chr(5), or CtrlE.
Ah, memories ...
#x is indeed the equivalent of chr(x), like Greg Hewgill said.
I'd like to add a little info.
Extended keys, ie the arrow keys, send zero and the char's code:
ch := ReadKey;
if ch = #0 then
begin // extended key
ch := ReadKey; // <-- read again to get the actual code
end else ...

Resources