editing good number code and description
This commit is contained in:
@@ -10,15 +10,15 @@ In this lab, you will practice using std::unordered_set, std::unordered_map, and
|
|||||||
|
|
||||||
*estimate: 15-30 minutes*
|
*estimate: 15-30 minutes*
|
||||||
|
|
||||||
Complete the *isHappy* function in this [program](happy_number.cpp). This function determines if a number n is happy or not. You can assume 1<=n<50000. You must write two versions of the function, one version uses std::unordered_set, the other version uses std::unordered_map.
|
Complete the *isGood* function in this [program](good_number.cpp). This function determines if a number n is good or not. You can assume 1<=n<50000. You must write two versions of the function, one version uses std::unordered_set, the other version uses std::unordered_map.
|
||||||
|
|
||||||
A happy number is a number defined by the following process:
|
A good 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.
|
- 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.
|
- 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.
|
- Those numbers for which this process ends in 1 are good.
|
||||||
|
|
||||||
Return true if n is a happy number, and false if not. Here are some examples:
|
Return true if n is a good number, and false if not. Here are some examples:
|
||||||
|
|
||||||
```console
|
```console
|
||||||
Example 1:
|
Example 1:
|
||||||
@@ -56,7 +56,7 @@ For the second part of this checkpoint, experiment with the hash function. In th
|
|||||||
|
|
||||||
Finally, implement and test the *resize* function. This function is automatically called from the *insert* function when the set gets “too full”. This function should make a new top level vector structure of the requested size and copy all the data from the old structure to the new structure. Note that the elements will likely be shuffled around from the old structure to the new structure.-->
|
Finally, implement and test the *resize* function. This function is automatically called from the *insert* function when the set gets “too full”. This function should make a new top level vector structure of the requested size and copy all the data from the old structure to the new structure. Note that the elements will likely be shuffled around from the old structure to the new structure.-->
|
||||||
|
|
||||||
Complete the *isHappy* function using separate chaining. Do not use any of these: std::unordered_map, std::unordered_set, std::map, std::set.
|
Complete the *isGood* function using separate chaining. Do not use any of these: std::unordered_map, std::unordered_set, std::map, std::set.
|
||||||
|
|
||||||
**To complete this checkpoint**: Show a TA these additions and the test output.
|
**To complete this checkpoint**: Show a TA these additions and the test output.
|
||||||
|
|
||||||
|
|||||||
23
labs/12_hash_tables/good_number.cpp
Normal file
23
labs/12_hash_tables/good_number.cpp
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
bool isGood(int n) {
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// Test cases
|
||||||
|
// 2, 4, 5, 6, 17, 18, 20 are not good numbers.
|
||||||
|
// 1, 7, 10, 13, 19, 23, 28, 68 are good numbers.
|
||||||
|
|
||||||
|
int testCases[] = {2,4,5,6,17,18,20,1,7,10,13,19,23,28,68};
|
||||||
|
|
||||||
|
for (int n : testCases) {
|
||||||
|
if (isGood(n)) {
|
||||||
|
std::cout << n << " is a good number." << std::endl;
|
||||||
|
} else {
|
||||||
|
std::cout << n << " is not a good number." << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
|
|
||||||
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 happy numbers.
|
|
||||||
|
|
||||||
int testCases[] = {2,4,5,6,17,18,20,1,7,10,13,19,23,28,68};
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user