diff options
63 files changed, 2340 insertions, 1307 deletions
diff --git a/challenge-007/paulo-custodio/perl/ch-1.pl b/challenge-007/paulo-custodio/perl/ch-1.pl index 77631cbc38..d60477d7fa 100644 --- a/challenge-007/paulo-custodio/perl/ch-1.pl +++ b/challenge-007/paulo-custodio/perl/ch-1.pl @@ -19,9 +19,9 @@ sub solve { } sub sum { - my($sum, @a) = @_; - $sum += $_ for @a; - return $sum; + my($sum, @a) = @_; + $sum += $_ for @a; + return $sum; } diff --git a/challenge-008/paulo-custodio/perl/ch-2.pl b/challenge-008/paulo-custodio/perl/ch-2.pl index 3dd165177d..39d44bd75a 100644 --- a/challenge-008/paulo-custodio/perl/ch-2.pl +++ b/challenge-008/paulo-custodio/perl/ch-2.pl @@ -20,11 +20,11 @@ sub center { } sub max { - my($max, @a) = @_; - for (@a) { - $max = $_ if $max < $_; - } - return $max; + my($max, @a) = @_; + for (@a) { + $max = $_ if $max < $_; + } + return $max; } diff --git a/challenge-010/paulo-custodio/perl/ch-2.pl b/challenge-010/paulo-custodio/perl/ch-2.pl index 22b9340025..85ea6e9281 100644 --- a/challenge-010/paulo-custodio/perl/ch-2.pl +++ b/challenge-010/paulo-custodio/perl/ch-2.pl @@ -85,19 +85,19 @@ sub jaro_winkler_distance { } sub min { - my($min, @a) = @_; - for (@a) { - $min = $_ if $min > $_; - } - return $min; + my($min, @a) = @_; + for (@a) { + $min = $_ if $min > $_; + } + return $min; } sub max { - my($max, @a) = @_; - for (@a) { - $max = $_ if $max < $_; - } - return $max; + my($max, @a) = @_; + for (@a) { + $max = $_ if $max < $_; + } + return $max; } diff --git a/challenge-016/stuart-little/haskell/ch-1.hs b/challenge-016/stuart-little/haskell/ch-1.hs new file mode 100755 index 0000000000..d6127006c9 --- /dev/null +++ b/challenge-016/stuart-little/haskell/ch-1.hs @@ -0,0 +1,6 @@ +#!/usr/bin/env runghc + +-- run <script> + +main = do + print $ let top = head $ dropWhile (\n -> (div (n*(n+1)) 2) < 100) [1..] in max (top-1) (100-(div ((top-1)*top) 2)) diff --git a/challenge-016/stuart-little/haskell/ch-2.hs b/challenge-016/stuart-little/haskell/ch-2.hs new file mode 100755 index 0000000000..90017bd3b1 --- /dev/null +++ b/challenge-016/stuart-little/haskell/ch-2.hs @@ -0,0 +1,43 @@ +#!/usr/bin/env runghc + +-- run <script> <bitcoin address> + +import Control.Monad (liftM) +import Crypto.Hash.SHA256 (hash) +import Data.ByteString (ByteString) +import Data.Digits (digits,unDigits) +import Data.List ((\\),elemIndex) +import System.Environment (getArgs) +import qualified Data.ByteString as B + +digs :: String +digs = ['1'..'9'] ++ (['A'..'Z']\\"IO") ++ (['a'..'z']\\"l") + +dec58 :: String -> Integer +dec58 = maybe (error "Invalid chars") id . + liftM (unDigits 58) . + traverse ((liftM toInteger) . (flip elemIndex digs)) + +intToB :: Integer -> ByteString +intToB = B.pack . map fromInteger . digits 256 + +validCs :: ByteString -> Bool +validCs address = + let (value, checksum) = B.splitAt 21 $ leftPad address + in and $ B.zipWith (==) checksum $ hash $ hash $ value + where + leftPad bs = B.concat [B.replicate (25 - B.length bs) 0, bs] + +valid :: String -> String +valid btc + |B.length bs > 25 = error "Too long" + |B.length bs < 4 = error "Too short" + |not $ validCs bs = error "Checksum fail" + |otherwise = "Valid" where + bs = intToB $ dec58 btc + +main = do + btc <- getArgs >>= return . head + putStrLn $ valid btc + +-- ref: https://rosettacode.org/wiki/Bitcoin/address_validation#Haskell diff --git a/challenge-018/paulo-custodio/perl/ch-1.pl b/challenge-018/paulo-custodio/perl/ch-1.pl index 46faec62ef..b79fbe788b 100644 --- a/challenge-018/paulo-custodio/perl/ch-1.pl +++ b/challenge-018/paulo-custodio/perl/ch-1.pl @@ -43,7 +43,7 @@ sub longest_substr { } sub all { - my($sub, @a) = @_; - for (@a) { return if !$sub->($_); } - return 1; + my($sub, @a) = @_; + for (@a) { return if !$sub->($_); } + return 1; } diff --git a/challenge-026/paulo-custodio/perl/ch-2.pl b/challenge-026/paulo-custodio/perl/ch-2.pl index b3a43ffec7..e2a27e4736 100644 --- a/challenge-026/paulo-custodio/perl/ch-2.pl +++ b/challenge-026/paulo-custodio/perl/ch-2.pl @@ -32,7 +32,7 @@ sub mean { } sub sum { - my($sum, @a) = @_; - $sum += $_ for @a; - return $sum; + my($sum, @a) = @_; + $sum += $_ for @a; + return $sum; } diff --git a/challenge-067/cheok-yin-fung/smalltalk/ch-2.st b/challenge-067/cheok-yin-fung/smalltalk/ch-2.st new file mode 100644 index 0000000000..0d2ce1f971 --- /dev/null +++ b/challenge-067/cheok-yin-fung/smalltalk/ch-2.st @@ -0,0 +1,41 @@ +"GNU Smalltalk 3.2.5" +"The Weekly Challenge 067 Task 2" +"Letter Phone" +"Usage: gst -S ch-2.st" +"written on 27th Jan 2021" + +'Integers composed of the digit 2 to 9: ' printNl. +myI := stdin nextLine. + +board := Dictionary new. +board at: $2 put:#($A $B $C). +board at: $3 put:#($D $E $F). +board at: $4 put:#($G $H $I). +board at: $5 put:#($J $K $L). +board at: $6 put:#($M $N $O). +board at: $7 put:#($P $Q $R $S). +board at: $8 put:#($T $U $V). +board at: $9 put:#($W $X $Y $Z). + +mySet := Set new. +myNewSet := Set new. +myNewSet add: ''. + +1 to: (myI size) do: + [:index| + c := myI at: index. + mySet := myNewSet copy. + myNewSet empty. + (board at: c) do: [ + :each | + mySet do: [ + :term| + s := (term, each displayString). + myNewSet add: s. + ] + ] + ]. + +myNewSet printNl. + +ObjectMemory quit. diff --git a/challenge-076/paulo-custodio/perl/ch-1.pl b/challenge-076/paulo-custodio/perl/ch-1.pl index 3e8a365725..a3adbf928e 100644 --- a/challenge-076/paulo-custodio/perl/ch-1.pl +++ b/challenge-076/paulo-custodio/perl/ch-1.pl @@ -95,7 +95,7 @@ sub find_set { } sub sum { - my($sum, @a) = @_; - $sum += $_ for @a; - return $sum; + my($sum, @a) = @_; + $sum += $_ for @a; + return $sum; } diff --git a/challenge-077/paulo-custodio/perl/ch-1.pl b/challenge-077/paulo-custodio/perl/ch-1.pl index 13c7ff09ec..7939010631 100644 --- a/challenge-077/paulo-custodio/perl/ch-1.pl +++ b/challenge-077/paulo-custodio/perl/ch-1.pl @@ -60,7 +60,7 @@ sub compute_fib { } sub sum { - my($sum, @a) = @_; - $sum += $_ for @a; - return $sum; + my($sum, @a) = @_; + $sum += $_ for @a; + return $sum; } diff --git a/challenge-079/paulo-custodio/perl/ch-1.pl b/challenge-079/paulo-custodio/perl/ch-1.pl index c789f38b70..dba39d1251 100644 --- a/challenge-079/paulo-custodio/perl/ch-1.pl +++ b/challenge-079/paulo-custodio/perl/ch-1.pl @@ -71,7 +71,7 @@ sub bit_count { } sub sum { - my($sum, @a) = @_; - $sum += $_ for @a; - return $sum; + my($sum, @a) = @_; + $sum += $_ for @a; + return $sum; } diff --git a/challenge-079/paulo-custodio/perl/ch-2.pl b/challenge-079/paulo-custodio/perl/ch-2.pl index f9f75bcad4..1c4248898a 100644 --- a/challenge-079/paulo-custodio/perl/ch-2.pl +++ b/challenge-079/paulo-custodio/perl/ch-2.pl @@ -72,15 +72,15 @@ sub draw_hist { } sub max { - my($max, @a) = @_; - for (@a) { - $max = $_ if $max < $_; - } - return $max; + my($max, @a) = @_; + for (@a) { + $max = $_ if $max < $_; + } + return $max; } sub sum { - my($sum, @a) = @_; - $sum += $_ for @a; - return $sum; + my($sum, @a) = @_; + $sum += $_ for @a; + return $sum; } diff --git a/challenge-080/paulo-custodio/perl/ch-2.pl b/challenge-080/paulo-custodio/perl/ch-2.pl index 6a87c0e855..fffbdd9ba1 100644 --- a/challenge-080/paulo-custodio/perl/ch-2.pl +++ b/challenge-080/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 081 # -# TASK #2 › Count Candies +# TASK #2 › Count Candies # Submitted by: Mohammad S Anwar # You are given rankings of @N candidates. # @@ -41,7 +41,7 @@ sub candies { } sub sum { - my($sum, @a) = @_; - $sum += $_ for @a; - return $sum; + my($sum, @a) = @_; + $sum += $_ for @a; + return $sum; } diff --git a/challenge-095/cheok-yin-fung/smalltalk/ch-1.st b/challenge-095/cheok-yin-fung/smalltalk/ch-1.st new file mode 100644 index 0000000000..68ea37ecf7 --- /dev/null +++ b/challenge-095/cheok-yin-fung/smalltalk/ch-1.st @@ -0,0 +1,10 @@ +"GNU Smalltalk 3.2.5" +"The Weekly Challenge 096 Task 1 Palindrome Number" +"Usage: gst -S ch-1.st" +"written on 27th Jan 2021" + +myN := stdin nextLine. +(myN reverse = myN) ifTrue: ['1' printNl.] + ifFalse: ['0' printNl.]. + +ObjectMemory quit. diff --git a/challenge-095/paulo-custodio/perl/ch-2.pl b/challenge-095/paulo-custodio/perl/ch-2.pl index c61c03f598..b3eace4cb3 100644 --- a/challenge-095/paulo-custodio/perl/ch-2.pl +++ b/challenge-095/paulo-custodio/perl/ch-2.pl @@ -33,14 +33,14 @@ use 5.030; sub pop { my($self) = @_; pop @$self; } sub top { my($self) = @_; return $self->[-1]; } sub min { my($self) = @_; return min_(@$self); } - - sub min_ { - my($min, @a) = @_; - for (@a) { - $min = $_ if $min > $_; - } - return $min; - } + + sub min_ { + my($min, @a) = @_; + for (@a) { + $min = $_ if $min > $_; + } + return $min; + } } my $stack = Stack->new; diff --git a/challenge-096/cheok-yin-fung/smalltalk/ch-1.st b/challenge-096/cheok-yin-fung/smalltalk/ch-1.st new file mode 100644 index 0000000000..fabe3907d0 --- /dev/null +++ b/challenge-096/cheok-yin-fung/smalltalk/ch-1.st @@ -0,0 +1,21 @@ +"GNU Smalltalk 3.2.5" +"The Weekly Challenge 096 Task 1" +"Reverse Words" +"Usage: gst -S ch-1.st" +"written on 27th Jan 2021" + +'String going to be modified: ' printNl. +s := stdin nextLine. + + +"s := 'The Weekly Challenge'." +parts := s substrings. +parts := parts reverse copy. + +t := parts first. +parts := parts allButFirst. +parts do: [:each | t := (t,' ',each) ]. + +t printNl. + +ObjectMemory quit. diff --git a/challenge-096/paulo-custodio/perl/ch-2.pl b/challenge-096/paulo-custodio/perl/ch-2.pl index 4d8fefa243..f2733fc227 100644 --- a/challenge-096/paulo-custodio/perl/ch-2.pl +++ b/challenge-096/paulo-custodio/perl/ch-2.pl @@ -111,9 +111,9 @@ sub wag_fis_dist { } sub min { - my($min, @a) = @_; - for (@a) { - $min = $_ if $min > $_; - } - return $min; + my($min, @a) = @_; + for (@a) { + $min = $_ if $min > $_; + } + return $min; } diff --git a/challenge-096/paulo-custodio/perl/ch-2a.pl b/challenge-096/paulo-custodio/perl/ch-2a.pl index d3331a7a21..22294b7e03 100644 --- a/challenge-096/paulo-custodio/perl/ch-2a.pl +++ b/challenge-096/paulo-custodio/perl/ch-2a.pl @@ -59,9 +59,9 @@ sub lev_dist { } sub min { - my($min, @a) = @_; - for (@a) { - $min = $_ if $min > $_; - } - return $min; + my($min, @a) = @_; + for (@a) { + $min = $_ if $min > $_; + } + return $min; } diff --git a/challenge-097/abigail/README.md b/challenge-097/abigail/README.md index 9e5afd71ff..3427067f32 100644 --- a/challenge-097/abigail/README.md +++ b/challenge-097/abigail/README.md @@ -1,84 +1,98 @@ # Solution by Abigail -## [Reverse Words](https://perlweeklychallenge.org/blog/perl-weekly-challenge-096/#TASK1) +## [Ceasar Cipher](https://perlweeklychallenge.org/blog/perl-weekly-challenge-097/#TASK1) -You are given a string `$S`. +You are given string `$S` containing alphabets `A..Z` only and a number `$N`. -Write a script to reverse the order of words in the given string. -The string may contain leading/trailing spaces. The string may have -more than one space between words in the string. Print the result -without leading/trailing spaces and there should be only one space -between words. +Write a script to encrypt the given string `$S` using Caesar Cipher with +left shift of size `$N`. -### Examples -~~~~ -Input: $S = "The Weekly Challenge" -Output: "Challenge Weekly The" - -Input: $S = " Perl and Raku are part of the same family " -Output: "family same the of part are Raku and Perl" +### Example ~~~~ +Input: $S = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG", $N = 3 +Output: "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD" -### Notes +Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ +Cipher: XYZABCDEFGHIJKLMNOPQRSTUVW -The challenge isn't quite clear on whether we should output a number -(the minimal number of operations required), or the actual operations. -The examples show both -- but separated by a blank line. Previous -challenges typically use a blank line to separate the required output -from the explaination on why that it is the correct answer. +Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG +Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD +~~~~ -We're opting to only print the number of operations, not the actual -operations. +### Note +We will be reading the plain text from STDIN, and use an option (-s) +to indicate the left shift. ### Solutions * [AWK](awk/ch-1.awk) -* [Bash](sh/ch-1.sh) -* [C](c/ch-1.c) +* [Bash](bash/ch-1.sh) * [Lua](lua/ch-1.lua) +* [C](c/ch-1.c) * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) * [Ruby](ruby/ch-1.rb) ### Blog -[Perl Weekly Challenge 96: Reverse Words](https://wp.me/pcxd30-mj) -## [Edit Distance](https://perlweeklychallenge.org/blog/perl-weekly-challenge-096/#TASK2) +## [Binary Substrings](https://perlweeklychallenge.org/blog/perl-weekly-challenge-097/#TASK2) -You are given two strings `$S1` and `$S2`. +You are given a binary string `$B` and an integer `$S`. -Write a script to find out the minimum operations required to convert -`$S1` into `$S2`. The operations can be insert, remove or replace a -character. Please check out [Wikipedia -page](https://en.wikipedia.org/wiki/Edit_distance) for more information. +Write a script to split the binary string `$B` of size `$S` and then +find the minimum number of flips required to make it all the same. -### Example +### Examples +#### Example 1 ~~~~ -Input: $S1 = "kitten"; $S2 = "sitting" -Output: 3 +Input: $B = "101100101" $S = 3 +Output: 1 -Operation 1: replace 'k' with 's' -Operation 2: replace 'e' with 'i' -Operation 3: insert 'g' at the end +Binary Substrings: + "101": 0 flip + "100": 1 flip to make it "101" + "101": 0 flip ~~~~ +#### Example 2 ~~~~ -Input: $S1 = "sunday"; $S2 = "monday" +Input $B = "10110111", $S = 4 Output: 2 -Operation 1: replace 's' with 'm' -Operation 2: replace 'u' with 'o' +Binary Substrings: + "1011": 0 flip + "0111": 2 flips to make it "1011" ~~~~ +### Notes +We will be reading the strings from STDIN. The number of sections +will be passed in as an option: -s SECTIONS. + +To calculate the mininim number of flips required, note that we +can calculate the number of flips for each position independently; +that is, flipping a bit at position $i doesn't influence how the +number of flips required for position $j, if $i != $j. + +For each position $i, we either need to flip all the 0s to 1s, +or all the 1s to 0s. To minimize the number of flips, we count +the number of 0s in each position, and compare that with the +number of 1s in that position. Then we take the minimum of 0s +and 1s, and sum this for all positions. + +There is no need to split the input string into chunks, +we can leave it as is. The $i-th character of the $j-th section +is as position $j * $s_len + $i, where $s_len is the length +of a section. + ### Solutions * [AWK](awk/ch-2.awk) +* [Bash](bash/ch-2.sh) * [C](c/ch-2.c) * [Lua](lua/ch-2.lua) -* [Node.js](node/ch-2.js) +* [Node](node/ch-2.js) * [Perl](perl/ch-2.pl) * [Python](python/ch-2.py) * [Ruby](ruby/ch-2.rb) ### Blog -[Perl Weekly Challenge 96: Edit Distance](https://wp.me/pcxd30-n7) diff --git a/challenge-097/abigail/awk/ch-1.awk b/challenge-097/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..a33507ee33 --- /dev/null +++ b/challenge-097/abigail/awk/ch-1.awk @@ -0,0 +1,53 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk -s SHIFT < input-file +# + |
