diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-06-27 20:22:30 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-06-27 20:22:30 +0100 |
| commit | 287d70fb03bbb2ba51e636cf051775400e94bcb4 (patch) | |
| tree | 85a39b2a331bbb5b49d17f63ae76796f3999e7ce /challenge-009 | |
| parent | 9beb73e6913f7e462c4aca8719048a7be98e4ad2 (diff) | |
| parent | 1b3142a75a8db8a886596770514e2d9fa1cd9187 (diff) | |
| download | perlweeklychallenge-club-287d70fb03bbb2ba51e636cf051775400e94bcb4.tar.gz perlweeklychallenge-club-287d70fb03bbb2ba51e636cf051775400e94bcb4.tar.bz2 perlweeklychallenge-club-287d70fb03bbb2ba51e636cf051775400e94bcb4.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-009')
| -rw-r--r-- | challenge-009/paulo-custodio/bc/ch-1.bc | 31 | ||||
| -rw-r--r-- | challenge-009/paulo-custodio/c/ch-1.c | 38 | ||||
| -rw-r--r-- | challenge-009/paulo-custodio/c/ch-2.c | 134 | ||||
| -rw-r--r-- | challenge-009/paulo-custodio/cpp/ch-1.cpp | 37 | ||||
| -rw-r--r-- | challenge-009/paulo-custodio/cpp/ch-2.cpp | 127 | ||||
| -rw-r--r-- | challenge-009/paulo-custodio/t/test-1.yaml | 25 | ||||
| -rw-r--r-- | challenge-009/paulo-custodio/t/test-2.yaml | 9 | ||||
| -rw-r--r-- | challenge-009/paulo-custodio/test.pl | 33 |
8 files changed, 404 insertions, 30 deletions
diff --git a/challenge-009/paulo-custodio/bc/ch-1.bc b/challenge-009/paulo-custodio/bc/ch-1.bc new file mode 100644 index 0000000000..0449047840 --- /dev/null +++ b/challenge-009/paulo-custodio/bc/ch-1.bc @@ -0,0 +1,31 @@ +#!/usr/bin/bc -ql + +/* +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. +*/ + +scale = 0 +num = read() + +define num_diff_digits(n) { + auto digit, digits[], i, sum + while (n > 0) { + digit = n % 10 + n /= 10 + digits[digit] = 1 + } + sum = 0 + for (i = 0; i < 10; i++) + sum += digits[i] + return sum; +} + +n = 1 +while (num_diff_digits(n*n) < num) + n = n+1 +n*n +quit diff --git a/challenge-009/paulo-custodio/c/ch-1.c b/challenge-009/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..1a199fdf2c --- /dev/null +++ b/challenge-009/paulo-custodio/c/ch-1.c @@ -0,0 +1,38 @@ +/* +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 <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + +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++; + printf("%d\n", n * n); +} diff --git a/challenge-009/paulo-custodio/c/ch-2.c b/challenge-009/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..c5b0a0be59 --- /dev/null +++ b/challenge-009/paulo-custodio/c/ch-2.c @@ -0,0 +1,134 @@ +/* +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 <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + +typedef struct score_t { + int seq; + int score; + int rank; +} score_t; + +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + +int rev_by_score(const void* a, const void* b) { + return -(((score_t*)a)->score - ((score_t*)b)->score); // reverse-sort +} + +int by_seq(const void* a, const void* b) { + return ((score_t*)a)->seq - ((score_t*)b)->seq; +} + +void standard_ranking(int num_scores, score_t* scores) { + qsort(scores, num_scores, sizeof(score_t), rev_by_score); + int rank = 1; + for (int i = 0; i < num_scores; i++) { + int count = 0; + for (int j = i; j < num_scores && scores[i].score == scores[j].score; j++) { + count++; + scores[j].rank = rank; + } + rank += count; + i += count - 1; + } + qsort(scores, num_scores, sizeof(score_t), by_seq); +} + +void modified_ranking(int num_scores, score_t* scores) { + qsort(scores, num_scores, sizeof(score_t), rev_by_score); + int rank = 1; + for (int i = 0; i < num_scores; i++) { + int count = 0; + for (int j = i; j < num_scores && scores[i].score == scores[j].score; j++) + count++; + rank += count-1; + for (int j = i; j < num_scores && scores[i].score == scores[j].score; j++) + scores[j].rank = rank; + rank++; + i += count - 1; + } + qsort(scores, num_scores, sizeof(score_t), by_seq); +} + +void dense_ranking(int num_scores, score_t* scores) { + qsort(scores, num_scores, sizeof(score_t), rev_by_score); + int rank = 1; + for (int i = 0; i < num_scores; i++) { + int count = 0; + for (int j = i; j < num_scores && scores[i].score == scores[j].score; j++) { + count++; + scores[j].rank = rank; + } + rank++; + i += count - 1; + } + qsort(scores, num_scores, sizeof(score_t), by_seq); +} + + +int main(int argc, char* argv[]) { + if (argc < 2) return EXIT_FAILURE; + int num_scores = argc - 1; + score_t* scores = check_mem(calloc(num_scores, sizeof(score_t))); + for (int i = 0; i < num_scores; i++) { + scores[i].seq = i; + scores[i].score = atoi(argv[i + 1]); + } + + printf("Data: "); + for (int i = 0; i < num_scores; i++) { + printf("%d", scores[i].score); + if (i + 1 < num_scores) + printf(", "); + } + printf("\n"); + + standard_ranking(num_scores, scores); + printf("Standard ranking: "); + for (int i = 0; i < num_scores; i++) { + printf("%d", scores[i].rank); + if (i + 1 < num_scores) + printf(", "); + } + printf("\n"); + + modified_ranking(num_scores, scores); + printf("Modified ranking: "); + for (int i = 0; i < num_scores; i++) { + printf("%d", scores[i].rank); + if (i + 1 < num_scores) + printf(", "); + } + printf("\n"); + + dense_ranking(num_scores, scores); + printf("Dense ranking: "); + for (int i = 0; i < num_scores; i++) { + printf("%d", scores[i].rank); + if (i + 1 < num_scores) + printf(", "); + } + printf("\n"); + + free(scores); +} 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; +} diff --git a/challenge-009/paulo-custodio/t/test-1.yaml b/challenge-009/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..0e7966c514 --- /dev/null +++ b/challenge-009/paulo-custodio/t/test-1.yaml @@ -0,0 +1,25 @@ +- setup: + cleanup: + args: 1 + input: + output: 1 +- setup: + cleanup: + args: 2 + input: + output: 16 +- setup: + cleanup: + args: 3 + input: + output: 169 +- setup: + cleanup: + args: 4 + input: + output: 1024 +- setup: + cleanup: + args: 5 + input: + output: 12769 diff --git a/challenge-009/paulo-custodio/t/test-2.yaml b/challenge-009/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..06e3445fb4 --- /dev/null +++ b/challenge-009/paulo-custodio/t/test-2.yaml @@ -0,0 +1,9 @@ +- setup: + cleanup: + args: 2 3 1 4 2 2 1 0 + input: + output: | + |Data: 2, 3, 1, 4, 2, 2, 1, 0 + |Standard ranking: 3, 2, 6, 1, 3, 3, 6, 8 + |Modified ranking: 5, 2, 7, 1, 5, 5, 7, 8 + |Dense ranking: 3, 2, 4, 1, 3, 3, 4, 5 diff --git a/challenge-009/paulo-custodio/test.pl b/challenge-009/paulo-custodio/test.pl index c5275ed8f4..ba6c37260b 100644 --- a/challenge-009/paulo-custodio/test.pl +++ b/challenge-009/paulo-custodio/test.pl @@ -1,31 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; +#!/usr/bin/env perl +use Modern::Perl; use Test::More; - -is capture("perl perl/ch-1.pl 1"), "1\n"; -is capture("perl perl/ch-1.pl 2"), "16\n"; -is capture("perl perl/ch-1.pl 3"), "169\n"; -is capture("perl perl/ch-1.pl 4"), "1024\n"; -is capture("perl perl/ch-1.pl 5"), "12769\n"; - - -is capture("perl perl/ch-2.pl 2 3 1 4 2 2 1 0"), <<END; -Data: 2, 3, 1, 4, 2, 2, 1, 0 -Standard ranking: 3, 2, 6, 1, 3, 3, 6, 8 -Modified ranking: 5, 2, 7, 1, 5, 5, 7, 8 -Dense ranking: 3, 2, 4, 1, 3, 3, 4, 5 -END - - -done_testing; - - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \r\t]*\n/\n/g; - return $out; -} +require '../../challenge-001/paulo-custodio/test.pl'; |
