Task 1: Distinct Digits Square
Write a script to find the first square number with at least 5 distinct digits.
Output
The first square number with at least 5 distinct digits is: 12769
Logic
We start with $num = 1$ and continuously square it. For each square number, we split it into its individual digit characters, convert them to a set of unique digits, and check if the number of unique digits is $\ge 5$. Since we search incrementally from 1, the first one we find is guaranteed to be the smallest/first square number satisfying this condition.
Perl Solution
ch-1.pl
my $num = 1;
while (1) {
my $square = $num * $num;
my @digits = split( //, $square );
my %unique_digits;
foreach my $digit (@digits) {
$unique_digits{$digit} = 1;
}
if ( scalar( keys %unique_digits ) >= 5 ) {
print "The first square number with at least 5 distinct digits is: $square\n";
last;
}
$num++;
}
Python Solution
ch-1.py
num = 1
while True:
square = num * num
digits = set(str(square))
if len(digits) >= 5:
print(f"The first square number with at least 5 distinct digits is: {square}")
break
num += 1
Task 2: Ranking Methods
Write a script to implement three common ranking methods: Standard, Modified, and Dense ranking.
Example Output
Scores: 10, 20, 30, 20, 40, 50, 10, 30
Standard Ranking: 7, 5, 3, 5, 2, 1, 7, 3
Modified Ranking: 8, 6, 4, 6, 2, 1, 8, 4
Dense Ranking: 5, 4, 3, 4, 2, 1, 5, 3
Logic
We implement three distinct ranking models for list matching:
- Standard (1224) Ranking: Items with equal scores get the same rank, and a gap is left in the following ranks (equal to the number of duplicates).
- Modified (1334) Ranking: Equal items receive the highest potential rank within the group (equal to the last duplicate index).
- Dense (1223) Ranking: Equal items receive the same rank, and the next rank is simply incremented by 1 (no gaps).
Perl Solution
ch-2.pl
sub standard_ranking {
my @scores = @_;
my @sorted_scores = sort { $b <=> $a } @scores;
my @ranks;
my $rank = 1;
foreach my $score (@sorted_scores) {
push @ranks, $rank
if ( !$ranks[-1] || $score != $sorted_scores[ -$ranks[-1] ] );
$rank++;
}
my @standard_ranks = map {
my $index = $_;
scalar( grep { $sorted_scores[$_] > $sorted_scores[$index] }
0 .. $#sorted_scores ) + $ranks[$_]
} 0 .. $#scores;
return @standard_ranks;
}
sub modified_ranking {
my @scores = @_;
my @sorted_scores = sort { $b <=> $a } @scores;
my %ranks;
my $rank = 1;
foreach my $score (@sorted_scores) {
$ranks{$score} = $rank if !$ranks{$score};
$rank++ unless $score == $sorted_scores[ -$ranks{$score} ];
}
my @modified_ranks = map { $ranks{$_} } @scores;
return @modified_ranks;
}
sub dense_ranking {
my @scores = @_;
my @sorted_scores = sort { $b <=> $a } @scores;
my %ranks;
my $rank = 1;
foreach my $score (@sorted_scores) {
$ranks{$score} = $rank unless $ranks{$score};
$rank++ unless $score == $sorted_scores[ -$ranks{$score} ];
}
my @dense_ranks = map { $ranks{$_} } @scores;
return @dense_ranks;
}
Python Solution
ch-2.py
def standard_ranking(scores):
sorted_scores = sorted(scores, reverse=True)
ranks = [sorted_scores.index(score) + 1 for score in scores]
return ranks
def modified_ranking(scores):
sorted_scores = sorted(scores, reverse=True)
ranks = []
rank = 1
for score in sorted_scores:
if score not in ranks:
rank = sorted_scores.index(score) + 1
ranks.append(rank)
return [ranks[sorted_scores.index(score)] for score in scores]
def dense_ranking(scores):
sorted_scores = sorted(scores, reverse=True)
ranks = [1]
for i in range(1, len(sorted_scores)):
if sorted_scores[i] == sorted_scores[i - 1]:
ranks.append(ranks[i - 1])
else:
ranks.append(ranks[i - 1] + 1)
return [ranks[sorted_scores.index(score)] for score in scores]