Professional Documents
Culture Documents
Student Name:
Tooba Arshad
Roll No:
221526
QNO:1
Create a function that splits a string into an array of identical clusters.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<string> splitGroups(const string& str) {
vector<string> result;
if (str.empty()) {
return result;
}
string currentCluster(1, str[0]);
for (size_t i = 1; i < str.size(); ++i) {
if (str[i] == str[i - 1]) {
currentCluster += str[i];
} else {
result.push_back(currentCluster);
currentCluster = string(1, str[i]);
}
}
result.push_back(currentCluster);
return result;
}
int main() {
// Examples
vector<string> result1 = splitGroups("555");
for (const string& cluster : result1) {
cout << cluster << " ";
}
cout << endl;
vector<string> result2 = splitGroups("5556667788");
for (const string& cluster : result2) {
cout << cluster << " ";
}
cout << endl;
vector<string> result3 = splitGroups("aaabbbaabbab");
for (const string& cluster : result3) {
cout << cluster << " ";
}
cout << endl;
vector<string> result4 = splitGroups("abbbcc88999&&!!!_");
for (const string& cluster : result4) {
cout << cluster << " ";
}
cout << endl;
return 0;
}
QNO:2
Loopsregexstringsvalidation
#include <iostream>
#include <string>
using namespace std;
bool sameLength(const string& str) {
int onesCount = 0;
int zeroesCount = 0;
for (char ch : str) {
if (ch == '1') {
onesCount++;
} else if (ch == '0') {
zeroesCount++;
} else {
return false;
}
return 0;
}
QNO:3
A class can have 9 students max; each class can have 1 teacher and 9 students
(makingthetotal 10). Write a function where we take total students and
number of teachers and write howmany max classes there will be. A class
should have one teacher min/max. Return the answer in an Array
CODE:
#include <iostream>
#include <vector>
if (tTeachers == 0) {
return 0; // No classes can be formed without a teacher
}
int main() {
int tStudents = 25;
int tTeachers = 3;
cout << "Number of classes: " << result.size() << " " << endl;
cout << "Distribution of students per class: ";
for (int numStudents : result) {
cout << numStudents << " ";
}
cout << " " << endl;
return 0;
}