aboutsummaryrefslogtreecommitdiff
path: root/challenge-198
diff options
context:
space:
mode:
authorDavid Ferrone <zapwai@gmail.com>2023-01-02 10:49:35 -0500
committerDavid Ferrone <zapwai@gmail.com>2023-01-02 10:49:35 -0500
commitb976b3f467032bcb75335b41224e1f22fad3e79c (patch)
tree2e0dfbba659d00504907a8e70819788474305db6 /challenge-198
parent00cc4d779f1fdbdd6a22b1c029f2fbdc857ff132 (diff)
downloadperlweeklychallenge-club-b976b3f467032bcb75335b41224e1f22fad3e79c.tar.gz
perlweeklychallenge-club-b976b3f467032bcb75335b41224e1f22fad3e79c.tar.bz2
perlweeklychallenge-club-b976b3f467032bcb75335b41224e1f22fad3e79c.zip
Week 198
Diffstat (limited to 'challenge-198')
-rw-r--r--challenge-198/zapwai/perl/ch-1.pl35
-rw-r--r--challenge-198/zapwai/perl/ch-2.pl4
-rw-r--r--challenge-198/zapwai/rust/ch-1.rs23
-rw-r--r--challenge-198/zapwai/rust/ch-2.rs19
4 files changed, 81 insertions, 0 deletions
diff --git a/challenge-198/zapwai/perl/ch-1.pl b/challenge-198/zapwai/perl/ch-1.pl
new file mode 100644
index 0000000000..1d75a5b9ca
--- /dev/null
+++ b/challenge-198/zapwai/perl/ch-1.pl
@@ -0,0 +1,35 @@
+my @list1 = (5,8,2,1,11,17,20,12,15);
+my @list2 = ();
+my @list3 = (2,5,1);
+foreach my $ref (\@list1, \@list2, \@list3) {
+ my @list = @$ref;
+ print "Input: \@list = (".join(",",@list).")\n";
+ @list = sort { $a <=> $b } @list;
+ my $gap = 0;
+ for (0 .. $#list - 1) {
+ my $diff = $list[$_ + 1] - $list[$_];
+ $gap = $diff if ($gap < $diff);
+ }
+ my @index;
+ for (0 .. $#list - 1) {
+ my $diff = $list[$_ + 1] - $list[$_];
+ push @index, $_ if ($diff == $gap);
+ }
+ print "Output: ", (scalar @index)."\n";
+ if (scalar @index > 1) {
+ print "\nSince the sorted list (".
+ join(",",@list).") contains ".(scalar @index)." such pairs.\n";
+ my @lists;
+ foreach (@index) {
+ my $str = "(".$list[$_].", ".$list[$_+1].")";
+ push @lists, $str;
+ }
+ my $last = pop @lists;
+ my $output = join(", ", @lists)." and $last";
+ print $output."\n";
+ } elsif (scalar @index == 1) {
+ print "\n(".join(",",@list).") contains ";
+ print "(".$list[$index[0]].", ".$list[$index[0]+1].")\n";
+ }
+ print "\n";
+}
diff --git a/challenge-198/zapwai/perl/ch-2.pl b/challenge-198/zapwai/perl/ch-2.pl
new file mode 100644
index 0000000000..b299714c65
--- /dev/null
+++ b/challenge-198/zapwai/perl/ch-2.pl
@@ -0,0 +1,4 @@
+my $n = $ARGV[0] || 10;
+my @cnt = grep { $_ == 1 } map { 1 if &is_prime } (2 .. $n);
+print "Input: \$n = $n\nOutput: ".($#cnt + 1)."\n";
+sub is_prime { for my $i (2 .. sqrt($_)) { return 0 if ($_ % $i == 0) } 1}
diff --git a/challenge-198/zapwai/rust/ch-1.rs b/challenge-198/zapwai/rust/ch-1.rs
new file mode 100644
index 0000000000..a60da9a555
--- /dev/null
+++ b/challenge-198/zapwai/rust/ch-1.rs
@@ -0,0 +1,23 @@
+fn main() {
+ // let mut list = [1,5,1,1,6,4];
+ let mut list = vec![1,3,2,2,3,1];
+ println!("Input: {:?}",list);
+ list.sort();
+ let mut gap = 0;
+ for i in 1 .. list.len()-1 {
+ let diff = list[i+1] - list[i];
+ if gap < diff {
+ gap = diff;
+ }
+ }
+ println!("The sorted list is {:?}",list);
+ print!("Which contains: ");
+ for i in 1 .. list.len()-1 {
+ let diff = list[i+1] - list[i];
+ if diff == gap {
+ print!("({},{}) ",list[i],list[i+1]);
+ }
+ }
+ println!("");
+
+}
diff --git a/challenge-198/zapwai/rust/ch-2.rs b/challenge-198/zapwai/rust/ch-2.rs
new file mode 100644
index 0000000000..342266433a
--- /dev/null
+++ b/challenge-198/zapwai/rust/ch-2.rs
@@ -0,0 +1,19 @@
+use std::io;
+fn main() {
+ println!("Please enter a value for n: ");
+ let mut n = String::new();
+ io::stdin() .read_line(&mut n);
+ let num = n.trim().parse::<i32>().unwrap();
+ let mut cnt = 0;
+ for i in 2 ..= num {
+ if is_prime(i) { cnt += 1 }
+ }
+ println!("Input: {num}");
+ println!("Output: {cnt}");
+}
+fn is_prime( n: i32 ) -> bool {
+ for i in 2 ..= n/2 {
+ if n % i == 0 { return false }
+ }
+ return true;
+}