I’ve been doing the Weekly
Challenges. The
latest
involved sequence generation and sports score analysis. (Note that
this ends today.) Only a few languages this time, since I was doing
other things over Christmas.
Task 1: Kolakoski Sequence
You are given an integer, $int > 3.
Write a script to generate the Kolakoski Sequence of given length
$int and return the count of 1 in the generated sequence. Please
follow the wikipedia page for more informations.
This is a fiddly one, a sort of self-describing sequence: term N
(starting at 1) is the length of the Nth run of identical values, and
all values must be 1 or 2.
So given an initial 1, the first run must be of length 1, so the next
term is 2. That means the next run is of length 2, so so far we have
1, 2, 2, 1. The next run will also be of length 2, and it starts
with that last 1, so we have 1, 2, 2, 1, 1, 2. And so on.
Since this is always to a set length, I could use a fixed-length
array. Raku:
sub magicalstring($a) {
Initialise the sequence.
my @s = 0 xx ($a * 2);
@s[0] = 1;
Set last value, sequence index, and new entry position.
my $l = 1;
my $n = 0;
my $ic = 0;
We only need a entries in the sequence.
while ($ic < $a) {
If it's a run of 2, insert a value after the current one.
if (@s[$n] == 2) {
@s[$ic + 1] = $l;
$ic++;
}
Flip to the alternate value, and insert that.
$l = 3 - $l;
$ic++;
@s[$ic] = $l;
Increment the sequence index.
$n++;
}
Prune the list (in case any extra values were generated) and count 1s.
splice @s, $a;
@s.grep({$_ == 1}).elems;
}
Task 2: Who Wins
It's NFL playoff time. Since the 2020 season, seven teams from
each of the league's two conferences (AFC and NFC) qualify for
the playoffs based on regular season winning percentage, with a
tie-breaking procedure if required. The top team in each conference
receives a first-round bye, automatically advancing to the second
round.
The following games are played. Some times the games are played in a
different order. To make things easier, assume the order is always
as below.
Week 1: Wild card playoffs
- Team 1 gets a bye
- Game 1: Team 2 hosts Team 7
- Game 2: Team 3 hosts Team 6
- Game 3: Team 4 hosts Team 5
- Week 2: Divisional playoffs
- Game 4: Team 1 hosts the third seeded winner from the previous week.
- Game 5: The highest seeded winner from the previous week hosts the second seeded winner.
- Week 3: Conference final
- Game 6: The highest seeded winner from the previous week hosts
the other wommer
You are given a six character string containing only
H (home) and
A away which has the winner of each game. Which two teams competed
in the the conference final and who won?
I built this one with a hashset for each round's participants. In
Rust:
fn whowins(a: &str) -> String {
let mut round2 = HashSet::new();
let mut round3 = HashSet::new();
for (i, c) in a.chars().enumerate() {
match i {
Game 1, team 2 or team 7.
0 => {
if c == 'H' {
round2.insert(2);
} else {
round2.insert(7);
}
}
Game 2, team 3 or team 6.
1 => {
if c == 'H' {
round2.insert(3);
} else {
round2.insert(6);
}
}
Game 3, team 4 or team 5.
2 => {
if c == 'H' {
round2.insert(4);
} else {
round2.insert(5);
}
}
Game 4, team 1 or highest-numbered participant in round 2.
3 => {
if c == 'H' {
round3.insert(1);
} else {
round3.insert(*round2.iter().max().unwrap());
}
}
Game 5, one of the other participants in round 2.
4 => {
let mut k = round2.iter().collect::<Vec<_>>();
k.sort();
let _ = k.pop();
if c == 'H' {
round3.insert(*k[0]);
} else {
round3.insert(*k[1]);
}
}
Game 6, one of the participants in round 3.
5 => {
let mut k = round3.iter().collect::<Vec<_>>();
k.sort();
if c == 'H' {
return format!("Team {} defeated Team {}", k[0], k[1]);
} else {
return format!("Team {} defeated Team {}", k[1], k[0]);
}
}
_ => {}
};
}
String::new()
}
Full code on
github.