aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrage311 <rage_311@hotmail.com>2019-11-10 10:07:32 -0700
committerrage311 <rage_311@hotmail.com>2019-11-10 10:07:32 -0700
commit3bcb22585e34cd3743869a5c459716b2c41aba3b (patch)
tree0bd95677ab08217f172ee1990e27da3b8e2c24b2
parent11a399aca6169d8ca634254d48a99328fffba237 (diff)
downloadperlweeklychallenge-club-3bcb22585e34cd3743869a5c459716b2c41aba3b.tar.gz
perlweeklychallenge-club-3bcb22585e34cd3743869a5c459716b2c41aba3b.tar.bz2
perlweeklychallenge-club-3bcb22585e34cd3743869a5c459716b2c41aba3b.zip
Added challenge-033 files
-rw-r--r--challenge-033/rage311/perl5/ch-1.pl74
-rw-r--r--challenge-033/rage311/perl5/ch-2.pl29
-rw-r--r--challenge-033/rage311/perl5/example.txt1
-rw-r--r--challenge-033/rage311/rust/ch-1.rs92
-rw-r--r--challenge-033/rage311/rust/ch-2.rs47
5 files changed, 243 insertions, 0 deletions
diff --git a/challenge-033/rage311/perl5/ch-1.pl b/challenge-033/rage311/perl5/ch-1.pl
new file mode 100644
index 0000000000..178b159a1f
--- /dev/null
+++ b/challenge-033/rage311/perl5/ch-1.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/env perl
+
+use 5.030;
+use warnings;
+use strict;
+
+undef $/;
+
+for my $file (@ARGV) {
+ warn "$!" and next unless open my $fh, '<', $file;
+ my %count;
+ $count{$_}++ for grep { /[a-z]/ } map { lc } split //, <$fh>;
+ say $file;
+ say "$_: $count{$_}" for sort keys %count;
+ print "\n";
+}
+
+
+__DATA__
+
+Output:
+
+ch-2.pl
+a: 7
+b: 3
+c: 1
+d: 3
+e: 11
+f: 5
+g: 1
+h: 1
+i: 10
+j: 1
+l: 7
+m: 4
+n: 6
+o: 4
+p: 4
+r: 8
+s: 15
+t: 11
+u: 14
+v: 1
+w: 1
+y: 5
+
+example.txt
+a: 1
+b: 1
+c: 1
+d: 1
+e: 3
+f: 1
+g: 1
+h: 2
+i: 1
+j: 1
+k: 1
+l: 1
+m: 1
+n: 1
+o: 4
+p: 1
+q: 1
+r: 2
+s: 1
+t: 2
+u: 2
+v: 1
+w: 1
+x: 1
+y: 1
+z: 1
+
diff --git a/challenge-033/rage311/perl5/ch-2.pl b/challenge-033/rage311/perl5/ch-2.pl
new file mode 100644
index 0000000000..caa1750461
--- /dev/null
+++ b/challenge-033/rage311/perl5/ch-2.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+
+use 5.030;
+use warnings;
+use strict;
+
+my @results = [ undef, 1..11 ];
+for my $i (1..11) {
+ push @results, [ $i, map { $_ >= $i ? $_ * $i : undef } 1..11 ]
+}
+
+say for map { join ' ', map { sprintf '%3s', $_ // '' } @$_ } @results;
+
+__DATA__
+Output:
+
+ 1 2 3 4 5 6 7 8 9 10 11
+ 1 1 2 3 4 5 6 7 8 9 10 11
+ 2 4 6 8 10 12 14 16 18 20 22
+ 3 9 12 15 18 21 24 27 30 33
+ 4 16 20 24 28 32 36 40 44
+ 5 25 30 35 40 45 50 55
+ 6 36 42 48 54 60 66
+ 7 49 56 63 70 77
+ 8 64 72 80 88
+ 9 81 90 99
+ 10 100 110
+ 11 121
+
diff --git a/challenge-033/rage311/perl5/example.txt b/challenge-033/rage311/perl5/example.txt
new file mode 100644
index 0000000000..2fe6575e76
--- /dev/null
+++ b/challenge-033/rage311/perl5/example.txt
@@ -0,0 +1 @@
+The quick brown fox jumps over the lazy dog.
diff --git a/challenge-033/rage311/rust/ch-1.rs b/challenge-033/rage311/rust/ch-1.rs
new file mode 100644
index 0000000000..dd69db1c85
--- /dev/null
+++ b/challenge-033/rage311/rust/ch-1.rs
@@ -0,0 +1,92 @@
+use std::collections::BTreeMap;
+use std::env;
+use std::fs;
+use std::path::PathBuf;
+use std::process::exit;
+
+fn main() {
+ let args: Vec<String> = env::args().collect();
+ if args.len() < 2 {
+ eprintln!("Error: need filename arguments");
+ exit(1);
+ };
+
+ args[1..].iter()
+ .map(|file| (file, fs::read_to_string(PathBuf::from(file))) )
+ .filter(|(file, content)| match content {
+ Ok(_) => true,
+ Err(e) => {
+ eprintln!("{}: ({})", e, file);
+ false
+ }
+ })
+ .for_each(|(file, content)| {
+ let input = content.unwrap();
+
+ let mut found: BTreeMap<char, u16> = BTreeMap::new();
+
+ input.to_lowercase()
+ .chars()
+ .filter(|&c| c >= 'a' && c <= 'z')
+ .for_each(|c| *(found.entry(c).or_insert(0)) += 1 );
+
+ println!("{}", file);
+ found.keys()
+ .for_each(|key|
+ println!("{}: {}", key, found.get(&key).unwrap())
+ );
+ println!();
+ });
+}
+
+/* $ ./ch-1 ch-1.rs ch-2.rs
+ * Output:
+ *
+ * ch-1.rs
+a: 26
+b: 5
+c: 22
+d: 11
+e: 64
+f: 25
+g: 8
+h: 9
+i: 26
+k: 5
+l: 25
+m: 10
+n: 40
+o: 22
+p: 17
+r: 38
+s: 27
+t: 47
+u: 19
+v: 3
+w: 4
+x: 2
+y: 5
+z: 1
+
+ch-2.rs
+a: 7
+c: 15
+d: 2
+e: 25
+f: 6
+g: 2
+h: 5
+i: 16
+j: 4
+l: 11
+m: 12
+n: 20
+o: 28
+p: 12
+r: 19
+s: 15
+t: 25
+u: 13
+v: 6
+w: 5
+*/
diff --git a/challenge-033/rage311/rust/ch-2.rs b/challenge-033/rage311/rust/ch-2.rs
new file mode 100644
index 0000000000..72aed2ad4c
--- /dev/null
+++ b/challenge-033/rage311/rust/ch-2.rs
@@ -0,0 +1,47 @@
+fn main() {
+ let mut results: Vec<Vec<Option<u8>>> = vec![(0..12)
+ .map(|n| match n {
+ 0 => None,
+ _ => Some(n),
+ })
+ .collect()];
+
+ for i in 1..12 {
+ let mut row: Vec<Option<u8>> = vec![Some(i)];
+ for j in 1..12 {
+ row.push(if j >= i { Some(i * j) } else { None });
+ }
+ results.push(row);
+ }
+
+ results.iter().for_each(|row| {
+ println!(
+ "{}",
+ row.iter()
+ .map(|product| format!(
+ "{:>3}",
+ match product {
+ Some(p) => p.to_string(),
+ None => "".to_string(),
+ }
+ ))
+ .collect::<Vec<_>>()
+ .join(" ")
+ )
+ });
+}
+
+/* Output:
+ 1 2 3 4 5 6 7 8 9 10 11
+ 1 1 2 3 4 5 6 7 8 9 10 11
+ 2 4 6 8 10 12 14 16 18 20 22
+ 3 9 12 15 18 21 24 27 30 33
+ 4 16 20 24 28 32 36 40 44
+ 5 25 30 35 40 45 50 55
+ 6 36 42 48 54 60 66
+ 7 49 56 63 70 77
+ 8 64 72 80 88
+ 9 81 90 99
+ 10 100 110
+ 11 121
+*/