diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-12-13 17:00:36 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-13 17:00:36 +0000 |
| commit | 4a625c93a6def20445291e4a72245c1bbbf48089 (patch) | |
| tree | ff327f7e430b23520431fc5fa2b497b6a1fdc319 | |
| parent | fefc7c358edfe567b1444cb88caed7738c4eb4fe (diff) | |
| parent | 6f0d501281b8c133f05fa5649209a1c0bbdc59e5 (diff) | |
| download | perlweeklychallenge-club-4a625c93a6def20445291e4a72245c1bbbf48089.tar.gz perlweeklychallenge-club-4a625c93a6def20445291e4a72245c1bbbf48089.tar.bz2 perlweeklychallenge-club-4a625c93a6def20445291e4a72245c1bbbf48089.zip | |
Merge pull request #7252 from pjcs00/wk195
Week 195 solutions.
| -rw-r--r-- | challenge-195/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-195/peter-campbell-smith/perl/ch-1.pl | 76 | ||||
| -rwxr-xr-x | challenge-195/peter-campbell-smith/perl/ch-2.pl | 47 |
3 files changed, 124 insertions, 0 deletions
diff --git a/challenge-195/peter-campbell-smith/blog.txt b/challenge-195/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..7aba44cf61 --- /dev/null +++ b/challenge-195/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +https://pjcs-pwc.blogspot.com/2022/12/some-numbers-are-special-and-others-are.html diff --git a/challenge-195/peter-campbell-smith/perl/ch-1.pl b/challenge-195/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..15bc7892f3 --- /dev/null +++ b/challenge-195/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,76 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2022-12-12 +# PWC 195 task 1 + +use v5.28; +use utf8; +use warnings; + +# You are given a positive integer, $n > 0. Write a script to print the count of all special integers between 1 and $n. +# An integer is special when all of its digits are unique. + +# Blog: https://pjcs-pwc.blogspot.com/2022/12/some-numbers-are-special-and-others-are.html + +my (@tests, $test, $count, $j, $digit, $start); +@tests = (15, 34, 77, 123, 1234, 12345, 1000000, 5000000); + +# loop over tests +say qq[\n--- METHOD 1 ---]; +for $test (@tests) { + + # loop over 1 to $n + $start = time; + $count = 0; + TEST: for $j (1 .. $test) { + + # check for 2 of each digit + for $digit (0..9) { + + # don't count if digit repeated + next TEST if $j =~ m|$digit.*$digit|; + } + + # is good + $count ++; + } + say qq[\nInput: $test\nOutput: $count (] . (time - $start) . qq[ secs)]; +} + +# loop over tests +say qq[\n--- METHOD 2 ---]; +for $test (@tests) { + + # loop over 1 to $n + $start = time; + $count = 0; + $j = 0; + + while (1) { + + $j ++; + last if $j > $test; + if ($j =~ m|(.+?)(0+)$|) { + $j = ($1 + 1) * 10 ** length($2) - 1 if unspecial($1); + } + $count ++ unless unspecial($j); + } + say qq[\nInput: $test\nOutput: $count (] . (time - $start) . qq[ secs)]; +} + +sub unspecial { + + # returns 0 if special, 1 if not + return 1 if $_[0] =~ m|1.*1|; + return 1 if $_[0] =~ m|2.*2|; + return 1 if $_[0] =~ m|3.*3|; + return 1 if $_[0] =~ m|4.*4|; + return 1 if $_[0] =~ m|5.*5|; + return 1 if $_[0] =~ m|6.*6|; + return 1 if $_[0] =~ m|7.*7|; + return 1 if $_[0] =~ m|8.*8|; + return 1 if $_[0] =~ m|9.*9|; + return 1 if $_[0] =~ m|0.*0|; + + return 0; +} diff --git a/challenge-195/peter-campbell-smith/perl/ch-2.pl b/challenge-195/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..015fff7b8f --- /dev/null +++ b/challenge-195/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2022-12-12 +# PWC 195 task 1 + +use v5.28; +use utf8; +use warnings; + +# You are given a list of numbers, @list. Write a script to find most frequent even numbers in the list. If +# you get more than one with the same frequency return the smallest. For all other case, return -1. + +# Blog: https://pjcs-pwc.blogspot.com/2022/12/some-numbers-are-special-and-others-are.html + +my (@tests, $test, @freq, $max_freq, $max_freq_no, $j, @list); + +@tests = ([1,1,2,6,2], [1,3,5,7], [6,4,4,6,1], [23, 46, 72, 15, 0, 33, 34, 72, 1, 99, 1000, 14], + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0], [1 .. 100], [1, 3, 5, 7, 9, 11, 13,15, 17, 19], [8, 8, 6, 6, 4, 4, 2, 2]); + +# loop over tests +for $test (@tests) { + + # initialise + $max_freq = 0; + $max_freq_no = -1; + @freq = (); + + # look at each member of input list, sorted numerically so that we see the smallest first + for $j (sort {$a <=> $b} @$test) { + + # ignore it if it is odd + next if $j & 1; + + # increment its frequency + $freq[$j] ++; + + # if more that the highest seen so far, remember it + if ($freq[$j] > $max_freq) { + $max_freq = $freq[$j]; + $max_freq_no = $j; + } + } + + # and show the result + say qq[\nInput: \@list = (] . join(', ', @$test) . qq[)\nOutput: $max_freq_no]; + +} |
