0% found this document useful (0 votes)
22 views10 pages

Assignment 2

The document outlines the assignment for CS-1004 Object Oriented Programming at the National University of Computer & Emerging Sciences, due on March 24, 2025. It includes individual coding tasks focused on creating a custom String class, implementing a text editor with pagination, and solving a problem related to anagrams and prefixes. Strict guidelines on submission format, plagiarism, and coding standards are emphasized, with specific instructions for each question's implementation.

Uploaded by

dyssart
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views10 pages

Assignment 2

The document outlines the assignment for CS-1004 Object Oriented Programming at the National University of Computer & Emerging Sciences, due on March 24, 2025. It includes individual coding tasks focused on creating a custom String class, implementing a text editor with pagination, and solving a problem related to anagrams and prefixes. Strict guidelines on submission format, plagiarism, and coding standards are emphasized, with specific instructions for each question's implementation.

Uploaded by

dyssart
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

NATIONAL UNIVERSITY OF COMPUTER & EMERGING

SCIENCES ISLAMABAD CAMPUS

CS-1004 Object Oriented Programming Spring-2025


ASSIGNMENT-02
Section (All)
Submission Deadline: 24th March 2025, 11:59 pm

Instructions:
1. Assignments are to be done individually. You must complete this assignment by yourself.
You cannot work with anyone else in the class or with someone outside of the class. The
code you write must be your own and you must understand each part of your code. You
are encouraged to get help from the instructional staff through google classroom.
2. Do not use any String or math libraries (such as cmath, cstring, string etc) and also do not
use built-in function (such as strlen, strcmp etc). Caution: zero marks will be awarded.
3. Do not edit Function Prototypes. Caution: zero marks will be awarded.
4. The usage of string is strictly prohibited.
5. Your code must be generic.
6. Marks distribution and test Cases are provided for each question. Your code will be
evaluated with similar test cases. If the required output is generated, you will be awarded
full marks. Failing to generate the correct output will result in zero marks. Total Marks:
200.
7. Static and global variables are not allowed.
8. Plagiarism: Plagiarism of any kind (copying from others, copying from the internet, etc)
is not allowed. If found plagiarized, you will be awarded zero marks in the assignment.
Repeating such an act can lead to strict disciplinary actions and failure in the course.
9. Please start early otherwise you will struggle with the assignment.
10. Test cases: Test cases (in gtest) will be shared with you on Google Classroom. We will be
running your code against our test cases, and a test case failure or a segmentation
fault/incorrect result or even syntax error will result in ZERO marks. Question 1 on Gtest
, Question 2 will be checked on demo and Question 3 on codeforces.
11. Submission Guidelines: Dear students, we will be using auto-grading tools (gtest), so
failure to submit according to the below format would result in ZERO marks in the relevant
evaluation instrument.
a. Make separate files for each question in format i24xxxx_Qx.cpp
b. Your files must contain your name, student-id, and assignment # on the top of the
in the comments.
c. Move all the cpp files in one folder. The folder must contain only cpp files (no
binaries, no exe files etc.,). If we unable to download your submission due to any
reason you will be awarded zero mark.
d. Run and test your program on a lab machine before submission. If there is a syntax
error, ZERO marks will be awarded in that specific question.
e. Rename the folder as ROLL-NUM_SECTION (e.g. 24i-0001_A) and compress the
folder as a zip file. (e.g. 24i-0001_A.zip). Only zip file will be acceptable.
f. Submit the .zip file on Google Classroom within the deadline.
g. Submission of Question 3 will be on Codeforces. The link is attached with the
submission on GCR. The submission will also be closed on Codeforces on deadline.
h. Submission other than Google classroom (e.g. email etc.) will not be accepted.

1
NATIONAL UNIVERSITY OF COMPUTER & EMERGING
SCIENCES ISLAMABAD CAMPUS

i. The student is solely responsible to check the final zip files for issues like corrupt
files, viruses in the file, mistakenly exe sent. If we cannot download the file from
Google classroom due to any reason it will lead to zero marks in the assignment.
You are required to use Visual Studio 19 or above for the assignment.
Note: Follow the given instruction to the letter, failing to do so will result in a zero.

Q1: String Implementation [50 Marks]


You have directly or indirectly worked in a string whenever you use double quotation marks (“”) in C++.
It makes an array of characters in continuous memory in the backend. This character array is very useful to
store large numbers and even data of different categories. But writing different operations and functions for
a string separately is very difficult to manage. So you are required to create a String class of your own.

Since this is your own String class, you should know how things work in this and create all the
functionalities for the correct implementation of many features in it. Following functionalities will be
required for your class.

