Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit0139d20b8844b4f1946c84b07e567faf5ba618c7 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2021-11-23 16:59:41 +0200
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2021-11-23 16:59:41 +0200
Filesd2d06058ccce1750c0a1cdc0ac590eac53487010
uni-projects-0139d20b8844b4f1946c84b07e567faf5ba618c7.tar
uni-projects-0139d20b8844b4f1946c84b07e567faf5ba618c7.tar.gz
uni-projects-0139d20b8844b4f1946c84b07e567faf5ba618c7.zip
Parent7a59e1811ccc5f2ff2d91647434a34900ca23f82 (diff)
Added my solutions to todays programming introduction exam
diff --git a/Year 1/Programming introduction/Exams/Exam1/prob1.cpp b/Year 1/Programming introduction/Exams/Exam1/prob1.cpp
new file mode 100644
index 0000000..541c60c
--- /dev/null
+++ b/Year 1/Programming introduction/Exams/Exam1/prob1.cpp
@@ -0,0 +1,45 @@
+#include <iostream>
+
+using namespace std;
+
+bool canConcatNumber(unsigned int N, unsigned int K) {
+ if (N < 100) return false;
+
+ /* Get middle digits (which are reversed) */
+
+ unsigned int middleN = 1; // using 1 for cases where we have zeroes between our end digits, like in 1002001
+ for (unsigned int i = 0; N != 0; i++) {
+ if (i > 0 && N > 9)
+ (middleN *= 10) += N % 10;
+ N /= 10;
+ }
+
+ /* Get width of K as a power of 10 and reverse K */
+
+ unsigned int kWidth = 1, revK = 0;
+ for (unsigned int tmpK = K; tmpK != 0; kWidth *= 10) {
+ (revK *= 10) += tmpK % 10;
+ tmpK /= 10;
+ }
+
+ /* Check if revK is inside middleN */
+
+ while (middleN != 1) {
+ if ((middleN - revK) % kWidth == 0)
+ return true;
+ middleN /= 10;
+ }
+
+ return false;
+}
+
+int main() {
+ cout << "N = ";
+ int N;
+ cin >> N;
+ cout << "K = ";
+ int K;
+ cin >> K;
+
+ cout << (canConcatNumber(N, K) ? "True" : "False") << endl;
+}
diff --git a/Year 1/Programming introduction/Exams/Exam1/prob2.cpp b/Year 1/Programming introduction/Exams/Exam1/prob2.cpp
new file mode 100644
index 0000000..c941112
--- /dev/null
+++ b/Year 1/Programming introduction/Exams/Exam1/prob2.cpp
@@ -0,0 +1,47 @@
+#include <iostream>
+
+using namespace std;
+
+bool isNeighbourNum(int n) {
+ while (n != 0) {
+ if (n % 10 == (n / 10) % 10)
+ return false;
+ n /= 10;
+ }
+ return true;
+}
+
+int subNeighbours(int a, int b) {
+ int smallest = 0, biggest = 0;
+
+ for (int i = a; i <= b; i++) {
+ if (isNeighbourNum(i)) {
+ if (i < smallest || !smallest)
+ smallest = i;
+ if (i > biggest || !biggest)
+ biggest = i;
+ }
+ }
+
+ if (!smallest || !biggest) {
+ return -1;
+ }
+
+ return biggest - smallest;
+}
+
+int main() {
+ cout << "a = ";
+ int a;
+ cin >> a;
+ cout << "b = ";
+ int b;
+ cin >> b;
+
+ int sub = subNeighbours(a, b);
+
+ if (sub == -1)
+ cout << "No adjacent numbers!" << endl;
+ else
+ cout << sub << endl;
+}
diff --git a/Year 1/Programming introduction/Exams/Exam1/prob3.cpp b/Year 1/Programming introduction/Exams/Exam1/prob3.cpp
new file mode 100644
index 0000000..07a21f2
--- /dev/null
+++ b/Year 1/Programming introduction/Exams/Exam1/prob3.cpp
@@ -0,0 +1,114 @@
+#include <iostream>
+
+using namespace std;
+
+const int MAX_SIZE = 1024;
+
+int deletedFromStartToSubset(char src[], int srcLen, char toCheck[], int toCheckLen) {
+ for (int i = 0, j = 0; i <= srcLen - toCheckLen; i++) {
+ /* Check if values from toCheck correspond to those of src with offset */
+
+ for (j = 0; j < toCheckLen; j++) {
+ if (src[j + i] != toCheck[j])
+ break;
+ }
+
+ if (j == toCheckLen)
+ return i;
+ }
+ return -1;
+}
+
+int deletedAndRotationIsSubset(char src[], int srcLen, char toCheck[], int toCheckLen) {
+ for (int rot = 0; rot < toCheckLen; rot++) {
+ /* Check if subset */
+
+ int deleted = deletedFromStartToSubset(src, srcLen, toCheck, toCheckLen);
+ if (deleted >= 0)
+ return deleted;
+
+ /* Rotate toCheck */
+
+ char lastVal = toCheck[toCheckLen - 1];
+ for (int i = toCheckLen - 2; i >= 0; i--) {
+ toCheck[i + 1] = toCheck[i];
+ }
+ toCheck[0] = lastVal;
+ }
+ return -1;
+}
+
+/* Bonus */
+
+int validRotationByDelSrc(char src[], int srcLen, char toCheck[], int toCheckLen, int deletedIndexes[]) {
+ char common[MAX_SIZE];
+
+ int di = 1;
+ for (int i = 0, ci = 0; i < srcLen; i++) {
+ for (int j = 0; j < toCheckLen; j++) {
+ if (src[i] == toCheck[j]) {
+ common[ci++] = toCheck[j];
+ deletedIndexes[di++] = i; // I know this is wrong, but I didn't have time to fix it :D
+ break;
+ }
+ }
+ }
+ deletedIndexes[0] = di; // Length at position 0
+
+ return deletedAndRotationIsSubset(common, toCheckLen, toCheck, toCheckLen);
+}
+
+int main() {
+
+
+ /* Get input */
+
+ cout << "size1: ";
+ int size1;
+ cin >> size1;
+ cout << "size2: ";
+ int size2;
+ cin >> size2;
+
+ cout << "firstArr: ";
+ char firstArr[MAX_SIZE];
+ for (int i = 0; i < size1; i++)
+ cin >> firstArr[i];
+
+ cout << "secondArr: ";
+ char secondArr[MAX_SIZE];
+ for (int i = 0; i < size2; i++)
+ cin >> secondArr[i];
+
+ /* Output result */
+
+ int deleted = deletedAndRotationIsSubset(firstArr, size1, secondArr, size2);
+
+ if (deleted == -1) {
+ int deletedIndexes[MAX_SIZE];
+ deleted = validRotationByDelSrc(firstArr, size1, secondArr, size2, deletedIndexes);
+
+ if (deleted == -1)
+ cout << "No rotations found!" << endl;
+ else {
+ cout << "Rotation \"";
+
+ for (int i = 0; i < size2; i++)
+ cout << secondArr[i];
+
+ cout << "\" found after deleting characters: ";
+
+ for (int i = 1; i < deletedIndexes[0]; i++)
+ cout << deletedIndexes[i] << " ";
+ cout << endl;
+ }
+ }
+ else {
+ cout << "Rotation \"";
+
+ for (int i = 0; i < size2; i++)
+ cout << secondArr[i];
+
+ cout << "\" found after deleting " << deleted << " characters from the start and " << (size1 - size2 - deleted) << " characters from the end." << endl;
+ }
+}