You are on page 1of 6

Teacher Name:

Sir Hilmand khan

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;
}

if (zeroesCount > 0 && onesCount != zeroesCount) {


return false;
}
}

return onesCount == zeroesCount;


}
int main() {

cout << boolalpha << sameLength("110011100010") << endl; // Output: tru


cout << boolalpha << sameLength("101010110") << endl; // Output: false
cout << boolalpha << sameLength("111100001100") << endl; // Output: true
cout << boolalpha << sameLength("111") << endl; // Output: 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>

using namespace std;


vector<int> calculateClasses(int tStudents, int tTeachers) {
const int maxspClass = 9;
const int minspClass = 1;
const int studentsPerTeacher = maxspClass - minspClass + 1;

if (tTeachers == 0) {
return 0; // No classes can be formed without a teacher
}

int maxClasses = min(tStudents / studentsPerTeacher, tTeachers);


int remainingStudents = tStudents % studentsPerTeacher;

vector<int> classes(maxClasses, maxspClass);

for (int i = 0; i < maxClasses; ++i) {


if (remainingStudents > 0) {
classes[i] += minspClass;
remainingStudents--;
}
}
return classes;
}

int main() {
int tStudents = 25;
int tTeachers = 3;

vector<int> result = calculateClasses(tStudents, tTeachers);

cout << "Number of classes: " << result.size() << " " << endl;
cout << "Distribution of students per class: ";
for (int numStudents : result) {
cout << numStudents << " ";
}
cout << " " << endl;

return 0;
}

You might also like