diff --git a/labs/11_hash_tables/README.md b/labs/11_hash_tables/README.md index 53e336a..2c982c2 100644 --- a/labs/11_hash_tables/README.md +++ b/labs/11_hash_tables/README.md @@ -24,9 +24,38 @@ Finally, implement and test the *resize* function. This function is automaticall **To complete this checkpoint**: Show a TA these additions and the test output. -## Checkpoint 3 +## Checkpoint 3: Using std::unordered_map -*estimate: remainder of lab time* +*estimate: 20-30 minutes* -To be added. +Complete the *isHappy* function in this [program](test_happy_number.cpp). This function determines if a number n is happy or not. You can assume 1<=n<50000. Do not include extra libraries. +A happy number is a number defined by the following process: + +- Starting with any positive integer, replace the number by the sum of the squares of its digits. +- Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. +- Those numbers for which this process ends in 1 are happy. + +Return true if n is a happy number, and false if not. Here are some examples: + +```console +Example 1: + +Input: n = 19 +Output: true + +Explanation: +12 + 92 = 82 +82 + 22 = 68 +62 + 82 = 100 +12 + 02 + 02 = 1 +``` + +```console +Example 2: + +Input: n = 2 +Output: false +``` + +**To complete this checkpoint**: Show a TA your program and the test results. diff --git a/labs/11_hash_tables/test_happy_number.cpp b/labs/11_hash_tables/test_happy_number.cpp new file mode 100644 index 0000000..4b0b1a6 --- /dev/null +++ b/labs/11_hash_tables/test_happy_number.cpp @@ -0,0 +1,24 @@ +#include +#include + +bool isHappy(int n) { +} + +int main() { + // Test cases + // 2, 4, 5, 6, 17, 18, 20 are not happy numbers. + // 1, 7, 10, 13, 19, 23, 28, 68 are not happy numbers. + + int testCases[] = {2,4,5,6,17,18,20,68,1,7,10,13,19,23,28}; + + for (int n : testCases) { + if (isHappy(n)) { + std::cout << n << " is a happy number." << std::endl; + } else { + std::cout << n << " is not a happy number." << std::endl; + } + } + + return 0; +} +