Academia.eduAcademia.edu

CS 102-Data Structure and Algorithm

Q-1 Gauss-Jordan method to solve a system of linear equations [15 Points] Gauss-Jordan method is a very useful method in solving a system of linear equations. It is a technique in which a system of linear equations is solved by means of matrices. The method is described at http://pages.pacificcoast.net/~cazelais/251/gauss-jordan.pdf with examples of solving a system of three/four linear equations. Write a python program that solves a system of n linear equations (where n<=4). The program will take coefficients of equations as a matrix and will apply Gauss-Jordan method to find the solution. Q 2-Matching tags in a Markup Language [15 Points] One of the application of matching delimiters is in the validation of markup languages such as HTML or XML. HTML is the standard format for hyperlinked documents on the Internet and XML is an extensible markup language used for a variety of structured data sets. We show a sample HTML document and a possible rendering in figure below:

CS 102- Data Structure and Algorithm Habib University - Spring’ 8 Assignment 1 Due on: Feb 01, 2018 Q-1 Gauss-Jordan method to solve a system of linear equations [15 Points] Gauss-Jordan method is a very useful method in solving a system of linear equations. It is a technique in which a system of linear equations is solved by means of matrices. The method is described at http://pages.pacificcoast.net/~cazelais/251/gauss-jordan.pdf with examples of solving a system of three/four linear equations. Write a python program that solves a system of n linear equations (where n<=4). The program will take coefficients of equations as a matrix and will apply Gauss-Jordan method to find the solution. Q 2 - Matching tags in a Markup Language [15 Points] One of the application of matching delimiters is in the validation of markup languages such as HTML or XML. HTML is the standard format for hyperlinked documents on the Internet and XML is an extensible markup language used for a variety of structured data sets. We show a sample HTML document and a possible rendering in figure below: In a HTML document, portions of text are delimited by HTML tags. A simple opening HTML tag has the for < a e> a d the orrespo di g losi g tag has the for </ a e> . For exa ple, we see the <body> tag on the first line of Figure (a), and the matching </body> tag at the close of that document. Other commonly used HTML tags that are used in this example include:       body: document body h1: section header center: center justify p: paragraph ol: numbered (ordered) list li: list item Write a function isHtmlMatched (raw) that takes raw html text and checks if the text has matching tags. Q 3 – Infix/Postfix/Prefix Expressions [20 Points] Infix, Postfix and Prefix notations are three different but equivalent ways of writing expressions. In infix notations, operators are written in-between their operands. However, in prefix/postfix expressions, the operator comes before/after the operands. The details of these expressions and the method of converting them from one form to another is given in Appendix A. a) Write a python program that converts an arithmetic expression with infix notation into the corresponding arithmetic expression with postfix and prefix notation. b) Write a program that evaluates a postfix expression. More specifically, the program will take, as input, an expression in postfix notation, and return, as output, the computed value of the given expression. The expression is evaluated from the left to right using a stack:  when encountering an operand: push it  when encountering an operator: pop two operands, evaluate the result and push it. c) Write an algorithm that evaluate a prefix expression. More specifically, the program you write should take, as input, an expression in prefix notation, and return, as output, the computed value of the given expression. Appendix - A Infix, Prefix and Postfix Expressions When you write an arithmetic expression such as B * C, the form of the expression provides you with information so that you can interpret it correctly. Infix notation, that we normally use, is the one in which the operator is in between the two operands that it is working on. However, Infix notation has certain problems. For example, in A + B * C, which operator will be processed first. Does the + work on A and B or does the * take B and C? The expression seems ambiguous. This ambiguity is resolved by operator precedence levels. Each operator has a precedence level. Operators of higher precedence are used before operators of lower precedence. The only thing that can ha ge that order is the prese e of pare theses. The pre ede e order for arith eti operator’s pla es multiplication and division above addition and subtraction. If two operators of equal precedence appear, then a left-to-right ordering or associativity is used. Let’s i terpret the trou leso e expressio A + B * C usi g operator pre ede e. B a d C are ultiplied first, and A is then added to that result. (A + B) * C would force the addition of A and B to be done first before the multiplication. In expression A + B + C, by precedence (via associativity), the leftmost + would be done first. An alternate way of avoiding this ambiguity is to use postfix/prefix expressions. Prefix expression notation requires that all operators precede the two operands that they work on. Postfix, on the other hand, requires that its operators come after the corresponding operands. Infix Expression A+B*C+D (A + B) * (C + D) A*B+C*D A+B+C+D Prefix Expression ++A*BCD *+AB+CD +*AB*CD +++ABCD Postfix Expression ABC*+D+ AB+CD+* AB*CD*+ AB+C+D+ Since postfix/prefix expressions are unambiguous, parenthesis are not needed at all. Only infix notation requires these additional symbols. The order of operations within prefix and postfix expressions is completely determined by the position of the operator and nothing else. In many ways, this makes infix the least desirable notation to use. Conversion from Infix to Postfix The method is using the stack. The following examples explain how to convert infix into postfix using the stack. A * B + C becomes A B * C + Statement no. 1 2 3 4 current symbol A * B + operator stack 5 6 C + * * + postfix string A A AB A B * {pop and print the '*' before pushing the '+'} AB*C AB*C+ A subexpression in parentheses must be done before the rest of the expression. A * (B + C) becomes A B C+* 1 2 3 4 5 6 7 8 current symbol A * ( B + C ) operator stack * *( *( *(+ *(+ * postfix string A A AB AB AB ABC ABC+ ABC+* Here is a summary of the rules that are followed during conversion: 1. Print operands as they arrive. 2. If the stack is empty or contains a left parenthesis on top, push the incoming operator onto the stack. 3. If the incoming symbol is a left parenthesis, push it on the stack. 4. If the incoming symbol is a right parenthesis, pop the stack and print the operators until you see a left parenthesis. Discard the pair of parentheses. 5. If the incoming symbol has higher precedence than the top of the stack, push it on the stack. 6. If the incoming symbol has equal precedence with the top of the stack, use association. If the association is left to right, pop and print the top of the stack and then push the incoming operator. If the association is right to left, push the incoming operator. 7. If the incoming symbol has lower precedence than the symbol on the top of the stack, pop the stack and print the top operator. Then test the incoming operator against the new top of stack. 8. At the end of the expression, pop and print all operators on the stack. (No parentheses should remain.)