class String{
// think about the private data members...
public:
// provide definitions of following functions...
String(); // a default constructor
String(const char*);
String(const char*);
String(const String&);
String add(const String&); // appends a String to the end
String add(const char*); // appends a String to the end
String add(char*); // appends a String to the end
String add(const int&); // if the stored String is an integer e.g. “123” it adds the given
integer into it and returns the answer. If not then returns “NaN”
String remove(const String&); //removes a substring anywhere in the String
String remove(const char*); //removes a substring anywhere in the String
String remove(char*); //removes a substring anywhere in the String
String remove(const int&); // if the stored String is an integer e.g. “123” it subtracts the
given integer into it and returns the answer. If not then returns “NaN”
bool equal(const String&)const;
bool equal(const char *)const;
bool equal(char *)const;
bool operator!();
void addnew(const String&); //appends a new string into the same string
void removesub(const String&); // removes the substring from the same string
String slice(const String&); // Performs slicing according to explanation below
String slice(const char *); // Performs slicing according to explanation below
int len();// returns length
String lower();
String upper();
String title();
String swapcase();
bool startsWith();

2
NATIONAL UNIVERSITY OF COMPUTER & EMERGING
SCIENCES ISLAMABAD CAMPUS

bool endsWith();
String replace(String&, String&);
String replace(const char *, const char *);
String join(String&);
String join(const char *);
String join(char *);
String* split(String&, int &size);
String* split(const char *, int &size);
String* split(char *, int &size);
~String(); // destructor...
}

Python is a strong language to handle string function, there are multiple functionalities of string function
given below you must implement them in your string class

Let suppose, str = "Owais"

1. Slicing & Indexing


Expression Output Explanation
str.slice(”::-1”) "siawO" Reverses the string.
str.slice(“-1:”) "s" Gets the last character.
str.slice(“:2”) "Ow" Gets the first two characters.
str.slice(“1:4”) "wai" Slices from index 1 to 3.
str.slice(“-3:-1”) "ai" Slices from the third-last to second-last character.
str.slice(“::2”) "Oai" Takes every second character.
str.slice(“1::2”) "ws" Takes every second character starting from index 1.
str.slice(“:-1”) "Owai" Removes the last character.
str.slice(“:3”) "Owa" Takes first 3 characters.
str.slice(“-2:”) "is" Takes last two characters.

2. String Methods & Operations


Expression Output Explanation
str.len(str) 5 Returns length of string.
str.upper() "OWAIS" Converts to uppercase.
str.lower() "owais" Converts to lowercase.
str.title() "Owais" Converts to title case.
str.swapcase() "oWAIS" Swaps uppercase/lowercase.
str.startswith("O") True Checks if string starts with "O".
str.endswith("s") True Checks if string ends with "s".
str.replace("O", "X") "Xwais" Replaces "O" with "X".

3. Splitting & Joining


Expression Output Explanation
str.join("-") "O-w-a-i-s" Joins characters with "-".

3
NATIONAL UNIVERSITY OF COMPUTER & EMERGING
SCIENCES ISLAMABAD CAMPUS

Expression Output Explanation


str.split("a") ["Ow", "is"] Splits on "a".

Q2: Text Editor [75 Marks]


Pagination is a concept used very widely in many platforms. When you write a paragraph or a sentence in
Microsoft Word or Google Docs or any other editor, Pagination always takes place. Moreover, in Web
development, instead of making very long pages with long data, you divide your data into pages and provide
your data separately. Google does it like this:

In this question, you are required to implement a small pagination problem with this concept.
Instead of making a single long character array to store long sentences. We will divide our sentences into
lines. We will make sure that a line does not exceed our screen. So each line will have a maximum limit of
characters. For example, consider my line can store a maximum of 20 characters. Then No more than 20
characters can occur in a line. But we also need to make sure that a word does not break while changing
lines.

Consider the Following:


I add the following String to my Line of Limit 10:
“Hello Everyone”. If I use put a maximum Limit then the word will break in two lines like:
Hello Ever
yone
It should have been:
Hello
Everyone
Make a Line class for this implementation. The line class should have the following structure:
Line l1(100);
l1 += “Hello This is my Assignment 2”;
cout << l1 << endl;

Now Microsoft Word provides the feature of adding separate columns in a Page. Each page contains 2
columns in it. The columns look like this:

