├── .husky ├── .gitignore ├── pre-commit └── _ │ └── husky.sh ├── images └── recipes-logo.png ├── .vscode ├── settings.json ├── extensions.json └── launch.json ├── force-app └── main │ └── default │ ├── classes │ ├── pass_by │ │ ├── PassBy_Tests.cls │ │ ├── PassBy.cls-meta.xml │ │ ├── PassBy_Tests.cls-meta.xml │ │ └── PassBy.cls │ ├── recursion │ │ ├── triangular-numbers │ │ │ ├── TriangleCalcSimpe.cls │ │ │ └── TriangleCalcSimpe.cls-meta.xml │ │ ├── Hannoi.cls-meta.xml │ │ ├── RecursionExamples.cls-meta.xml │ │ ├── RecursionExamples_Tests.cls-meta.xml │ │ ├── Hannoi.cls │ │ ├── RecursionExamples_Tests.cls │ │ └── RecursionExamples.cls │ ├── ListUtils.cls-meta.xml │ ├── ListUtils_Tests.cls-meta.xml │ ├── StringProblems.cls-meta.xml │ ├── stack │ │ ├── IStack.cls-meta.xml │ │ ├── ListStack.cls-meta.xml │ │ ├── MinStack.cls-meta.xml │ │ ├── StackDemo.cls-meta.xml │ │ ├── LinkedStack.cls-meta.xml │ │ ├── LinkedStack_Tests.cls-meta.xml │ │ ├── ListStack_Tests.cls-meta.xml │ │ ├── StackDemo_Tests.cls-meta.xml │ │ ├── LinkedStack.cls │ │ ├── MinStack.cls │ │ ├── ListStack.cls │ │ ├── StackDemo_Tests.cls │ │ ├── IStack.cls │ │ ├── ListStack_Tests.cls │ │ ├── LinkedStack_Tests.cls │ │ └── StackDemo.cls │ ├── trees │ │ ├── BST_Node.cls-meta.xml │ │ ├── Node_Opp_BST.cls-meta.xml │ │ ├── BinarySearchTree.cls-meta.xml │ │ ├── OpportunityBST.cls-meta.xml │ │ ├── OpportunityBST_Tests.cls-meta.xml │ │ ├── BinarySearchTree.cls │ │ ├── BST_Node.cls │ │ ├── Node_Opp_BST.cls │ │ ├── OpportunityBST_Tests.cls │ │ └── OpportunityBST.cls │ ├── StringProblems_Test.cls-meta.xml │ ├── hashing │ │ ├── HashEntry.cls-meta.xml │ │ ├── HashTable.cls-meta.xml │ │ ├── HashTable_Tests.cls-meta.xml │ │ ├── HashTable_Tests.cls │ │ ├── HashEntry.cls │ │ └── HashTable.cls │ ├── linked-list │ │ ├── Node.cls-meta.xml │ │ ├── SobjectNode.cls-meta.xml │ │ ├── SinglyLinkedList.cls-meta.xml │ │ ├── SobjectLinkedList.cls-meta.xml │ │ ├── SobjectNode_Tests.cls-meta.xml │ │ ├── CircularLinkedList.cls-meta.xml │ │ ├── SinglyLinkedList_Tests.cls-meta.xml │ │ ├── SobjectLinkedList_Tests.cls-meta.xml │ │ ├── Node.cls │ │ ├── SobjectNode.cls │ │ ├── SobjectNode_Tests.cls │ │ ├── CircularLinkedList.cls │ │ ├── SinglyLinkedList.cls │ │ ├── SinglyLinkedList_Tests.cls │ │ ├── SobjectLinkedList.cls │ │ └── SobjectLinkedList_Tests.cls │ ├── search │ │ ├── BinarySearch.cls-meta.xml │ │ ├── LinearSearch.cls-meta.xml │ │ ├── BinarySearch_Tests.cls-meta.xml │ │ ├── LinearSearch_Tests.cls-meta.xml │ │ ├── LinearSearch.cls │ │ ├── LinearSearch_Tests.cls │ │ ├── BinarySearch_Tests.cls │ │ └── BinarySearch.cls │ ├── sorting │ │ ├── BubbleSort.cls-meta.xml │ │ ├── MergeSort.cls-meta.xml │ │ ├── BubbleSort_Tests.cls-meta.xml │ │ ├── InsertionSort.cls-meta.xml │ │ ├── MergeSort_Tests.cls-meta.xml │ │ ├── SelectionSort.cls-meta.xml │ │ ├── InsertionSort_Tests.cls-meta.xml │ │ ├── SelectionSort_Tests.cls-meta.xml │ │ ├── InsertionSort_Tests.cls │ │ ├── MergeSort_Tests.cls │ │ ├── SelectionSort_Tests.cls │ │ ├── BubbleSort_Tests.cls │ │ ├── InsertionSort.cls │ │ ├── SelectionSort.cls │ │ ├── BubbleSort.cls │ │ └── MergeSort.cls │ ├── LeetCode │ │ ├── BitShifting.cls-meta.xml │ │ ├── BuySellStock.cls-meta.xml │ │ ├── ValidPalindrome.cls-meta.xml │ │ ├── BuySellStock_Tests.cls-meta.xml │ │ ├── ContainsDuplicates.cls-meta.xml │ │ ├── ContainsDuplicates_Tests.cls-meta.xml │ │ ├── ValidPalindrome_Tests.cls-meta.xml │ │ ├── BitShifting.cls │ │ ├── RemoveDuplicatesFromSortedArray.cls │ │ ├── BuySellStock_Tests.cls │ │ ├── ValidPalindrome.cls │ │ ├── ContainsDuplicates.cls │ │ ├── ContainsDuplicates_Tests.cls │ │ ├── BuySellStock.cls │ │ └── ValidPalindrome_Tests.cls │ ├── ListPerformance_Tests.cls-meta.xml │ ├── ListProblems │ │ ├── ListProbs.cls-meta.xml │ │ ├── ListProbs_Tests.cls-meta.xml │ │ ├── ListProbs.cls │ │ └── ListProbs_Tests.cls │ ├── Bitwise-Operations │ │ ├── BitWiseBasics_Tests.cls │ │ ├── BitWiseBasics.cls-meta.xml │ │ ├── BitWiseBasics_Tests.cls-meta.xml │ │ └── BitWiseBasics.cls │ ├── doubly-linked-list │ │ ├── DoublyLinkedList.cls-meta.xml │ │ ├── DoublyLinkedList_Tests.cls-meta.xml │ │ ├── DoublyLinkedList_Tests.cls │ │ └── DoublyLinkedList.cls │ ├── problem_solving_patterns │ │ ├── AnagramSolver.cls-meta.xml │ │ ├── SlidingWindow.cls-meta.xml │ │ ├── AnagramSolver_Tests.cls-meta.xml │ │ ├── FrequencyCounting.cls-meta.xml │ │ ├── MultiplePointers.cls-meta.xml │ │ ├── SlidingWindow_Tests.cls-meta.xml │ │ ├── FrequencyCounting_Tests.cls-meta.xml │ │ ├── MultiplePointers_Tests.cls-meta.xml │ │ ├── MultiplePointers_Tests.cls │ │ ├── SlidingWindow_Tests.cls │ │ ├── MultiplePointers.cls │ │ ├── FrequencyCounting_Tests.cls │ │ ├── SlidingWindow.cls │ │ ├── FrequencyCounting.cls │ │ ├── AnagramSolver_Tests.cls │ │ └── AnagramSolver.cls │ ├── ListUtils_Tests.cls │ ├── ListUtils.cls │ ├── Queues │ │ ├── Queue.cls │ │ └── Queue_Tests.cls │ ├── StringProblems_Test.cls │ ├── ListPerformance_Tests.cls │ └── StringProblems.cls │ ├── lwc │ ├── factorialCalculator │ │ ├── factorialCalculator.css │ │ ├── factorialCalculator.js-meta.xml │ │ ├── factorialCalculator.js │ │ ├── factorialCalculator.html │ │ └── __tests__ │ │ │ └── factorialCalculatorTest.js │ ├── recursionExamples │ │ ├── recursionExamples.js │ │ ├── recursionExamples.js-meta.xml │ │ └── __tests__ │ │ │ └── recursionExamplesTest.js │ ├── .eslintrc.json │ ├── linkedList │ │ ├── linkedList.js-meta.xml │ │ └── linkedList.js │ └── containsDuplicates │ │ ├── containsDuplicates.js-meta.xml │ │ └── containsDuplicates.js │ └── aura │ └── .eslintrc.json ├── jest.config.js ├── out └── production │ └── Apex_CS │ └── classes │ ├── sorting │ ├── MergeSort_Tests.cls │ ├── BubbleSort.cls-meta.xml │ ├── MergeSort.cls-meta.xml │ ├── BubbleSort_Tests.cls-meta.xml │ ├── InsertionSort.cls-meta.xml │ ├── MergeSort_Tests.cls-meta.xml │ ├── SelectionSort.cls-meta.xml │ ├── InsertionSort_Tests.cls-meta.xml │ ├── SelectionSort_Tests.cls-meta.xml │ ├── MergeSort.cls │ ├── InsertionSort_Tests.cls │ ├── SelectionSort_Tests.cls │ ├── BubbleSort_Tests.cls │ ├── InsertionSort.cls │ ├── SelectionSort.cls │ └── BubbleSort.cls │ ├── search │ ├── BinarySearch.cls-meta.xml │ ├── LinearSearch.cls-meta.xml │ ├── BinarySearch_Tests.cls-meta.xml │ ├── LinearSearch_Tests.cls-meta.xml │ ├── BinarySearch.cls │ ├── LinearSearch.cls │ ├── BinarySearch_Tests.cls │ └── LinearSearch_Tests.cls │ ├── stack │ ├── BasicStack.cls-meta.xml │ ├── Stack_Test.cls-meta.xml │ ├── StringStack.cls-meta.xml │ ├── StringStack_Tests.cls-meta.xml │ ├── StringStack.cls │ ├── Stack_Test.cls │ ├── BasicStack.cls │ └── StringStack_Tests.cls │ ├── recursion │ ├── RecursionExamples.cls-meta.xml │ ├── RecursionExamples_Tests.cls-meta.xml │ ├── RecursionExamples_Tests.cls │ └── RecursionExamples.cls │ └── problem_solving_patterns │ ├── AnagramSolver.cls-meta.xml │ ├── AnagramSolver_Tests.cls-meta.xml │ ├── FrequencyCounting.cls-meta.xml │ ├── MultiplePointers.cls-meta.xml │ ├── FrequencyCounting_Tests.cls-meta.xml │ ├── MultiplePointers_Tests.cls-meta.xml │ ├── MultiplePointers_Tests.cls │ ├── MultiplePointers.cls │ ├── FrequencyCounting_Tests.cls │ ├── AnagramSolver_Tests.cls │ ├── FrequencyCounting.cls │ └── AnagramSolver.cls ├── .prettierignore ├── scripts ├── soql │ └── account.soql └── apex │ └── hello.apex ├── .prettierrc ├── .eslintignore ├── config └── project-scratch-def.json ├── .forceignore ├── sfdx-project.json ├── .gitignore ├── manifest └── package.xml ├── package.json └── README.md /.husky/.gitignore: -------------------------------------------------------------------------------- 1 | _ 2 | -------------------------------------------------------------------------------- /.husky/pre-commit: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | npm run precommit -------------------------------------------------------------------------------- /images/recipes-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bdJohnson72/Algorithms_And_Data_Structures_In_Apex/HEAD/images/recipes-logo.png -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "search.exclude": { 3 | "**/node_modules": true, 4 | "**/bower_components": true, 5 | "**/.sfdx": true 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /force-app/main/default/classes/pass_by/PassBy_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 4/10/2022. 3 | */ 4 | 5 | @IsTest 6 | private class PassBy_Tests { 7 | } 8 | -------------------------------------------------------------------------------- /force-app/main/default/classes/recursion/triangular-numbers/TriangleCalcSimpe.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 2/13/2022. 3 | */ 4 | 5 | public with sharing class TriangleCalcSimpe { 6 | } 7 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | const {jestConfig} = require('@salesforce/sfdx-lwc-jest/config'); 2 | 3 | module.exports = { 4 | ...jestConfig, 5 | modulePathIgnorePatterns: ['/.localdevserver'] 6 | }; 7 | -------------------------------------------------------------------------------- /force-app/main/default/lwc/factorialCalculator/factorialCalculator.css: -------------------------------------------------------------------------------- 1 | .output-field { 2 | border: 1px solid #dddbda; 3 | line-height: 1.875rem; 4 | min-height: calc(1.875rem + (1px * 2)); 5 | margin-bottom: 1rem; 6 | } 7 | -------------------------------------------------------------------------------- /force-app/main/default/classes/ListUtils.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/sorting/MergeSort_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/15/2022. 3 | */ 4 | 5 | @IsTest 6 | private class MergeSort_Tests { 7 | @IsTest 8 | static void testBehavior() { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "salesforce.salesforcedx-vscode", 4 | "redhat.vscode-xml", 5 | "dbaeumer.vscode-eslint", 6 | "esbenp.prettier-vscode", 7 | "financialforce.lana" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /force-app/main/default/classes/ListUtils_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/StringProblems.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 55.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/pass_by/PassBy.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/stack/IStack.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/stack/ListStack.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/stack/MinStack.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 55.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/stack/StackDemo.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/trees/BST_Node.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | # List files or directories below to ignore them when running prettier 2 | # More information: https://prettier.io/docs/en/ignore.html 3 | # 4 | 5 | **/staticresources/** 6 | .localdevserver 7 | .sfdx 8 | .vscode 9 | 10 | coverage/ -------------------------------------------------------------------------------- /force-app/main/default/classes/StringProblems_Test.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 55.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/hashing/HashEntry.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 55.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/hashing/HashTable.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 55.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/linked-list/Node.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/recursion/Hannoi.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/search/BinarySearch.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/search/LinearSearch.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/sorting/BubbleSort.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/sorting/MergeSort.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/stack/LinkedStack.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/trees/Node_Opp_BST.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/search/BinarySearch.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/search/LinearSearch.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/sorting/BubbleSort.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/sorting/MergeSort.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/stack/BasicStack.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/stack/Stack_Test.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/stack/StringStack.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/LeetCode/BitShifting.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/LeetCode/BuySellStock.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/LeetCode/ValidPalindrome.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/ListPerformance_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/ListProblems/ListProbs.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 55.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/hashing/HashTable_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 55.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/linked-list/SobjectNode.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/pass_by/PassBy_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/search/BinarySearch_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/search/LinearSearch_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/sorting/BubbleSort_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/sorting/InsertionSort.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/sorting/MergeSort_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/sorting/SelectionSort.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/stack/LinkedStack_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/stack/ListStack_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/stack/StackDemo_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/trees/BinarySearchTree.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/trees/OpportunityBST.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/sorting/BubbleSort_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/sorting/InsertionSort.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/sorting/MergeSort_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/sorting/SelectionSort.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/stack/StringStack_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/Bitwise-Operations/BitWiseBasics_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 3/6/2022. 3 | */ 4 | 5 | @IsTest 6 | private class BitWiseBasics_Tests { 7 | @IsTest 8 | static void testBehavior() { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /force-app/main/default/classes/LeetCode/BuySellStock_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/LeetCode/ContainsDuplicates.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/ListProblems/ListProbs_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 55.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/linked-list/SinglyLinkedList.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/linked-list/SobjectLinkedList.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/linked-list/SobjectNode_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/recursion/RecursionExamples.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/sorting/InsertionSort_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/sorting/SelectionSort_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/trees/OpportunityBST_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/recursion/RecursionExamples.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/search/BinarySearch_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/search/LinearSearch_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/sorting/InsertionSort_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/sorting/SelectionSort_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/Bitwise-Operations/BitWiseBasics.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/LeetCode/ContainsDuplicates_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/LeetCode/ValidPalindrome_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/linked-list/CircularLinkedList.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/linked-list/SinglyLinkedList_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/recursion/RecursionExamples_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/recursion/RecursionExamples_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/Bitwise-Operations/BitWiseBasics_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/doubly-linked-list/DoublyLinkedList.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/linked-list/SobjectLinkedList_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/problem_solving_patterns/AnagramSolver.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/problem_solving_patterns/SlidingWindow.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/problem_solving_patterns/AnagramSolver.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/doubly-linked-list/DoublyLinkedList_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/problem_solving_patterns/AnagramSolver_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/problem_solving_patterns/FrequencyCounting.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/problem_solving_patterns/MultiplePointers.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/problem_solving_patterns/SlidingWindow_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/problem_solving_patterns/AnagramSolver_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/problem_solving_patterns/FrequencyCounting.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/problem_solving_patterns/MultiplePointers.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/sorting/MergeSort.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Use divide and conquer approach to get to single element arrays. Arrays of size 1 are sorted by default. 3 | */ 4 | 5 | public with sharing class MergeSort { 6 | public static void mergeLists() { 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /force-app/main/default/classes/problem_solving_patterns/FrequencyCounting_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/problem_solving_patterns/MultiplePointers_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /force-app/main/default/classes/recursion/triangular-numbers/TriangleCalcSimpe.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/problem_solving_patterns/FrequencyCounting_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/problem_solving_patterns/MultiplePointers_Tests.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /scripts/soql/account.soql: -------------------------------------------------------------------------------- 1 | // Use .soql files to store SOQL queries. 2 | // You can execute queries in VS Code by selecting the 3 | // query text and running the command: 4 | // SFDX: Execute SOQL Query with Currently Selected Text 5 | 6 | SELECT Id, Name 7 | FROM Account 8 | -------------------------------------------------------------------------------- /force-app/main/default/classes/trees/BinarySearchTree.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 2/21/2022. 3 | */ 4 | 5 | public with sharing class BinarySearchTree { 6 | public BST_Node root; 7 | 8 | public BinarySearchTree() { 9 | this.root = null; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "trailingComma": "none", 3 | "overrides": [ 4 | { 5 | "files": "**/lwc/**/*.html", 6 | "options": { "parser": "lwc" } 7 | }, 8 | { 9 | "files": "*.{cmp,page,component}", 10 | "options": { "parser": "html" } 11 | } 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /force-app/main/default/classes/linked-list/Node.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 4/16/2022. 3 | */ 4 | 5 | public with sharing class Node { 6 | public Object data { get; private set; } 7 | public Node next; 8 | 9 | public Node(Object data) { 10 | this.data = data; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /force-app/main/default/aura/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | "@salesforce/eslint-plugin-aura" 4 | ], 5 | "extends": [ 6 | "plugin:@salesforce/eslint-plugin-aura/recommended", 7 | "prettier" 8 | ], 9 | "rules": { 10 | "vars-on-top": "off", 11 | "no-unused-expressions": "off" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /force-app/main/default/lwc/recursionExamples/recursionExamples.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/7/2022. 3 | */ 4 | 5 | function factorialCalculator(number) { 6 | if (number === 1) { 7 | return 1; 8 | } 9 | return number * factorialCalculator(number - 1); 10 | } 11 | 12 | export { factorialCalculator }; 13 | -------------------------------------------------------------------------------- /force-app/main/default/classes/hashing/HashTable_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 7/19/2022. 3 | */ 4 | 5 | @IsTest 6 | public with sharing class HashTable_Tests { 7 | @IsTest 8 | public static void itShouldInsert() { 9 | HashTable table = new HashTable(); 10 | table.insertTable(10, 'hello'); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | **/lwc/**/*.css 2 | **/lwc/**/*.html 3 | **/lwc/**/*.json 4 | **/lwc/**/*.svg 5 | **/lwc/**/*.xml 6 | **/aura/**/*.auradoc 7 | **/aura/**/*.cmp 8 | **/aura/**/*.css 9 | **/aura/**/*.design 10 | **/aura/**/*.evt 11 | **/aura/**/*.json 12 | **/aura/**/*.svg 13 | **/aura/**/*.tokens 14 | **/aura/**/*.xml 15 | **/aura/**/*.app 16 | .sfdx 17 | -------------------------------------------------------------------------------- /force-app/main/default/classes/sorting/InsertionSort_Tests.cls: -------------------------------------------------------------------------------- 1 | @IsTest 2 | private class InsertionSort_Tests { 3 | @IsTest 4 | static void testSort() { 5 | //given 6 | List integers = new List{ 5, 4, 6, -22, 10, 500, 76 }; 7 | InsertionSort.sort(integers); 8 | System.assert(integers[0] == -22); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/sorting/InsertionSort_Tests.cls: -------------------------------------------------------------------------------- 1 | @IsTest 2 | private class InsertionSort_Tests { 3 | @IsTest 4 | static void testSort() { 5 | //given 6 | List integers = new List{ 5, 4, 6, -22, 10, 500, 76 }; 7 | InsertionSort.sort(integers); 8 | System.assert(integers[0] == -22); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /force-app/main/default/classes/linked-list/SobjectNode.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * @description Implementation of node for a linked list for the Contact SObject type 3 | */ 4 | 5 | public with sharing class SobjectNode { 6 | public SObject sobj; 7 | public SobjectNode next; 8 | 9 | public SobjectNode(SObject sobj) { 10 | this.sobj = sobj; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /force-app/main/default/lwc/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "@salesforce/eslint-config-lwc/recommended", 4 | "prettier" 5 | ], 6 | "overrides": [ 7 | { 8 | "files": [ 9 | "*.test.js" 10 | ], 11 | "rules": { 12 | "@lwc/lwc/no-unexpected-wire-adapter-usages": "off" 13 | } 14 | } 15 | ] 16 | } 17 | -------------------------------------------------------------------------------- /force-app/main/default/classes/hashing/HashEntry.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 7/19/2022. 3 | */ 4 | 5 | public with sharing class HashEntry { 6 | public Integer key; 7 | public Object value; 8 | 9 | public HashEntry next; 10 | 11 | public HashEntry(Integer key, Object value) { 12 | this.key = key; 13 | this.value = value; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /force-app/main/default/lwc/linkedList/linkedList.js-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Linked List 5 | false 6 | Linked List 7 | 8 | -------------------------------------------------------------------------------- /config/project-scratch-def.json: -------------------------------------------------------------------------------- 1 | { 2 | "orgName": "Demo company", 3 | "edition": "Developer", 4 | "features": [ 5 | "EnableSetPasswordInApi" 6 | ], 7 | "settings": { 8 | "lightningExperienceSettings": { 9 | "enableS1DesktopEnabled": true 10 | }, 11 | "mobileSettings": { 12 | "enableS1EncryptedStoragePref2": false 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /force-app/main/default/classes/trees/BST_Node.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 2/21/2022. 3 | */ 4 | 5 | public with sharing class BST_Node { 6 | public Object value; 7 | public BST_Node left; 8 | public BST_Node right; 9 | 10 | public BST_Node(Object value) { 11 | this.value = value; 12 | this.left = null; 13 | this.right = null; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /force-app/main/default/lwc/recursionExamples/recursionExamples.js-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Recursion Examples 5 | false 6 | Recursion Examples 7 | 8 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/stack/StringStack.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by brooks on 9/25/2021. 3 | */ 4 | 5 | public with sharing class StringStack extends BasicStack { 6 | protected override void checkType(Object obj) { 7 | System.debug(obj); 8 | if (obj instanceof String) { 9 | return; 10 | } else { 11 | throw new BasicStackException(); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /force-app/main/default/lwc/containsDuplicates/containsDuplicates.js-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Contains Duplicates 5 | false 6 | Contains Duplicates 7 | 8 | -------------------------------------------------------------------------------- /force-app/main/default/lwc/linkedList/linkedList.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/18/2022. 3 | */ 4 | class Node { 5 | constructor(val) { 6 | this.val = val; 7 | this.next = null; 8 | } 9 | } 10 | 11 | class LinkedList { 12 | constructor() { 13 | this.size = 0; 14 | this.head = null; 15 | this.tail = null; 16 | } 17 | } 18 | 19 | export { LinkedList, Node }; 20 | -------------------------------------------------------------------------------- /force-app/main/default/classes/pass_by/PassBy.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 4/10/2022. 3 | */ 4 | 5 | public with sharing class PassBy { 6 | public static List timesTwo(List integers) { 7 | List deepCopy = integers.clone(); 8 | for (Integer i = 0; i < deepCopy.size(); i++) { 9 | deepCopy[i] *= 2; 10 | } 11 | return deepCopy; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /force-app/main/default/lwc/factorialCalculator/factorialCalculator.js-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 52.0 4 | Factorial Calculator 5 | true 6 | Factorial Calculator 7 | 8 | -------------------------------------------------------------------------------- /force-app/main/default/lwc/recursionExamples/__tests__/recursionExamplesTest.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/7/2022. 3 | */ 4 | 5 | import { factorialCalculator } from "../recursionExamples"; 6 | 7 | describe("recursion test suite", () => { 8 | it("should return the correct factorial", () => { 9 | const result = factorialCalculator(5); 10 | expect(result).toBe(120); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /force-app/main/default/classes/ListUtils_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/14/2022. 3 | */ 4 | 5 | @IsTest 6 | private class ListUtils_Tests { 7 | @IsTest 8 | static void itShouldGenerateAList() { 9 | //given 10 | //when 11 | List results = ListUtils.generateList(1000); 12 | //then 13 | System.assertEquals(1000, results.size()); 14 | } 15 | } -------------------------------------------------------------------------------- /.forceignore: -------------------------------------------------------------------------------- 1 | # List files or directories below to ignore them when running force:source:push, force:source:pull, and force:source:status 2 | # More information: https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_exclude_source.htm 3 | # 4 | 5 | package.xml 6 | 7 | # LWC configuration files 8 | **/jsconfig.json 9 | **/.eslintrc.json 10 | 11 | # LWC Jest 12 | **/__tests__/** 13 | 14 | **/profiles -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/stack/Stack_Test.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Johnson on 9/24/2021. 3 | * @description The stack is LIFO or Last In, First Out 4 | * The stack must have a push and pop methods 5 | * Push adds at an element to the top of the stack at constant time 6 | * Pop takes the last element and is also at constant time 7 | * BasicStack search has linear performance 8 | */ 9 | 10 | public with sharing class Stack_Test { 11 | } 12 | -------------------------------------------------------------------------------- /force-app/main/default/classes/LeetCode/BitShifting.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 2/27/2022. 3 | */ 4 | 5 | public with sharing class BitShifting { 6 | public static Integer hammingWeight(Integer n) { 7 | Integer bits = 0; 8 | Integer mask = 1; 9 | 10 | for (Integer i = 0; i < 32; i++) { 11 | if ((n & mask) != 0) { 12 | bits++; 13 | } 14 | mask <<= 1; 15 | } 16 | return bits; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /force-app/main/default/classes/ListUtils.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/14/2022. 3 | */ 4 | 5 | public with sharing class ListUtils { 6 | public static List generateList(Integer sizeOfList) { 7 | List listToReturn = new List(); 8 | for (Integer i = 0; i < sizeOfList; i++) { 9 | listToReturn.add(Integer.valueOf(Math.random() * 10000)); 10 | } 11 | return listToReturn; 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /force-app/main/default/classes/Queues/Queue.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Basic Queue FIFO data structure 3 | */ 4 | public with sharing class Queue { 5 | private List data; 6 | 7 | public Queue() { 8 | this.data = new List(); 9 | } 10 | 11 | public void push(Object obj) { 12 | this.data.add(obj); 13 | } 14 | 15 | public Object pop() { 16 | return this.data.remove(0); 17 | } 18 | 19 | public Integer size() { 20 | return this.data.size(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /scripts/apex/hello.apex: -------------------------------------------------------------------------------- 1 | // Use .apex files to store anonymous Apex. 2 | // You can execute anonymous Apex in VS Code by selecting the 3 | // apex text and running the command: 4 | // SFDX: Execute Anonymous Apex with Currently Selected Text 5 | // You can also execute the entire file by running the command: 6 | // SFDX: Execute Anonymous Apex with Editor Contents 7 | 8 | string tempvar = 'Enter_your_name_here'; 9 | System.debug('Hello World!'); 10 | System.debug('My name is ' + tempvar); -------------------------------------------------------------------------------- /force-app/main/default/classes/linked-list/SobjectNode_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/18/2022. 3 | */ 4 | 5 | @IsTest 6 | private class SobjectNode_Tests { 7 | @IsTest 8 | static void itShouldCreateANode() { 9 | //given 10 | Account a = new Account(Name = 'The Avengers'); 11 | //when 12 | SobjectNode node = new SobjectNode(a); 13 | //then 14 | System.assertEquals( 15 | node.sobj, 16 | a, 17 | 'The account should be added to the node' 18 | ); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "Launch Apex Replay Debugger", 9 | "type": "apex-replay", 10 | "request": "launch", 11 | "logFile": "${command:AskForLogFileName}", 12 | "stopOnEntry": true, 13 | "trace": true 14 | } 15 | ] 16 | } 17 | -------------------------------------------------------------------------------- /force-app/main/default/classes/sorting/MergeSort_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/15/2022. 3 | */ 4 | 5 | @IsTest 6 | private class MergeSort_Tests { 7 | @IsTest 8 | static void itShouldSort() { 9 | //given 10 | List listOne = new List{ 1, 4, 5, 8, 100, 2, 6, 9 }; 11 | //when 12 | MergeSort.sort(listOne); 13 | //then 14 | System.assertEquals(listOne[1], 2, 'It should sort the list'); 15 | System.assertEquals(listOne[7], 100, 'It should sort the list'); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /sfdx-project.json: -------------------------------------------------------------------------------- 1 | { 2 | "packageDirectories": [ 3 | { 4 | "path": "force-app", 5 | "default": true, 6 | "package": "DS_And_Algos_In_Apex", 7 | "versionName": "ver 0.1", 8 | "versionNumber": "0.1.0.NEXT" 9 | } 10 | ], 11 | "name": "Apex_CS", 12 | "namespace": "", 13 | "sfdcLoginUrl": "https://login.salesforce.com", 14 | "sourceApiVersion": "52.0", 15 | "packageAliases": { 16 | "DS_And_Algos_In_Apex": "0Ho5f000000Cb1eCAC" 17 | } 18 | } -------------------------------------------------------------------------------- /force-app/main/default/lwc/factorialCalculator/factorialCalculator.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/7/2022. 3 | */ 4 | 5 | import { LightningElement } from "lwc"; 6 | import { factorialCalculator } from "c/recursionExamples"; 7 | 8 | export default class FactorialCalculator extends LightningElement { 9 | result; 10 | 11 | handleClick() { 12 | const input = this.template.querySelector("lightning-input"); 13 | this.result = factorialCalculator(parseInt(input.value, 10)); 14 | console.log(this.result); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /force-app/main/default/classes/sorting/SelectionSort_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/14/2022. 3 | */ 4 | 5 | @IsTest 6 | private class SelectionSort_Tests { 7 | @IsTest 8 | static void itShouldSortList() { 9 | // given 10 | List integers = new List{ 7, 8, 5, 4, 9, 2 }; 11 | // when 12 | SelectionSort.sort(integers); 13 | // when 14 | System.assertEquals(2, integers[0], 'The list should be sorted'); 15 | System.assertEquals(9, integers[5], 'The list should be sorted'); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/sorting/SelectionSort_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/14/2022. 3 | */ 4 | 5 | @IsTest 6 | private class SelectionSort_Tests { 7 | @IsTest 8 | static void itShouldSortList() { 9 | // given 10 | List integers = new List{ 7, 8, 5, 4, 9, 2 }; 11 | // when 12 | SelectionSort.sort(integers); 13 | // when 14 | System.assertEquals(2, integers[0], 'The list should be sorted'); 15 | System.assertEquals(9, integers[5], 'The list should be sorted'); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /force-app/main/default/classes/sorting/BubbleSort_Tests.cls: -------------------------------------------------------------------------------- 1 | @IsTest 2 | private class BubbleSort_Tests { 3 | @IsTest 4 | static void itShouldSortList() { 5 | //given 6 | List integers = new List{ 7 | 18, 8 | 9, 9 | 0, 10 | 75, 11 | 75, 12 | 22, 13 | 44, 14 | 2, 15 | 145, 16 | -22 17 | }; 18 | //when 19 | Test.startTest(); 20 | BubbleSort bubbleSort = new BubbleSort(); 21 | bubbleSort.sort(integers); 22 | Test.stopTest(); 23 | //then 24 | System.assert(integers[0] == -22); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/sorting/BubbleSort_Tests.cls: -------------------------------------------------------------------------------- 1 | @IsTest 2 | private class BubbleSort_Tests { 3 | @IsTest 4 | static void itShouldSortList() { 5 | //given 6 | List integers = new List{ 7 | 18, 8 | 9, 9 | 0, 10 | 75, 11 | 75, 12 | 22, 13 | 44, 14 | 2, 15 | 145, 16 | -22 17 | }; 18 | //when 19 | Test.startTest(); 20 | BubbleSort bubbleSort = new BubbleSort(); 21 | bubbleSort.sort(integers); 22 | Test.stopTest(); 23 | //then 24 | System.assert(integers[0] == -22); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /force-app/main/default/classes/problem_solving_patterns/MultiplePointers_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/3/2022. 3 | */ 4 | 5 | @IsTest 6 | public with sharing class MultiplePointers_Tests { 7 | 8 | @IsTest 9 | static void returnsPairSumofZero(){ 10 | //given 11 | List integers = new List{-4, -3, -2, -1, 0, 1, 2, 3, 12}; 12 | //when 13 | List results = MultiplePointers.findZeroSumPair(integers); 14 | //then 15 | System.debug(results); 16 | System.assert(results[0] == -3 && results[1] == 3); 17 | } 18 | 19 | 20 | } -------------------------------------------------------------------------------- /force-app/main/default/classes/trees/Node_Opp_BST.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 2/21/2022. 3 | */ 4 | 5 | public with sharing class Node_Opp_BST { 6 | public Opportunity value; 7 | public Node_Opp_BST left; 8 | public Node_Opp_BST right; 9 | 10 | public Node_Opp_BST(Opportunity opp) { 11 | this.value = opp; 12 | this.left = null; 13 | this.right = null; 14 | } 15 | 16 | public override String toString() { 17 | return 'value is ' + 18 | value + 19 | '\n' + 20 | ' right node: ' + 21 | right + 22 | '\n' + 23 | ' left node ' + 24 | left; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /force-app/main/default/lwc/containsDuplicates/containsDuplicates.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Example solutions for LeetCode problem 217: contains duplicates 3 | */ 4 | 5 | function containsDuplicate(nums) { 6 | for (let i = 0; i < nums.length; i++) { 7 | for (let j = i + 1; j < nums.length; j++) { 8 | if (nums[i] === nums[j]) { 9 | return true; 10 | } 11 | } 12 | } 13 | return false; 14 | } 15 | 16 | function containsDuplicates(nums) { 17 | const dupes = new Set(); 18 | for (let i = 0; i < nums.length; i++) { 19 | if (dupes.has(nums[i])) { 20 | return true; 21 | } 22 | dupes.add(nums[i]); 23 | } 24 | return false; 25 | } 26 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/problem_solving_patterns/MultiplePointers_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/3/2022. 3 | */ 4 | 5 | @IsTest 6 | public with sharing class MultiplePointers_Tests { 7 | @IsTest 8 | static void returnsPairSumofZero() { 9 | //given 10 | List integers = new List{ 11 | -4, 12 | -3, 13 | -2, 14 | -1, 15 | 0, 16 | 1, 17 | 2, 18 | 3, 19 | 12 20 | }; 21 | //when 22 | List results = MultiplePointers.findZeroSumPair(integers); 23 | //then 24 | System.debug(results); 25 | System.assert(results[0] == -3 && results[1] == 3); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /force-app/main/default/lwc/factorialCalculator/factorialCalculator.html: -------------------------------------------------------------------------------- 1 | 23 | -------------------------------------------------------------------------------- /force-app/main/default/classes/recursion/Hannoi.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 3/24/2022. 3 | */ 4 | 5 | public with sharing class Hannoi { 6 | public static void towers( 7 | Integer n, 8 | String fromRod, 9 | String toRod, 10 | String helperRod 11 | ) { 12 | if (n == 1) { 13 | System.debug('take disk 1 from rod ' + fromRod + ' to rod ' + toRod); 14 | return; 15 | } 16 | 17 | towers(n - 1, fromRod, helperRod, toRod); 18 | 19 | System.debug( 20 | 'Take disk ' + 21 | n + 22 | ' from rod ' + 23 | fromRod + 24 | ' to rod ' + 25 | toRod 26 | ); 27 | //towers(n -1, helperRod, toRod, fromRod); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /.husky/_/husky.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | if [ -z "$husky_skip_init" ]; then 3 | debug () { 4 | [ "$HUSKY_DEBUG" = "1" ] && echo "husky (debug) - $1" 5 | } 6 | 7 | readonly hook_name="$(basename "$0")" 8 | debug "starting $hook_name..." 9 | 10 | if [ "$HUSKY" = "0" ]; then 11 | debug "HUSKY env variable is set to 0, skipping hook" 12 | exit 0 13 | fi 14 | 15 | if [ -f ~/.huskyrc ]; then 16 | debug "sourcing ~/.huskyrc" 17 | . ~/.huskyrc 18 | fi 19 | 20 | export readonly husky_skip_init=1 21 | sh -e "$0" "$@" 22 | exitCode="$?" 23 | 24 | if [ $exitCode != 0 ]; then 25 | echo "husky - $hook_name hook exited with code $exitCode (error)" 26 | exit $exitCode 27 | fi 28 | 29 | exit 0 30 | fi 31 | -------------------------------------------------------------------------------- /force-app/main/default/classes/stack/LinkedStack.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by brooks.johnson on 4/21/2022. 3 | */ 4 | 5 | public with sharing class LinkedStack implements IStack{ 6 | 7 | private SinglyLinkedList linkedList = new SinglyLinkedList(); 8 | 9 | 10 | public void push(Object obj) { 11 | this.linkedList.addToFront(obj); 12 | } 13 | 14 | public Object pop() { 15 | return this.linkedList.removeFromFront(); 16 | } 17 | 18 | public Object top() { 19 | return this.linkedList.head.data; 20 | } 21 | 22 | public Integer size() { 23 | return this.linkedList.size; 24 | } 25 | 26 | public Boolean isEmpty() { 27 | return this.linkedList.size == 0; 28 | } 29 | } -------------------------------------------------------------------------------- /force-app/main/default/classes/ListProblems/ListProbs.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 9/10/2022. 3 | */ 4 | 5 | public with sharing class ListProbs { 6 | /** 7 | * From Algo Expert 8 | * @param coins 9 | * 10 | * @return 11 | */ 12 | public static Integer nonConstructibleChange(List coins) { 13 | Integer currentChange = 0; 14 | coins.sort(); 15 | if (coins.size() == 0) { 16 | return 1; 17 | } 18 | for (Integer i = 0; i < coins.size(); i++) { 19 | Integer current = coins.get(i); 20 | if (current > currentChange + 1) { 21 | break; 22 | } else { 23 | currentChange += current; 24 | } 25 | } 26 | return currentChange + 1; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /force-app/main/default/classes/linked-list/CircularLinkedList.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 3/19/2022. 3 | */ 4 | 5 | public with sharing class CircularLinkedList { 6 | private Node tail; 7 | private Integer size; 8 | 9 | public CircularLinkedList() { 10 | this.tail = null; 11 | this.size = 0; 12 | } 13 | 14 | public Boolean isEmpty() { 15 | return this.size == 0; 16 | } 17 | 18 | public Integer size() { 19 | return this.size; 20 | } 21 | 22 | public void addFirst(Object data) { 23 | if (this.size == 0) { 24 | this.tail = new Node(data, null); 25 | tail.setNext(tail); // link tail to itself 26 | } else { 27 | Node newNode = new Node(data, this.tail.next()); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /force-app/main/default/classes/LeetCode/RemoveDuplicatesFromSortedArray.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 2/23/2022. 3 | */ 4 | 5 | public with sharing class RemoveDuplicatesFromSortedArray { 6 | public static Integer removeDuplicates(List nums) { 7 | Integer length = nums.size(); 8 | if (length == 0) { 9 | return 0; 10 | } 11 | Integer insertIndex = 1; 12 | for (Integer i = 1; i < length; i++) { 13 | //because this is a sorted array we can assume that index 0 is a value we have not seen before 14 | //0,0,1,1,1,2,2,3,3,4 15 | if (nums[i] != nums[i - 1]) { 16 | nums[insertIndex] = nums[i]; 17 | System.debug('new nums = ' + nums); 18 | insertIndex++; 19 | } 20 | } 21 | return insertIndex; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /force-app/main/default/classes/problem_solving_patterns/SlidingWindow_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 3/3/2022. 3 | */ 4 | 5 | @IsTest 6 | private class SlidingWindow_Tests { 7 | private static List data = new List{ 8 | 2, 9 | 6, 10 | 9, 11 | 2, 12 | 1, 13 | 8, 14 | 5, 15 | 6, 16 | 3 17 | }; 18 | @IsTest 19 | static void testBruteForce() { 20 | //when 21 | Integer result = SlidingWindow.bfMaxSubArraySum(data, 3); 22 | //then 23 | System.assertEquals(19, result, 'It should return 19'); 24 | } 25 | 26 | @IsTest 27 | static void testSlidingWindow() { 28 | //when 29 | Integer result = SlidingWindow.swMaxSubArraySum(data, 3); 30 | //then 31 | System.assertEquals(19, result, 'It should calculate 19'); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /force-app/main/default/classes/ListProblems/ListProbs_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 9/10/2022. 3 | */ 4 | 5 | @IsTest 6 | public with sharing class ListProbs_Tests { 7 | @IsTest 8 | static void nonConstructibleChange() { 9 | List coins = new List{ 5, 7, 1, 1, 2, 3, 22 }; 10 | System.assert(ListProbs.nonConstructibleChange(coins) == 20); 11 | List coins2 = new List{ 87 }; 12 | List coins3 = new List{ 13 | 109, 14 | 2000, 15 | 8765, 16 | 19, 17 | 18, 18 | 17, 19 | 16, 20 | 8, 21 | 1, 22 | 1, 23 | 2, 24 | 4 25 | }; 26 | 27 | System.assert(ListProbs.nonConstructibleChange(coins2) == 1); 28 | System.assert(ListProbs.nonConstructibleChange(coins3) == 87); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /force-app/main/default/classes/Bitwise-Operations/BitWiseBasics.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 3/6/2022. 3 | */ 4 | 5 | public with sharing class BitWiseBasics { 6 | public static Integer bitwiseAddition(Integer x, Integer y) { 7 | while (y != 0) { 8 | System.debug('value of y ' + y); 9 | //carry holds places where both values have a common bit 10 | Integer carry = x & y; 11 | System.debug('carry: ' + carry); 12 | //xor gives us where at least one bit is 0 13 | // 6 xor 6 = 0000 14 | // 5 ^ 3 : 0101 ^ 0011 = 0110 15 | x = x ^ y; 16 | System.debug('xor value ' + x); 17 | y = carry << 1; 18 | System.debug('shifted Y' + y); 19 | } 20 | return x; 21 | } 22 | 23 | public static Boolean isEven(Integer x) { 24 | // 0101 25 | // 0001 26 | return (x & 1) != 1; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /force-app/main/default/classes/stack/MinStack.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 9/6/2022. 3 | */ 4 | 5 | public with sharing class MinStack { 6 | private List stack; 7 | private List minStack; 8 | 9 | public MinStack() { 10 | this.stack = new List(); 11 | this.minStack = new List(); 12 | } 13 | 14 | public void push(Integer value) { 15 | stack.add(value); 16 | if (minStack.isEmpty() || value <= getMin()) { 17 | minStack.add(value); 18 | } 19 | } 20 | 21 | public Integer pop() { 22 | if (top() == getMin()) { 23 | minStack.remove(minStack.size() - 1); 24 | } 25 | 26 | return stack.remove(stack.size() - 1); 27 | } 28 | 29 | public Integer top() { 30 | return stack[stack.size() - 1]; 31 | } 32 | 33 | public Integer getMin() { 34 | return minStack[minStack.size() - 1]; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /force-app/main/default/classes/stack/ListStack.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by brooks.johnson on 4/21/2022. 3 | */ 4 | 5 | public with sharing class ListStack implements IStack{ 6 | 7 | private List stack; 8 | 9 | public ListStack(){ 10 | this.stack = new List(); 11 | } 12 | 13 | public void push(Object obj) { 14 | stack.add(obj); 15 | } 16 | 17 | public Object pop() { 18 | if (this.stack.isEmpty()) { 19 | return null; 20 | } 21 | return this.stack.remove(this.stack.size() -1); 22 | } 23 | 24 | public Object top() { 25 | if (this.stack.isEmpty()) { 26 | return null; 27 | } 28 | return stack[this.stack.size() -1]; 29 | } 30 | 31 | public Integer size() { 32 | return stack.size(); 33 | } 34 | 35 | public Boolean isEmpty() { 36 | return this.stack.isEmpty(); 37 | } 38 | } -------------------------------------------------------------------------------- /force-app/main/default/classes/Queues/Queue_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 2/9/2022. 3 | */ 4 | 5 | @IsTest 6 | private class Queue_Tests { 7 | @IsTest 8 | static void itShouldPushToQueue() { 9 | //given 10 | Queue queue = new Queue(); 11 | //when 12 | queue.push('hamburger'); 13 | queue.push(new Account(Name = 'Test')); 14 | queue.push(100); 15 | //then 16 | System.assertEquals(3, queue.size()); 17 | } 18 | @IsTest 19 | static void itShouldPopFromQueue() { 20 | //given 21 | Queue queue = new Queue(); 22 | queue.push('hamburger'); 23 | queue.push(new Account(Name = 'Test')); 24 | queue.push(100); 25 | //when 26 | Object obj1 = queue.pop(); 27 | Object obj2 = queue.pop(); 28 | //then 29 | System.assertEquals('hamburger', obj1); 30 | System.assert(obj2 instanceof Account); 31 | System.assertEquals(1, queue.size()); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used for Git repositories to specify intentionally untracked files that Git should ignore. 2 | # If you are not using git, you can delete this file. For more information see: https://git-scm.com/docs/gitignore 3 | # For useful gitignore templates see: https://github.com/github/gitignore 4 | 5 | # Salesforce cache 6 | .sfdx/ 7 | .localdevserver/ 8 | 9 | # LWC VSCode autocomplete 10 | **/lwc/jsconfig.json 11 | 12 | # LWC Jest coverage reports 13 | coverage/ 14 | 15 | # Logs 16 | logs 17 | *.log 18 | npm-debug.log* 19 | yarn-debug.log* 20 | yarn-error.log* 21 | 22 | # Dependency directories 23 | node_modules/ 24 | 25 | # Eslint cache 26 | .eslintcache 27 | 28 | # MacOS system files 29 | .DS_Store 30 | 31 | # Windows system files 32 | Thumbs.db 33 | ehthumbs.db 34 | [Dd]esktop.ini 35 | $RECYCLE.BIN/ 36 | 37 | # Local environment variables 38 | .env 39 | 40 | #IntelliJ and IC 41 | /IlluminatedCloud/ 42 | /.idea/ 43 | /Apex_CS.iml 44 | -------------------------------------------------------------------------------- /force-app/main/default/classes/sorting/InsertionSort.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 9/27/2021. 3 | * We have a sorted and unsorted partition of our array 4 | * The sorted partition begins at array[0]. A collection with one element is by definition sorted 5 | * The first unsorted partition index = 1 6 | * We traverse the sorted partition from right to left 7 | */ 8 | 9 | public with sharing class InsertionSort { 10 | /* List integers = new List{5, 4, 6, -22, 10, 500, 76};*/ 11 | public static void sort(List valuesToSort) { 12 | //we start at index 1 the first unsorted index 13 | for (Integer i = 1; i < valuesToSort.size(); i++) { 14 | Integer current = valuesToSort[i]; 15 | Integer j = i - 1; 16 | //check the sorted partition 17 | while (j >= 0 && valuesToSort[j] > current) { 18 | valuesToSort[j + 1] = valuesToSort[j]; 19 | j--; 20 | } 21 | valuesToSort[j + 1] = current; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/sorting/InsertionSort.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 9/27/2021. 3 | * We have a sorted and unsorted partition of our array 4 | * The sorted partition begins at array[0]. A collection with one element is by definition sorted 5 | * The first unsorted partition index = 1 6 | * We traverse the sorted partition from right to left 7 | */ 8 | 9 | public with sharing class InsertionSort { 10 | /* List integers = new List{5, 4, 6, -22, 10, 500, 76};*/ 11 | public static void sort(List valuesToSort) { 12 | //we start at index 1 the first unsorted index 13 | for (Integer i = 1; i < valuesToSort.size(); i++) { 14 | Integer current = valuesToSort[i]; 15 | Integer j = i - 1; 16 | //check the sorted partition 17 | while (j >= 0 && valuesToSort[j] > current) { 18 | valuesToSort[j + 1] = valuesToSort[j]; 19 | j--; 20 | } 21 | valuesToSort[j + 1] = current; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /force-app/main/default/classes/stack/StackDemo_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by brooks.johnson on 4/21/2022. 3 | */ 4 | 5 | @IsTest 6 | private class StackDemo_Tests { 7 | @IsTest 8 | static void itShouldReturnFalseMissingDelimiter() { 9 | //given 10 | String test = '{'; 11 | //when 12 | Boolean result = StackDemo.isMatched(test); 13 | System.assert(result == false); 14 | } 15 | @IsTest 16 | static void itShouldReturnFalseWrongDelimiter() { 17 | //given 18 | String test = '{]'; 19 | //when 20 | Boolean result = StackDemo.isMatched(test); 21 | System.assert(result == false); 22 | } 23 | 24 | @IsTest 25 | static void itShouldReturnTrue() { 26 | //given 27 | String test = '{ () }'; 28 | //when 29 | Boolean result = StackDemo.isMatched(test); 30 | System.assert(result == true); 31 | } 32 | @IsTest 33 | static void itShouldReturnTrue2() { 34 | String test = '((([[{}]])))'; 35 | System.assert(StackDemo.isMatched(test) == true); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /force-app/main/default/classes/sorting/SelectionSort.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * @description A basic sorting algorithm that runs at O(n^2) time. 3 | */ 4 | 5 | public with sharing class SelectionSort { 6 | public static void sort(List integers) { 7 | Integer listLength = integers.size() - 1; 8 | for (Integer i = 0; i < listLength; i++) { 9 | Integer minIndex = i; 10 | for (Integer j = i + 1; j <= listLength; j++) { 11 | System.debug( 12 | LoggingLevel.INFO, 13 | 'comparing ' + 14 | integers[j] + 15 | ' ' + 16 | integers[i] 17 | ); 18 | if (integers[j] < integers[minIndex]) { 19 | minIndex = j; 20 | } 21 | } 22 | Integer temp = integers[i]; 23 | integers[i] = integers[minIndex]; 24 | integers[minIndex] = temp; 25 | System.debug(LoggingLevel.INFO, 'integers after inner loop ' + integers); 26 | } 27 | System.debug(LoggingLevel.INFO, 'Value of sorted list = ' + integers); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/sorting/SelectionSort.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * @description A basic sorting algorithm that runs at O(n^2) time. 3 | */ 4 | 5 | public with sharing class SelectionSort { 6 | public static void sort(List integers) { 7 | Integer listLength = integers.size() - 1; 8 | for (Integer i = 0; i < listLength; i++) { 9 | Integer minIndex = i; 10 | for (Integer j = i + 1; j <= listLength; j++) { 11 | System.debug( 12 | LoggingLevel.INFO, 13 | 'comparing ' + 14 | integers[j] + 15 | ' ' + 16 | integers[i] 17 | ); 18 | if (integers[j] < integers[minIndex]) { 19 | minIndex = j; 20 | } 21 | } 22 | Integer temp = integers[i]; 23 | integers[i] = integers[minIndex]; 24 | integers[minIndex] = temp; 25 | System.debug(LoggingLevel.INFO, 'integers after inner loop ' + integers); 26 | } 27 | System.debug(LoggingLevel.INFO, 'Value of sorted list = ' + integers); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /manifest/package.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | * 5 | ApexClass 6 | 7 | 8 | * 9 | ApexComponent 10 | 11 | 12 | * 13 | ApexPage 14 | 15 | 16 | * 17 | ApexTestSuite 18 | 19 | 20 | * 21 | ApexTrigger 22 | 23 | 24 | * 25 | AuraDefinitionBundle 26 | 27 | 28 | * 29 | LightningComponentBundle 30 | 31 | 32 | * 33 | StaticResource 34 | 35 | 52.0 36 | -------------------------------------------------------------------------------- /force-app/main/default/lwc/factorialCalculator/__tests__/factorialCalculatorTest.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/7/2022. 3 | */ 4 | 5 | import { createElement } from "lwc"; 6 | import factorialCalculator from "c/factorialCalculator"; 7 | 8 | describe("factorial calculator test suite", () => { 9 | afterEach(() => { 10 | while (document.body.firstChild) { 11 | document.body.removeChild(document.body.firstChild); 12 | } 13 | }); 14 | it("should render result", () => { 15 | const element = createElement("c-factorial-calculator", { 16 | is: factorialCalculator 17 | }); 18 | document.body.appendChild(element); 19 | const input = element.shadowRoot.querySelector("lightning-input"); 20 | input.value = 5; 21 | const button = element.shadowRoot.querySelector("lightning-button"); 22 | button.dispatchEvent(new CustomEvent("click")); 23 | const result = element.shadowRoot.querySelector("p"); 24 | return Promise.resolve().then(() => { 25 | expect(result.textContent).toBe("result: 120"); 26 | }); 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /force-app/main/default/classes/stack/IStack.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * The stack ADT 3 | * A collection of objects that are inserted and removed according to 4 | * the last-in / first-out principle or LIFO. 5 | */ 6 | 7 | public interface IStack { 8 | /** 9 | * @description returns the number of elements in the stack 10 | * @return the number of elements in the stack 11 | */ 12 | Integer size(); 13 | 14 | /** 15 | * @description Tests if the stack is empty 16 | * @return true if the stack is empty, false otherwise 17 | */ 18 | Boolean isEmpty(); 19 | 20 | /** 21 | *@description Inserts object at the top of the stack 22 | * @param o The object to insert 23 | */ 24 | void push(Object o); 25 | 26 | /** 27 | * @description Returns but does not remove the object at the top of the stack 28 | * @return The object at the top of the stack 29 | */ 30 | Object top(); 31 | 32 | /** 33 | * @description Removes and returns the object at the top of the stack 34 | * @return object removed from the top of the stack 35 | */ 36 | Object pop(); 37 | } 38 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/search/BinarySearch.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/14/2022. 3 | */ 4 | 5 | public with sharing class BinarySearch { 6 | // List with values of 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 7 | 8 | // Find the index on the right hand side of the array 9 | // Find the Index on the left hand side of the array 10 | // Determine the middle value 11 | // if the middle value == the value we are searching for we return the index 12 | // if the middle value is less than the value we are searching for we discard the left hand side of the array 13 | // if the middle value is greater then the value we are searching for we discard the right hand side of the array 14 | public static Integer binarySearchIterative( 15 | List listToSearch, 16 | Integer target 17 | ) { 18 | return -1; 19 | } 20 | @SuppressWarnings('PMD.ExcessiveParameterList') 21 | public static Integer binarySearchRecursive( 22 | List listToSearch, 23 | Integer target, 24 | Integer left, 25 | Integer right 26 | ) { 27 | return -1; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /force-app/main/default/classes/StringProblems_Test.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 9/10/2022. 3 | */ 4 | 5 | @IsTest 6 | private class StringProblems_Test { 7 | @IsTest 8 | static void testGetCommonChars() { 9 | List strs = new List{ 'bellla', 'label', 'roller' }; 10 | List results = StringProblems.commonChars(strs); 11 | results.sort(); 12 | System.assert(results[0] == 'e'); 13 | System.assert(results[1] == 'l'); 14 | System.assert(results[2] == 'l'); 15 | System.assert(results.size() == 3); 16 | } 17 | @IsTest 18 | static void testGetCommonChars2() { 19 | List strs = new List{ 20 | 'cattle', 21 | 'tattle', 22 | 'battle', 23 | 'rattle' 24 | }; 25 | List results = StringProblems.commonChars(strs); 26 | results.sort(); 27 | System.assert(results.size() == 5); 28 | System.assert(results[0] == 'a'); 29 | System.assert(results[1] == 'e'); 30 | System.assert(results[2] == 'l'); 31 | System.assert(results[3] == 't'); 32 | System.assert(results[4] == 't'); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /force-app/main/default/classes/problem_solving_patterns/MultiplePointers.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/3/2022. 3 | */ 4 | 5 | public with sharing class MultiplePointers { 6 | /** 7 | * @param integers a sorted list to compare. 8 | *@description This is the O(n) implementation. 9 | * @return the first pair with a sum of 0 10 | */ 11 | public static List findZeroSumPair(List integers) { 12 | Integer left = 0; 13 | Integer right = integers.size() - 1; 14 | while (left < right) { 15 | Integer sum = integers[left] + integers[right]; 16 | if (sum == 0) { 17 | List results = new List{ 18 | integers[left], 19 | integers[right] 20 | }; 21 | return results; 22 | } else if (sum > 0) { 23 | //if the sum is greater than zero we know what a possible pair might still exist 24 | //so we decrement the right index; 25 | right--; 26 | } else { 27 | //If the sum is less than zero we can move the left index ahead 28 | left++; 29 | } 30 | } 31 | return null; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/problem_solving_patterns/MultiplePointers.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/3/2022. 3 | */ 4 | 5 | public with sharing class MultiplePointers { 6 | /** 7 | * @param integers a sorted list to compare. 8 | *@description This is the O(n) implementation. 9 | * @return the first pair with a sum of 0 10 | */ 11 | public static List findZeroSumPair(List integers) { 12 | Integer left = 0; 13 | Integer right = integers.size() - 1; 14 | while (left < right) { 15 | Integer sum = integers[left] + integers[right]; 16 | if (sum == 0) { 17 | List results = new List{ 18 | integers[left], 19 | integers[right] 20 | }; 21 | return results; 22 | } else if (sum > 0) { 23 | //if the sum is greater than zero we know what a possible pair might still exist 24 | //so we decrement the right index; 25 | right--; 26 | } else { 27 | //If the sum is less than zero we can move the left index ahead 28 | left++; 29 | } 30 | } 31 | return null; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /force-app/main/default/classes/LeetCode/BuySellStock_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 2/19/2022. 3 | */ 4 | 5 | @IsTest 6 | public with sharing class BuySellStock_Tests { 7 | private static final List PRICES_ONE = new List{ 8 | 7, 9 | 1, 10 | 5, 11 | 3, 12 | 6, 13 | 4 14 | }; 15 | private static final List PRICES_TWO = new List{ 16 | 7, 17 | 6, 18 | 4, 19 | 3, 20 | 1 21 | }; 22 | @IsTest 23 | static void testBruteForce() { 24 | //when 25 | Integer profit = BuySellStock.buySellBruteForce(PRICES_ONE); 26 | System.assertEquals(5, profit, 'It should return 5'); 27 | //when 28 | profit = BuySellStock.buySellBruteForce(PRICES_TWO); 29 | System.assertEquals(0, profit, 'It should return 0'); 30 | } 31 | 32 | @IsTest 33 | static void testBuyStock() { 34 | //when 35 | Integer profit = BuySellStock.buyStock(PRICES_ONE); 36 | System.assertEquals(5, profit, 'It should return 5'); 37 | //when 38 | profit = BuySellStock.buyStock(PRICES_TWO); 39 | System.assertEquals(0, profit, 'It should return 0'); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /force-app/main/default/classes/LeetCode/ValidPalindrome.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 2/27/2022. 3 | */ 4 | 5 | public with sharing class ValidPalindrome { 6 | //amanaplanacanalpanama 7 | public static Boolean isPalindrome(String str) { 8 | String regex = '[/\\W]'; 9 | str = str.replaceAll(regex, '').toLowerCase(); 10 | Integer rightPointer = str.length() - 1; 11 | for (Integer i = 0; i < rightPointer; i++) { 12 | if (str.charAt(i) != str.charAt(rightPointer)) { 13 | return false; 14 | } else { 15 | rightPointer--; 16 | } 17 | } 18 | return true; 19 | } 20 | 21 | public static Boolean isPalindromeRecurse(String str) { 22 | System.debug('checking ' + str); 23 | String regex = '[/\\W]'; 24 | str = str.replaceAll(regex, '').toLowerCase(); 25 | System.debug(str.length()); 26 | if (str.length() <= 1) { 27 | System.debug('in base case'); 28 | return true; 29 | } 30 | if (str.charAt(0) == str.charAt(str.length() - 1)) { 31 | return isPalindromeRecurse(str.substring(1, str.length() - 1)); 32 | } else { 33 | return false; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/stack/BasicStack.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 9/24/2021. 3 | */ 4 | 5 | public abstract class BasicStack { 6 | public class BasicStackException extends Exception { 7 | } 8 | 9 | private List data; 10 | private Integer stackPointer = 0; 11 | 12 | protected abstract void checkType(Object obj); 13 | 14 | public BasicStack() { 15 | this.data = new List(); 16 | System.debug(data); 17 | this.stackPointer = data.size(); 18 | System.debug('stackPointer' + stackPointer); 19 | } 20 | 21 | public void push(Object obj) { 22 | checkType(obj); 23 | this.data.add(obj); 24 | } 25 | public Object pop() { 26 | return this.data.remove(data.size() - 1); 27 | } 28 | 29 | public Boolean contains(Object obj) { 30 | return this.data.contains(obj); 31 | } 32 | 33 | public Object access(Object obj) { 34 | while (this.data.size() > 0) { 35 | Object temp = pop(); 36 | if (temp.equals(obj)) { 37 | return temp; 38 | } 39 | } 40 | return null; 41 | } 42 | 43 | public Integer getSize() { 44 | return this.data.size(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /force-app/main/default/classes/doubly-linked-list/DoublyLinkedList_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 2/5/2022. 3 | */ 4 | 5 | @IsTest 6 | private class DoublyLinkedList_Tests { 7 | @IsTest 8 | static void itShouldAddToEnd() { 9 | //given 10 | DoublyLinkedList doublyLinkedList = new DoublyLinkedList(); 11 | //when 12 | doublyLinkedList.addToEnd(10); 13 | doublyLinkedList.addToEnd(20); 14 | //then 15 | System.assert(doublyLinkedList.size == 2, 'It should increment the size'); 16 | System.assert(doublyLinkedList.head.value == 10); 17 | System.assertEquals(20, doublyLinkedList.tail.value); 18 | } 19 | 20 | @IsTest 21 | static void itShouldRemoveFromEnd() { 22 | //given 23 | DoublyLinkedList doublyLinkedList = new DoublyLinkedList(); 24 | //when 25 | doublyLinkedList.addToEnd(10); 26 | doublyLinkedList.addToEnd(20); 27 | doublyLinkedList.addToEnd(30); 28 | //then 29 | DoublyLinkedList.Node node = doublyLinkedList.removeFromEnd(); 30 | System.assert(30 == node.value); 31 | System.assert(20 == doublyLinkedList.tail.value); 32 | System.assert(2 == doublyLinkedList.size); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /force-app/main/default/classes/stack/ListStack_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by brooks.johnson on 4/21/2022. 3 | */ 4 | 5 | @IsTest 6 | private class ListStack_Tests { 7 | @IsTest 8 | static void itShouldPushToStack() { 9 | //given 10 | Istack stack = new ListStack(); 11 | //when 12 | stack.push(1); 13 | stack.push(2); 14 | //then 15 | System.assert(stack.size() == 2); 16 | System.assert(stack.isEmpty() == false); 17 | } 18 | 19 | @isTest 20 | static void itShouldPopFromFront(){ 21 | //given 22 | IStack stack = new ListStack(); 23 | stack.push(1); 24 | stack.push(2); 25 | //when 26 | Integer result = (Integer)stack.pop(); 27 | System.assert(stack.size() == 1); 28 | System.assert(result == 2); 29 | } 30 | 31 | @IsTest 32 | static void itShouldReturnFromFront(){ 33 | //given 34 | Istack stack = new ListStack(); 35 | stack.push(1); 36 | stack.push(2); 37 | stack.push(3); 38 | stack.push(4); 39 | //when 40 | Integer result = (Integer)stack.top(); 41 | System.assert(result == 4); 42 | System.assert(stack.size() == 4); 43 | } 44 | } -------------------------------------------------------------------------------- /force-app/main/default/classes/stack/LinkedStack_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by brooks.johnson on 4/21/2022. 3 | */ 4 | 5 | @IsTest 6 | private class LinkedStack_Tests { 7 | @IsTest 8 | static void itShouldPush() { 9 | //given 10 | IStack stack = new LinkedStack(); 11 | //when 12 | stack.push(1); 13 | stack.push(2); 14 | stack.push(3); 15 | //then 16 | System.assert(stack.size() == 3); 17 | System.assert(stack.isEmpty() == false); 18 | } 19 | 20 | @IsTest 21 | static void itShouldPop(){ 22 | //given 23 | IStack stack = new LinkedStack(); 24 | stack.push(1); 25 | stack.push(2); 26 | stack.push(3); 27 | //when 28 | Integer result = (Integer)stack.pop(); 29 | System.assert(result == 3); 30 | System.assert(stack.size() == 2); 31 | } 32 | 33 | @IsTest 34 | static void itShouldGetTop(){ 35 | //given 36 | IStack stack = new LinkedStack(); 37 | stack.push(1); 38 | stack.push(2); 39 | stack.push(3); 40 | //when 41 | Integer result = (Integer)stack.pop(); 42 | System.assert(stack.top() == 3); 43 | System.assert(stack.size() == 3); 44 | } 45 | 46 | 47 | } -------------------------------------------------------------------------------- /force-app/main/default/classes/search/LinearSearch.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * @description simple linear or "brute force" search 3 | * This is an o(N) algorithm the time complexity is directly related to the size of the inputs 4 | */ 5 | 6 | public with sharing class LinearSearch { 7 | 8 | 9 | public static Integer search(List valuesToSearch, Integer value) { 10 | Integer steps = 0; 11 | for (Integer i = 0; i < valuesToSearch.size(); i++) { 12 | steps++; 13 | if (valuesToSearch[i] == value) { 14 | System.debug(LoggingLevel.INFO, 'it took ' + steps + ' steps to search'); 15 | return i; 16 | } 17 | } 18 | System.debug(LoggingLevel.INFO, 'It took ' + steps + ' steps to search'); 19 | return -1; 20 | } 21 | 22 | public static Integer search(List valuesToSearch, String value) { 23 | Integer steps = 0; 24 | for (Integer i = 0; i < valuesToSearch.size(); i++) { 25 | steps++; 26 | if (valuesToSearch[i] == value) { 27 | System.debug(LoggingLevel.INFO, 'It took ' + steps + ' steps to search'); 28 | return i; 29 | } 30 | } 31 | System.debug(LoggingLevel.INFO, 'It took ' + steps + ' steps to search'); 32 | return -1; 33 | } 34 | } 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /force-app/main/default/classes/search/LinearSearch_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/14/2022. 3 | */ 4 | 5 | @IsTest 6 | public with sharing class LinearSearch_Tests { 7 | 8 | public static final List INTEGER_LIST = new List{ 9 | 1, 97, 14, 362, 10, 278, 10 | 45, 66, 77, 235, 99, 586 11 | }; 12 | public static final List ALPHABET = new List{ 13 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 14 | 'i', 'j', 'k', 'l', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' 15 | }; 16 | 17 | @IsTest 18 | static void itShouldFindIntegerValue() { 19 | //given 20 | //when 21 | Integer index = LinearSearch.search(INTEGER_LIST, 586); 22 | //then 23 | System.assert(index == 11, 'It should return the correct index'); 24 | } 25 | 26 | @IsTest 27 | static void itShouldFindCorrectIndexInAlphabet() { 28 | //given 29 | //when 30 | Integer searchIndex = LinearSearch.search(ALPHABET, 'a'); 31 | //then 32 | System.assertEquals(0, searchIndex, 'It should return the correct index'); 33 | } 34 | 35 | @IsTest 36 | static void itShouldReturnNegOne() { 37 | //given 38 | //when 39 | Integer searchIndex = LinearSearch.search(ALPHABET, 'brooks'); 40 | System.assertEquals(searchIndex, -1, 'It should return negative one'); 41 | } 42 | } -------------------------------------------------------------------------------- /force-app/main/default/classes/sorting/BubbleSort.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Brooks Johnson 3 | * @description The most basic sort algo. 4 | * Bubble sort partitions the array into sorted and unsorted 5 | * O(n^2) time complexity / quadratic 6 | * 100 steps to sort 100 items, 10,000 to sort 100 etc... 7 | */ 8 | 9 | public with sharing class BubbleSort { 10 | /** 11 | * @description The for loop is initialized to to the length of the list and decremented to 0 12 | * A list with 5 items will start with and unsortedIndex of 5 and decrement after each loop until it reaches 0 13 | * @param integersToSort The list of integers we want to sort. 14 | * 15 | */ 16 | public void sort(List integersToSort) { 17 | for ( 18 | Integer unsortedIndex = integersToSort.size() - 1; 19 | unsortedIndex > 0; 20 | unsortedIndex-- 21 | ) { 22 | //A nested for loop should be an automatic signal that an algo will degrade quickly 23 | for (Integer i = 0; i < unsortedIndex; i++) { 24 | if (integersToSort[i] > integersToSort[i + 1]) { 25 | //bubble the larger value to the end of the array 26 | swap(integersToSort, i, i + 1); 27 | } 28 | } 29 | } 30 | } 31 | private void swap(List arrayToSort, Integer i, Integer j) { 32 | if (i == j) { 33 | return; 34 | } 35 | Integer temp = arrayToSort[i]; 36 | arrayToSort[i] = arrayToSort[j]; 37 | arrayToSort[j] = temp; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/sorting/BubbleSort.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Brooks Johnson 3 | * @description The most basic sort algo. 4 | * Bubble sort partitions the array into sorted and unsorted 5 | * O(n^2) time complexity / quadratic 6 | * 100 steps to sort 100 items, 10,000 to sort 100 etc... 7 | */ 8 | 9 | public with sharing class BubbleSort { 10 | /** 11 | * @description The for loop is initialized to to the length of the list and decremented to 0 12 | * A list with 5 items will start with and unsortedIndex of 5 and decrement after each loop until it reaches 0 13 | * @param integersToSort The list of integers we want to sort. 14 | * 15 | */ 16 | public void sort(List integersToSort) { 17 | for ( 18 | Integer unsortedIndex = integersToSort.size() - 1; 19 | unsortedIndex > 0; 20 | unsortedIndex-- 21 | ) { 22 | //A nested for loop should be an automatic signal that an algo will degrade quickly 23 | for (Integer i = 0; i < unsortedIndex; i++) { 24 | if (integersToSort[i] > integersToSort[i + 1]) { 25 | //bubble the larger value to the end of the array 26 | swap(integersToSort, i, i + 1); 27 | } 28 | } 29 | } 30 | } 31 | private void swap(List arrayToSort, Integer i, Integer j) { 32 | if (i == j) { 33 | return; 34 | } 35 | Integer temp = arrayToSort[i]; 36 | arrayToSort[i] = arrayToSort[j]; 37 | arrayToSort[j] = temp; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /force-app/main/default/classes/doubly-linked-list/DoublyLinkedList.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 2/5/2022. 3 | */ 4 | 5 | public with sharing class DoublyLinkedList { 6 | public Node head; 7 | public Node tail; 8 | public Integer size; 9 | 10 | public DoublyLinkedList() { 11 | this.head = null; 12 | this.tail = null; 13 | this.size = 0; 14 | } 15 | 16 | public void addToEnd(Object value) { 17 | Node node = new Node(value); 18 | if (this.head == null) { 19 | this.head = node; 20 | this.tail = node; 21 | } else { 22 | this.tail.next = node; 23 | node.previous = tail; 24 | this.tail = node; 25 | } 26 | increment(); 27 | } 28 | 29 | public Node removeFromEnd() { 30 | if (this.size == 0) { 31 | return null; 32 | } 33 | Node returnValue = this.tail; 34 | 35 | if (this.size == 1) { 36 | this.head = null; 37 | this.tail = null; 38 | returnValue.previous = null; 39 | return returnValue; 40 | } 41 | 42 | this.tail = this.tail.previous; 43 | this.tail.next = null; 44 | decrement(); 45 | return returnValue; 46 | } 47 | 48 | private void increment() { 49 | this.size++; 50 | } 51 | 52 | private void decrement() { 53 | this.size--; 54 | } 55 | 56 | public class Node { 57 | public Object value; 58 | public Node next; 59 | public Node previous; 60 | 61 | public Node(Object obj) { 62 | this.value = obj; 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /force-app/main/default/classes/problem_solving_patterns/FrequencyCounting_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 12/29/2021. 3 | */ 4 | 5 | @IsTest 6 | private class FrequencyCounting_Tests { 7 | 8 | private static final List listOne = new List{1, 2, 3, 4, 3}; 9 | private static final List listTwo= new List{1, 4, 9, 9, 16}; 10 | private static final List listThree= new List{1, 4, 9, 4, 16}; 11 | @IsTest 12 | static void listsShouldMatch() { 13 | Boolean result = FrequencyCounting.testSquaresOne(listOne, listTwo); 14 | System.assert(result == true); 15 | } 16 | 17 | @IsTest 18 | static void listsShouldNotMatch() { 19 | Boolean result = FrequencyCounting.testSquaresOne(listOne, listThree); 20 | System.assert(result == false); 21 | } 22 | 23 | @IsTest 24 | static void testForEqualityofSize(){ 25 | List emptyList = new List(); 26 | Boolean result = FrequencyCounting.testSquaresOne(listOne, emptyList); 27 | System.assert(result == false); 28 | } 29 | 30 | @IsTest 31 | static void listsShouldMatchOptimized() { 32 | Boolean result = FrequencyCounting.testSquaresTwo(listOne, listTwo); 33 | System.assert(result == true); 34 | } 35 | 36 | @IsTest 37 | static void listsShouldNotMatchOptimized() { 38 | Boolean result = FrequencyCounting.testSquaresTwo(listOne, listThree); 39 | System.assert(result == false); 40 | } 41 | } -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/problem_solving_patterns/FrequencyCounting_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 12/29/2021. 3 | */ 4 | 5 | @IsTest 6 | private class FrequencyCounting_Tests { 7 | private static final List listOne = new List{ 8 | 1, 9 | 2, 10 | 3, 11 | 4, 12 | 3 13 | }; 14 | private static final List listTwo = new List{ 15 | 1, 16 | 4, 17 | 9, 18 | 9, 19 | 16 20 | }; 21 | private static final List listThree = new List{ 22 | 1, 23 | 4, 24 | 9, 25 | 4, 26 | 16 27 | }; 28 | @IsTest 29 | static void listsShouldMatch() { 30 | Boolean result = FrequencyCounting.testSquaresOne(listOne, listTwo); 31 | System.assert(result == true); 32 | } 33 | 34 | @IsTest 35 | static void listsShouldNotMatch() { 36 | Boolean result = FrequencyCounting.testSquaresOne(listOne, listThree); 37 | System.assert(result == false); 38 | } 39 | 40 | @IsTest 41 | static void testForEqualityofSize() { 42 | List emptyList = new List(); 43 | Boolean result = FrequencyCounting.testSquaresOne(listOne, emptyList); 44 | System.assert(result == false); 45 | } 46 | 47 | @IsTest 48 | static void listsShouldMatchOptimized() { 49 | Boolean result = FrequencyCounting.testSquaresTwo(listOne, listTwo); 50 | System.assert(result == true); 51 | } 52 | 53 | @IsTest 54 | static void listsShouldNotMatchOptimized() { 55 | Boolean result = FrequencyCounting.testSquaresTwo(listOne, listThree); 56 | System.assert(result == false); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/search/LinearSearch.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * @description simple linear or "brute force" search 3 | * This is an O(n) algorithm the time complexity is directly related to the size of the inputs 4 | */ 5 | 6 | public with sharing class LinearSearch { 7 | public static Integer search(List valuesToSearch, Integer value) { 8 | Integer steps = 0; 9 | for (Integer i = 0; i < valuesToSearch.size(); i++) { 10 | steps++; 11 | if (valuesToSearch[i] == value) { 12 | System.debug( 13 | LoggingLevel.INFO, 14 | 'it took ' + 15 | steps + 16 | ' steps to search' 17 | ); 18 | return i; 19 | } 20 | } 21 | System.debug(LoggingLevel.INFO, 'It took ' + steps + ' steps to search'); 22 | return -1; 23 | } 24 | 25 | public static Integer search(List valuesToSearch, String value) { 26 | Integer steps = 0; 27 | for (Integer i = 0; i < valuesToSearch.size(); i++) { 28 | steps++; 29 | if (valuesToSearch[i] == value) { 30 | System.debug( 31 | LoggingLevel.INFO, 32 | 'It took ' + 33 | steps + 34 | ' steps to search' 35 | ); 36 | return i; 37 | } 38 | } 39 | System.debug(LoggingLevel.INFO, 'It took ' + steps + ' steps to search'); 40 | return -1; 41 | } 42 | 43 | public static Integer search(List accounts, String valueToSearch) { 44 | for (Integer i = 0; i < accounts.size(); i++) { 45 | if (accounts[i].Name == valueToSearch) { 46 | return i; 47 | } 48 | } 49 | return -1; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /force-app/main/default/classes/LeetCode/ContainsDuplicates.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * LeetCode problem 217 Contains Duplicates 3 | * Given an integer array nums, 4 | * return true if any value appears at least twice in the array, 5 | * and return false if every element is distinct. 6 | */ 7 | 8 | public with sharing class ContainsDuplicates { 9 | /** 10 | * @description This is the O(n^2) solution to the problem 11 | * @param nums 12 | * 13 | * @return 14 | */ 15 | public static Boolean containsDuplicatesOne(List nums) { 16 | // 1, 2, 3, 1 17 | for (Integer i = 0; i < nums.size(); i++) { 18 | for (Integer j = i + 1; j < nums.size(); j++) { 19 | if (nums[i] == nums[j]) { 20 | return true; 21 | } 22 | } 23 | } 24 | return false; 25 | } 26 | /** 27 | * constant time solution to the problem 28 | * @param nums 29 | * 30 | * @return 31 | */ 32 | public static Boolean containsDuplicates(List nums) { 33 | Set dupes = new Set(); 34 | for (Integer num : nums) { 35 | if (dupes.contains(num)) { 36 | return true; 37 | } 38 | dupes.add(num); 39 | } 40 | return false; 41 | } 42 | 43 | /** 44 | * @description O (n log n) solution to the problem 45 | * based on sorting 46 | * @param nums 47 | * 48 | * @return 49 | */ 50 | public static Boolean containsDuplicatesSort(List nums) { 51 | nums.sort(); 52 | for (Integer i = 0; i < nums.size() - 1; i++) { 53 | if (nums[i] == nums[i + 1]) { 54 | return true; 55 | } 56 | } 57 | return false; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /force-app/main/default/classes/LeetCode/ContainsDuplicates_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 2/20/2022. 3 | */ 4 | 5 | @IsTest 6 | private class ContainsDuplicates_Tests { 7 | private static final List containsDupes = new List{ 8 | 1, 2, 3, 1 9 | }; 10 | private static final List containsNoDupes = new List{ 11 | 1, 2, 3, 4 12 | }; 13 | @IsTest 14 | static void testSolutionOne() { 15 | //when 16 | Boolean result = ContainsDuplicates.containsDuplicatesOne(containsDupes); 17 | System.assertEquals(true, result, 'It should find duplicates'); 18 | // when 19 | result = ContainsDuplicates.containsDuplicatesOne(containsNoDupes); 20 | System.assertEquals(false, result, 'It should not find duplicates'); 21 | } 22 | 23 | @IsTest 24 | static void testSolutionTwo() { 25 | Boolean result = ContainsDuplicates.containsDuplicates(containsDupes); 26 | System.assertEquals(true, result, 'It should find duplicates'); 27 | // when 28 | result = ContainsDuplicates.containsDuplicates(containsNoDupes); 29 | System.assertEquals(false, result, 'It should not find duplicates'); 30 | } 31 | 32 | @IsTest 33 | static void testSolutionThree() { 34 | Boolean result = ContainsDuplicates.containsDuplicatesSort(containsDupes); 35 | System.assertEquals(true, result, 'It should find duplicates'); 36 | // when 37 | result = ContainsDuplicates.containsDuplicatesSort(containsNoDupes); 38 | System.assertEquals(false, result, 'It should not find duplicates'); 39 | } 40 | } -------------------------------------------------------------------------------- /force-app/main/default/classes/ListPerformance_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 2/8/2022. 3 | */ 4 | 5 | @IsTest 6 | private class ListPerformance_Tests { 7 | @IsTest 8 | static void addToStart() { 9 | List integers = new List(); 10 | for (Integer i = 0; i < 10000; i++) { 11 | integers.add(i); 12 | } 13 | Test.startTest(); 14 | Integer startTime = Limits.getCpuTime(); 15 | System.debug('start time: ' + startTime); 16 | integers.add(0, 8675309); 17 | Integer endTime = Limits.getCpuTime(); 18 | System.debug('endTime ' + endTime); 19 | test.stopTest(); 20 | System.debug(endTime - startTime); 21 | } 22 | @IsTest 23 | static void readMiddle() { 24 | List integers = new List(); 25 | for (Integer i = 0; i < 100000; i++) { 26 | integers.add(i); 27 | } 28 | Test.startTest(); 29 | Integer startTime = Limits.getCpuTime(); 30 | System.debug('start time: ' + startTime); 31 | Integer middle = integers[50000]; 32 | Integer endTime = Limits.getCpuTime(); 33 | System.debug('endTime ' + endTime); 34 | test.stopTest(); 35 | System.debug(endTime - startTime); 36 | } 37 | @IsTest 38 | static void addToStartLinkedList() { 39 | SobjectLinkedList linkedList = new SobjectLinkedList(); 40 | for (Integer i = 0; i < 10000; i++) { 41 | Account account = new Account(); 42 | linkedList.push(account); 43 | } 44 | Test.startTest(); 45 | Integer startTime = Limits.getCpuTime(); 46 | System.debug('start time: ' + startTime); 47 | linkedList.push(new Account()); 48 | Integer endTime = Limits.getCpuTime(); 49 | System.debug('endTime ' + endTime); 50 | test.stopTest(); 51 | System.debug(endTime - startTime); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "salesforce-app", 3 | "private": true, 4 | "version": "1.0.0", 5 | "description": "Salesforce App", 6 | "scripts": { 7 | "lint": "npm run lint:lwc && npm run lint:aura", 8 | "lint:aura": "eslint **/aura/**", 9 | "lint:lwc": "eslint **/lwc/**", 10 | "test": "npm run test:unit", 11 | "test:unit": "sfdx-lwc-jest", 12 | "test:unit:watch": "sfdx-lwc-jest --watch", 13 | "test:unit:debug": "sfdx-lwc-jest --debug", 14 | "test:unit:coverage": "sfdx-lwc-jest --coverage", 15 | "prettier": "prettier --write \"**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}\"", 16 | "prettier:verify": "prettier --list-different \"**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}\"", 17 | "postinstall": "husky install", 18 | "precommit": "lint-staged", 19 | "prepare": "husky install" 20 | }, 21 | "devDependencies": { 22 | "@lwc/eslint-plugin-lwc": "^1.0.1", 23 | "@prettier/plugin-xml": "^0.13.1", 24 | "@salesforce/eslint-config-lwc": "^2.0.0", 25 | "@salesforce/eslint-plugin-aura": "^2.0.0", 26 | "@salesforce/eslint-plugin-lightning": "^0.1.1", 27 | "@salesforce/sfdx-lwc-jest": "^0.13.0", 28 | "eslint": "^7.29.0", 29 | "eslint-config-prettier": "^8.3.0", 30 | "eslint-plugin-import": "^2.23.4", 31 | "eslint-plugin-jest": "^24.3.6", 32 | "husky": "^6.0.0", 33 | "jest": "^26.6.3", 34 | "lint-staged": "^11.0.0", 35 | "prettier": "^2.3.1", 36 | "prettier-plugin-apex": "^1.9.1" 37 | }, 38 | "lint-staged": { 39 | "**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}": [ 40 | "prettier --write" 41 | ], 42 | "**/{aura,lwc}/**": [ 43 | "eslint" 44 | ] 45 | }, 46 | "dependencies": { 47 | "g": "^2.0.1" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /force-app/main/default/classes/StringProblems.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 9/10/2022. 3 | */ 4 | 5 | public with sharing class StringProblems { 6 | public static List commonChars(List words) { 7 | Map union = new Map(); 8 | //build or first map from the first word 9 | String firstWord = words[0]; 10 | for (Integer i = 0; i < firstWord.length(); i++) { 11 | if (union.containsKey(firstWord.charAt(i))) { 12 | union.put(firstWord.charAt(i), union.get(firstWord.charAt(i)) + 1); 13 | } else { 14 | union.put(firstWord.charAt(i), 1); 15 | } 16 | } 17 | 18 | // test the rest of the list of words 19 | for (Integer i = 1; i < words.size(); i++) { 20 | String tempWord = words[i]; 21 | Map tempMap = new Map(); 22 | for (Integer j = 0; j < tempWord.length(); j++) { 23 | Integer currentChar = tempWord.charAt(j); 24 | if (union.containsKey(currentChar)) { 25 | if (tempMap.containsKey(currentChar)) { 26 | tempMap.put(currentChar, tempMap.get(currentChar) + 1); 27 | } else { 28 | tempMap.put(currentChar, 1); 29 | } 30 | } 31 | } 32 | //after we have built the temp map 33 | for (Integer key : tempMap.keySet()) { 34 | tempMap.put(key, Math.min(tempMap.get(key), union.get(key))); 35 | } 36 | union = tempMap; 37 | System.debug(union); 38 | } 39 | List charList = new List(); 40 | for (Integer key : union.keySet()) { 41 | while (union.get(key) > 0) { 42 | charList.add(key); 43 | union.put(key, union.get(key) - 1); 44 | } 45 | } 46 | return String.fromCharArray(charList).split(''); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /force-app/main/default/classes/LeetCode/BuySellStock.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 2/19/2022. 3 | */ 4 | 5 | public with sharing class BuySellStock { 6 | public static Integer buySellBruteForce(List stockPrices) { 7 | Integer buyPrice = 0; 8 | Integer sellPrice = 0; 9 | Integer profit = 0; 10 | for (Integer i = 0; i < stockPrices.size(); i++) { 11 | buyPrice = stockPrices[i]; 12 | for (Integer j = i; j < stockPrices.size(); j++) { 13 | sellPrice = stockPrices[j]; 14 | if (sellPrice - buyPrice > profit) { 15 | profit = sellPrice - buyPrice; 16 | } 17 | } 18 | } 19 | return profit; 20 | } 21 | 22 | //7, 1, 5, 3, 6, 4 23 | public static Integer buyStock(List stockPrices) { 24 | //look at the problem as a series of sub problems 25 | Integer buyPrice = stockPrices[0]; 26 | Integer profit = 0; 27 | for (Integer i = 0; i < stockPrices.size() - 1; i++) { 28 | //In the pair of i and i +1 is there a profit to be made? 29 | Integer tempProfit = stockPrices[i + 1] - stockPrices[i]; 30 | System.debug(tempProfit); 31 | if (tempProfit > 0) { 32 | //if there is profit to made by buying and selling at i and i = 1 33 | if (stockPrices[i] < buyPrice) { 34 | //This is a profitable pair. Check if the profitable buy price is lower than the current buy price 35 | // if it is then update the buy price. This gives us the lowest buy in the array that could be profitable. 36 | buyPrice = stockPrices[i]; 37 | } 38 | if (stockPrices[i + 1] - buyPrice > profit) { 39 | //does this sell price provide a greater profit than the previous sell price?? 40 | // update profit to reflect selling on this day. 41 | profit = stockPrices[i + 1] - buyPrice; 42 | } 43 | } 44 | } 45 | return profit; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /force-app/main/default/classes/hashing/HashTable.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 7/19/2022. 3 | */ 4 | 5 | public with sharing class HashTable { 6 | private List bucket; 7 | private Integer slots; 8 | private Integer size; 9 | 10 | public HashTable() { 11 | this.bucket = new HashEntry[10]; 12 | this.slots = 10; 13 | this.size = 0; 14 | } 15 | 16 | public Integer getIndex(Object key) { 17 | Integer hashCode = key.hashCode(); 18 | Integer index = Math.mod(hashCode, slots); 19 | //check for a negative index 20 | if (index < 0) { 21 | index = Math.mod((index + slots), slots); 22 | } 23 | return index; 24 | } 25 | 26 | public void insertTable(Integer key, Object value) { 27 | Integer b_index = getIndex(key); 28 | HashEntry head = bucket.get(b_index); 29 | //check if key exists 30 | while (head != null) { 31 | if (head.key == key) { 32 | head.value = value; 33 | return; 34 | } 35 | head = head.next; 36 | } 37 | //insert key in chain 38 | size++; 39 | head = bucket.get(b_index); 40 | HashEntry newSlot = new HashEntry(key, value); 41 | newSlot.next = head; 42 | bucket.add(b_index, newSlot); 43 | //Checks if array >60% of the array gets filled 44 | if ((1.0 * size) / slots >= 0.6) { 45 | List temp = bucket; 46 | bucket = new List(); 47 | slots = 2 * slots; 48 | size = 0; 49 | for (Integer i = 0; i < slots; i++) 50 | bucket.add(null); 51 | 52 | for (HashEntry head_Node : temp) { 53 | while (head_Node != null) { 54 | insertTable(head_Node.key, head_Node.value); 55 | head_Node = head_Node.next; 56 | } 57 | } 58 | } 59 | } 60 | 61 | public Integer getSize() { 62 | return this.size; 63 | } 64 | 65 | public Boolean isEmpty() { 66 | return this.size == 0; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/problem_solving_patterns/AnagramSolver_Tests.cls: -------------------------------------------------------------------------------- 1 | @IsTest 2 | private class AnagramSolver_Tests { 3 | @IsTest 4 | static void returnFalseOnDifferentLengths() { 5 | //given 6 | String name1 = 'Brooks'; 7 | String name2 = 'brook'; 8 | //when 9 | Boolean result = AnagramSolver.isAnagram(name1, name2); 10 | //then 11 | System.assert(result == false); 12 | } 13 | 14 | @IsTest 15 | static void convertStringToList() { 16 | //given 17 | String testString = 'Tom Marvolo Riddle'; 18 | //when 19 | List convertedString = AnagramSolver.convertString(testString); 20 | //then 21 | System.assert(convertedString.size() == 16); 22 | } 23 | 24 | @IsTest 25 | static void countFrequency() { 26 | //given 27 | String name = 'Brooks'; 28 | //when 29 | Map frequencyCountMap = AnagramSolver.countFrequency( 30 | AnagramSolver.convertString(name) 31 | ); 32 | //then 33 | System.assert(frequencyCountMap.get('o') == 2); 34 | } 35 | 36 | @IsTest 37 | static void returnTrueOnAnagram() { 38 | //given 39 | String name1 = 'Tom Marvolo Riddle'; 40 | String name2 = 'I am Lord Voldemort'; 41 | //when 42 | Boolean result = AnagramSolver.isAnagram(name1, name2); 43 | //then 44 | System.assert(result == true); 45 | } 46 | 47 | @IsTest 48 | static void returnFalseNotAnagram() { 49 | //given 50 | String name1 = 'dog'; 51 | String name2 = 'cat'; 52 | //when 53 | Boolean result = AnagramSolver.isAnagram(name1, name2); 54 | //then 55 | System.assert(result == false); 56 | } 57 | 58 | @IsTest 59 | static void returnFalseUnequalLength() { 60 | //given 61 | String name1 = 'Hellooo'; 62 | String name2 = 'Helloo'; 63 | //when 64 | Boolean result = AnagramSolver.isAnagram(name1, name2); 65 | //then 66 | System.assert(result == false); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /force-app/main/default/classes/LeetCode/ValidPalindrome_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 2/27/2022. 3 | */ 4 | 5 | @IsTest 6 | private class ValidPalindrome_Tests { 7 | @IsTest 8 | static void isPalindrome() { 9 | //given 10 | String testString = 'A man, a plan, a canal: Panama'; 11 | //when 12 | Boolean result = ValidPalindrome.isPalindrome(testString); 13 | //then 14 | System.assertEquals(true, result, 'It is a palindrome'); 15 | } 16 | 17 | @IsTest 18 | static void isPalindrome2() { 19 | //given 20 | String testString = 'A Toyota. Race fast, safe car. A Toyota'; 21 | //when 22 | Boolean result = ValidPalindrome.isPalindrome(testString); 23 | //then 24 | System.assertEquals(true, result, 'It is a palindrome'); 25 | } 26 | @IsTest 27 | static void isPalindrome3() { 28 | //given 29 | String testString = 'A lot not new I saw as I went on to L.A'; 30 | //when 31 | Boolean result = ValidPalindrome.isPalindrome(testString); 32 | //then 33 | System.assertEquals(true, result, 'It is a palindrome'); 34 | } 35 | @IsTest 36 | static void isPalindromeNegative() { 37 | //given 38 | String testString = 'race a car'; 39 | //when 40 | Boolean result = ValidPalindrome.isPalindrome(testString); 41 | //then 42 | System.assertEquals(false, result, 'It is not a palindrome'); 43 | } 44 | 45 | @IsTest 46 | static void isPalindromeRecurse() { 47 | //given 48 | String testString = 'A man, a plan, a canal: Panama'; 49 | //when 50 | Boolean result = ValidPalindrome.isPalindromeRecurse(testString); 51 | //then 52 | System.assertEquals(true, result, 'It is a palindrome'); 53 | } 54 | 55 | @IsTest 56 | static void isPalindromeNegativeRecurse() { 57 | //given 58 | String testString = 'race a car'; 59 | //when 60 | Boolean result = ValidPalindrome.isPalindromeRecurse(testString); 61 | //then 62 | System.assertEquals(false, result, 'It is not a palindrome'); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /force-app/main/default/classes/problem_solving_patterns/SlidingWindow.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 3/3/2022. 3 | */ 4 | 5 | public with sharing class SlidingWindow { 6 | /** 7 | * @description This is the naive solution and will run at O(n^2) time. 8 | * @param nums a list of integers order does not matter 9 | * @param target: The size of the sub array 10 | * 11 | * @return value of the max sub array 12 | */ 13 | public static Integer bfMaxSubArraySum(List nums, Integer target) { 14 | if (target > nums.size()) { 15 | return null; 16 | } 17 | //could have an issue with negative numbers and would want to get the 18 | //absolute value instead 19 | Integer max = 0; 20 | Integer length = nums.size() - target + 1; 21 | for (Integer i = 0; i < length; i++) { 22 | Integer temp = 0; 23 | for (Integer j = 0; j < target; j++) { 24 | temp += nums[i + j]; 25 | } 26 | if (temp > max) { 27 | max = temp; 28 | } 29 | } 30 | return max; 31 | } 32 | /** 33 | * @description This is the O(n) solution. Using the Sliding window technique 34 | * @param nums 35 | * @param target 36 | * 37 | * @return 38 | */ 39 | public static Integer swMaxSubArraySum(List nums, Integer target) { 40 | Integer maxSum = 0; 41 | Integer tempSum = 0; 42 | if (nums.size() < target) { 43 | return null; 44 | } 45 | //We are going to add the first n numbers together as defined in the target argument 46 | for (Integer i = 0; i < target; i++) { 47 | maxSum += nums[i]; 48 | } 49 | tempSum = maxSum; 50 | //We already have the sum of the first window. 51 | //Now we can just increment by i and compare the sliding window value 52 | // by subtracting the last value and adding the value at i; 53 | for (Integer i = target; i < nums.size(); i++) { 54 | tempSum = tempSum - nums[i - target] + nums[i]; 55 | maxSum = Math.max(maxSum, tempSum); 56 | } 57 | return maxSum; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /force-app/main/default/classes/search/BinarySearch_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/14/2022. 3 | */ 4 | 5 | @IsTest 6 | private class BinarySearch_Tests { 7 | public static final List INTEGER_LIST = new List{ 8 | 1, 9 | 97, 10 | 14, 11 | 362, 12 | 10, 13 | 278, 14 | 45, 15 | 66, 16 | 77, 17 | 235, 18 | 99, 19 | 586 20 | }; 21 | 22 | @IsTest 23 | static void itShouldReturnAValueIterative() { 24 | //given 25 | INTEGER_LIST.sort(); 26 | // when 27 | Integer result = BinarySearch.binarySearchIterative(INTEGER_LIST, 586); 28 | System.assertEquals( 29 | 11, 30 | result, 31 | 'It should have returned the correct index' 32 | ); 33 | } 34 | @IsTest 35 | static void itShouldReturnNegOneIterative() { 36 | //given 37 | INTEGER_LIST.sort(); 38 | // when 39 | Integer result = BinarySearch.binarySearchIterative(INTEGER_LIST, 12345); 40 | System.assertEquals( 41 | -1, 42 | result, 43 | 'It should have returned the correct index' 44 | ); 45 | } 46 | @IsTest 47 | static void itShouldReturnValueRecursive() { 48 | //given 49 | List listToSearch = ListUtils.generateList(1000); 50 | listToSearch.add(976); 51 | listToSearch.sort(); 52 | //when 53 | Integer result = BinarySearch.binarySearchRecursive( 54 | listToSearch, 55 | 976, 56 | 0, 57 | listToSearch.size() - 1 58 | ); 59 | System.assertNotEquals(-1, result, 'It should not return -1'); 60 | } 61 | 62 | @IsTest 63 | static void itShouldReturnNegOneRecursive() { 64 | //given 65 | List listToSearch = new List{ 66 | 1, 67 | 2, 68 | 3, 69 | 4, 70 | 5, 71 | 6, 72 | 7, 73 | 8, 74 | 9, 75 | 10 76 | }; 77 | //when 78 | Integer result = BinarySearch.binarySearchRecursive( 79 | listToSearch, 80 | 100, 81 | 0, 82 | 9 83 | ); 84 | System.assertEquals(-1, result, 'It should return negative 1'); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/search/BinarySearch_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/14/2022. 3 | */ 4 | 5 | @IsTest 6 | private class BinarySearch_Tests { 7 | public static final List INTEGER_LIST = new List{ 8 | 1, 9 | 97, 10 | 14, 11 | 362, 12 | 10, 13 | 278, 14 | 45, 15 | 66, 16 | 77, 17 | 235, 18 | 99, 19 | 586 20 | }; 21 | 22 | @IsTest 23 | static void itShouldReturnAValueInterative() { 24 | //given 25 | INTEGER_LIST.sort(); 26 | // when 27 | Integer result = BinarySearch.binarySearchIterative(INTEGER_LIST, 586); 28 | System.assertEquals( 29 | 11, 30 | result, 31 | 'It should have returned the correct index' 32 | ); 33 | } 34 | @IsTest 35 | static void itShouldReturnNegOneIterative() { 36 | //given 37 | INTEGER_LIST.sort(); 38 | // when 39 | Integer result = BinarySearch.binarySearchIterative(INTEGER_LIST, 12345); 40 | System.assertEquals( 41 | -1, 42 | result, 43 | 'It should have returned the correct index' 44 | ); 45 | } 46 | @IsTest 47 | static void itShouldReturnValueRecursive() { 48 | //given 49 | List listToSearch = ListUtils.generateList(1000); 50 | listToSearch.add(976); 51 | listToSearch.sort(); 52 | //when 53 | Integer result = BinarySearch.binarySearchRecursive( 54 | listToSearch, 55 | 976, 56 | 0, 57 | listToSearch.size() - 1 58 | ); 59 | System.assertNotEquals(-1, result, 'It should not return -1'); 60 | } 61 | 62 | @IsTest 63 | static void itShouldReturnNegOneRecursive() { 64 | //given 65 | List listToSearch = new List{ 66 | 1, 67 | 2, 68 | 3, 69 | 4, 70 | 5, 71 | 6, 72 | 7, 73 | 8, 74 | 9, 75 | 10 76 | }; 77 | //when 78 | Integer result = BinarySearch.binarySearchRecursive( 79 | listToSearch, 80 | 100, 81 | 0, 82 | 9 83 | ); 84 | System.assertEquals(-1, result, 'It should return negative 1'); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/search/LinearSearch_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/14/2022. 3 | */ 4 | 5 | @IsTest 6 | public with sharing class LinearSearch_Tests { 7 | public static final List INTEGER_LIST = new List{ 8 | 1, 9 | 97, 10 | 14, 11 | 362, 12 | 10, 13 | 278, 14 | 45, 15 | 66, 16 | 77, 17 | 235, 18 | 99, 19 | 586 20 | }; 21 | public static final List ALPHABET = new List{ 22 | 'a', 23 | 'b', 24 | 'c', 25 | 'd', 26 | 'e', 27 | 'f', 28 | 'g', 29 | 'h', 30 | 'i', 31 | 'j', 32 | 'k', 33 | 'l', 34 | 'n', 35 | 'o', 36 | 'p', 37 | 'q', 38 | 'r', 39 | 's', 40 | 't', 41 | 'u', 42 | 'v', 43 | 'w', 44 | 'x', 45 | 'y', 46 | 'z' 47 | }; 48 | 49 | @IsTest 50 | static void itShouldFindIntegerValue() { 51 | //given 52 | //when 53 | Integer index = LinearSearch.search(INTEGER_LIST, 586); 54 | //then 55 | System.assert(index == 11, 'It should return the correct index'); 56 | } 57 | 58 | @IsTest 59 | static void itShouldFindCorrectIndexInAlphabet() { 60 | //given 61 | //when 62 | Integer searchIndex = LinearSearch.search(ALPHABET, 'a'); 63 | //then 64 | System.assertEquals(0, searchIndex, 'It should return the correct index'); 65 | } 66 | 67 | @IsTest 68 | static void itShouldReturnNegOne() { 69 | //given 70 | //when 71 | Integer searchIndex = LinearSearch.search(ALPHABET, 'brooks'); 72 | System.assertEquals(searchIndex, -1, 'It should return negative one'); 73 | } 74 | @IsTest 75 | static void itShouldFindCorrectAccount() { 76 | // given 77 | Account first = new Account(Name = 'Picard'); 78 | Account second = new Account(Name = 'Kirk'); 79 | Account third = new Account(Name = 'Pike'); 80 | Account fourth = new Account(Name = 'Archer'); 81 | List accounts = new List{ first, second, third, fourth }; 82 | //when 83 | Integer index = LinearSearch.search(accounts, 'Pike'); 84 | //then 85 | System.assertEquals(2, index, 'It should return the correct index'); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/problem_solving_patterns/FrequencyCounting.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 12/29/2021. 3 | */ 4 | 5 | public with sharing class FrequencyCounting { 6 | public static Boolean testSquaresOne( 7 | List listOne, 8 | List listTwo 9 | ) { 10 | Boolean result = false; 11 | if (listOne.size() != listTwo.size()) { 12 | return false; 13 | } 14 | for (Integer i = 0; i < listOne.size(); i++) { 15 | Integer squaredIndex = listTwo.indexOf( 16 | Integer.valueOf(Math.pow(listOne[i], 2)) 17 | ); 18 | if (squaredIndex == -1) { 19 | return false; 20 | } else { 21 | listTwo.remove(squaredIndex); 22 | System.debug('removing' + listTwo); 23 | } 24 | } 25 | return true; 26 | } 27 | 28 | public static Boolean testSquaresTwo( 29 | List listOne, 30 | List listTwo 31 | ) { 32 | Boolean result = false; 33 | Map countOne = new Map(); 34 | Map countTwo = new Map(); 35 | 36 | Integer listOneSize = listOne.size(); 37 | Integer listTwoSize = listTwo.size(); 38 | for (Integer i = 0; i < listOneSize; i++) { 39 | if (countOne.containsKey(listOne[i])) { 40 | countOne.put(listOne[i], countOne.get(listOne[i]) + 1); 41 | } else { 42 | countOne.put(listOne[i], 1); 43 | } 44 | } 45 | for (Integer i = 0; i < listTwoSize; i++) { 46 | if (countTwo.containsKey(listTwo[i])) { 47 | countTwo.put(listTwo[i], countTwo.get(listTwo[i]) + 1); 48 | } else { 49 | countTwo.put(listTwo[i], 1); 50 | } 51 | } 52 | //Look for matches 53 | for (Integer i : countOne.keySet()) { 54 | Integer keySquared = Integer.valueOf(Math.pow(i, 2)); 55 | Integer countOneValue = countOne.get(i); 56 | if ( 57 | countTwo.containsKey(keySquared) && 58 | countTwo.get(keySquared) == countOneValue 59 | ) { 60 | result = true; 61 | } else { 62 | return false; 63 | } 64 | } 65 | System.debug('countOne' + countOne); 66 | System.debug('count two' + countTwo); 67 | return result; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /force-app/main/default/classes/problem_solving_patterns/FrequencyCounting.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 12/29/2021. 3 | */ 4 | 5 | public with sharing class FrequencyCounting { 6 | public static Boolean testSquaresOne(List listOne, List listTwo) { 7 | Boolean result = false; 8 | if (listOne.size() != listTwo.size()) { 9 | return false; 10 | } 11 | for (Integer i = 0; i < listOne.size(); i++) { 12 | Integer squaredIndex = listTwo.indexOf(Integer.valueOf(Math.pow(listOne[i], 2))); 13 | if (squaredIndex == -1) { 14 | return false; 15 | } else { 16 | listTwo.remove(squaredIndex); 17 | System.debug('removing' + listTwo ); 18 | 19 | } 20 | } 21 | return true; 22 | } 23 | 24 | public static Boolean testSquaresTwo(List listOne, List listTwo) { 25 | Boolean result = false; 26 | Map countOne = new Map(); 27 | Map countTwo = new Map(); 28 | 29 | Integer listOneSize = listOne.size(); 30 | Integer listTwoSize = listTwo.size(); 31 | for (Integer i = 0; i < listOneSize; i++) { 32 | if (countOne.containsKey(listOne[i])) { 33 | countOne.put(listOne[i], countOne.get(listOne[i]) +1); 34 | }else { 35 | countOne.put(listOne[i], 1); 36 | } 37 | } 38 | for (Integer i = 0; i < listTwoSize; i ++){ 39 | if (countTwo.containsKey(listTwo[i])) { 40 | countTwo.put(listTwo[i], countTwo.get(listTwo[i]) +1); 41 | } 42 | else { 43 | countTwo.put(listTwo[i], 1); 44 | } 45 | 46 | } 47 | //Look for matches 48 | for (Integer i : countOne.keySet()) { 49 | Integer keySquared = Integer.valueOf(Math.pow(i, 2)); 50 | Integer countOneValue = countOne.get(i); 51 | if (countTwo.containsKey(keySquared) && countTwo.get(keySquared) == countOneValue){ 52 | result = true; 53 | }else { 54 | return false; 55 | } 56 | } 57 | System.debug('countOne' + countOne); 58 | System.debug('count two' + countTwo); 59 | return result; 60 | } 61 | 62 | } -------------------------------------------------------------------------------- /force-app/main/default/classes/search/BinarySearch.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/14/2022. 3 | */ 4 | 5 | public with sharing class BinarySearch { 6 | public static Integer binarySearchIterative( 7 | List listToSearch, 8 | Integer target 9 | ) { 10 | // Steps is only here to show the performance of binary search. It is not required for the algo. 11 | Integer steps = 0; 12 | Integer left = 0; 13 | Integer right = listToSearch.size(); 14 | while (left < right) { 15 | steps++; 16 | // find the middle value 17 | Integer middle = (left + right) / 2; 18 | // if the target value is equal to the middle index value we return the index 19 | if (target == listToSearch[middle]) { 20 | System.debug( 21 | LoggingLevel.INFO, 22 | 'It took ' + 23 | steps + 24 | ' steps to search' 25 | ); 26 | return middle; 27 | } 28 | // If the target value is less than the middle 29 | // We throw out halve the problem and reset our right hand side index 30 | if (target < listToSearch[middle]) { 31 | System.debug( 32 | LoggingLevel.INFO, 33 | 'Discarding the left hand side of the problem' 34 | ); 35 | right = middle - 1; 36 | System.debug(LoggingLevel.INFO, 'The new left index is ' + left); 37 | } else { 38 | // Discarding the left hand side of the problem 39 | left = middle + 1; 40 | } 41 | } 42 | //value was not found 43 | System.debug(LoggingLevel.INFO, 'It took ' + steps + ' steps to search'); 44 | return -1; 45 | } 46 | @SuppressWarnings('PMD.ExcessiveParameterList') 47 | public static Integer binarySearchRecursive( 48 | List listToSearch, 49 | Integer target, 50 | Integer left, 51 | Integer right 52 | ) { 53 | // Base Case 54 | if (left > right) { 55 | return -1; 56 | } 57 | // Get the middle index 58 | Integer middle = (left + right) / 2; 59 | // Base case a match is found 60 | if (target == listToSearch[middle]) { 61 | return middle; 62 | } else if (target < listToSearch[middle]) { 63 | // Discarding the right hand side of the problem 64 | return binarySearchRecursive(listToSearch, target, left, middle - 1); 65 | } else { 66 | return binarySearchRecursive(listToSearch, target, middle + 1, right); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /force-app/main/default/classes/problem_solving_patterns/AnagramSolver_Tests.cls: -------------------------------------------------------------------------------- 1 | @IsTest 2 | private class AnagramSolver_Tests { 3 | @IsTest 4 | static void returnFalseOnDifferentLengths() { 5 | //given 6 | String name1 = 'Brooks'; 7 | String name2 = 'brook'; 8 | //when 9 | Boolean result = AnagramSolver.isAnagram(name1, name2); 10 | //then 11 | System.assert(result == false); 12 | } 13 | 14 | @IsTest 15 | static void convertStringToList() { 16 | //given 17 | String testString = 'Tom Marvolo Riddle'; 18 | //when 19 | List convertedString = AnagramSolver.convertString(testString); 20 | //then 21 | System.assert(convertedString.size() == 16); 22 | } 23 | 24 | @IsTest 25 | static void countFrequency() { 26 | //given 27 | String name = 'Brooks'; 28 | //when 29 | Map frequencyCountMap = AnagramSolver.countFrequency(AnagramSolver.convertString(name)); 30 | //then 31 | System.assert(frequencyCountMap.get('o') == 2); 32 | } 33 | 34 | @IsTest 35 | static void returnTrueOnAnagram() { 36 | //given 37 | String name1 = 'Tom Marvolo Riddle'; 38 | String name2 = 'I am Lord Voldemort'; 39 | //when 40 | Boolean result = AnagramSolver.isAnagram(name1, name2); 41 | //then 42 | System.assert(result == true); 43 | } 44 | 45 | @IsTest 46 | static void returnTrueOnAnagram2() { 47 | //given 48 | String name1 = 'Tom Marvolo Riddle'; 49 | String name2 = 'I am Lord Voldemort'; 50 | //when 51 | Boolean result = AnagramSolver.isAnagram2(name1, name2); 52 | //then 53 | System.assert(result == true); 54 | } 55 | 56 | @IsTest 57 | static void returnFalseNotAnagram() { 58 | //given 59 | String name1 = 'dog'; 60 | String name2 = 'cat'; 61 | //when 62 | Boolean result = AnagramSolver.isAnagram(name1, name2); 63 | //then 64 | System.assert(result == false); 65 | } 66 | 67 | @IsTest 68 | static void returnFalseUnequalLength() { 69 | //given 70 | String name1 = 'Hellooo'; 71 | String name2 = 'Helloo'; 72 | //when 73 | Boolean result = AnagramSolver.isAnagram(name1, name2); 74 | //then 75 | System.assert(result == false); 76 | } 77 | } -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/problem_solving_patterns/AnagramSolver.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/2/2022. 3 | */ 4 | 5 | public with sharing class AnagramSolver { 6 | /** 7 | * @param stringOne string to compare 8 | * @param stringTwo string to compare 9 | * 10 | * @return true if the strings are anagram of each other 11 | */ 12 | public static Boolean isAnagram(String stringOne, String stringTwo) { 13 | //convert stringsToLists - you could do this in the for loop but I think this is more readable and testable 14 | List stringOneList = convertString(stringOne); 15 | List stringTwoList = convertString(stringTwo); 16 | //check the count and return if strings are not the same length 17 | if (stringOneList.size() != stringTwoList.size()) { 18 | return false; 19 | } 20 | //make a maps to count the frequencies 21 | Map mapOne = countFrequency(stringOneList); 22 | Map mapTwo = countFrequency(stringTwoList); 23 | 24 | // Iterate over the maps and determine if it is an anagram 25 | for (String character : mapOne.keySet()) { 26 | Integer countFrequency = mapOne.get(character); 27 | if ( 28 | !(mapTwo.containsKey(character)) || 29 | (mapTwo.get(character) != countFrequency) 30 | ) { 31 | return false; 32 | } 33 | } 34 | return true; 35 | } 36 | 37 | /** 38 | * @param stringToConvert string to be converted to a list, 39 | * in lowercase and white space removed, 40 | * 41 | * @return list containing all characters in the string 42 | */ 43 | public static List convertString(String stringToConvert) { 44 | return stringToConvert.toLowerCase().deleteWhitespace().split(''); 45 | } 46 | 47 | /** 48 | * @param stringList a string that has been converted to a list 49 | * 50 | * @return a map with key value pairs of the characters in the string and their count 51 | */ 52 | public static Map countFrequency(List stringList) { 53 | Map frequencyCountMap = new Map(); 54 | for (String character : stringList) { 55 | if (frequencyCountMap.containsKey(character)) { 56 | frequencyCountMap.put(character, frequencyCountMap.get(character) + 1); 57 | } else { 58 | frequencyCountMap.put(character, 1); 59 | } 60 | } 61 | return frequencyCountMap; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /force-app/main/default/classes/stack/StackDemo.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by brooks.johnson on 4/21/2022. 3 | */ 4 | 5 | public with sharing class StackDemo { 6 | public static final String OPENING = '({['; 7 | public static final String CLOSING = ')}]'; 8 | 9 | public static Boolean isMatched(String expression) { 10 | //We use a String as lifo data structure 11 | List stack = new List(); 12 | List stringArray = expression.split(''); 13 | for (String str : stringArray) { 14 | if (OPENING.indexOf(str) != -1) { 15 | stack.add(str); 16 | } else if (CLOSING.indexOf(str) != -1) { 17 | if (stack.isEmpty()) { 18 | //nothing to match with 19 | return false; 20 | } 21 | //the next closing delimiter should match the opening delimiter at the top of the stack 22 | if ( 23 | CLOSING.indexOf(str) != 24 | OPENING.indexOf((String) stack.remove(stack.size() - 1)) 25 | ) { 26 | return false; 27 | } 28 | } 29 | } 30 | return stack.isEmpty(); 31 | } 32 | 33 | public Boolean companionPlants(List plants) { 34 | if (plants.size() == 1) { 35 | return false; 36 | } 37 | if (plants.size() == 0) { 38 | return true; 39 | } 40 | Map> companionPlants = new Map>(); 41 | companionPlants.put('lettuce', new Set{ 'cucumbers, onions' }); 42 | companionPlants.put('cucumbers', new Set{ 'lettuce' }); 43 | companionPlants.put( 44 | 'onions', 45 | new Set{ 'lettuce, carrots, tomatoes' } 46 | ); 47 | companionPlants.put('carrots', new Set{ 'onions' }); 48 | companionPlants.put('tomatoes', new Set{ 'onions' }); 49 | for (Integer i = 0; i < plants.size() - 1; i++) { 50 | Set companions = companionPlants.get(plants[i]); 51 | if (!companions.contains(plants[i + 1])) { 52 | return false; 53 | } 54 | } 55 | return true; 56 | } 57 | public Boolean companionPlans2(String plant1, String plant2) { 58 | Map> companionPlants = new Map>(); 59 | companionPlants.put('lettuce', new Set{ 'cucumbers', 'onions' }); 60 | companionPlants.put('cucumbers', new Set{ 'lettuce' }); 61 | companionPlants.put( 62 | 'onions', 63 | new Set{ 'lettuce', 'carrots', 'tomatoes' } 64 | ); 65 | companionPlants.put('carrots', new Set{ 'onions' }); 66 | companionPlants.put('tomatoes', new Set{ 'onions' }); 67 | 68 | return companionPlants.get(plant1).contains(plant2); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /force-app/main/default/classes/trees/OpportunityBST_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 2/21/2022. 3 | */ 4 | 5 | @IsTest 6 | private class OpportunityBST_Tests { 7 | @IsTest 8 | static void insertWithNoRoot() { 9 | //given 10 | Opportunity opp = new Opportunity(Amount = 1000); 11 | OpportunityBST bst = new OpportunityBST(); 12 | //when 13 | bst.push(opp); 14 | //then 15 | System.assertEquals(opp, bst.root.value, 'It should insert at root'); 16 | } 17 | 18 | @IsTest 19 | static void pushNodes() { 20 | //given 21 | Opportunity opp = new Opportunity(Amount = 1000); 22 | OpportunityBST bst = new OpportunityBST(); 23 | bst.push(opp); 24 | Opportunity opp1 = new Opportunity(Amount = 1100); 25 | Opportunity opp2 = new Opportunity(Amount = 900); 26 | Opportunity opp3 = new Opportunity(Amount = 800); 27 | //when 28 | bst.push(opp1); 29 | bst.push(opp2); 30 | bst.push(opp3); 31 | Opportunity result = bst.search(800.00); 32 | 33 | //then 34 | System.assertEquals( 35 | 1100, 36 | bst.root.right.value.Amount, 37 | 'It should add the right node of the root' 38 | ); 39 | System.assertEquals( 40 | 900, 41 | bst.root.left.value.Amount, 42 | 'It should add the left node of the root' 43 | ); 44 | System.assertEquals( 45 | 800, 46 | bst.root.left.left.value.Amount, 47 | 'It should add the left node of the root' 48 | ); 49 | } 50 | 51 | @isTest 52 | static void itShouldSearch() { 53 | //given 54 | Opportunity opp = new Opportunity(Amount = 1000); 55 | OpportunityBST bst = new OpportunityBST(); 56 | bst.push(opp); 57 | Opportunity opp1 = new Opportunity(Amount = 1100); 58 | Opportunity opp2 = new Opportunity(Amount = 900); 59 | Opportunity opp3 = new Opportunity(Amount = 800); 60 | //when 61 | bst.push(opp1); 62 | bst.push(opp2); 63 | bst.push(opp3); 64 | Opportunity result = bst.search(800.00); 65 | System.assertEquals( 66 | 800, 67 | result.Amount, 68 | 'It should return the correct node' 69 | ); 70 | } 71 | @isTest 72 | static void testBreadthFirstSearch() { 73 | //given 74 | Opportunity opp = new Opportunity(Amount = 1000); 75 | OpportunityBST bst = new OpportunityBST(); 76 | bst.push(opp); 77 | Opportunity opp1 = new Opportunity(Amount = 1100); 78 | Opportunity opp2 = new Opportunity(Amount = 900); 79 | Opportunity opp3 = new Opportunity(Amount = 800); 80 | bst.push(opp1); 81 | bst.push(opp2); 82 | bst.push(opp3); 83 | //when 84 | List bfsresults = bst.breadthFirstSearch(); 85 | System.assertEquals(4, bfsresults.size(), 'It should return all nodes'); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /force-app/main/default/classes/linked-list/SinglyLinkedList.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 4/14/2022. 3 | */ 4 | 5 | public with sharing class SinglyLinkedList { 6 | public class SinglyLinkedListException extends Exception { 7 | } 8 | 9 | public Node head { get; private set; } 10 | public Node tail { get; private set; } 11 | public Integer size { get; private set; } 12 | 13 | public SinglyLinkedList() { 14 | this.head = null; 15 | this.tail = null; 16 | this.size = 0; 17 | } 18 | 19 | /** 20 | * @description This is a constant time operation 21 | * @param data Object to used in Node 22 | */ 23 | public void addToFront(Object data) { 24 | Node newNode = new Node(data); 25 | if (this.head == null) { 26 | this.head = newNode; 27 | this.tail = this.head; 28 | } else { 29 | //do these in order!! 30 | newNode.next = this.head; 31 | this.head = newNode; 32 | } 33 | this.size++; 34 | } 35 | 36 | /** 37 | * @description This is a O(1) operation because we have a tail property. 38 | * If there was no tail this would be O(n) 39 | * @param data Object to be used in Node 40 | */ 41 | public void addToBack(Object data) { 42 | Node newNode = new Node(data); 43 | if (this.head == null) { 44 | this.head = newNode; 45 | this.tail = this.head; 46 | } else { 47 | this.tail.next = newNode; 48 | this.tail = newNode; 49 | } 50 | this.size++; 51 | } 52 | 53 | /** 54 | * @description This is a O(1) operation. The head pointer is updated and the old head 55 | * is garbage collected 56 | * @return data value from head Node 57 | */ 58 | public Object removeFromFront() { 59 | if (this.size == 0) { 60 | throw new SinglyLinkedListException('The list is empty'); 61 | } 62 | Node remove = this.head; 63 | this.head = this.head.next; 64 | this.size--; 65 | return remove.data; 66 | } 67 | 68 | /** 69 | * @description This is a O(n) operation. we need to now the value of the node that is previous to the 70 | * current tail. We have to iterate over the entire list. 71 | * @return Data value from tail Node 72 | */ 73 | public Object removeFromBack() { 74 | if (this.size == 0) { 75 | throw new SinglyLinkedListException('The list is empty'); 76 | } 77 | if (this.size == 1) { 78 | Node temp = this.head; 79 | this.head = null; 80 | this.size--; 81 | return temp.data; 82 | } 83 | Node current = this.head; 84 | Node newTail = null; 85 | while (current.next != null) { 86 | newTail = current; 87 | current = current.next; 88 | } 89 | this.tail = newTail; 90 | tail.next = null; 91 | this.size--; 92 | return current.data; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /force-app/main/default/classes/recursion/RecursionExamples_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/6/2022. 3 | */ 4 | 5 | @IsTest 6 | private class RecursionExamples_Tests { 7 | @IsTest 8 | static void itShouldReturnReversedString() { 9 | //given 10 | String test = 'hello'; 11 | //when 12 | String results = RecursionExamples.stringReversalIterative(test); 13 | //then 14 | System.assert(results == 'olleh', 'The string should be reversed'); 15 | } 16 | 17 | @IsTest 18 | static void itShouldReturnReversedRecursive() { 19 | //given 20 | String test = 'hello'; 21 | //when 22 | RecursionExamples.stringReversalRecursive(test); 23 | String results = RecursionExamples.stringReversalRecursive(test); 24 | //then 25 | System.assert(results == 'olleh', 'The string should be reversed'); 26 | System.assert(results == test.reverse(), 'The string should be reversed'); 27 | } 28 | 29 | @IsTest 30 | static void itShouldReturnIterativeFactorial() { 31 | //given 32 | //when 33 | Integer result = RecursionExamples.factorialIterative(5); 34 | //then 35 | System.assert(result == 120, 'It should compute factorial iteratively'); 36 | } 37 | 38 | @IsTest 39 | static void itShouldReturnRecursiveFactorial() { 40 | //given 41 | //when 42 | Integer result = RecursionExamples.factorialRecursive(5); 43 | //then 44 | System.assert(result == 120, 'It should compute factorial recursively'); 45 | } 46 | 47 | @IsTest 48 | static void itShouldCalculatePower() { 49 | //given 50 | //when 51 | Integer result = RecursionExamples.power(10, 3); 52 | //then 53 | System.assert(result == 1000, 'It should compute power of 10 ^ 2'); 54 | } 55 | 56 | @IsTest 57 | static void itShouldCalculateRecursiveRange() { 58 | //given 59 | //when 60 | Integer result = RecursionExamples.range(6); 61 | //then 62 | System.assert(result == 21, 'It should compute product of list'); 63 | } 64 | 65 | @IsTest 66 | static void itShouldReturnFibonacci() { 67 | //given 68 | //when 69 | Integer result = RecursionExamples.fibonacci(5); 70 | System.assert(result == 5, 'It should compute fibonacci'); 71 | } 72 | 73 | @IsTest 74 | static void isShouldReturnTrueForPalindrome() { 75 | //given 76 | String work = 'madam'; 77 | //when 78 | Boolean result = RecursionExamples.isPalindrome(work); 79 | //then 80 | System.assert(result == true, 'It should return true for anagram'); 81 | } 82 | @IsTest 83 | static void itShouldReturnFalseForPalindrome() { 84 | //given 85 | String word = 'test'; 86 | //when 87 | Boolean result = RecursionExamples.isPalindrome(word); 88 | //then 89 | System.assert( 90 | result == false, 91 | 'It should not return true for a palindrome' 92 | ); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/recursion/RecursionExamples_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/6/2022. 3 | */ 4 | 5 | @IsTest 6 | private class RecursionExamples_Tests { 7 | @IsTest 8 | static void itShouldReturnReversedString() { 9 | //given 10 | String test = 'hello'; 11 | //when 12 | String results = RecursionExamples.stringReversalIterative(test); 13 | //then 14 | System.assert(results == 'olleh', 'The string should be reversed'); 15 | } 16 | 17 | @IsTest 18 | static void itShouldReturnReversedRecursive() { 19 | //given 20 | String test = 'hello'; 21 | //when 22 | RecursionExamples.stringReversalRecursive(test); 23 | String results = RecursionExamples.stringReversalRecursive(test); 24 | //then 25 | System.assert(results == 'olleh', 'The string should be reversed'); 26 | System.assert(results == test.reverse(), 'The string should be reversed'); 27 | } 28 | 29 | @IsTest 30 | static void itShouldReturnIterativeFactorial() { 31 | //given 32 | //when 33 | Integer result = RecursionExamples.factorialIterative(5); 34 | //then 35 | System.assert(result == 120, 'It should compute factorial iteratively'); 36 | } 37 | 38 | @IsTest 39 | static void itShouldReturnRecursiveFactorial() { 40 | //given 41 | //when 42 | Integer result = RecursionExamples.factorialRecursive(5); 43 | //then 44 | System.assert(result == 120, 'It should compute factorial recursively'); 45 | } 46 | 47 | @IsTest 48 | static void itShouldCalculatePower() { 49 | //given 50 | //when 51 | Integer result = RecursionExamples.power(10, 3); 52 | //then 53 | System.assert(result == 1000, 'It should compute power of 10 ^ 2'); 54 | } 55 | 56 | @IsTest 57 | static void itShouldCalculateRecursiveRange() { 58 | //given 59 | //when 60 | Integer result = RecursionExamples.range(6); 61 | //then 62 | System.assert(result == 21, 'It should compute product of list'); 63 | } 64 | 65 | @IsTest 66 | static void itShouldReturnFibonacci() { 67 | //given 68 | //when 69 | Integer result = RecursionExamples.fibonacci(5); 70 | System.assert(result == 5, 'It should compute fibonacci'); 71 | } 72 | 73 | @IsTest 74 | static void isShouldReturnTrueForPalindrome() { 75 | //given 76 | String work = 'rotor'; 77 | //when 78 | Boolean result = RecursionExamples.isPalindrome(work); 79 | //then 80 | System.assert(result == true, 'It should return true for anagram'); 81 | } 82 | @IsTest 83 | static void itShouldReturnFalseForPalindrome() { 84 | //given 85 | String word = 'test'; 86 | //when 87 | Boolean result = RecursionExamples.isPalindrome(word); 88 | //then 89 | System.assert( 90 | result == false, 91 | 'It should not return true for a palindrome' 92 | ); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/stack/StringStack_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 9/26/2021. 3 | */ 4 | 5 | @IsTest 6 | public with sharing class StringStack_Tests { 7 | @IsTest 8 | static void itShouldPush() { 9 | //given 10 | String firstName = 'James'; 11 | String lastName = 'Kirk'; 12 | //when 13 | BasicStack stack = new StringStack(); 14 | stack.push(firstName); 15 | stack.push(lastName); 16 | //then 17 | System.assertEquals( 18 | 2, 19 | stack.getSize(), 20 | 'Two Items should have been pushed to the stack' 21 | ); 22 | } 23 | 24 | @IsTest 25 | static void itShouldPop() { 26 | //given 27 | String firstName = 'James'; 28 | String lastName = 'Kirk'; 29 | //when 30 | BasicStack stack = new StringStack(); 31 | stack.push(firstName); 32 | stack.push(lastName); 33 | String result = (String) stack.pop(); 34 | //then 35 | System.assertEquals( 36 | 1, 37 | stack.getSize(), 38 | 'One item should have been popped off the stack' 39 | ); 40 | System.assertEquals( 41 | 'Kirk', 42 | result, 43 | 'The last value should have been popped LIFO' 44 | ); 45 | } 46 | 47 | @IsTest 48 | static void itShouldThrowException() { 49 | //given 50 | BasicStack stack = new StringStack(); 51 | //when 52 | try { 53 | stack.push(7); 54 | } catch (Exception e) { 55 | System.assert( 56 | e instanceof BasicStack.BasicStackException, 57 | 'It should throw a custom exception' 58 | ); 59 | } 60 | } 61 | @IsTest 62 | static void itShouldContainString_Positive() { 63 | //given 64 | BasicStack stack = new StringStack(); 65 | stack.push('Brooks'); 66 | //when 67 | Boolean result = stack.contains('Brooks'); 68 | //then 69 | System.assert(result == true, 'It should contain the value'); 70 | } 71 | 72 | @IsTest 73 | static void itShouldContainString_Negative() { 74 | //given 75 | BasicStack stack = new StringStack(); 76 | stack.push('Brooks'); 77 | //when 78 | Boolean result = stack.contains('Regis'); 79 | //then 80 | System.assert(result == false, 'It should not contain the value'); 81 | } 82 | @IsTest 83 | static void itShouldAccessTheStack() { 84 | //given 85 | BasicStack stack = new StringStack(); 86 | stack.push('Bilbo'); 87 | stack.push('Frodo'); 88 | stack.push('Merry'); 89 | stack.push('Samwise'); 90 | stack.push('Pippen'); 91 | System.assert( 92 | stack.getSize() == 5, 93 | 'There should be 5 elements in the stack' 94 | ); 95 | //when 96 | String result = (String) stack.access('Frodo'); 97 | //then 98 | System.assert( 99 | stack.getSize() == 1, 100 | 'The elements should have been popped in the access operation' 101 | ); 102 | System.assert(result == 'Frodo', 'It should have returned the value'); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /force-app/main/default/classes/linked-list/SinglyLinkedList_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 4/14/2022. 3 | */ 4 | 5 | @IsTest 6 | private class SinglyLinkedList_Tests { 7 | @IsTest 8 | static void itShouldAddToFront() { 9 | //given 10 | SinglyLinkedList linkedList = new SinglyLinkedList(); 11 | //when 12 | linkedList.addToFront(new Contact(FirstName = 'Tony', LastName = 'Stark')); 13 | System.assert(linkedList.size == 1); 14 | System.assert(linkedList.head.data instanceof Contact); 15 | } 16 | @IsTest 17 | static void itShouldAddToBack() { 18 | //given 19 | SinglyLinkedList linkedList = new SinglyLinkedList(); 20 | //when 21 | linkedList.addToBack(new Contact(FirstName = 'Tony', LastName = 'Stark')); 22 | linkedList.addToBack(new Contact(FirstName = 'Steve', LastName = 'Rogers')); 23 | //then 24 | System.assert(linkedList.size == 2); 25 | Contact con = (Contact) linkedList.tail.data; 26 | System.assert(con.FirstName == 'Steve'); 27 | } 28 | 29 | @IsTest 30 | static void itShouldThrowErrorWhenListIsEmptyFromFront() { 31 | //given 32 | SinglyLinkedList linkedList = new SinglyLinkedList(); 33 | //when 34 | try { 35 | linkedList.removeFromFront(); 36 | } catch (Exception e) { 37 | //then 38 | System.assert(e instanceof SinglyLinkedList.SinglyLinkedListException); 39 | } 40 | } 41 | 42 | @IsTest 43 | static void itShouldRemoveFront() { 44 | //given 45 | SinglyLinkedList linkedList = new SinglyLinkedList(); 46 | linkedList.addToFront(1); 47 | linkedList.addToFront(2); 48 | linkedList.addToFront(3); 49 | //when 50 | Integer data = (Integer) linkedList.removeFromFront(); 51 | System.assert(data == 3); 52 | } 53 | @IsTest 54 | static void itShouldThrowErrorWhenListIsEmpty() { 55 | //given 56 | SinglyLinkedList linkedList = new SinglyLinkedList(); 57 | //when 58 | try { 59 | Object data = linkedList.removeFromBack(); 60 | } catch (Exception e) { 61 | System.assert(e instanceof SinglyLinkedList.SinglyLinkedListException); 62 | } 63 | } 64 | 65 | @IsTest 66 | static void itShouldReturnListSizeOne() { 67 | //given 68 | SinglyLinkedList linkedList = new SinglyLinkedList(); 69 | linkedList.addToFront(1); 70 | //when 71 | Integer data = (Integer) linkedList.removeFromBack(); 72 | System.assert(data == 1); 73 | System.assert(linkedList.size == 0); 74 | } 75 | 76 | @IsTest 77 | static void itShouldReturnBack() { 78 | //given 79 | SinglyLinkedList linkedList = new SinglyLinkedList(); 80 | linkedList.addToFront(1); 81 | linkedList.addToFront(2); 82 | linkedList.addToFront(3); 83 | linkedList.addToFront(4); 84 | linkedList.addToFront(5); 85 | //when 86 | Integer data = (Integer) linkedList.removeFromBack(); 87 | System.assert(data == 1); 88 | System.assert(linkedList.size == 4); 89 | System.assert(linkedList.tail.data == 2); 90 | System.assert(linkedList.tail.next == null); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /force-app/main/default/classes/sorting/MergeSort.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Uses divide and conquer approach to get to single element arrays. Arrays of size 1 are sorted by default. 3 | */ 4 | 5 | public with sharing class MergeSort { 6 | /** 7 | * @description Divide portion of the Algo. It uses recursion to split the List input in sorted lists of one element 8 | * Notice that the space complexity of Merge Sort in O(n) because we are creating new Lists with every recursive call 9 | * @param integers A list of integers to be sorted. 10 | */ 11 | public static void sort(List integers) { 12 | Integer length = integers.size(); 13 | if (length < 2) { 14 | return; 15 | } 16 | Integer middle = length / 2; 17 | List left = new List(); 18 | List right = new List(); 19 | //Create two new arrays 20 | for (Integer i = 0; i < middle; i++) { 21 | left.add(integers[i]); 22 | } 23 | System.debug(LoggingLevel.INFO, 'value of left array = ' + left); 24 | for (Integer i = middle; i < length; i++) { 25 | right.add(integers[i]); 26 | } 27 | System.debug(LoggingLevel.INFO, 'Value of right array = ' + right); 28 | sort(left); 29 | sort(right); 30 | mergeLists(left, right, integers); 31 | } 32 | 33 | /** 34 | * @description This method will take two sorted lists and merge them together 35 | * @param left left-hand side of the list to be sorted 36 | * @param right right-hand side of the list to be sorted 37 | * @param input List that will be updated 38 | * 39 | * @return Sorted list 40 | */ 41 | public static List mergeLists( 42 | List left, 43 | List right, 44 | List input 45 | ) { 46 | // Create pointers for each array 47 | Integer i = 0; 48 | Integer j = 0; 49 | Integer k = 0; 50 | Integer leftSize = left.size(); 51 | Integer rightSize = right.size(); 52 | 53 | while (i < leftSize && j < rightSize) { 54 | //compare the list values 55 | System.debug( 56 | LoggingLevel.INFO, 57 | 'Comparing ' + 58 | left[i] + 59 | ' to ' + 60 | right[j] 61 | ); 62 | if (left[i] <= right[j]) { 63 | input[k] = left[i]; 64 | System.debug(LoggingLevel.INFO, 'Results list = ' + input); 65 | i++; 66 | } else { 67 | input[k] = right[j]; 68 | System.debug( 69 | LoggingLevel.INFO, 70 | 'In Else condition results updated to ' + input 71 | ); 72 | j++; 73 | } 74 | k++; 75 | } 76 | // We have dropped out of the first loop but may still elements left in one of the two lists 77 | // We take the remainder of the list and add it to the end of results 78 | while (i < leftSize) { 79 | input[k] = left[i]; 80 | System.debug(LoggingLevel.INFO, 'Adding remainder of list one ' + input); 81 | i++; 82 | k++; 83 | } 84 | while (j < rightSize) { 85 | input[k] = right[j]; 86 | System.debug(LoggingLevel.INFO, 'Adding remainder of list two ' + input); 87 | j++; 88 | k++; 89 | } 90 | System.debug(LoggingLevel.INFO, 'Results list = ' + input); 91 | return input; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /force-app/main/default/classes/trees/OpportunityBST.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 2/21/2022. 3 | */ 4 | 5 | public with sharing class OpportunityBST { 6 | public Node_Opp_BST root; 7 | 8 | public OpportunityBST() { 9 | this.root = null; 10 | } 11 | 12 | public OpportunityBST push(Opportunity opp) { 13 | Node_Opp_BST node = new Node_Opp_BST(opp); 14 | if (this.root == null) { 15 | this.root = node; 16 | return this; 17 | } else { 18 | Node_Opp_BST currentNode = this.root; 19 | while (true) { 20 | if (currentNode.value.Amount == node.value.Amount) { 21 | return null; 22 | } 23 | if (opp.Amount > currentNode.value.Amount) { 24 | if (currentNode.right == null) { 25 | currentNode.right = node; 26 | return this; 27 | } else { 28 | // move the current node the next BST left node 29 | currentNode = currentNode.right; 30 | } 31 | } else if (opp.Amount < currentNode.value.Amount) { 32 | if (currentNode.left == null) { 33 | currentNode.left = node; 34 | return this; 35 | } else { 36 | // move the current BST right node 37 | currentNode = currentNode.left; 38 | } 39 | } 40 | } 41 | } 42 | return null; 43 | } 44 | 45 | public Opportunity search(Decimal oppAmount) { 46 | Node_Opp_BST currentNode = this.root; 47 | while (true) { 48 | if (currentNode.value.Amount == oppAmount) { 49 | return currentNode.value; 50 | } 51 | if (oppAmount > currentNode.value.Amount) { 52 | currentNode = currentNode.right; 53 | } else { 54 | currentNode = currentNode.left; 55 | } 56 | } 57 | return null; 58 | } 59 | 60 | /** 61 | * use to traverse the tree 62 | */ 63 | public List breadthFirstSearch() { 64 | Node_Opp_BST current = this.root; 65 | List data = new List(); 66 | //we need to use the list as FIFO data structure 67 | List queue = new List(); 68 | queue.add(this.root); 69 | while (!queue.isEmpty()) { 70 | current = queue.remove(0); 71 | data.add(current); 72 | if (current.left != null) { 73 | queue.add(current.left); 74 | } 75 | if (current.right != null) { 76 | queue.add(current.right); 77 | } 78 | } 79 | return data; 80 | } 81 | 82 | public override String toString() { 83 | return super.toString(); 84 | } 85 | 86 | public void dfs_PreOrder(Node_Opp_BST node) { 87 | if (node == null) { 88 | return; 89 | } 90 | System.debug(node.value); 91 | //recurse on the left subtree 92 | dfs_PreOrder(node.left); 93 | //recurse on the right subtree 94 | dfs_PreOrder(node.right); 95 | } 96 | 97 | public void preOrderDFSNoRecursion() { 98 | // Instead of recursions we can use a list as a queue 99 | List stack = new List(); 100 | stack.add(this.root); 101 | 102 | while (!stack.isEmpty()) { 103 | //pop last item off the stack 104 | Node_Opp_BST current = stack.remove(stack.size() - 1); 105 | System.debug(current.toString()); 106 | // Note that in the iterative solution we need to 107 | // add the right subtree to the queue first. 108 | // The opposite of the recursive solution 109 | if (current.right != null) { 110 | stack.add(current.right); 111 | } 112 | if (current.left != null) { 113 | stack.add(current.left); 114 | } 115 | } 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Algorithms and Data Structures in Apex 2 | 3 | ![Image of Cody from Salesforce](https://github.com/bdJohnson72/Algorithms_And_Data_Structures_In_Apex/blob/master/images/recipes-logo.png) 4 | 5 | Welcome this repo is meant as a companion to 6 | my [youtube series](https://www.youtube.com/playlist?list=PLdt84CMzTQBbon-6Oy2a_rIsOyzMTrjnn). 7 | 8 | ## [Recursion](https://github.com/bdJohnson72/Algorithms_And_Data_Structures_In_Apex/tree/master/force-app/main/default/classes/recursion) 9 | 10 | - [intro to recursion](https://github.com/bdJohnson72/Algorithms_And_Data_Structures_In_Apex/tree/master/force-app/main/default/classes/recursion) 11 | - [Watch it on Youtube](https://www.youtube.com/watch?v=4xpg0NnS90w&list=PLdt84CMzTQBbon-6Oy2a_rIsOyzMTrjnn&index=2) 12 | - I provided some code challenges for writing recursive code. The answers are in the repo. But try to solve these first. 13 | 1. Get the sum or a range of numbers. 14 | 2. Compute a number to a certain power. 15 | 3. Determine if a word is a palindrome 16 | 4. Compute a number in the fibonacci sequence 17 | 5. Reverse a string 18 | 19 | ## [Search](https://github.com/bdJohnson72/Algorithms_And_Data_Structures_In_Apex/tree/master/force-app/main/default/classes/search) 20 | 21 | - [Linear Search](https://github.com/bdJohnson72/Algorithms_And_Data_Structures_In_Apex/blob/master/force-app/main/default/classes/search/LinearSearch.cls) 22 | - [Watch it on Youtube](https://www.youtube.com/watch?v=a8kPwN3b8vk&lc=UgyJwVycuT5JKGj2oil4AaABAg.9XH1gpNj-C-9XHPnpyVIUZ) 23 | - just fine for a small data set. Runs at o(N) 24 | - [Binary Search](https://github.com/bdJohnson72/Algorithms_And_Data_Structures_In_Apex/blob/master/force-app/main/default/classes/search/BinarySearch.cls) 25 | - The algo that made me feel like a genius the first time I saw it and understood it. It needs a sorted list to work 26 | but runs at o(Log N). 27 | 28 | ## [Sorting](https://github.com/bdJohnson72/Algorithms_And_Data_Structures_In_Apex/tree/master/force-app/main/default/classes/sorting) 29 | 30 | - [Bubble Sort](https://github.com/bdJohnson72/Algorithms_And_Data_Structures_In_Apex/blob/master/force-app/main/default/classes/sorting/BubbleSort.cls) 31 | - [Watch it on Youtube](https://youtu.be/KJVDLgfzCL8) 32 | - Pretty much only used as an example of what not do it. But perfect for undertanding why nested for loops are bad. 33 | - [Selection Sort](https://github.com/bdJohnson72/Algorithms_And_Data_Structures_In_Apex/blob/master/force-app/main/default/classes/sorting/SelectionSort.cls) 34 | - Like Bubble Sort but puts the smallest value into sorted position instead of the largest. Algo runs at O(n^2) but is 35 | slightly optomized compared to Bubble Sort. It does one swap per pass for the lowest value. 36 | - [Insertion Sort](https://github.com/bdJohnson72/Algorithms_And_Data_Structures_In_Apex/blob/master/force-app/main/default/classes/sorting/InsertionSort.cls) 37 | - Runs at O(n^2). Iterates from index 1 to final position in the collection. If value is smaller than its predecessor 38 | it compares to elements before and swaps. 39 | - [Merg Sort](https://github.com/bdJohnson72/Algorithms_And_Data_Structures_In_Apex/blob/master/force-app/main/default/classes/sorting/MergeSort.cls) 40 | -The first of our "advanced sorting algos" and an important one to understand. It is a divide and conquer algo and runs at O(nLogn). It has a space complexity of O(n). 41 | 42 | ## [Data Structures]() 43 | 44 | - [Singly Linked List]() :construction: 45 | - A data structure with a head, tail and length property. Unlike a Apex List it has no index. Each node in the list 46 | contains a pointer to the next element. You can not use collection[i] instead it is collection.next(). 47 | - It contains the properties head, length and tail. 48 | -------------------------------------------------------------------------------- /force-app/main/default/classes/problem_solving_patterns/AnagramSolver.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/2/2022. 3 | */ 4 | 5 | public with sharing class AnagramSolver { 6 | /** 7 | * @param stringOne string to compare 8 | * @param stringTwo string to compare 9 | * 10 | * @return true if the strings are anagram of each other 11 | */ 12 | public static Boolean isAnagram(String stringOne, String stringTwo) { 13 | //convert stringsToLists - you could do this in the for loop but I think this is more readable and testable 14 | List stringOneList = convertString(stringOne); 15 | List stringTwoList = convertString(stringTwo); 16 | //check the count and return if strings are not the same length 17 | if (stringOneList.size() != stringTwoList.size()) { 18 | return false; 19 | } 20 | //make a maps to count the frequencies 21 | Map mapOne = countFrequency(stringOneList); 22 | Map mapTwo = countFrequency(stringTwoList); 23 | 24 | // Iterate over the maps and determine if it is an anagram 25 | for (String character : mapOne.keySet()) { 26 | Integer countFrequency = mapOne.get(character); 27 | if (!(mapTwo.containsKey(character)) || (mapTwo.get(character) != countFrequency)) { 28 | return false; 29 | } 30 | } 31 | return true; 32 | } 33 | 34 | /** 35 | * @param stringToConvert string to be converted to a list, 36 | * in lowercase and white space removed, 37 | * 38 | * @return list containing all characters in the string 39 | */ 40 | public static List convertString(String stringToConvert) { 41 | return stringToConvert.toLowerCase() 42 | .deleteWhitespace() 43 | .split(''); 44 | } 45 | 46 | /** 47 | * @param stringList a string that has been converted to a list 48 | * 49 | * @return a map with key value pairs of the characters in the string and their count 50 | */ 51 | public static Map countFrequency(List stringList) { 52 | Map frequencyCountMap = new Map(); 53 | for (String character : stringList) { 54 | if (frequencyCountMap.containsKey(character)) { 55 | frequencyCountMap.put(character, frequencyCountMap.get(character) + 1); 56 | } else { 57 | frequencyCountMap.put(character, 1); 58 | } 59 | } 60 | return frequencyCountMap; 61 | } 62 | 63 | public static Boolean isAnagram2(String str1, String str2) { 64 | str1 = str1.toLowerCase().deleteWhitespace(); 65 | str2 = str2.toLowerCase().deleteWhitespace(); 66 | if(str1.length() != str2.length()){ 67 | return false; 68 | } 69 | Map charFrequencyMap1 = getCharFrequencyMap(str1); 70 | Map charFrequencyMap2 = getCharFrequencyMap(str2); 71 | 72 | for(Integer charFrequency : charFrequencyMap1.keySet()){ 73 | if (!charFrequencyMap2.containsKey(charFrequency) || charFrequencyMap1.get(charFrequency) != charFrequencyMap2.get(charFrequency)) { 74 | return false; 75 | } 76 | } 77 | return true; 78 | } 79 | 80 | private static Map getCharFrequencyMap(String str){ 81 | Map charFrequencyMap = new Map(); 82 | for (Integer i = 0; i < str.length(); i++) { 83 | if (charFrequencyMap.containsKey(str.charAt(i))) { 84 | charFrequencyMap.put(str.charAt(i), charFrequencyMap.get(str.charAt(i)) + 1); 85 | }else { 86 | charFrequencyMap.put(str.charAt(i), 1); 87 | } 88 | } 89 | return charFrequencyMap; 90 | } 91 | } -------------------------------------------------------------------------------- /out/production/Apex_CS/classes/recursion/RecursionExamples.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/6/2022. 3 | * @DESCRIPTION Two ways to reverse a string in Apex. Not including simply calling String.reverse() 4 | */ 5 | 6 | public with sharing class RecursionExamples { 7 | /** 8 | * @description The iterative solution 9 | * 10 | * @param stringToReverse The string we want to reverse 11 | * 12 | * @return the reversed string 13 | */ 14 | public static String stringReversalIterative(String stringToReverse) { 15 | List stringList = stringToReverse.split(''); 16 | List results = new List(); 17 | for (Integer i = stringList.size() - 1; i >= 0; i--) { 18 | results.add(stringList[i]); 19 | } 20 | return String.join(results, ''); 21 | } 22 | 23 | /** 24 | * @description The recursive solution 25 | * @param str the string we want to reverse 26 | * 27 | * @return The reversed string 28 | */ 29 | public static String stringReversalRecursive(String str) { 30 | String result = ''; 31 | if (str.length() == 1) { 32 | return str; 33 | } 34 | result += 35 | str.substring(str.length() - 1, str.length()) + 36 | stringReversalRecursive(str.substring(0, str.length() - 1)); 37 | return result; 38 | } 39 | 40 | /** 41 | * @description computes a factorial iteratively 42 | * @param num The number to compute the factorial for 43 | * 44 | * @return factorial of input 45 | */ 46 | public static Integer factorialIterative(Integer num) { 47 | // factorial is the product of the integers and all the integers below it 48 | // 5! = 5 * 4 * 3 * 2 * 1 = 120 49 | Integer result = 1; 50 | for (Integer i = 1; i <= num; i++) { 51 | result *= i; 52 | } 53 | return result; 54 | } 55 | 56 | /** 57 | * @description computes a factorial recursively 58 | * @param num number to compute the factorial for 59 | * 60 | * @return factorial of input 61 | */ 62 | public static Integer factorialRecursive(Integer num) { 63 | // Base case 64 | if (num == 1) { 65 | return 1; 66 | } 67 | // recursive case 68 | return num *= factorialRecursive(num - 1); 69 | } 70 | 71 | /** 72 | * @description calculates a number to a specific power recursively. 73 | * @param num the number that we starting with 74 | * @param power the power to multiply the number by 75 | * 76 | * @return the number to power ex: 10, 2 returns 100 77 | */ 78 | public static Integer power(Integer num, Integer power) { 79 | //Our base case 80 | if (power == 1) { 81 | return num; 82 | } 83 | // our recursive case 84 | return num *= power(num, power - 1); 85 | } 86 | 87 | /** 88 | * @description Calcuate sum of numbers in a range 89 | * @param num The highest number in the range 90 | * 91 | * @return Integer: The sum 92 | */ 93 | public static Integer range(Integer num) { 94 | // Base case 95 | if (num == 0) { 96 | return 0; 97 | } 98 | // Recursive case 99 | return num += range(num - 1); 100 | } 101 | 102 | public static Integer fibonacci(Integer num) { 103 | if (num < 2) { 104 | return num; 105 | } 106 | return fibonacci(num - 2) + fibonacci(num - 1); 107 | } 108 | 109 | /** 110 | * @description A recursive method to determine is a word is a palindrome. 111 | * like rotor or madam, 112 | * @param word to evaluate 113 | * 114 | * @return Boolean 115 | */ 116 | 117 | public static Boolean isPalindrome(String word) { 118 | if (word.length() <= 1) { 119 | return true; 120 | } 121 | if ( 122 | word.substring(0, 1) == word.substring(word.length() - 1, word.length()) 123 | ) { 124 | return isPalindrome(word.substring(1, word.length() - 1)); 125 | } else { 126 | return false; 127 | } 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /force-app/main/default/classes/recursion/RecursionExamples.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/6/2022. 3 | * @DESCRIPTION Two ways to reverse a string in Apex. Not including simply calling String.reverse() 4 | */ 5 | 6 | public with sharing class RecursionExamples { 7 | /** 8 | * @description The iterative solution 9 | * 10 | * @param stringToReverse The string we want to reverse 11 | * 12 | * @return the reversed string 13 | */ 14 | public static String stringReversalIterative(String stringToReverse) { 15 | List stringList = stringToReverse.split(''); 16 | List results = new List(); 17 | for (Integer i = stringList.size() - 1; i >= 0; i--) { 18 | results.add(stringList[i]); 19 | } 20 | return String.join(results, ''); 21 | } 22 | 23 | /** 24 | * @description The recursive solution 25 | * @param str the string we want to reverse 26 | * 27 | * @return The reversed string 28 | */ 29 | public static String stringReversalRecursive(String str) { 30 | String result = ''; 31 | if (str.length() == 1) { 32 | return str; 33 | } 34 | result += 35 | str.substring(str.length() - 1, str.length()) + 36 | stringReversalRecursive(str.substring(0, str.length() - 1)); 37 | return result; 38 | } 39 | 40 | /** 41 | * @description computes a factorial iteratively 42 | * @param num The number to compute the factorial for 43 | * 44 | * @return factorial of input 45 | */ 46 | public static Integer factorialIterative(Integer num) { 47 | // factorial is the product of the integers and all the integers below it 48 | // 5! = 5 * 4 * 3 * 2 * 1 = 120 49 | Integer result = 1; 50 | for (Integer i = 1; i <= num; i++) { 51 | result *= i; 52 | } 53 | return result; 54 | } 55 | 56 | /** 57 | * @description computes a factorial recursively 58 | * @param num number to compute the factorial for 59 | * 60 | * @return factorial of input 61 | */ 62 | public static Integer factorialRecursive(Integer num) { 63 | // Base case 64 | if (num == 1) { 65 | return 1; 66 | } 67 | // recursive case 68 | return num *= factorialRecursive(num - 1); 69 | } 70 | 71 | /** 72 | * @description calculates a number to a specific power recursively. 73 | * @param num the number that we starting with 74 | * @param power the power to multiply the number by 75 | * 76 | * @return the number to power ex: 10, 2 returns 100 77 | */ 78 | public static Integer power(Integer num, Integer power) { 79 | //Our base case 80 | if (power == 1) { 81 | return num; 82 | } 83 | // our recursive case 84 | return num *= power(num, power - 1); 85 | } 86 | 87 | /** 88 | * @description Calcuate sum of numbers in a range 89 | * @param num The highest number in the range 90 | * 91 | * @return Integer: The sum 92 | */ 93 | public static Integer range(Integer num) { 94 | // Base case 95 | if (num == 0) { 96 | return 0; 97 | } 98 | // Recursive case 99 | return num += range(num - 1); 100 | } 101 | 102 | public static Integer fibonacci(Integer num) { 103 | if (num < 2) { 104 | return num; 105 | } 106 | return fibonacci(num - 1) + fibonacci(num - 2); 107 | } 108 | 109 | /** 110 | * @description A recursive method to determine is a word is a palindrome. 111 | * like rotor or madam, 112 | * @param word to evaluate 113 | * 114 | * @return Boolean 115 | */ 116 | 117 | public static Boolean isPalindrome(String word) { 118 | System.debug('current word = ' + word); 119 | if (word.length() <= 1) { 120 | return true; 121 | } 122 | if ( 123 | word.substring(0, 1) == word.substring(word.length() - 1, word.length()) 124 | ) { 125 | return isPalindrome(word.substring(1, word.length() - 1)); 126 | } else { 127 | return false; 128 | } 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /force-app/main/default/classes/linked-list/SobjectLinkedList.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/16/2022. 3 | */ 4 | 5 | public with sharing class SobjectLinkedList { 6 | public class LinkedListException extends Exception { 7 | } 8 | 9 | public SobjectNode head; 10 | public SobjectNode tail; 11 | public Integer size { get; private set; } 12 | 13 | public SobjectLinkedList() { 14 | this.size = 0; 15 | this.head = null; 16 | this.tail = null; 17 | } 18 | 19 | public SobjectLinkedList push(SObject sobj) { 20 | SobjectNode node = new SobjectNode(sobj); 21 | if (this.head == null) { 22 | this.head = node; 23 | this.tail = node; 24 | } else { 25 | this.tail.next = node; 26 | this.tail = node; 27 | } 28 | this.size++; 29 | return this; 30 | } 31 | /** 32 | * @description Add a new node to the start of the list. O(1) operation 33 | * @return updated list 34 | */ 35 | public SobjectLinkedList unshift(SObject sobj) { 36 | SobjectNode node = new SobjectNode(sobj); 37 | // Update head 38 | if (this.head == null) { 39 | this.head = node; 40 | this.tail = this.head; 41 | } else { 42 | node.next = this.head; 43 | this.head = node; 44 | } 45 | this.size++; 46 | return this; 47 | } 48 | /** 49 | * @description Removing from the tail is an O(n) operation. We have to traverse the entire list to find the 50 | * second to last element and set it to the new tail. 51 | * @return The last element of the list. 52 | */ 53 | public SobjectNode pop() { 54 | if (this.head == null) { 55 | return null; 56 | } 57 | SobjectNode current = this.head; 58 | SobjectNode newTail = current; 59 | while (current.next != null) { 60 | newTail = current; 61 | current = current.next; 62 | System.debug('current = ' + current); 63 | } 64 | this.tail = newTail; 65 | this.tail.next = null; 66 | this.size--; 67 | if (this.size == 0) { 68 | this.head = null; 69 | this.tail = null; 70 | } 71 | return current; 72 | } 73 | 74 | /** 75 | * @description Remove a node from the front of the list. Runs at constant time 76 | * @return the vale at head 77 | */ 78 | public SobjectNode shift() { 79 | if (this.isEmpty()) { 80 | return null; 81 | } 82 | SobjectNode removeNode = this.head; 83 | // Move the head over 84 | this.head = head.next; 85 | // Decrement the length 86 | size--; 87 | return removeNode; 88 | } 89 | 90 | public void addToEnd(SObject sobj) { 91 | SobjectNode node = new SobjectNode(sobj); 92 | if (this.head == null) { 93 | this.head = node; 94 | this.tail = node; 95 | } else { 96 | node.next = null; 97 | this.tail = node; 98 | this.size++; 99 | } 100 | } 101 | 102 | public Boolean isEmpty() { 103 | return this.head == null ? true : false; 104 | } 105 | /** 106 | * @description Returns the node at the index. 107 | * This is an O(n) operation. 108 | * @param index The index we want to return 109 | * 110 | * @return 111 | */ 112 | public SobjectNode get(Integer index) { 113 | Integer start = 0; 114 | SobjectNode current = this.head; 115 | if (index < 0 || index >= this.size) { 116 | Exception e = new LinkedListException(); 117 | e.setMessage('Index was out bounds'); 118 | throw e; 119 | } 120 | while (start != index) { 121 | current = current.next; 122 | start++; 123 | } 124 | return current; 125 | } 126 | 127 | public Boolean set(SObject value, Integer index) { 128 | SobjectNode node = get(index); 129 | if (node == null) { 130 | return false; 131 | } 132 | node.sobj = value; 133 | return true; 134 | } 135 | 136 | public SobjectLinkedList insertToList(SObject sobj, Integer index) { 137 | indexOutOfBounds(index); 138 | if (index == this.size) { 139 | return push(sobj); 140 | } 141 | if (index == 0) { 142 | return unshift(sobj); 143 | } 144 | SobjectNode node = new SobjectNode(sobj); 145 | SobjectNode previous = get(index - 1); 146 | System.debug(LoggingLevel.INFO, 'Value of Previous = ' + previous); 147 | node.next = previous.next; 148 | previous.next = node; 149 | this.size++; 150 | return this; 151 | } 152 | private void indexOutOfBounds(Integer index) { 153 | if (index < 0 || index >= this.size) { 154 | Exception e = new LinkedListException(); 155 | e.setMessage('Index was out of bounds'); 156 | throw e; 157 | } 158 | } 159 | public SobjectNode remove(Integer index) { 160 | indexOutOfBounds(index); 161 | SobjectNode returnValue; 162 | SobjectNode current = this.head; 163 | SobjectNode previous = this.head; 164 | Integer count = 0; 165 | if (index == 0) { 166 | return this.pop(); 167 | } 168 | if (index == this.size) { 169 | return this.shift(); 170 | } 171 | previous = get(index - 1); 172 | current = previous.next; 173 | previous.next = current.next; 174 | this.size--; 175 | return current; 176 | } 177 | 178 | public SobjectLinkedList reverse() { 179 | SobjectNode node = this.head; 180 | this.head = this.tail; 181 | this.tail = node; 182 | SobjectNode next; 183 | SobjectNode previous = null; 184 | Integer length = this.size; 185 | for (Integer i = 0; i < length; i++) { 186 | next = node.next; 187 | node.next = previous; 188 | previous = node; 189 | node = next; 190 | } 191 | return this; 192 | } 193 | } 194 | -------------------------------------------------------------------------------- /force-app/main/default/classes/linked-list/SobjectLinkedList_Tests.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Brooks Johnson on 1/16/2022. 3 | */ 4 | 5 | @IsTest 6 | private class SobjectLinkedList_Tests { 7 | public static Contact ironMan = new Contact( 8 | FirstName = 'Tony', 9 | LastName = 'Stark' 10 | ); 11 | public static Contact hulk = new Contact( 12 | FirstName = 'Bruce', 13 | LastName = 'Banner' 14 | ); 15 | public static Contact spiderMan = new Contact( 16 | FirstName = 'Peter', 17 | LastName = 'Parker' 18 | ); 19 | public static Contact cap = new Contact( 20 | firstName = 'Steve', 21 | LastName = 'Rogers' 22 | ); 23 | 24 | public static Contact drStrange = new Contact( 25 | FirstName = 'Steven', 26 | LastName = 'Strange' 27 | ); 28 | @IsTest 29 | static void itShouldShiftValueFromFront() { 30 | //given 31 | SobjectLinkedList contactList = new SobjectLinkedList(); 32 | contactList.push(ironMan); 33 | contactList.push(hulk); 34 | contactList.push(spiderMan); 35 | //when 36 | SobjectNode node = contactList.shift(); 37 | System.assertEquals( 38 | 2, 39 | contactList.size, 40 | 'It should return the correct size' 41 | ); 42 | Contact contact = (Contact) node.sobj; 43 | System.assertEquals( 44 | 'Tony', 45 | contact.FirstName, 46 | 'It should return the first node' 47 | ); 48 | } 49 | 50 | @IsTest 51 | static void itShouldShiftElementToFront() { 52 | // given 53 | SobjectLinkedList contactList = new SobjectLinkedList(); 54 | contactList.push(ironMan); 55 | contactList.push(hulk); 56 | //when 57 | contactList = contactList.unshift(cap); 58 | System.debug(contactList); 59 | System.assertEquals( 60 | 3, 61 | contactList.size, 62 | 'It should return the correct size' 63 | ); 64 | Contact head = (Contact) contactList.head.sobj; 65 | System.assertEquals( 66 | 'Steve', 67 | head.FirstName, 68 | 'It should return the updated head' 69 | ); 70 | } 71 | 72 | @IsTest 73 | static void itShouldAddToTail() { 74 | //given 75 | SobjectLinkedList contactList = new SobjectLinkedList(); 76 | contactList.push(ironMan); 77 | //when 78 | contactList.addToEnd(hulk); 79 | //then 80 | System.debug(contactList); 81 | } 82 | @IsTest 83 | static void itShouldPopValue() { 84 | // given 85 | SobjectLinkedList contactList = new SobjectLinkedList(); 86 | contactList.push(ironMan); 87 | contactList.push(hulk); 88 | contactList.push(spiderMan); 89 | //when 90 | SobjectNode node = contactList.pop(); 91 | Contact contact = (Contact) node.sobj; 92 | //then 93 | System.assertEquals( 94 | 'Peter', 95 | contact.FirstName, 96 | 'It should pop the last value off the list' 97 | ); 98 | } 99 | @IsTest 100 | static void itShouldHandleEmptyList() { 101 | // given 102 | SobjectLinkedList contactList = new SobjectLinkedList(); 103 | contactList.push(ironMan); 104 | contactList.push(hulk); 105 | // when 106 | contactList.pop(); 107 | contactList.pop(); 108 | //then 109 | System.assertEquals(0, contactList.size, 'Size should be zero'); 110 | System.assertEquals(null, contactList.head, 'Head should be null'); 111 | System.assertEquals(null, contactList.tail, 'Tail should be null'); 112 | } 113 | 114 | @IsTest 115 | static void itShouldReturnValueAtIndex() { 116 | // given 117 | SobjectLinkedList contactList = new SobjectLinkedList(); 118 | contactList.push(ironMan); 119 | contactList.push(hulk); 120 | contactList.push(spiderMan); 121 | contactList.push(cap); 122 | // when 123 | SobjectNode node = contactList.get(2); 124 | Contact contact = (Contact) node.sobj; 125 | System.assertEquals( 126 | 'Peter', 127 | contact.FirstName, 128 | 'It should return the correct value from the index' 129 | ); 130 | } 131 | @IsTest 132 | static void itShouldThrowException() { 133 | // given 134 | SobjectLinkedList contactList = new SobjectLinkedList(); 135 | // when 136 | try { 137 | SobjectNode node = contactList.get(1); 138 | } catch (Exception e) { 139 | System.debug(LoggingLevel.DEBUG, e.getMessage()); 140 | System.assert( 141 | e instanceof SobjectLinkedList.LinkedListException, 142 | 'It should throw a custom exception' 143 | ); 144 | } 145 | } 146 | 147 | @IsTest 148 | static void itShouldSetValueAtIndex() { 149 | // given 150 | SobjectLinkedList contactList = new SobjectLinkedList(); 151 | contactList.push(ironMan); 152 | contactList.push(hulk); 153 | contactList.push(spiderMan); 154 | contactList.push(cap); 155 | // when 156 | Boolean result = contactList.set(drStrange, 2); 157 | SobjectNode node = contactList.get(2); 158 | Contact contact = (Contact) node.sobj; 159 | //then 160 | System.assertEquals(true, result, 'It should set the value'); 161 | System.assertEquals( 162 | 'Strange', 163 | contact.LastName, 164 | 'It should return the correct value from the index' 165 | ); 166 | } 167 | @IsTest 168 | static void itShouldInsertVallue() { 169 | // given 170 | SobjectLinkedList contactList = new SobjectLinkedList(); 171 | contactList.push(ironMan); 172 | contactList.push(hulk); 173 | contactList.push(spiderMan); 174 | contactList.push(cap); 175 | // when 176 | SobjectLinkedList linkedList = contactList.insertToList(drStrange, 3); 177 | // then 178 | SobjectNode node = linkedList.get(3); 179 | Contact contact = (Contact) node.sobj; 180 | System.debug(LoggingLevel.INFO, 'The returned contact is: ' + contact); 181 | System.debug(LoggingLevel.INFO, 'The new list is ' + linkedList); 182 | System.debug(linkedList.head); 183 | System.debug(linkedList.tail); 184 | System.assertEquals( 185 | 'Strange', 186 | contact.LastName, 187 | 'It should insert the node into the correct possition' 188 | ); 189 | } 190 | 191 | @IsTest 192 | static void itShouldReverseList() { 193 | // given 194 | SobjectLinkedList contactList = new SobjectLinkedList(); 195 | contactList.push(ironMan); 196 | contactList.push(hulk); 197 | contactList.push(spiderMan); 198 | contactList.push(cap); 199 | contactList = contactList.reverse(); 200 | System.debug('final list is ' + contactList); 201 | System.debug('final list is ' + contactList.head); 202 | System.debug(contactList.tail); 203 | System.debug(contactList.toString()); 204 | } 205 | 206 | @IsTest 207 | static void itShouldRemoveValueFromIndex() { 208 | // given 209 | SobjectLinkedList contactList = new SobjectLinkedList(); 210 | contactList.push(ironMan); 211 | contactList.push(hulk); 212 | contactList.push(spiderMan); 213 | contactList.push(cap); 214 | // when 215 | SobjectNode node = contactList.remove(1); 216 | Contact contact = (Contact) node.sobj; 217 | System.assertEquals( 218 | contact.LastName, 219 | 'Banner', 220 | 'It should remove the value at the index' 221 | ); 222 | System.assertEquals(3, contactList.size, 'It should decrement list size'); 223 | } 224 | } 225 | --------------------------------------------------------------------------------