diff --git a/13_ExOh.cpp b/13_ExOh.cpp index 1629c46..ac1940f 100644 --- a/13_ExOh.cpp +++ b/13_ExOh.cpp @@ -9,13 +9,21 @@ string ExOh(string str) { int xCount = 0, oCount = 0; - // Simple iteration to keep count on how many characters are either x or o - for (int x = 0; x < str.length(); x++) + // // Simple iteration to keep count on how many characters are either x or o + // for (int x = 0; x < str.length(); x++) + // { + // if (str[x] == 'x') + // xCount++; + + // if (str[x] == 'o') + // oCount++; + // } + for (auto& x : str) { - if (str[x] == 'x') + if (x == 'x') xCount++; - if (str[x] == 'o') + if (x == 'o') oCount++; } diff --git a/14_Palidrome.cpp b/14_Palidrome.cpp index a1c9639..1ab5754 100644 --- a/14_Palidrome.cpp +++ b/14_Palidrome.cpp @@ -8,26 +8,32 @@ string Palindrome(string str) { string temp; - for (int y = 0; y < str.length(); y++) + // for (int y = 0; y < str.length(); y++) + // { + // if (str[y] != ' ') + // { + // temp.push_back(str[y]); + // } + // } + // int size = temp.length() - 1; + + // // Compare the new string by analyzing the characters from the front and back + // for (int x = 0; x < temp.length(); x++) + // { + // if (temp[x] != temp[size]) + // { + // return "false"; + // } + // size--; + // } + + for (int i = 0; i <= str.length() / 2; i++) { - if (str[y] != ' ') - { - temp.push_back(str[y]); - } - } - int size = temp.length() - 1; - - // Compare the new string by analyzing the characters from the front and back - for (int x = 0; x < temp.length(); x++) - { - if (temp[x] != temp[size]) - { + if (str[i] != str[str.length()-i-1]) return "false"; - } - size--; } - return "true"; + return "true"; } int main() { @@ -36,7 +42,7 @@ int main() { cout << Palindrome("racecar") << endl; // true cout << Palindrome("eye") << endl; // true cout << Palindrome("what is this") << endl; // false - cout << Palindrome("never odd or even") << endl; // true + cout << Palindrome("never odd or even") << endl; // false return 0; diff --git a/15_ArithGeo.cpp b/15_ArithGeo.cpp index ee6d776..c2e2463 100644 --- a/15_ArithGeo.cpp +++ b/15_ArithGeo.cpp @@ -14,28 +14,42 @@ string ArithGeo(int arr[], const int size) { difference = arr[1] - arr[0]; difference2 = arr[1] / arr[0]; - // Check is the difference is true for all elements - // If true than the array is arithmetic - for (int y = 0; y < size-1 && arith; y++) + // // Check is the difference is true for all elements + // // If true than the array is arithmetic + // for (int y = 0; y < size-1 && arith; y++) + // { + // arith = false; + // if (arr[y] + difference == arr[y + 1]) + // { + // arith = true; + // } + // } + + // // Check is the difference is true for all elements + // // If true than the array is geometric + // for (int z = 0; z < size - 1 && geo; z++) + // { + // geo = false; + // if (arr[z] * difference2 == arr[z + 1]) + // { + // geo = true; + // } + // } + + for (int x = 0; x < size - 1 && (geo || arith); x++) { - arith = false; - if (arr[y] + difference == arr[y + 1]) + if (arr[x] + difference != arr[x + 1]) { - arith = true; + arith = false; } - } - - // Check is the difference is true for all elements - // If true than the array is geometric - for (int z = 0; z < size - 1 && geo; z++) - { - geo = false; - if (arr[z] * difference2 == arr[z + 1]) + if (arr[x] * difference2 != arr[x + 1]) { - geo = true; + geo = false; } } + + if (arith) { return "Arithmetic"; diff --git a/16_ArrayAddition.cpp b/16_ArrayAddition.cpp index 7fd9d06..6bedc62 100644 --- a/16_ArrayAddition.cpp +++ b/16_ArrayAddition.cpp @@ -2,10 +2,11 @@ // take the array of numbers stored in arr and return the string true if any combination of numbers in the array can be added up to equal the largest number in the array, otherwise return the string false. For example: if arr contains [4, 6, 23, 10, 1, 3] the output should return true because 4 + 6 + 10 + 3 = 23. The array will not be empty, will not contain all the same elements, and may contain negative numbers. -#include +// #include #include #include #include +#include using namespace std; // Linear search function to aid with the ArrayAddition function @@ -85,19 +86,94 @@ string ArrayAdditionI(int arr[], int size) return "false"; } +// ChatGPT +string combination_sum(vector arr) { + int largest = *max_element(arr.begin(), arr.end()); + arr.erase(remove(arr.begin(), arr.end(), largest), arr.end()); + if (largest == 0) { + return "true"; + } + for (int i = 1; i < (1 << arr.size()); i++) { + vector combination; + for (int j = 0; j < arr.size(); j++) { + if (i & (1 << j)) { + combination.push_back(arr[j]); + } + } + if (accumulate(combination.begin(), combination.end(), 0) == largest) { + return "true"; + } + } + return "false"; +} + +// ChatGPT2 +// void generate_combinations(int index, vector arr, vector combination, vector>& combinations){ +// if (index == arr.size()) { +// combinations.push_back(combination); +// return; +// } +// generate_combinations(index + 1, arr, combination, combinations); +// combination.push_back(arr[index]); +// generate_combinations(index + 1, arr, combination, combinations); +// } +string combination_sum2(vector arr) { + int largest_num = *max_element(arr.begin(), arr.end()); + arr.erase(remove(arr.begin(), arr.end(), largest_num), arr.end()); + vector> combinations; + + function)> generate_combinations = [&](int index, vector combination){ + if (index == arr.size()) + { + combinations.push_back(combination); + return; + } + generate_combinations(index + 1, combination); + combination.push_back(arr[index]); + generate_combinations(index + 1, combination); + }; + + // generate_combinations(0, arr, {}, combinations); + generate_combinations(0, {}); + + for (const auto& combination : combinations) { + if (accumulate(combination.begin(), combination.end(), 0) == largest_num) { + return "true"; + } + } + return "false"; + +} + int main() { // keep this function call here /* Note: In C++ you first have to initialize an array and set it equal to the stdin to test your code with arrays. */ - int A[] = { 5, 7, 16, 1, 2 }; - int B[] = { 3, 5, -1, 8, 12 }; - int C[] = { 4, 6, 23, 10, 1, 3 }; - int D[] = { 10, 12, 500, 1, -5, 1, 0 }; - cout << ArrayAdditionI(A, sizeof(A) / sizeof(A[0])) << endl; // false - cout << ArrayAdditionI(B, sizeof(B) / sizeof(B[0])) << endl; // true - cout << ArrayAdditionI(C, sizeof(C) / sizeof(C[0])) << endl; // true - cout << ArrayAdditionI(D, sizeof(D) / sizeof(D[0])) << endl; // false + // int A[] = { 5, 7, 16, 1, 2 }; + // int B[] = { 3, 5, -1, 8, 12 }; + // int C[] = { 4, 6, 23, 10, 1, 3 }; + // int D[] = { 10, 12, 500, 1, -5, 1, 0 }; + // cout << ArrayAdditionI(A, sizeof(A) / sizeof(A[0])) << endl; // false + // cout << ArrayAdditionI(B, sizeof(B) / sizeof(B[0])) << endl; // true + // cout << ArrayAdditionI(C, sizeof(C) / sizeof(C[0])) << endl; // true + // cout << ArrayAdditionI(D, sizeof(D) / sizeof(D[0])) << endl; // false + vector arrA = {5, 7, 16, 1, 2}; + vector arrB = {3, 5, -1, 8, 12}; + vector arrC = {4, 6, 23, 10, 1, 3}; + vector arrD = {10, 12, 500, 1, -5, 1, 0}; + + cout << combination_sum(arrA) << endl; // false + cout << combination_sum(arrB) << endl; // true + cout << combination_sum(arrC) << endl; // true + cout << combination_sum(arrD) << endl; // false + + cout << combination_sum2(arrA) << endl; // false + cout << combination_sum2(arrB) << endl; // true + cout << combination_sum2(arrC) << endl; // true + cout << combination_sum2(arrD) << endl; // false return 0; -} \ No newline at end of file +} + + \ No newline at end of file diff --git a/17_LetterCount1.cpp b/17_LetterCount1.cpp index 6612dfa..b4f0302 100644 --- a/17_LetterCount1.cpp +++ b/17_LetterCount1.cpp @@ -3,95 +3,134 @@ #include #include +#include using namespace std; string LetterCountI(string str) { - int size = str.length(); - int count = 0, count2 = 0, high = 0, high2 = 0; - int start = -1, end = 0; - int temp, temp2; - char letter; - - for (int x = 0; x < size; x++, end++) - { - if (start == -1) // find beginning of the word - { - start = x; - } - - if (str[x] == ' ' || x == size-1) - { - // Safeguard in the case the correct word is at the end of the string - if (end == size - 1) - { - end += 1; - } - - for (int y = start; y < end; y++) - { - // selecting letter from word to compare - letter = str[y]; - - // Loop to check if letter repeats - for (int z = start; z < end; z++) - { - if (y == z) - { - continue; - } - else if (letter == str[z]) - { - count++; - } - } - - if (count) - { - count2++; - } + // int size = str.length(); + // int count = 0, count2 = 0, high = 0, high2 = 0; + // int start = -1, end = 0; + // int temp, temp2; + // char letter; + + // for (int x = 0; x < size; x++, end++) + // { + // if (start == -1) // find beginning of the word + // { + // start = x; + // } + + // if (str[x] == ' ' || x == size-1) + // { + // // Safeguard in the case the correct word is at the end of the string + // if (end == size - 1) + // { + // end += 1; + // } + + // for (int y = start; y < end; y++) + // { + // // selecting letter from word to compare + // letter = str[y]; + + // // Loop to check if letter repeats + // for (int z = start; z < end; z++) + // { + // if (y == z) + // { + // continue; + // } + // else if (letter == str[z]) + // { + // count++; + // } + // } + + // if (count) + // { + // count2++; + // } - // Keep track of words with repeated letters - if (count > high && count2 > high2) - { - high = count; - high2 = count2; - temp = start; - temp2 = end; - } - - count = 0; - } - - // Keep track of words with THE MOST repeated letters - if (count2 > high2) + // // Keep track of words with repeated letters + // if (count > high && count2 > high2) + // { + // high = count; + // high2 = count2; + // temp = start; + // temp2 = end; + // } + + // count = 0; + // } + + // // Keep track of words with THE MOST repeated letters + // if (count2 > high2) + // { + // high2 = count2; + // temp = start; + // temp2 = end; + // } + // count2 = 0; + + // start = end+1; + // } + // } + + // // Passing the first word with most repeated letters + // if (high > 0) + // { + // string repeated; + + // for (temp; temp < temp2; temp++) + // { + // repeated.push_back(str[temp]); + // } + + // return repeated; + // } + // else + // { + // return "-1"; + // } + + unordered_map count; + int curr_c = 0; + string word; + int pre_c = 1; + string result = "-1"; + set> test; + for (auto s : str) + { + if (s == ' '){ + test.insert({curr_c, word}); + if (curr_c > pre_c) { - high2 = count2; - temp = start; - temp2 = end; + result = word; } - count2 = 0; - - start = end+1; + word.clear(); + count.clear(); + pre_c = curr_c; + curr_c = 0; + continue; } - } - // Passing the first word with most repeated letters - if (high > 0) - { - string repeated; - - for (temp; temp < temp2; temp++) + count[s] += 1; + word += s; + if (count[s] > curr_c) { - repeated.push_back(str[temp]); + curr_c = count[s]; } - - return repeated; } - else + + cout << test.rbegin()->second << endl; + cout << (--test.end())->second << endl; + + if (curr_c > pre_c) { - return "-1"; + result = word; } + return result; } int main() { diff --git a/18_SecondGreatLow.cpp b/18_SecondGreatLow.cpp index f83b443..045befd 100644 --- a/18_SecondGreatLow.cpp +++ b/18_SecondGreatLow.cpp @@ -3,71 +3,78 @@ #include #include +#include using namespace std; void SecondGreatLow(int arr[], int size) { - if (size == 2) // In the case that there is only 2 numbers - { - if (arr[0] < arr[1]) - { - cout << arr[0] << " " << arr[1]; - } - else - { - cout << arr[1] << " " << arr[0]; - } - } - else - { - // Implementing a bubble sort to sort the array from least to greatest - bool swap; - int temp; + // if (size == 2) // In the case that there is only 2 numbers + // { + // if (arr[0] < arr[1]) + // { + // cout << arr[0] << " " << arr[1]; + // } + // else + // { + // cout << arr[1] << " " << arr[0]; + // } + // } + // else + // { + // // Implementing a bubble sort to sort the array from least to greatest + // bool swap; + // int temp; - do - { - swap = false; + // do + // { + // swap = false; - for (int x = 0; x < size - 1; x++) - { - if (arr[x] > arr[x + 1]) - { - temp = arr[x]; - arr[x] = arr[x + 1]; - arr[x + 1] = temp; - swap = true; - } - } - } while (swap); + // for (int x = 0; x < size - 1; x++) + // { + // if (arr[x] > arr[x + 1]) + // { + // temp = arr[x]; + // arr[x] = arr[x + 1]; + // arr[x + 1] = temp; + // swap = true; + // } + // } + // } while (swap); - int index = 0; - int index2 = size - 1; - // Loop to check if the first values are the same ex . 2,2,2,2..... - for (int y = 0; y < size-1; y++) - { - if (arr[y] == arr[y + 1]) - { - index++; - } - else - { - break; - } - } - // Loop to check if the last values are the same ex .......45,45,45 - for (int z = size - 1; size > 0; z--) - { - if (arr[z] == arr[z - 1]) - { - index2--; - } - else - { - break; - } - } - cout << arr[index+1] << " " << arr[index2-1]; + // int index = 0; + // int index2 = size - 1; + // // Loop to check if the first values are the same ex . 2,2,2,2..... + // for (int y = 0; y < size-1; y++) + // { + // if (arr[y] == arr[y + 1]) + // { + // index++; + // } + // else + // { + // break; + // } + // } + // // Loop to check if the last values are the same ex .......45,45,45 + // for (int z = size - 1; size > 0; z--) + // { + // if (arr[z] == arr[z - 1]) + // { + // index2--; + // } + // else + // { + // break; + // } + // } + // cout << arr[index+1] << " " << arr[index2-1]; + // } + set tmp; + for (auto i = 0; i < size;i++) + { + tmp.insert(arr[i]); } + cout << *++tmp.begin() << " " << *--tmp.rbegin(); } int main() { diff --git a/19_DivisionSpringified.cpp b/19_DivisionSpringified.cpp index cd669e2..34f6561 100644 --- a/19_DivisionSpringified.cpp +++ b/19_DivisionSpringified.cpp @@ -5,6 +5,7 @@ #include #include #include +#include using namespace std; string DivisionStringified(int num1, int num2) { @@ -34,7 +35,7 @@ string DivisionStringified(int num1, int num2) { count++; } } - + return temp2; } diff --git a/1_FirstReverse.cpp b/1_FirstReverse.cpp index 58b5f5c..b53ef45 100644 --- a/1_FirstReverse.cpp +++ b/1_FirstReverse.cpp @@ -17,6 +17,14 @@ string FirstReverse(string str) { index++; } return str; + + // // my self + // string temp; + // for (int x = str.length()-1; x >= 0; x--) + // { + // temp += str[x]; + // } + // return temp; } int main() diff --git a/34_RectangleArea.cpp b/34_RectangleArea.cpp index 295e0ee..912a934 100644 --- a/34_RectangleArea.cpp +++ b/34_RectangleArea.cpp @@ -1,144 +1,59 @@ // For this challenge you will determine the area of a rectangle. -// have the function RectangleArea(strArr) take the array of strings stored in strArr, which will only contain 4 elements and be in the form (x y) where x and y are both integers, and return the area of the rectangle formed by the 4 points on a Cartesian grid. The 4 elements will be in arbitrary order. For example: if strArr is ["(0 0)", "(3 0)", "(0 2)", "(3 2)"] then your program should return 6 because the width of the rectangle is 3 and the height is 2 and the area of a rectangle is equal to the width * height. +/* +have the function RectangleArea(strArr) take the array of strings stored in strArr, +which will only contain 4 elements and be in the form (x y) where x and y are both integers, +and return the area of the rectangle formed by the 4 points on a Cartesian grid. +The 4 elements will be in arbitrary order. For example: if strArr is ["(0 0)", "(3 0)", "(0 2)", "(3 2)"] +then your program should return 6 because the width of the rectangle is 3 and the height is 2 and the area +of a rectangle is equal to the width * height. +*/ + +#include -#include -#include using namespace std; int RectangleArea(string strArr[]) { - // Strings to store the points for X and Y - string tempX; - string tempY; - - // Will store the first X and Y point from the first string - // Using condition to check if number is negative or positive - if (strArr[0][1] == '-') - { - tempX.push_back(strArr[0][2]); - if (strArr[0][4] == '-') - { - tempY.push_back(strArr[0][5]); - } - else - { - tempY.push_back(strArr[0][4]); - } - } - else - { - tempX.push_back(strArr[0][1]); - if (strArr[0][3] == '-') - { - tempY.push_back(strArr[0][4]); - } - else - { - tempY.push_back(strArr[0][3]); - } - } - - // Check the x and y point of every string to determine the width and height - // Again will check for negative number - for (int x = 0; x < 4; x++) - { - if (strArr[x][1] == '-') - { - if (strArr[x][2] != tempX[0] && tempX.length() == 1) - { - tempX.push_back(strArr[x][2]); - } - - if (strArr[0][4] == '-') - { - if (strArr[x][5] != tempY[0] && tempY.length() == 1) - { - tempY.push_back(strArr[x][5]); - } - } - else - { - if (strArr[x][4] != tempY[0] && tempY.length() == 1) - { - tempY.push_back(strArr[x][4]); - } - } - - } - else - { - if (strArr[x][1] != tempX[0] && tempX.length() == 1) - { - tempX.push_back(strArr[x][1]); - } - - if (strArr[x][3] == '-') - { - if (strArr[x][4] != tempY[0] && tempY.length() == 1) - { - tempY.push_back(strArr[x][4]); - } - } - else - { - if (strArr[x][3] != tempY[0] && tempY.length() == 1) - { - tempY.push_back(strArr[x][3]); - } - } - } - } - - // In the case that the axis were the same - // Copy the same value for ease of calculation - if (tempX.length() == 1) - { - tempX.push_back(tempX[0]); - } - if (tempY.length() == 1) - { - tempY.push_back(tempY[0]); - } - - // Calculating to find the numeric value of the width and the height - int width, height; - if (tempX[0] >= tempX[1]) - { - width = (int(tempX[0]) - 48) - (int(tempX[1]) - 48); - } - else - { - width = (int(tempX[1]) - 48) - (int(tempX[0]) - 48); - } - if (tempY[0] >= tempY[1]) - { - height = (int(tempY[0]) - 48) - (int(tempY[1]) - 48); - } - else - { - height = (int(tempY[1]) - 48) - (int(tempY[0]) - 48); - } - - return width * height; + vector x, y; + stringstream ss; + int num; + char c; + + for (auto i = 0; i < 4; i++){ + ss << strArr[i]; + ss >> c; + ss >> num; + x.push_back(num); + ss >> num; + y.push_back(num); + ss >> c; + } + + int x_min = *min_element(x.begin(),x.end()); + int x_max = *max_element(x.begin(),x.end()); + int y_min = *min_element(y.begin(),y.end()); + int y_max = *max_element(y.begin(),y.end()); + + return (x_max - x_min)*(y_max - y_min); } int main() { - // keep this function call here - /* Note: In C++ you first have to initialize an array and set - it equal to the stdin to test your code with arrays. */ - - string A[] = { "(1 1)", "(1 3)", "(3 1)", "(3 3)" }; - string B[] = { "(0 0)", "(1 0)", "(1 1)", "(0 1)" }; - string C[] = { "(0 0)", "(3 0)", "(0 2)", "(3 2)" }; - string D[] = { "(-1 -1)", "(0 0)", "(-1 0)", "(0 -1)" }; - string E[] = { "(-2 -2)", "(0 0)", "(-2 0)", "(0 -2)" }; - string F[] = { "(0 0)", "(0 0)", "(0 0)", "(0 0)" }; - cout << RectangleArea(A) << endl; // 4 - cout << RectangleArea(B) << endl; // 1 - cout << RectangleArea(C) << endl; // 6 - cout << RectangleArea(D) << endl; // 1 - cout << RectangleArea(E) << endl; // 4 - cout << RectangleArea(F) << endl; // 0 - return 0; + // keep this function call here + /* Note: In C++ you first have to initialize an array and set + it equal to the stdin to test your code with arrays. */ + + string A[] = { "(1 1)", "(1 3)", "(3 1)", "(3 3)" }; + string B[] = { "(0 0)", "(1 0)", "(1 1)", "(0 1)" }; + string C[] = { "(0 0)", "(3 0)", "(0 2)", "(3 2)" }; + string D[] = { "(-1 -1)", "(0 0)", "(-1 0)", "(0 -1)" }; + string E[] = { "(-2 -2)", "(0 0)", "(-2 0)", "(0 -2)" }; + string F[] = { "(0 0)", "(0 0)", "(0 0)", "(0 0)" }; + cout << RectangleArea(A) << endl; // 4 + cout << RectangleArea(B) << endl; // 1 + cout << RectangleArea(C) << endl; // 6 + cout << RectangleArea(D) << endl; // 1 + cout << RectangleArea(E) << endl; // 4 + cout << RectangleArea(F) << endl; // 0 + return 0; } \ No newline at end of file diff --git a/46_BinaryReversal.cpp b/46_BinaryReversal.cpp index cb9a914..a06951c 100644 --- a/46_BinaryReversal.cpp +++ b/46_BinaryReversal.cpp @@ -7,25 +7,19 @@ #include using namespace std; -int BinaryReversal(string str) +string BinaryReversal1(string str) { - int num; - istringstream(str) >> num; + int num = stoi(str); // converting to a binary str = ""; - bool binary = false; - while (!binary) + + while (!(num == 0 && str.length() % 8 == 0)) { - stringstream convert; - convert << num % 2; - str += convert.str(); + int n = num % 2; + str += to_string(n); num /= 2; - if (num == 0 && str.length() % 8 == 0) // Condition to make sure the result is 8 bits - { - binary = true; - } } // Converting back to a decimal @@ -34,11 +28,26 @@ int BinaryReversal(string str) { if (str[x] == '1') { - total += pow(2, (str.length() - 1) - x); + // total += pow(2, (str.length() - 1) - x); + total += (1 << (str.length() - 1 - x)); } } - return total; + return to_string(total); +} +string BinaryReversal(string str){ + int num = stoi(str); + string tmp; + while (!(num == 0 && tmp.length()%8 == 0)){ + tmp += to_string(num % 2); + num /= 2; + } + int res = 0; + for (int i = 0; i < tmp.length(); i++){ + if (tmp[i] == '1') + res += 1 << (tmp.length() - 1 - i); + } + return to_string(res); } int main() diff --git a/47_ArrayMatching.cpp b/47_ArrayMatching.cpp index 1692416..24d44da 100644 --- a/47_ArrayMatching.cpp +++ b/47_ArrayMatching.cpp @@ -7,83 +7,71 @@ #include using namespace std; -string ArrayMatching(string strArr[], int size) -{ - int length = 0, length2 = 100 * 100, total, high; - vector > list(size); - vector newArray; - string result; +string ArrayMatching_GPT(string strArr[], int size){ + vector arr1, arr2, result; + stringstream ss1(strArr[0]), ss2(strArr[1]); + char c; + int num; - for (int row = 0; row < size; row++) - { - for (int col = 0; col < strArr[row].length(); col++) - { - if (strArr[row][col] == '[' || strArr[row][col] == ' ') // Removing unnecessary characters - { - strArr[row].erase(strArr[row].begin() + col); - } - } + // parse the first array + ss1 >> c; // skip opening bracket + while (ss1 >> num) { + arr1.push_back(num); + ss1 >> c; // skip comma or closing bracket + } - // Passing our string values to a new int list - // This will make it easier for calculation and other requirements - string temp; - for (int x = 0; x < strArr[row].length(); x++) - { - if (strArr[row][x] == ',' || x == strArr[row].length() - 1) - { - int num; - istringstream(temp) >> num; - list[row].push_back(num); - temp.clear(); - } - else - { - temp.push_back(strArr[row][x]); - } - } + // parse the second array + ss2 >> c; // skip opening bracket + while (ss2 >> num) { + arr2.push_back(num); + ss2 >> c; // skip comma or closing bracket + } - // Condition to keep track of the largest and shortest sub array - if (list[row].size() > length) - { - length = list[row].size(); - high = row; // will determine from which sub-array we need to append if need to - } - if (list[row].size() < length2) - { - length2 = list[row].size(); - } - } + // add the arrays element-wise + int n = max(arr1.size(), arr2.size()); + for (int i = 0; i < n; i++) { + int sum = (i < arr1.size() ? arr1[i] : 0) + (i < arr2.size() ? arr2[i] : 0); + result.push_back(sum); + } + + // convert the result to a string + stringstream ss; + for (int i = 0; i < result.size(); i++) { + ss << result[i]; + if (i < result.size() - 1) ss << "-"; + } + return ss.str(); +} +string ArrayMatching(string strArr[], int size){ + stringstream ss1(strArr[0]), ss2(strArr[1]); + vector arr1, arr2, res; + int num; + char c; - // Loop to perform the calculation - for (int x = 0; x < length2; x++) - { - total = 0; - total += list[0][x]; - total += list[1][x]; - newArray.push_back(total); + ss1 >> c; + while (ss1 >> num){ + arr1.push_back(num); + ss1 >> c; + } + ss2 >> c; + while (ss2 >> num){ + arr2.push_back(num); + ss2 >> c; } - // Condition to check if we need to append remaining elements - if (newArray.size() < length) - { - for (int x = length2; x < length; x++) - { - newArray.push_back(list[high][x]); - } + int n = max(arr1.size(), arr2.size()); + for (int i = 0; i < n; i++){ + int s = ((i < arr1.size()) ? arr1[i] : 0) + ((i < arr2.size()) ? arr2[i] : 0); + res.push_back(s); } - - // Loop for formatting - for (int x = 0; x < newArray.size(); x++) - { - stringstream convert; - convert << newArray[x]; - result += convert.str(); - if (x != newArray.size() - 1) - { - result += "-"; - } + stringstream ss; + for (int i = 0; i < res.size(); i++){ + ss << res[i]; + if (i != res.size()-1) + ss << '-'; } - return result; + + return ss.str(); } int main() diff --git a/7_SimpleSymbols.cpp b/7_SimpleSymbols.cpp index ca92b80..25510b8 100644 --- a/7_SimpleSymbols.cpp +++ b/7_SimpleSymbols.cpp @@ -8,73 +8,89 @@ using namespace std; string SimpleSymbols(string str) { - bool valid; - string temp; + // 原PO是錯的 + // bool valid; + // string temp; - // eliminate spaces and non letters from the string - // We only care about the symbols and letters to determine correct positions - for (int y = 0; y < str.length(); y++) - { - if ((str[y] >= 'A' && str[y] <= 'Z') || (str[y] >= 'a' && str[y] <= 'z') || str[y] == '=' || str[y] == '+') - { - temp.push_back(str[y]); - } - } + // // eliminate spaces and non letters from the string + // // We only care about the symbols and letters to determine correct positions + // for (int y = 0; y < str.length(); y++) + // { + // if ((str[y] >= 'A' && str[y] <= 'Z') || (str[y] >= 'a' && str[y] <= 'z') || str[y] == '=' || str[y] == '+') + // { + // temp.push_back(str[y]); + // } + // } - if (temp.length() > 1) - { - valid = true; - } - else - { - valid = false; - } + // if (temp.length() > 1) + // { + // valid = true; + // } + // else + // { + // valid = false; + // } + + // for (int x = 0; x < temp.length() - 1 && valid; x++) + // { + // valid = false; + // // Checking the first character of the string + // if (x == 0 && !isalpha(temp[x])) + // { + // valid = true; + // } + // // In the case that the starting character is a letter it will result in false + // else if (x == 0 && isalpha(temp[x])) + // { + // break; + // } + // // If the current character is a symbol ignore and continue searching for letters + // else if (!isalpha(temp[x])) + // { + // valid = true; + // } + // // If a letter is found analyze that the rules are followed + // else if (isalpha(temp[x]) && temp[x-1] == '+' && temp[x + 1] == '+') + // { + // valid = true; + // } + // } + + // if (valid) + // { + // return "true"; + // } + // else + // { + // return "false"; + // } - for (int x = 0; x < temp.length()-1 && valid; x++) + // 正確答案 + for (int i = 0; i < str.length(); i++) { - valid = false; - // Checking the first character of the string - if (x == 0 && !isalpha(temp[x])) - { - valid = true; - } - // In the case that the starting character is a letter it will result in false - else if (x == 0 && isalpha(temp[x])) + if (isalpha(str[i])) { - break; + if (i == 0 || i == str.length()-1) + return "false"; + if (str[i-1] != '+' || str[i+1] != '+') + return "false"; } - // If the current character is a symbol ignore and continue searching for letters - else if (!isalpha(temp[x])) - { - valid = true; - } - // If a letter is found analyze that the rules are followed - else if (isalpha(temp[x]) && temp[x-1] == '+' && temp[x + 1] == '+') - { - valid = true; - } - } - - if (valid) - { - return "true"; - } - else - { - return "false"; } + return "true"; } int main() { // keep this function call here - cout << SimpleSymbols("+d += 3 = +s +") << endl; // true + cout << SimpleSymbols("++d+===+c++==a") << endl; // false + cout << SimpleSymbols("+d += 3 = +s +") << endl; // false cout << SimpleSymbols("f++d+") << endl; // false cout << SimpleSymbols("+d===+a+") << endl; // false cout << SimpleSymbols("+z+z+z+") << endl; // true cout << SimpleSymbols("2+a+a+") << endl; // true cout << SimpleSymbols("+z+z+==+a+") << endl; // true cout << SimpleSymbols("==a+") << endl; // false - cout << SimpleSymbols("a") << endl; // false + cout << SimpleSymbols("=+=a") << endl; // false + return 0; } \ No newline at end of file diff --git a/8_TimeConvert.cpp b/8_TimeConvert.cpp index b57955f..5ce6909 100644 --- a/8_TimeConvert.cpp +++ b/8_TimeConvert.cpp @@ -6,7 +6,7 @@ using namespace std; void TimeConvert(int num) { - + /* bool hour; int count = 0; @@ -27,14 +27,18 @@ void TimeConvert(int num) { } while (hour); cout << count << ":" << num << endl; + */ + int h = num / 60, m = num % 60; + cout << h << ":" << m << endl; + } int main() { // keep this function call here - TimeConvert(63); - TimeConvert(60); - TimeConvert(120); + TimeConvert(stoi("63")); //1:3 + TimeConvert(60); //1:0 + TimeConvert(120); //2:0 return 0; } \ No newline at end of file diff --git a/9_AlphabetSoup.cpp b/9_AlphabetSoup.cpp index 370ddc3..568058e 100644 --- a/9_AlphabetSoup.cpp +++ b/9_AlphabetSoup.cpp @@ -3,10 +3,12 @@ #include #include +#include using namespace std; +using namespace __gnu_cxx; string AlphabetSoup(string str) { - + /* bool swap; char temp; @@ -38,6 +40,42 @@ string AlphabetSoup(string str) { } return temp2; + */ + + // // bubble sort + // string temp; + // int i = 0, j = 0; + // while (i < str.length()){ + // if (isalpha(str[i])){ + // temp += str[i]; + // } + // i++; + // } + + // while (j < temp.length()-1){ + // if (temp[j] > temp[j+1]){ + // swap(temp[j], temp[j + 1]); + // j = max(j-1,0); + // } + // else{ + // j++; + // } + // } + // return temp; + + // hash map + hash_map mp; + string ans; + + for(int i = 0;i< str.length(); i++){ + mp[str[i]]++; + } + for (int i = 97; i < 123; i++) + { + if (mp[char(i)] > 0) + ans += string(mp[char(i)], char(i)); + } + return ans; } int main() { diff --git a/tempCodeRunnerFile.cpp b/tempCodeRunnerFile.cpp new file mode 100644 index 0000000..d7d17fc --- /dev/null +++ b/tempCodeRunnerFile.cpp @@ -0,0 +1 @@ +-1 \ No newline at end of file