Lorem ipsum dolor sit amet, consectetur in voluptate velit esse cillum dolore eu fugiat
adipiscing elit, sed do eiusmod tempor nulla pariatur. Excepteur sint occaecat cupidatat
incididunt ut labore et dolore magna aliqua. Ut non proident, sunt in culpa qui officia deserunt
enim ad minim veniam, quis nostrud exercitation mollit anim id est laborum.
ullamco laboris nisi ut aliquip ex ea commodo Lorem ipsum dolor sit amet, consectetur
consequat. Duis aute irure dolor in reprehenderit adipiscing elit, sed do eiusmod tempor

4
NATIONAL UNIVERSITY OF COMPUTER & EMERGING
SCIENCES ISLAMABAD CAMPUS

incididunt ut labore et dolore magna aliqua. Ut in voluptate velit esse cillum dolore eu fugiat
enim ad minim veniam, quis nostrud exercitation nulla pariatur. Excepteur sint occaecat cupidatat
ullamco laboris nisi ut aliquip ex ea commodo non proident, sunt in culpa qui officia deserunt
consequat. Duis aute irure dolor in reprehenderit mollit anim id est laborum.
in voluptate velit esse cillum dolore eu fugiat Lorem ipsum dolor sit amet, consectetur
nulla pariatur. Excepteur sint occaecat cupidatat adipiscing elit, sed do eiusmod tempor
non proident, sunt in culpa qui officia deserunt incididunt ut labore et dolore magna aliqua. Ut
mollit anim id est laborum. enim ad minim veniam, quis nostrud exercitation
Lorem ipsum dolor sit amet, consectetur ullamco laboris nisi ut aliquip ex ea commodo
adipiscing elit, sed do eiusmod tempor consequat. Duis aute irure dolor in reprehenderit
incididunt ut labore et dolore magna aliqua. Ut in voluptate velit esse cillum dolore eu fugiat
enim ad minim veniam, quis nostrud exercitation nulla pariatur. Excepteur sint occaecat cupidatat
ullamco laboris nisi ut aliquip ex ea commodo non proident, sunt in culpa qui officia deserunt
consequat. Duis aute irure dolor in reprehenderit mollit anim id est laborum.

Here, we first fill the left column. It has a limit of number of line. After that, we fill the right column
which has a limit of number of lines as well.
So our structure goes like this:

1 Page -> 2 Columns -> n lines -> m characters

You need to implement this Editor class which contains this hierarchy. Your classes should work on the
following code:

int main(){
int n = 10; // number of lines in column
int m = 100; //number of characters in each line
Editor editor(n,m);
cin >> editor;
/* user enters:
“This is my test sentence. You should start doing the assignment as soon as possible to avoid any
problems in the end. The assignment is based on many applications of programming and will be very
helpful if you do it yourself.”;
*/
// This will be added as a new paragraph.
editor += “Plagiarism policy is very strict for this assignment and will be checked thoroughly.
Use of AI like chatgpt and deepseek is the easiest way of getting caught.”;

Page p(n,m);
cin >> p;
/* user enters:
“Now i want to add this in a new page. You can add as much as it fits in two columns. The rest
will be discarded.”;
*/
p+= “add this in new paragraph”;
cout << p[0] << endl;// prints first column of page.

Column c(n,m);
cin >> c;

5
NATIONAL UNIVERSITY OF COMPUTER & EMERGING
SCIENCES ISLAMABAD CAMPUS

/* user enters:
“Now add this as a new column. I can only add this to a page if it has the capacity of a column
left in it.” */
p += c;
editor += p;
cout << c[1] << endl;// prints second line of column.

Column c2(c);
c2 += “Now add more data into this column”;
editor += c2;
editor(“My Own Text Editor”);// gives Title to editor
editor(3);// restricts editor to only 3 pages
cout << editor[0] << endl;//print first page
cout << editor << endl;
}

6
NATIONAL UNIVERSITY OF COMPUTER & EMERGING
SCIENCES ISLAMABAD CAMPUS

Q3: Speed Programming [75 Marks]


