diff --git a/README.md b/README.md index f7d194e..e833619 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Blue Movement Boot Camp +# Blue Movement Boot Camp ## Getting Started diff --git a/week-2/C++/.vs/C++/v16/.suo b/week-2/C++/.vs/C++/v16/.suo new file mode 100644 index 0000000..a3afab1 Binary files /dev/null and b/week-2/C++/.vs/C++/v16/.suo differ diff --git a/week-2/C++/.vs/C++/v16/Browse.VC.db b/week-2/C++/.vs/C++/v16/Browse.VC.db new file mode 100644 index 0000000..dcbb36a Binary files /dev/null and b/week-2/C++/.vs/C++/v16/Browse.VC.db differ diff --git a/week-2/C++/.vs/C++/v16/ipch/AutoPCH/430a720f9042be13/MAIN.ipch b/week-2/C++/.vs/C++/v16/ipch/AutoPCH/430a720f9042be13/MAIN.ipch new file mode 100644 index 0000000..5e28c8a Binary files /dev/null and b/week-2/C++/.vs/C++/v16/ipch/AutoPCH/430a720f9042be13/MAIN.ipch differ diff --git a/week-2/C++/.vs/ProjectSettings.json b/week-2/C++/.vs/ProjectSettings.json new file mode 100644 index 0000000..38c5f00 --- /dev/null +++ b/week-2/C++/.vs/ProjectSettings.json @@ -0,0 +1,3 @@ +{ + "CurrentProjectSetting": "x86-Debug" +} \ No newline at end of file diff --git a/week-2/C++/.vs/VSWorkspaceState.json b/week-2/C++/.vs/VSWorkspaceState.json new file mode 100644 index 0000000..6b61141 --- /dev/null +++ b/week-2/C++/.vs/VSWorkspaceState.json @@ -0,0 +1,6 @@ +{ + "ExpandedNodes": [ + "" + ], + "PreviewInSolutionExplorer": false +} \ No newline at end of file diff --git a/week-2/C++/.vs/slnx.sqlite b/week-2/C++/.vs/slnx.sqlite new file mode 100644 index 0000000..0d49004 Binary files /dev/null and b/week-2/C++/.vs/slnx.sqlite differ diff --git a/week-2/C++/CppProperties.json b/week-2/C++/CppProperties.json new file mode 100644 index 0000000..659bf4e --- /dev/null +++ b/week-2/C++/CppProperties.json @@ -0,0 +1,21 @@ +{ + "configurations": [ + { + "inheritEnvironments": [ + "msvc_x86" + ], + "name": "x86-Debug", + "includePath": [ + "${env.INCLUDE}", + "${workspaceRoot}\\**" + ], + "defines": [ + "WIN32", + "_DEBUG", + "UNICODE", + "_UNICODE" + ], + "intelliSenseMode": "windows-msvc-x86" + } + ] +} \ No newline at end of file diff --git a/week-2/C++/main.cpp b/week-2/C++/main.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/week-2/C++/spencer-webster-bass-q1.cpp b/week-2/C++/spencer-webster-bass-q1.cpp new file mode 100644 index 0000000..b90e854 --- /dev/null +++ b/week-2/C++/spencer-webster-bass-q1.cpp @@ -0,0 +1,53 @@ +#include +#include + +// using namespace std; + +// inspired by DFS +void permutationsUtil(int depth, std::string& str, std::vector vis, std::vector stk, std::vector& acc) +{ + int n = vis.size(); + + // leaf node + if (depth == n) { + std::string s = ""; + for (int i : stk) + { + s.push_back(str.at(i)); + } + acc.push_back(s); + } else { + for (int i = 0; i < n; ++i) + { + if (!vis[i]) + { + stk.push_back(i); + vis[i] = true; + permutationsUtil(depth+1, str, vis, stk, acc); + vis[i] = false; + stk.pop_back(); + } + } + } +} + +std::vector permutations(std::string str) +{ + std::vector vis(str.size(), false); + std::vector stk; + std::vector acc; + permutationsUtil(0, str, vis, stk, acc); + return acc; +} + +// look at saving space by using pointers and references especially with using the recurisve stack +// you can just print the solution instead of storing it +int main(int argc, char const *argv[]) { + std::cout << "Given a string str, the task is to print all the permutations of str. A permutation is an arrangement of all or part of a set of objects, with regard to the order of the arrangement. For example, if given abb, the output should be abb abb bab bba bab bba" << std::endl; + + std::string input = "abb"; + std::vector output = permutations(input); + + for (std::string o : output) + std::cout << o << std::endl; +} \ No newline at end of file diff --git a/week-2/C++/spencer-webster-bass-q2.cpp b/week-2/C++/spencer-webster-bass-q2.cpp new file mode 100644 index 0000000..66042a2 --- /dev/null +++ b/week-2/C++/spencer-webster-bass-q2.cpp @@ -0,0 +1,63 @@ +#include + +// using namespace std; + +// dynamic programming problem +std::string longPalSubstr(std::string str) +{ + int n = str.length(); + bool isPal[n][n]; + + int a, b = 0; + + // i will be the starting index and j will be the ending index + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (i == j) { + // bsae case: m = 1 + isPal[i][j] = true; + } else if (j - i == 1 && str[i] == str[j]) { + // bsae case: m = 2 + isPal[i][j] = true; + a = i; + b = j; + } else { + isPal[i][j] = false; + } + } + } + + if (n > 2) + { + // let m be the size of the substring + for (int m = 3; m <= n; m++) + { + int i = 0; + int j = m - 1; + while (j < n) + { + isPal[i][j] = (str[i] == str[j]) && isPal[i+1][j-1]; + if (isPal[i][j] && m > b - a + 1) + { + a = i; + b = j; + } + i++; + j++; + } + } + } + + return str.substr(a, b - a + 1); +} + +int main(int argc, char const *argv[]) { + std::cout << "Given a string, find the longest substring which is palindrome. For example, if the given string is ababad, the output should be ababa." << std::endl << std::endl; + + std::string input = "ababad"; + std::string output = longPalSubstr(input); + + std::cout << output << std::endl; +} \ No newline at end of file diff --git a/week-3/C++/.vs/C++/v16/.suo b/week-3/C++/.vs/C++/v16/.suo new file mode 100644 index 0000000..220ecb3 Binary files /dev/null and b/week-3/C++/.vs/C++/v16/.suo differ diff --git a/week-3/C++/.vs/C++/v16/Browse.VC.db b/week-3/C++/.vs/C++/v16/Browse.VC.db new file mode 100644 index 0000000..2695403 Binary files /dev/null and b/week-3/C++/.vs/C++/v16/Browse.VC.db differ diff --git a/week-3/C++/.vs/C++/v16/ipch/AutoPCH/201c93c3f7e4c7dc/C++.ipch b/week-3/C++/.vs/C++/v16/ipch/AutoPCH/201c93c3f7e4c7dc/C++.ipch new file mode 100644 index 0000000..e1131f2 Binary files /dev/null and b/week-3/C++/.vs/C++/v16/ipch/AutoPCH/201c93c3f7e4c7dc/C++.ipch differ diff --git a/week-3/C++/C++.sln b/week-3/C++/C++.sln new file mode 100644 index 0000000..3a497c3 --- /dev/null +++ b/week-3/C++/C++.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30128.74 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "C++", "C++\C++.vcxproj", "{31ED04FC-A57E-4B26-9A84-0FFB389B5A0C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {31ED04FC-A57E-4B26-9A84-0FFB389B5A0C}.Debug|x64.ActiveCfg = Debug|x64 + {31ED04FC-A57E-4B26-9A84-0FFB389B5A0C}.Debug|x64.Build.0 = Debug|x64 + {31ED04FC-A57E-4B26-9A84-0FFB389B5A0C}.Debug|x86.ActiveCfg = Debug|Win32 + {31ED04FC-A57E-4B26-9A84-0FFB389B5A0C}.Debug|x86.Build.0 = Debug|Win32 + {31ED04FC-A57E-4B26-9A84-0FFB389B5A0C}.Release|x64.ActiveCfg = Release|x64 + {31ED04FC-A57E-4B26-9A84-0FFB389B5A0C}.Release|x64.Build.0 = Release|x64 + {31ED04FC-A57E-4B26-9A84-0FFB389B5A0C}.Release|x86.ActiveCfg = Release|Win32 + {31ED04FC-A57E-4B26-9A84-0FFB389B5A0C}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E405B9BE-B8D7-4647-96AE-D7E57AB66A58} + EndGlobalSection +EndGlobal diff --git a/week-3/C++/C++/C++.cpp b/week-3/C++/C++/C++.cpp new file mode 100644 index 0000000..22d4b6b --- /dev/null +++ b/week-3/C++/C++/C++.cpp @@ -0,0 +1,75 @@ +// C++.cpp : This file contains the 'main' function. Program execution begins and ends there. +// + +#include +#include +using namespace std; + + +string maxSubstringWrong(string s) { + int n = s.length(); + + if (n == 0) + return s; + + char maxC = 'a' - 1; + std::vector maxInds = std::vector(); + int maxCInd = -1; + for (int i = 0; i < n; i++) { + if (s.at(i) == maxC) { + maxInds.push_back(i); + } + else if (s.at(i) > maxC) { + maxInds.clear(); + maxC = s.at(i); + maxInds.push_back(i); + } + } + + // need a smart way to determine which is the correct substring in maxInds + int maxInd = maxInds.size() - 1; + for (int i = maxInds.size() - 1; i > 0; i--) { + if (s.substr(maxInds[maxInd], n - maxInds[maxInd]) <= s.substr(maxInds[i - 1], n - maxInds[i - 1])) { + maxInd = i; + } + } + return s.substr(maxInd, n - maxInd); + +} + + + +/* + * Complete the 'maxSubstring' function below. + * + * The function is expected to return a STRING. + * The function accepts STRING s as parameter. + */ +string maxSubstring(string s) { + int n = s.length(); + string maxStr = ""; + for (int i = 0; i < n; i++) { + if (maxStr < s.substr(i, n - i)) + maxStr = s.substr(i, n - i); + } + return maxStr; +} + +int main() +{ + std::cout << maxSubstring("aaa") << std::endl; + return 0; +} + + + +// Run program: Ctrl + F5 or Debug > Start Without Debugging menu +// Debug program: F5 or Debug > Start Debugging menu + +// Tips for Getting Started: +// 1. Use the Solution Explorer window to add/manage files +// 2. Use the Team Explorer window to connect to source control +// 3. Use the Output window to see build output and other messages +// 4. Use the Error List window to view errors +// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project +// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file diff --git a/week-3/C++/C++/C++.vcxproj b/week-3/C++/C++/C++.vcxproj new file mode 100644 index 0000000..cf96cf6 --- /dev/null +++ b/week-3/C++/C++/C++.vcxproj @@ -0,0 +1,147 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {31ed04fc-a57e-4b26-9a84-0ffb389b5a0c} + C + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/week-3/C++/C++/C++.vcxproj.filters b/week-3/C++/C++/C++.vcxproj.filters new file mode 100644 index 0000000..6993792 --- /dev/null +++ b/week-3/C++/C++/C++.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/week-3/C++/C++/C++.vcxproj.user b/week-3/C++/C++/C++.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/week-3/C++/C++/C++.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/week-3/C++/C++/Debug/C++.log b/week-3/C++/C++/Debug/C++.log new file mode 100644 index 0000000..492cbeb --- /dev/null +++ b/week-3/C++/C++/Debug/C++.log @@ -0,0 +1,2 @@ + C++.cpp + C++.vcxproj -> C:\Users\webst\OneDrive\Academic FIles\2020-21 SEN\0th semester\ibm\bootcamp-2020.1\week-3\C++\Debug\C++.exe diff --git a/week-3/C++/C++/Debug/C++.obj b/week-3/C++/C++/Debug/C++.obj new file mode 100644 index 0000000..c8a43c7 Binary files /dev/null and b/week-3/C++/C++/Debug/C++.obj differ diff --git a/week-3/C++/C++/Debug/C++.tlog/C++.lastbuildstate b/week-3/C++/C++/Debug/C++.tlog/C++.lastbuildstate new file mode 100644 index 0000000..02a3865 --- /dev/null +++ b/week-3/C++/C++/Debug/C++.tlog/C++.lastbuildstate @@ -0,0 +1,2 @@ +PlatformToolSet=v142:VCToolArchitecture=Native32Bit:VCToolsVersion=14.26.28801:TargetPlatformVersion=10.0.18362.0: +Debug|Win32|C:\Users\webst\OneDrive\Academic FIles\2020-21 SEN\0th semester\ibm\bootcamp-2020.1\week-3\C++\| diff --git a/week-3/C++/C++/Debug/C++.tlog/CL.command.1.tlog b/week-3/C++/C++/Debug/C++.tlog/CL.command.1.tlog new file mode 100644 index 0000000..104b09b Binary files /dev/null and b/week-3/C++/C++/Debug/C++.tlog/CL.command.1.tlog differ diff --git a/week-3/C++/C++/Debug/C++.tlog/CL.read.1.tlog b/week-3/C++/C++/Debug/C++.tlog/CL.read.1.tlog new file mode 100644 index 0000000..3c58615 Binary files /dev/null and b/week-3/C++/C++/Debug/C++.tlog/CL.read.1.tlog differ diff --git a/week-3/C++/C++/Debug/C++.tlog/CL.write.1.tlog b/week-3/C++/C++/Debug/C++.tlog/CL.write.1.tlog new file mode 100644 index 0000000..0465344 Binary files /dev/null and b/week-3/C++/C++/Debug/C++.tlog/CL.write.1.tlog differ diff --git a/week-3/C++/C++/Debug/C++.tlog/link.command.1.tlog b/week-3/C++/C++/Debug/C++.tlog/link.command.1.tlog new file mode 100644 index 0000000..41097de Binary files /dev/null and b/week-3/C++/C++/Debug/C++.tlog/link.command.1.tlog differ diff --git a/week-3/C++/C++/Debug/C++.tlog/link.read.1.tlog b/week-3/C++/C++/Debug/C++.tlog/link.read.1.tlog new file mode 100644 index 0000000..e7dd4d6 Binary files /dev/null and b/week-3/C++/C++/Debug/C++.tlog/link.read.1.tlog differ diff --git a/week-3/C++/C++/Debug/C++.tlog/link.write.1.tlog b/week-3/C++/C++/Debug/C++.tlog/link.write.1.tlog new file mode 100644 index 0000000..c8dd7d7 Binary files /dev/null and b/week-3/C++/C++/Debug/C++.tlog/link.write.1.tlog differ diff --git a/week-3/C++/C++/Debug/vc142.idb b/week-3/C++/C++/Debug/vc142.idb new file mode 100644 index 0000000..23ca15c Binary files /dev/null and b/week-3/C++/C++/Debug/vc142.idb differ diff --git a/week-3/C++/C++/Debug/vc142.pdb b/week-3/C++/C++/Debug/vc142.pdb new file mode 100644 index 0000000..959229f Binary files /dev/null and b/week-3/C++/C++/Debug/vc142.pdb differ diff --git a/week-3/C++/Debug/C++.exe b/week-3/C++/Debug/C++.exe new file mode 100644 index 0000000..f0c6ab0 Binary files /dev/null and b/week-3/C++/Debug/C++.exe differ diff --git a/week-3/C++/Debug/C++.ilk b/week-3/C++/Debug/C++.ilk new file mode 100644 index 0000000..f26bded Binary files /dev/null and b/week-3/C++/Debug/C++.ilk differ diff --git a/week-3/C++/Debug/C++.pdb b/week-3/C++/Debug/C++.pdb new file mode 100644 index 0000000..5bb949a Binary files /dev/null and b/week-3/C++/Debug/C++.pdb differ diff --git a/week-3/C++/main.cpp b/week-3/C++/main.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/week-4/C++/main.cpp b/week-4/C++/main.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/week-4/C++/spencer-webster-bass.cpp b/week-4/C++/spencer-webster-bass.cpp new file mode 100644 index 0000000..49d1658 --- /dev/null +++ b/week-4/C++/spencer-webster-bass.cpp @@ -0,0 +1,113 @@ +#include +#include +#include + +std::forward_list sumLists(std::forward_list L1, std::forward_list L2) +{ + std::forward_list sum = std::forward_list(); + + L1.reverse(); + L2.reverse(); + + std::forward_list::iterator it1 = L1.begin(); + std::forward_list::iterator it2 = L2.begin(); + + int r = 0; + + while (it1 != L1.end() || it2 != L2.end()) + { + + int a = 0; + int b = 0; + + if (it1 != L1.end()) + { + a = *it1; + it1++; + } + + if (it2 != L2.end()) + { + b = *it2; + it2++; + } + + sum.push_front((r + a + b) % 10); + r = (r + a + b) / 10; + } + + if (r != 0) + sum.push_front(r); + + return sum; +} + + +std::forward_list reverseSentence(std::forward_list sentence) +{ + + std::forward_list reverse = std::forward_list(); + std::stack word = std::stack(); + + for (std::forward_list::iterator it = sentence.begin(); it != sentence.end(); it++) + { + if (*it == ' ') + { + while (!word.empty()) + { + reverse.push_front(word.top()); + word.pop(); + } + reverse.push_front(' '); + } + else { + word.push(*it); + } + } + + while (!word.empty()) + { + reverse.push_front(word.top()); + word.pop(); + } + + return reverse; +} + +template +void printList(std::forward_list L) +{ + std::cout << "{ "; + + for (T t : L) + { + std::cout << t << ","; + } + + std::cout << " }" << std::endl; +} + +int main(char** args) +{ + // 1st Prompt... + std::cout << "1st Prompt:" << std::endl; + std::forward_list L1 = { 5,6,3 }; + std::forward_list L2 = { 8,4,2 }; + + printList(L1); + std::cout << "+" << std::endl; + printList(L2); + std::cout << "=" << std::endl; + + std::forward_list sum = sumLists(L1, L2); + + printList(sum); + + // 2nd Prompt... + std::cout << std::endl << std::endl << "2nd Prompt:" << std::endl; + std::forward_list sentence = { 'I',' ','l','o','v','e',' ','G','e','e','k','s',' ','f','o','r',' ','G','e','e','k','s' }; + printList(sentence); + printList(reverseSentence(sentence)); + + return 0; +} diff --git a/week-6/C++/main.cpp b/week-6/C++/main.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/week-6/C++/spencer-webster-bass.cpp b/week-6/C++/spencer-webster-bass.cpp new file mode 100644 index 0000000..7363020 --- /dev/null +++ b/week-6/C++/spencer-webster-bass.cpp @@ -0,0 +1,138 @@ +// C++.cpp : This file contains the 'main' function. Program execution begins and ends there. +// + +#include +#include +#include +#include "spencer-webster-bass1.cpp" + +void maxDiffUtil(Node* n, int maxVal, int& maxDiff) +{ + if (n->left) + { + if (maxVal - n->left->val > maxDiff) + maxDiff = maxVal - n->left->val; + + maxDiffUtil(n->left, (n->left->val > maxVal) ? n->left->val : maxVal, maxDiff); + } + + if (n->right) + { + if (maxVal - n->right->val > maxDiff) + maxDiff = maxVal - n->right->val; + + maxDiffUtil(n->right, (n->right->val > maxVal) ? n->right->val : maxVal, maxDiff); + } + + return; +} + +int maxDiff(Tree* t) +{ + int maxDiff = INT_MIN; + maxDiffUtil(t->root, t->root->val, maxDiff); + return maxDiff; +} + +void printLeafPathUtil(Node* n, std::vector& p) +{ + p.push_back(n->val); + if (n->isLeaf()) + { + std::string path = ""; + for (int i = p.size() - 1; i > 0; i--) + path.append(std::to_string(p.at(i)) + " -> "); + + path.append(std::to_string(p.at(0))); + std::cout << path << std::endl; + } else { + if (n->left) + printLeafPathUtil(n->left, p); + if (n->right) + printLeafPathUtil(n->right, p); + } + p.pop_back(); +} + +void printLeafPath(Tree* t) +{ + std::vector path = std::vector(); + printLeafPathUtil(t->root, path); +} + +void mirrorTree(Node* n) +{ + if (n->left || n->right) + { + Node* temp = n->left; + n->left = n->right; + n->right = temp; + if (n->left) + mirrorTree(n->left); + if (n->right) + mirrorTree(n->right); + } + else { + return; + } +} + +int main() +{ + + Node n8 = Node(8); + Node n9 = Node(9); + + Node n6 = Node(6, &n8, &n9); + Node n7 = Node(7); + + Node n3 = Node(3, &n6, &n7); + + Node n4 = Node(4); + Node n5 = Node(5); + + Node n2 = Node(2, &n4, &n5); + + Node n1 = Node(1, &n2, &n3); + + Tree tA = Tree(&n1); + + std::cout << "Print Leaf Paths Test: " << std::endl; + printLeafPath(&tA); + std::cout << std::endl; + + std::cout << "Mirror Tree Test: " << std::endl; + tA.printTree(); + std::cout << std::endl; + mirrorTree(tA.root); + tA.printTree(); + std::cout << std::endl; + + + Node n1B = Node(1); + Node n7B = Node(7); + Node n2B = Node(2, &n1B, &n7B); + + Node n4B = Node(4); + Node n8B = Node(8, &n2B, &n4B); + + Node n3B = Node(3); + Node n6B = Node(6, &n3B, &n8B); + + Tree tB = Tree(&n6B); + + std::cout << "Maximum Difference Test: " << std::endl; + std::cout << maxDiff(&tB); + +} + +// Run program: Ctrl + F5 or Debug > Start Without Debugging menu +// Debug program: F5 or Debug > Start Debugging menu + +// Tips for Getting Started: +// 1. Use the Solution Explorer window to add/manage files +// 2. Use the Team Explorer window to connect to source control +// 3. Use the Output window to see build output and other messages +// 4. Use the Error List window to view errors +// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project +// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file diff --git a/week-6/C++/spencer-webster-bass1.cpp b/week-6/C++/spencer-webster-bass1.cpp new file mode 100644 index 0000000..66b7738 --- /dev/null +++ b/week-6/C++/spencer-webster-bass1.cpp @@ -0,0 +1,77 @@ +#include +#include +#include + +class Node { + +public: + int val; + Node* left; + Node* right; + + Node() : + val(0), left(nullptr), right(nullptr) + {} + + Node(int v) : + val(v), left(nullptr), right(nullptr) + {} + + Node(int v, Node* l, Node* r) : + val(v), left(l), right(r) + {} + + bool isLeaf() + { + return left == nullptr && right == nullptr; + } +}; + +class Tree { + +public: + Node* root; + + Tree(Node* r) : + root(r) + {} + + void printTree() + { + // postorder(root, 0); + // return; + + // parent value, node pointer pair + std::cout << "Each pair is parent, node value. The 1st encounter with a child of a particular node is the left child, otherwise the pair is the right child of the parent." << std::endl; + std::queue> q = std::queue>(); + q.push(std::pair(0,root)); + while (!q.empty()) + { + if (q.front().second->left) + q.push(std::pair(q.front().second->val, q.front().second->left)); + if (q.front().second->right) + q.push(std::pair(q.front().second->val, q.front().second->right)); + std::cout << q.front().first << "," << q.front().second->val << " "; + q.pop(); + } + std::cout << std::endl; + } + + void postorder(Node* p, int indent) + { + if (p != NULL) { + if (p->right) { + postorder(p->right, indent + 4); + } + if (indent) { + std::cout << std::setw(indent) << ' '; + } + if (p->right) std::cout << " /\n" << std::setw(indent) << ' '; + std::cout << p->val << "\n "; + if (p->left) { + std::cout << std::setw(indent) << ' ' << " \\\n"; + postorder(p->left, indent + 4); + } + } + } +}; \ No newline at end of file diff --git a/week-8/C++/spencer-webster-bass.cpp b/week-8/C++/spencer-webster-bass.cpp new file mode 100644 index 0000000..7246285 --- /dev/null +++ b/week-8/C++/spencer-webster-bass.cpp @@ -0,0 +1,100 @@ +// C++.cpp : This file contains the 'main' function. Program execution begins and ends there. +// + +#include +#include +#include + +// helper function for problem 1 +// start and last are inclusive +// thankfully everyday she finds at least 1 stone so there +// will never be duplicates in the aggregate array +int binarySearch(std::vector& A, int start, int last, int tgt) +{ + if (last < start) + return -1; + + int mid = ((last - start) / 2) + start; + if (A.at(mid) == tgt) { + return mid; + } + else if (start == mid) { + if (A.at(mid) > tgt) + return start; + return last; + } + else if (A.at(mid) > tgt) { + return binarySearch(A, start, mid, tgt); + } + else if (A.at(mid) < tgt) { + return binarySearch(A, mid + 1, last, tgt); + } + else { + return -1; + } +} + +// function for problem 1 +void stoneLove(int n, int q, std::vector A, std::vector M) +{ + std::vector aggregate = std::vector(); + aggregate.push_back(A.at(0)); + + for (int i = 1; i < n; i++) + aggregate.push_back(aggregate.back() + A.at(i)); + + for (int m : M) + std::cout << binarySearch(aggregate, 0, n - 1, m) + 1 << std::endl; +} + + +// function for problem 2 +void repeatedKTimes(int n, int k, std::vector A) +{ + std::unordered_map repeats = std::unordered_map(); + + for (int a : A) + { + if (repeats.count(a) == 0) + { + repeats.insert({ a,1 }); + } + else { + repeats.at(a)++; + } + } + + int min = -1; + + for (std::pair r : repeats) + { + if (r.second == k) + { + if (min == -1 || min > r.first) + min = r.first; + } + } + + std::cout << min << std::endl; +} + +int main() +{ + std::vector A = { 2, 2, 1, 3, 1 }; + repeatedKTimes(5, 2, A); + repeatedKTimes(5, 1, A); + A = { 2, 2, 1, 3, 1, 4, 4, 4, 4 }; + repeatedKTimes(9, 4, A); + + std::cout << std::endl; + + A = { 1, 2, 3, 4, 5 }; + std::vector M = { 3, 8, 10, 14 }; + stoneLove(5, 4, A, M); + std::cout << std::endl; + A = { 3, 3, 5, 1 }; + M = { 1, 3, 8, 11, 12 }; + stoneLove(4, 5, A, M); +} + +