Erinevus lehekülje "Itx8071-task1" redaktsioonide vahel

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti
 
(ei näidata sama kasutaja 100 vahepealset redaktsiooni)
1. rida: 1. rida:
 +
 
This homework assignment requires the knowledge from Module 3.
 
This homework assignment requires the knowledge from Module 3.
  
−
==== Please write a regular expression for matching the sequence of numbers which follows these rules: ====
+
=== Please write a regular expression for matching the sequence of arithmetic expressions which follows these rules: ===
 +
 
 +
1) The sequence consists of one or more elements. If there are two or more elements, they are separated with a comma (,) character.
 +
 
 +
2) Each element of the sequence is an expression that can consist of digits 0 1 2 3 4 5 6 7 8 9, letters X Y Z, and the plus (+), minus (-), asterisk (*), slash (/), and circumflex (^) characters. No other characters (such as spaces or other letters than X, Y, and Z) are permitted in the element. The element must contain one or more operands, where operand is either an integer number or a variable. The integer number is defined as a non-empty sequence of digits. The variable is defined as a single letter X, Y, or Z.
 +
 
 +
3) If the operand is a variable, the operand may have the following suffix: a single circumflex (^) character, followed by an optional minus (-) character, followed by at least one digit. If the operand is an integer number, this suffix is not permitted.
 +
 
 +
4) If the element has more than one operand, two consecutive operands must be separated with a single plus, minus, asterisk or slash character. If the asterisk or slash separates two operands, an optional minus (-) character can appear before the second (right-hand side) operand. However, if two operands are separated by plus or minus character, the minus prefix is not allowed for the second operand. Also, an optional minus (-) character can appear before the first operand in the element (in other words, the first character of the element can be either a minus, a digit, or a letter X, Y, or Z).
 +
 
 +
For example, the following are valid sequence elements:
 +
 
 +
012
 +
X
 +
-1
 +
Y+3
 +
Z^0123
 +
-0+12-Z
 +
22+90*-2/60
 +
-100+16/-2*-30
 +
X^2+3*Y^-10+1
 +
 
 +
However, the following sequence elements are invalid:
 +
 
 +
1 + 2 (the space character appears both before and after the plus character, but the use of spaces is illegal)
 +
1%2 (an illegal character % appears between two operands)
 +
12C (an illegal character C appears after the operand)
 +
X1Y2Z3 (an illegal operand which contains both digits and letters)
 +
X--2 (if the minus character separates two operands, it is not allowed to use the minus prefix for the second operand)
 +
X^ (the circumflex character is not followed by at least one digit)
 +
10^1 (circumflex character can only appear after X, Y, or Z)
 +
 
 +
Please note that for accomplishing the task, '''one regular expression''' has to be submitted which is '''suitable for use with the egrep or pcre2grep tool'''. It is not acceptable to submit the solution in a fragmented way (e.g., several isolated expressions for addressing different parts of the task). Also, it is not allowed to submit programs in Java, Python, Perl (or any other language) for the solution. Finally, if the solution only works with the examples provided below but does NOT meet the task specification, it will be treated as incorrect.
 +
 
 +
=== Examples of sequences the regular expression must match: ===
 +
 
 +
1+2+3+X+Y+Z  (the sequence has one valid element)
 +
 
 +
10,X^-2+3,-20  (the sequence has three valid elements)
 +
 
 +
-0,2*-3+7,-X^-1,33/-11*-5,-12/6,9+Y^10,77,1-2+3-4+5  (the sequence has eight valid elements)
 +
 
 +
=== Examples of sequences the regular expression must NOT match: ===
  
−
1) The sequence consists of one or more parts. If there are two or more parts,
+
22+11,  (there is a comma after the first sequence element, but the second element is missing)
−
they are separated with a comma (",") character.
 
  
−
2) Each part of the sequence is either an integer or a real number.
+
120+,3-4  (the first sequence element contains a plus character not followed by operand)
−
An integer number consists of one or more digits and must not begin with
 
−
zero ("0"), unless the integer number has only one digit.
 
−
If the part of the sequence is a real number, it consists of one or more
 
−
digits which are followed by one punctuation mark ("."), with this
 
−
punctuation mark being followed by one or more digits. Like the integer
 
−
number, the real number must not begin with zero ("0"), unless there is
 
−
only one digit before the punctuation mark. Also, the real number must
 
−
not end with zero ("0"), unless there is only one digit after the punctuation
 
−
mark.
 
  
 +
-12-33,12+-1,5*X-3  (the second sequence element contains a plus character which is followed by a negative integer, but it is not allowed to use the minus character after plus)
  
−
Please note that for accomplishing the task, one regular expression has
+
1+2,,3+4,XY  (the second sequence element is missing; the fourth sequence element is an illegal variable XY, as only a single letter is permitted as a variable)
−
to be submitted which is suitable for use with the egrep or pcregrep tool.
 
−
It is not acceptable to submit the solution in a fragmented way (e.g.,
 
−
several isolated expressions for addressing different parts of the task).
 
−
Also, it is not allowed to submit programs in Java, Python, Perl (or any
 
−
other language) for the solution.
 
  
 +
11,12,+13  (the third sequence element starts with a plus character which is allowed only as a separator between two operands)
  
−
Examples of sequences the regular expression must match:
+
12^3,2+3,55//11  (the first sequence element contains ^ after an integer number, which is not permitted; also the third element contains two consecutive slash characters, while only one is allowed)
  
−
  12345                (the sequence has one valid integer part)