Constest Invitation Link: Link
Part A:
Ahmed is a renowned calligraphy artist who specializes in crafting beautiful Arabic and English letter
designs. His shop is famous for custom calligraphy, where customers order personalized artwork with their
names or meaningful words. One day, while Ahmed was busy preparing an order for an important customer,
a mischievous kid snuck into his shop and shuffled the letters on his unfinished works! Instead of the
elegant, flowing designs, the letters were now in complete disarray. Ahmed was furious, but there was no
time to redraw everything from scratch. Instead, he decided to salvage whatever he could. He needs to
quickly check whether any of his existing calligraphy pieces contain a prefix that is an anagram of a
customer’s requested word.
Can you help Ahmed restore order in his shop by finding which of his artworks can still be used for the
incoming customer requests?
Definitions Prefix: A prefix of a word is any substring of that word that starts at the beginning and can
include the whole word itself.
Example: All possible prefixes of "trap" are "t", "tr", "tra", and "trap". Anagram: Two strings are anagrams
of each other if one can be rearranged to form the other. Example: "abc" and "bca" are anagrams, but "abc"
and "bcaa" are not.
Input The first line contains an integer N — the number of calligraphy pieces Ahmed has in his shop. The
next N lines contain strings Si representing a calligraphy piece. The next line contains an integer Q — the
number of customer requests. The next Q lines contain a single word Qi, representing a customer's request.
Output For each request Qi, print the number of calligraphy pieces that contain a prefix that is an anagram
of Qi. If no such calligraphy piece exists, print “-1”. Limits 1 ≤ N, Q ≤ 10^5 The length of Si and Qi is in
the inclusive range [1, 40]. Si and Qi only include lowercase english alphabets. The calligraphy pieces are
unique, i.e: all N values of Si are unique.

Sample Input Sample Output


5 4
rat 2
art -1
tarp
part
trap
3
tra
ar
tp

7
NATIONAL UNIVERSITY OF COMPUTER & EMERGING
SCIENCES ISLAMABAD CAMPUS

Part B:
In the kingdom of Numerica, King Tony, known for his love of numbers, presented a challenge to his
subjects. The challenge was simple yet intriguing:
Given the number N, the King declared, "I want to know if a sequence of the first N consecutive numbers,
starting from 1, whose sum is exactly N, exists.
For example, if N is 6, the sum of the first 3 numbers (1 + 2 + 3) equals 6, so the answer is YES.
But what about other numbers? Can you determine whether such a sequence exists for any N?"
The task was clear: find out if there is a sequence of consecutive numbers starting from 1 that sums up to
N. If such a sequence exists, the answer is YES;
if not, it's NO. The King awaited the solution, knowing that those who succeeded would gain his favor and
admiration.
Will you take on the King's puzzle and find the answer?
Input Format
The first line contains the number of test cases - t (1 ≤ t ≤ 100) .
Each test case contains a number n (1 ≤ n ≤ 10^18).
Constraints
t (1 ≤ t ≤ 100) .
n (1 ≤ n ≤ 10^18).
Output Format
output "yes" if exists else "no" case sensitive without quotes

Sample Input Sample Output Explanation


2 yes There are two test cases:
6 no Test 1:
7 1+2+3 = 6

Test 2:
1+2+3+4 > 7

8
NATIONAL UNIVERSITY OF COMPUTER & EMERGING
SCIENCES ISLAMABAD CAMPUS

Part C:
In the heart of the Mughal Empire, Emperor Akbar's court was abuzz with rumors of a secret treasure buried
centuries ago by his ancestor, Babur. The treasure's location was encrypted in a mystical Cipher Grid, a gift
from Persian mathematicians. Only the most intelligent minds in the empire could decipher it.
The responsibility to unlock the secret fell to Mehrunissa, a brilliant scholar, and her mentor, Hakim Ahsan.
The grid, etched on an ancient silk scroll, was filled with numbers arranged in a mysterious pattern.
Mehrunissa decided to start at the top-left corner and traverse the grid diagonally to extract the encoded
sequence.
As Mehrunissa progressed through the grid, the numbers revealed themselves in a precise order, forming a
code. With Hakim Ahsan’s guidance, she then combined the numbers into a single value. To unlock the
treasure chest, they needed to calculate the modulus of this number with a specific divisor qq, chosen by
the Emperor himself.
“Mehrunissa, you’ve solved the puzzle of the emperors!” Hakim Ahsan exclaimed. The result was the key
that would guide them to Babur's long-lost treasure.
Task Details:
To simulate Mehrunissa’s challenge, you will:
1. Traverse the grid diagonally to extract numbers.
2. Combine the numbers into a single value.
3. Calculate the modulus of this value with q, as specified by Emperor Akbar.
Input Explanation:
1. The first line contains n, representing the size of the n×n Cipher Grid.
2. The second line contains the divisor q, chosen by the Emperor.
3. The next n lines represent the grid values.
4. If the grid is not n×n, print Impossible.
Example Input and Output:
Input:
3
396
123
456
789
Output:
124753689
225

Input:
4
177777
3792
1648
5203
8974
Output:
3715692428908374
156093

9
NATIONAL UNIVERSITY OF COMPUTER & EMERGING
SCIENCES ISLAMABAD CAMPUS

Instructions:
Traverse the Cipher Grid diagonally, combine the numbers into a sequence, and calculate the modulus
using q. This final number is the key to unlocking Babur’s legendary treasure!

Happy Coding 😊

10

You might also like