aboutsummaryrefslogtreecommitdiff
path: root/challenge-009/paulo-custodio/cpp
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-06-24 22:52:22 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2021-06-24 22:52:22 +0100
commitea269468630076e333ed440d17f4c8679d3d4e9a (patch)
treeed3a8dad98b1e84239ec76c2f6ff70f933f8735a /challenge-009/paulo-custodio/cpp
parent3f3d45004b145f2b8bc15617e0cbce80142ccf5b (diff)
downloadperlweeklychallenge-club-ea269468630076e333ed440d17f4c8679d3d4e9a.tar.gz
perlweeklychallenge-club-ea269468630076e333ed440d17f4c8679d3d4e9a.tar.bz2
perlweeklychallenge-club-ea269468630076e333ed440d17f4c8679d3d4e9a.zip
Add C and C++ solutions to challenge 009
Diffstat (limited to 'challenge-009/paulo-custodio/cpp')
-rw-r--r--challenge-009/paulo-custodio/cpp/ch-1.cpp37
-rw-r--r--challenge-009/paulo-custodio/cpp/ch-2.cpp127
2 files changed, 164 insertions, 0 deletions
diff --git a/challenge-009/paulo-custodio/cpp/ch-1.cpp b/challenge-009/paulo-custodio/cpp/ch-1.cpp
new file mode 100644
index 0000000000..a647b3cfd7
--- /dev/null
+++ b/challenge-009/paulo-custodio/cpp/ch-1.cpp
@@ -0,0 +1,37 @@
+/*
+Challenge 009
+
+Challenge #1
+Write a script that finds the first square number that has at least 5 distinct
+digits.This was proposed by Laurent Rosenfeld.
+*/
+
+#include <iostream>
+using namespace std;
+
+int num_diff_digits(int n) {
+ bool digits[10] = {0};
+ int count = 0;
+ while (n > 0) {
+ int digit = n % 10;
+ n /= 10;
+ if (!digits[digit]) {
+ digits[digit] = true;
+ count++;
+ }
+ }
+ return count;
+}
+
+int main(int argc, char* argv[]) {
+ int num = 0;
+ if (argc == 2)
+ num = atoi(argv[1]);
+ if (num == 0)
+ num = 5;
+
+ int n = 1;
+ while (num_diff_digits(n * n) < num)
+ n++;
+ cout << n * n << endl;
+}
diff --git a/challenge-009/paulo-custodio/cpp/ch-2.cpp b/challenge-009/paulo-custodio/cpp/ch-2.cpp
new file mode 100644
index 0000000000..fe621cfa33
--- /dev/null
+++ b/challenge-009/paulo-custodio/cpp/ch-2.cpp
@@ -0,0 +1,127 @@
+/*
+Challenge 009
+
+Challenge #2
+Write a script to perform different types of ranking as described below:
+
+1. Standard Ranking (1224): Items that compare equal receive the same ranking
+ number, and then a gap is left in the ranking numbers.
+2. Modified Ranking (1334): It is done by leaving the gaps in the ranking
+ numbers before the sets of equal-ranking items.
+3. Dense Ranking (1223): Items that compare equally receive the same
+ ranking number, and the next item(s) receive the immediately following
+ ranking number.
+*/
+
+#include <algorithm>
+#include <iostream>
+#include <vector>
+using namespace std;
+
+struct Score {
+ int seq;
+ int score;
+ int rank;
+};
+
+bool rev_by_score(const Score& a, const Score& b) {
+ return a.score > b.score;
+}
+
+bool by_seq(const Score& a, const Score& b) {
+ return a.seq < b.seq;
+}
+
+void standard_ranking(vector<Score>& scores) {
+ sort(scores.begin(), scores.end(), rev_by_score);
+ int rank = 1;
+ for (size_t i = 0; i < scores.size(); i++) {
+ int count = 0;
+ for (size_t j = i; j < scores.size() && scores[i].score == scores[j].score; j++) {
+ count++;
+ scores[j].rank = rank;
+ }
+ rank += count;
+ i += count - 1;
+ }
+ sort(scores.begin(), scores.end(), by_seq);
+}
+
+void modified_ranking(vector<Score>& scores) {
+ sort(scores.begin(), scores.end(), rev_by_score);
+ int rank = 1;
+ for (size_t i = 0; i < scores.size(); i++) {
+ int count = 0;
+ for (size_t j = i; j < scores.size() && scores[i].score == scores[j].score; j++)
+ count++;
+ rank += count - 1;
+ for (size_t j = i; j < scores.size() && scores[i].score == scores[j].score; j++)
+ scores[j].rank = rank;
+ rank++;
+ i += count - 1;
+ }
+ sort(scores.begin(), scores.end(), by_seq);
+}
+
+void dense_ranking(vector<Score>& scores) {
+ sort(scores.begin(), scores.end(), rev_by_score);
+ int rank = 1;
+ for (size_t i = 0; i < scores.size(); i++) {
+ int count = 0;
+ for (size_t j = i; j < scores.size() && scores[i].score == scores[j].score; j++) {
+ count++;
+ scores[j].rank = rank;
+ }
+ rank++;
+ i += count - 1;
+ }
+ sort(scores.begin(), scores.end(), by_seq);
+}
+
+
+int main(int argc, char* argv[]) {
+ if (argc < 2) return EXIT_FAILURE;
+ vector<Score> scores;
+ for (int i = 1; i < argc; i++) {
+ Score s;
+ s.seq = i;
+ s.score = atoi(argv[i]);
+ s.rank = 0;
+ scores.push_back(s);
+ }
+
+ cout << "Data: ";
+ for (size_t i = 0; i < scores.size(); i++) {
+ cout << scores[i].score;
+ if (i + 1 < scores.size())
+ cout << ", ";
+ }
+ printf("\n");
+
+ standard_ranking(scores);
+ cout << "Standard ranking: ";
+ for (size_t i = 0; i < scores.size(); i++) {
+ cout << scores[i].rank;
+ if (i + 1 < scores.size())
+ cout << ", ";
+ }
+ cout << endl;
+
+ modified_ranking(scores);
+ cout << "Modified ranking: ";
+ for (size_t i = 0; i < scores.size(); i++) {
+ cout << scores[i].rank;
+ if (i + 1 < scores.size())
+ cout << ", ";
+ }
+ cout << endl;
+
+ dense_ranking(scores);
+ cout << "Dense ranking: ";
+ for (size_t i = 0; i < scores.size(); i++) {
+ cout << scores[i].rank;
+ if (i + 1 < scores.size())
+ cout << ", ";
+ }
+ cout << endl;
+}