Assignment1 TOA 07042023 102134am

You might also like

You are on page 1of 7

Spring 2023

Department of Computer Science


Bahria University, Karachi Campus

ASSIGNMENT 1
Complex Computing Problem

Maximum Marks: 10

Following characteristics of Complex Computing Problem are focused in this assignment for
Theory of Automata Course

Attribute Complex Activities


Range of Students can use any programming language and/or libraries to solve
resources the problem
Depth of Theoretical and conceptual regular expressions knowledge would be
Knowledge needed to solve this well-defined computing problem.
Required
Depth of analysis Students are required to thoroughly understand and analyze the problem
to design the relevant regular expressions.

Name# Hoondraj

Enrollment# 02-134212-108

Class# BSCS(4B)

Group Member# Noman Tariq (02-134211-110)


Spring 2023
Department of Computer Science
Bahria University, Karachi Campus

(CLO3,PLO4,C5)Question#1:

Regular expressions are one of the more useful tools for many NLP tasks. The goals of this
assignment are to familiarize yourself with regular expressions, to play with them in different
languages/environments, to get some experience with some useful tools and to get a feeling for
text data analysis. This assignment involves coding and experimentation. You have to write a
function for each task that takes as input a string and returns a boolean indicating whether that
string is valid or invalid. Any programming language and/or platform (python, java, c++ etc.) and
libraries can be used for coding the following tasks.

a) Design a regular expression that matches the strings that are valid identifiers such that each
word should start with an alphabet then any combination of alphabet and digits or of special
character and digits. Alphabet can be in upper or lower case, special characters include
underscore _, $, % and @ symbols. a_b123, x, x$1 are all valid identifiers while %x,
123xyz, _x, $abc are not valid.

Code#

#include <iostream>
#include <regex>
using namespace std;

int main() {
regex identifier("^[a-zA-Z][a-zA-Z0-9_$%@]*$");

string str1 = "a_b123";


string str2 = "x";
string str3 = "x$1";
string str4 = "%x";
string str5 = "123xyz";
string str6 = "_x";
string str7 = "$abc";

cout << str1 << ": " << regex_match(str1, identifier) << endl;
cout << str2 << ": " << regex_match(str2, identifier) << endl;
cout << str3 << ": " << regex_match(str3, identifier) << endl;
cout << str4 << ": " << regex_match(str4, identifier) << endl;
Spring 2023
Department of Computer Science
Bahria University, Karachi Campus

cout << str5 << ": " << regex_match(str5, identifier) << endl;
cout << str6 << ": " << regex_match(str6, identifier) << endl;
cout << str7 << ": " << regex_match(str7, identifier) << endl;

return 0;
}

Output#
Spring 2023
Department of Computer Science
Bahria University, Karachi Campus

b) Design the regular expression date, which only matches dates written as ten-character
strings YYYY-MM-DD. For example, 2023-04-01 represents April 1 2023, whereas 2023
04-31 is not a valid date. You should account for leap years by assume that Feb 29th is
valid if the year is evenly divisible by four. E.g., 2024-02-29 is a valid date but 2023-02-
29 is invalid.

Code#
#include <iostream>
#include <regex>

int main() {
std::regex date_regex(R"(^(?:(?!0000)[0-9]{4}([-
])(?:(?:(?:0?[13578]|1[02])\1(?:31))|(?:(?:0?[13-9]|1[0-
2])\1(?:29|30))|(?:0?2\1(?:29)))(?!00)[0-9]{2}([-])(?:0?[1-9]|[12][0-
9]|3[01])$)$)");

std::string date_str = "2023-04-01";


if (std::regex_match(date_str, date_regex)) {
std::cout << "Valid date" << std::endl;
} else {
std::cout << "Invalid date" << std::endl;
}

return 0;
}

Output#
Spring 2023
Department of Computer Science
Bahria University, Karachi Campus

c) Develop the regular expression phone, which only matches mobile phone numbers in the
format 03XZ-YYYYYYY for mobile numbers in Pakistan, where the letter X is the single
letter code assigned to a specific mobile telephone operator and Z-YYYYYYY is the local
telephone number from any mobile phone or Land Line.
 3 - is the Mobile Access code
 Z can be any value between 0 and 9, assigned by the operator itself,
 X=0 Jazz Pakistan
 X=1 Zong Pakistan
 X=2 Jazz Pakistan
 X=3 Ufone
 X=4 Telenor Pakistan
 X=5 SCOM (Z=5 only)
 X=6 Instaphone
Existing Codes

 300, 301, 302, 303, 304, 305, 306, 307, 308, 309 - Jazz Pakistan
 310, 311, 312, 313, 314, 315, 316, 317, 318, 319 - Zong Pakistan
 320, 321, 322, 323, 324, 325, 326, 327, 328, 329 - Jazz Pakistan
 330, 331, 332, 333, 334, 335, 336, 337, 338, 339 - Ufone
 340, 341, 342, 343, 344, 345, 346, 347, 348, 349 - Telenor Pakistan
 355 - SCOM
 364 - Instaphone
International callers must dial +92-3XZ-YYYYYYY to reach a mobile number in Pakistan
from outside Pakistan, where '+92' is the Country Code, '3XZ' is Mobile Access Code as
per the above list and 'YYYYYYY' is personal number.

Within Pakistan the same number can be reached by dialing either 03XZ-YYYYYYY or
'0092-3XZ-YYYYYYY' or '+92-3XZ-YYYYYYY' from any mobile device or land line.
For example 0092-333-1234567, 0333-1234567 are valid but 0399-1234567,0123-
1234567 are invalid.
Spring 2023
Department of Computer Science
Bahria University, Karachi Campus

Code#
#include <iostream>
#include <regex>
#include <string>

int main() {
std::string phone_number;
std::regex phone_regex("^03[0-6][0-9]-[0-9]{7}$");
std::smatch matches;

std::cout << "Enter phone number in the format 03XZ-YYYYYYY: ";


std::getline(std::cin, phone_number);

if (std::regex_search(phone_number, matches, phone_regex)) {


std::string operator_code = phone_number.substr(2, 1);

switch (operator_code[0]) {
case '0':
std::cout << "Jazz Pakistan\n";
break;
case '1':
std::cout << "Zong Pakistan\n";
break;
case '2':
std::cout << "Jazz Pakistan\n";
break;
case '3':
std::cout << "Ufone\n";
break;
case '4':
std::cout << "Telenor Pakistan\n";
break;
case '5':
std::cout << "SCOM\n";
break;
case '6':
std::cout << "Instaphone\n";
break;
}
}
Spring 2023
Department of Computer Science
Bahria University, Karachi Campus

else {
std::cout << "Invalid phone number format\n";
}

return 0;
}

Output#

IMPORTANT INSTRUCTIONS:

 You are required to submit screenshots of queries along with outputs as well as regular
expressions coding.
 Submit your file in PDF format via LMS.
 Late submissions are not accepted
 Plagiarism is not allowed

Remember, the assignments may help you for the preparation of your future quizzes and/or examinations!

WISH YOU THE BEST OF LUCK


*******************************

You might also like