+
  1+2+3,12*,2/3  (the second sequence element ends with an asterisk character which is allowed only as a separator between two operands)
−
915,2.35            (the sequence has two valid parts - an integer and a real number)
 
−
1.205,754.0          (the sequence has two valid real number parts)
 
−
8.4,12,0.1          (the sequence has three valid parts - a real number integer, and a real number)
 
−
1,1.0,3,4.8,0.0,0,7.001,8.0      (the sequence has eight valid parts)
 
  
 +
11,45+X, 100  (there is an illegal space character after the second separating comma)
  
−
Examples of sequences the regular expression must NOT match:
+
60 ,-4+0  (there is an illegal space character before the first separating comma)
  
−
  1, 2          (the second part begins with a space character which is illegal)
+
  --10A  (the sequence has one element which starts with two minus characters, while only one minus is allowed; also, the element ends with an illegal character A)
−
1 2,3        (the first part contains a space character which is illegal)
 
−
9.0,          (the first part ends with comma, but there is no following second part)
 
−
9F            (the number contains an illegal character F)
 
−
02            (the integer number has more than one digit and begins with a zero which is illegal)
 
−
2.00          (the real number has more than one digit after the punctuation mark and ends with a zero which is illegal)
 
−
2-1          (the number contains an illegal character -)
 
−
2,,8.9        (the second part of the sequence is empty)
 
−
2.            (the real number contains a punctuation mark without at least one following digit)
 
−
.3            (the real number contains a punctuation mark without at least one preceding digit)
 
−
1.43.222      (the real number illegally contains two punctuation marks)
 

Viimane redaktsioon: 28. september 2026, kell 14:49

This homework assignment requires the knowledge from Module 3.

Please write a regular expression for matching the sequence of arithmetic expressions which follows these rules:

1) The sequence consists of one or more elements. If there are two or more elements, they are separated with a comma (,) character.

2) Each element of the sequence is an expression that can consist of digits 0 1 2 3 4 5 6 7 8 9, letters X Y Z, and the plus (+), minus (-), asterisk (*), slash (/), and circumflex (^) characters. No other characters (such as spaces or other letters than X, Y, and Z) are permitted in the element. The element must contain one or more operands, where operand is either an integer number or a variable. The integer number is defined as a non-empty sequence of digits. The variable is defined as a single letter X, Y, or Z.

3) If the operand is a variable, the operand may have the following suffix: a single circumflex (^) character, followed by an optional minus (-) character, followed by at least one digit. If the operand is an integer number, this suffix is not permitted.

4) If the element has more than one operand, two consecutive operands must be separated with a single plus, minus, asterisk or slash character. If the asterisk or slash separates two operands, an optional minus (-) character can appear before the second (right-hand side) operand. However, if two operands are separated by plus or minus character, the minus prefix is not allowed for the second operand. Also, an optional minus (-) character can appear before the first operand in the element (in other words, the first character of the element can be either a minus, a digit, or a letter X, Y, or Z).

For example, the following are valid sequence elements:

012
X
-1
Y+3
Z^0123
-0+12-Z
22+90*-2/60
-100+16/-2*-30
X^2+3*Y^-10+1

However, the following sequence elements are invalid:

1 + 2 (the space character appears both before and after the plus character, but the use of spaces is illegal)
1%2 (an illegal character % appears between two operands)
12C (an illegal character C appears after the operand)
X1Y2Z3 (an illegal operand which contains both digits and letters)
X--2 (if the minus character separates two operands, it is not allowed to use the minus prefix for the second operand)
X^ (the circumflex character is not followed by at least one digit)
10^1 (circumflex character can only appear after X, Y, or Z)

Please note that for accomplishing the task, one regular expression has to be submitted which is suitable for use with the egrep or pcre2grep tool. It is not acceptable to submit the solution in a fragmented way (e.g., several isolated expressions for addressing different parts of the task). Also, it is not allowed to submit programs in Java, Python, Perl (or any other language) for the solution. Finally, if the solution only works with the examples provided below but does NOT meet the task specification, it will be treated as incorrect.

Examples of sequences the regular expression must match:

1+2+3+X+Y+Z   (the sequence has one valid element)
10,X^-2+3,-20   (the sequence has three valid elements)
-0,2*-3+7,-X^-1,33/-11*-5,-12/6,9+Y^10,77,1-2+3-4+5  (the sequence has eight valid elements)

Examples of sequences the regular expression must NOT match:

22+11,   (there is a comma after the first sequence element, but the second element is missing)
120+,3-4   (the first sequence element contains a plus character not followed by operand)
-12-33,12+-1,5*X-3   (the second sequence element contains a plus character which is followed by a negative integer, but it is not allowed to use the minus character after plus)
1+2,,3+4,XY   (the second sequence element is missing; the fourth sequence element is an illegal variable XY, as only a single letter is permitted as a variable)
11,12,+13   (the third sequence element starts with a plus character which is allowed only as a separator between two operands)
12^3,2+3,55//11   (the first sequence element contains ^ after an integer number, which is not permitted; also the third element contains two consecutive slash characters, while only one is allowed)
1+2+3,12*,2/3   (the second sequence element ends with an asterisk character which is allowed only as a separator between two operands)
11,45+X, 100   (there is an illegal space character after the second separating comma)
60 ,-4+0  (there is an illegal space character before the first separating comma)
--10A   (the sequence has one element which starts with two minus characters, while only one minus is allowed; also, the element ends with an illegal character A)