aboutsummaryrefslogtreecommitdiff
path: root/challenge-284
diff options
context:
space:
mode:
authorAndrew Schneider <atschneider@temple.edu>2024-09-01 17:02:30 -0400
committerAndrew Schneider <atschneider@temple.edu>2024-09-01 17:02:30 -0400
commit4550a7a995088d850cf6b3a8af56b6d3421c24c5 (patch)
tree9bc506e6287f1aab136f956686d130c15e089150 /challenge-284
parent62d6092a8104c471ac2859ba90e0c9f6aa579360 (diff)
downloadperlweeklychallenge-club-4550a7a995088d850cf6b3a8af56b6d3421c24c5.tar.gz
perlweeklychallenge-club-4550a7a995088d850cf6b3a8af56b6d3421c24c5.tar.bz2
perlweeklychallenge-club-4550a7a995088d850cf6b3a8af56b6d3421c24c5.zip
initial PWC 284 commit
Diffstat (limited to 'challenge-284')
-rw-r--r--challenge-284/atschneid/README.md12
-rw-r--r--challenge-284/atschneid/perl/ch-1.pl25
-rw-r--r--challenge-284/atschneid/perl/ch-2.pl36
-rw-r--r--challenge-284/atschneid/rust/ch-1.rs24
-rw-r--r--challenge-284/atschneid/rust/ch-2.rs31
5 files changed, 122 insertions, 6 deletions
diff --git a/challenge-284/atschneid/README.md b/challenge-284/atschneid/README.md
index e0ac3b1494..f2503cad2f 100644
--- a/challenge-284/atschneid/README.md
+++ b/challenge-284/atschneid/README.md
@@ -1,11 +1,11 @@
-# PWC 283
+# PWC 284
-**Challenge 283 solutions by Andrew Schneider**
+**Challenge 284 solutions by Andrew Schneider**
-[PWC 283](https://theweeklychallenge.org/blog/perl-weekly-challenge-283/)
+[PWC 284](https://theweeklychallenge.org/blog/perl-weekly-challenge-284/)
-No blog this week. I had high hopes for this week. In Perl I planned to iteratively refine solutions to each challenge. I also wanted to try solving them using OCaml. But I ran out of time for the first, and just floundered on the second idea. It was a busy week I suppose.
+I haven't had the motivation to say much here lately, but I also feel like I haven't had much to say. I suppose I've been busy with other stuff, and haven't been doing my usual programming language roulette, although, really, what's more important than the PWC?!
-I liked Dave Jacoby's remark a couple weeks back about not wanting to blog if he didn't have anything interesting/original to offer. Definitely nothing of that sort from me this week, ergo, no blog.
+I did see there's some tight competition between Ruby and Rust, hence my Rust contribution in support of my pick. I do have a soft spot in my heart for Ruby, so nothing personal of course.
-See you (maybe) next week!
+Thanks for the challenges!
diff --git a/challenge-284/atschneid/perl/ch-1.pl b/challenge-284/atschneid/perl/ch-1.pl
new file mode 100644
index 0000000000..84192a5007
--- /dev/null
+++ b/challenge-284/atschneid/perl/ch-1.pl
@@ -0,0 +1,25 @@
+use warnings;
+use strict;
+
+use v5.38;
+
+use List::Util qw( max );
+
+sub find_highest_lucky_num( @ints ) {
+ my %counts;
+ grep { ++$counts{ $_ } } @ints;
+ my @lucky_nums = grep { $counts{ $_ } == $_ } keys %counts;
+ if ( @lucky_nums ) {
+ return max @lucky_nums;
+ }
+ return -1;
+}
+
+my @inputs = (
+ [2, 2, 3, 4],
+ [1, 2, 2, 3, 3, 3],
+ [1, 1, 1, 3],
+ );
+for (@inputs) {
+ say join ( ', ', @$_ ) . ' => ' . join ( ', ', find_highest_lucky_num( @$_ ) );
+}
diff --git a/challenge-284/atschneid/perl/ch-2.pl b/challenge-284/atschneid/perl/ch-2.pl
new file mode 100644
index 0000000000..e84095961b
--- /dev/null
+++ b/challenge-284/atschneid/perl/ch-2.pl
@@ -0,0 +1,36 @@
+use warnings;
+use strict;
+
+use v5.38;
+
+use List::Util qw( max );
+
+sub relative_sort( $list1, $list2 ) {
+ my @list1 = @$list1;
+ my @list2 = @$list2;
+
+ my %ordering = map { ( $list2[ $_ ] => $_ ) } 0..$#list2;
+ return (
+ ( sort { $ordering{ $a } <=> $ordering{ $b } } grep { exists $ordering{ $_ } } @list1 ),
+ ( sort grep { !exists $ordering{ $_ } } @list1 )
+ );
+}
+
+my @inputs = (
+ [
+ [2, 3, 9, 3, 1, 4, 6, 7, 2, 8, 5],
+ [2, 1, 4, 3, 5, 6]
+ ],
+ [
+ [3, 3, 4, 6, 2, 4, 2, 1, 3],
+ [1, 3, 2]
+ ],
+ [
+ [3, 0, 5, 0, 2, 1, 4, 1, 1],
+ [1, 0, 3, 2]
+ ]
+ );
+for (@inputs) {
+ say join ( ', ', @{$_->[0]} ) . ' :: ' . join ( ', ', @{$_->[1]} ) . ' => ' . join ( ', ', relative_sort( $_->[0], $_->[1] ) );
+ say '';
+}
diff --git a/challenge-284/atschneid/rust/ch-1.rs b/challenge-284/atschneid/rust/ch-1.rs
new file mode 100644
index 0000000000..5d43bddd4a
--- /dev/null
+++ b/challenge-284/atschneid/rust/ch-1.rs
@@ -0,0 +1,24 @@
+use std::collections::HashMap;
+
+fn find_highest_lucky_num(ints: &[i64]) -> i64 {
+ let mut counts = HashMap::new();
+
+ for i in ints {
+ *counts.entry(*i).or_insert(0) += 1;
+ }
+
+ let luckies = counts.iter().filter(|t| t.0 == t.1).map(|t| t.0);
+ match luckies.max() {
+ Some(x) => *x,
+ None => -1,
+ }
+}
+
+fn main() {
+ let inputs = vec![vec![2, 2, 3, 4], vec![1, 2, 2, 3, 3, 3], vec![1, 1, 1, 3]];
+
+ for input in &inputs {
+ let lucky_num = find_highest_lucky_num(input);
+ println!("{:?} => {}", input, lucky_num);
+ }
+}
diff --git a/challenge-284/atschneid/rust/ch-2.rs b/challenge-284/atschneid/rust/ch-2.rs
new file mode 100644
index 0000000000..50b2cb7ef2
--- /dev/null
+++ b/challenge-284/atschneid/rust/ch-2.rs
@@ -0,0 +1,31 @@
+use std::collections::HashMap;
+
+fn relative_sort(list1: &[i64], list2: &[i64]) -> std::vec::Vec<i64> {
+ let mut ordering = HashMap::new();
+
+ for (idx, val) in list2.iter().enumerate() {
+ ordering.insert(val, idx);
+ }
+
+ let mut prefix: Vec<&i64> = list1.iter().filter(|e| ordering.contains_key(e)).collect();
+ prefix.sort_by(|a, b| ordering[a].cmp(&ordering[b]));
+
+ let mut suffix: Vec<&i64> = list1.iter().filter(|e| !ordering.contains_key(e)).collect();
+ suffix.sort();
+
+ prefix.append(&mut suffix);
+ prefix.into_iter().map(|r| *r).collect()
+}
+
+fn main() {
+ // let inputs = vec![vec![2, 2, 3, 4], vec![1, 2, 2, 3, 3, 3], vec![1, 1, 1, 3]];
+
+ // for input in &inputs {
+ // let lucky_num = find_highest_lucky_num( input );
+ // println!("{:?} => {}", input, lucky_num);
+ // }
+ let list1 = vec![1, 2, 3, 4, 6, 7, 11, 8];
+ let list2 = vec![5, 4, 3, 2];
+ let sorted = relative_sort(&list1, &list2);
+ println!("{:?}, {:?} => {:?}", list1, list2, sorted);
+}