diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2021-01-26 16:13:59 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2021-01-26 16:13:59 -0500 |
| commit | 1ed6ac427cbce65a68f7f5704fa33bf892f1c1fd (patch) | |
| tree | d2aa0002dd04845071ea4454d9520b474b960de3 /challenge-097 | |
| parent | 3c7bc8e0bb03a8177808ef3223099f2e0fc07416 (diff) | |
| parent | c34bb5d7bd7fce08e8311a0f527ce7fbd69e4dae (diff) | |
| download | perlweeklychallenge-club-1ed6ac427cbce65a68f7f5704fa33bf892f1c1fd.tar.gz perlweeklychallenge-club-1ed6ac427cbce65a68f7f5704fa33bf892f1c1fd.tar.bz2 perlweeklychallenge-club-1ed6ac427cbce65a68f7f5704fa33bf892f1c1fd.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club into master
Diffstat (limited to 'challenge-097')
29 files changed, 1180 insertions, 0 deletions
diff --git a/challenge-097/e-choroba/perl/ch-1.pl b/challenge-097/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..39e7eefd4e --- /dev/null +++ b/challenge-097/e-choroba/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl +use warnings; +use strict; + +my $ALPHABET = join "", 'A' .. 'Z'; +sub caesar_cipher { + my ($s, $n) = @_; + $n %= 26; + my $key = $ALPHABET; + substr $key, 0, 0, substr $key, -$n, $n, ""; + eval "\$s =~ tr/$ALPHABET/$key/"; + return $s +} + +use Test::More tests => 3; + +is caesar_cipher('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG', 3), + 'QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD', + 'Example'; + +is caesar_cipher('ZABC', 54), 'XYZA', 'N>26'; +is caesar_cipher('YZAB', -1), 'ZABC', 'N<0'; diff --git a/challenge-097/e-choroba/perl/ch-2.pl b/challenge-097/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..d54fb97539 --- /dev/null +++ b/challenge-097/e-choroba/perl/ch-2.pl @@ -0,0 +1,64 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use List::Util qw{ sum }; + +sub brute_force { + my ($binary, $size) = @_; + my @strings = $binary =~ /(.{$size})/g; + die "Can't split evenly" unless @strings * $size == length $binary; + + my $same = 0 x $size; + my $best = $size * @strings; + until ($size < length $same) { + my $flips = sum(map { ($_ ^ $same) =~ tr/\x01// } @strings); + $best = $flips if $flips < $best; + $same = sprintf "%0${size}b", 1 + oct "b$same"; + } + return $best +} + +sub by_pos { + my ($binary, $size) = @_; + my @strings = $binary =~ /(.{$size})/g; + die "Can't split evenly" unless @strings * $size == length $binary; + + my $sum = 0; + for my $pos (0 .. $size - 1) { + my $ones += grep { substr $_, $pos, 1 } @strings; + $sum += $ones > @strings / 2 ? @strings - $ones : $ones; + } + return $sum +} + +use Test::More; + +for my $example (['101100101', 3, 1], + ['10110111', 4, 2], + ['0000000101101011', 2, 6], + ['000000101010111000110011001111110101', 6, 16], + ['000111111', 3, 3], + ['00000001001001001000', 4, 4], + ['0000100011101010', 4, 4] +){ + is by_pos(@$example[0, 1]), $example->[-1]; + is by_pos(@$example), brute_force(@$example); +} + +my $long = '101010101000100010010010111100010010101010101101001010100010101'; +is brute_force($long, 3), by_pos($long, 3); + +done_testing(); + +use Benchmark qw{ cmpthese }; + +cmpthese(-3, { + brute_force => sub { brute_force($long, 3) }, + by_pos => sub { by_pos($long, 3) }, +}); + +__END__ + Rate brute_force by_pos +brute_force 24884/s -- -69% +by_pos 80637/s 224% -- diff --git a/challenge-097/gustavo-chaves/perl/ch-1.pl b/challenge-097/gustavo-chaves/perl/ch-1.pl new file mode 100755 index 0000000000..11a2d2f608 --- /dev/null +++ b/challenge-097/gustavo-chaves/perl/ch-1.pl @@ -0,0 +1,21 @@ +#!/usr/bin/env perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-097/ +# TASK #1 › Caesar Cipher + +use 5.030; +use warnings; + +my ($N, $S) = @ARGV; + +# ($N, $S) = (3, "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"); + +my $alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; +my $caesar = substr($alphabet, -$N) . substr($alphabet, 0, length($alphabet) - $N); + +$alphabet .= lc $alphabet; +$caesar .= lc $caesar; + +for ($S) { + say eval "tr/$alphabet/$caesar/r"; +} diff --git a/challenge-097/gustavo-chaves/perl/ch-2.pl b/challenge-097/gustavo-chaves/perl/ch-2.pl new file mode 100755 index 0000000000..bad7ffa917 --- /dev/null +++ b/challenge-097/gustavo-chaves/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-097/ +# TASK #2 › Binary Substrings + +use 5.030; +use warnings; +use List::AllUtils qw(sum0 pairwise reduce); + +my ($B, $S) = @ARGV; + +# ($B, $S) = qw(101100101 3); +# ($B, $S) = qw(10110111 4); + +length($B) % $S == 0 + or die "The length of the string '$B' must be a multiple of $S\n"; + +my @substrings = $B =~ /(.{$S})/g; + +my (@distance, @flips); + +# Calculate the distances between each pair of substrings and the total number +# of flips to change all of them to be equal to each one. +for my $i (0 .. $#substrings) { + my @from = split //, $substrings[$i]; + for my $j (0 .. $#substrings) { + my @to = split //, $substrings[$j]; + $distance[$i][$j] = sum0 pairwise {$a != $b} @from, @to; + } + $flips[$i] = sum0 @{$distance[$i]}; +} + +# Find the index of the substring which requires the minimum number of flips. +my $i = reduce {$flips[$a] < $flips[$b] ? $a : $b} 0 .. $#flips; + +say "$flips[$i] to make all substrings equal to '$substrings[$i]' ($i)"; diff --git a/challenge-097/paulo-custodio/basic/ch-1.bas b/challenge-097/paulo-custodio/basic/ch-1.bas new file mode 100644 index 0000000000..b0110e5f99 --- /dev/null +++ b/challenge-097/paulo-custodio/basic/ch-1.bas @@ -0,0 +1,43 @@ +' Challenge 097 +' +' TASK #1 +' +' TASK #1 › Caesar Cipher +' Submitted by: Mohammad S Anwar +' You are given string $S containing alphabets A..Z only and a number $N. +' +' Write a script to encrypt the given string $S using Caesar Cipher with left +' shift of size $N. +' +' Example +' Input: $S = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG", $N = 3 +' Output: "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD" +' +' Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ +' Cipher: XYZABCDEFGHIJKLMNOPQRSTUVW +' +' Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG +' Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD + +function caeser(n as integer, text as string) as string + dim i as integer, cipher as string + dim c as integer + + for i=1 to len(text) + if mid(text,i,1)>="A" and mid(text,i,1)<="Z" then + c = ((asc(mid(text,i,1))-asc("A")+26-n) mod 26)+asc("A") + cipher = cipher & chr(c) + else + cipher = cipher & mid(text,i,1) + end if + next i + caeser = cipher +end function + +' main +dim text as string +dim n as integer, i as integer +n = val(command(1)) +i = 2: do while command(i)<>"": text = text & command(i) & " ": i=i+1: loop + +print caeser(n, text) diff --git a/challenge-097/paulo-custodio/basic/ch-2.bas b/challenge-097/paulo-custodio/basic/ch-2.bas new file mode 100644 index 0000000000..fa6bcfbdf9 --- /dev/null +++ b/challenge-097/paulo-custodio/basic/ch-2.bas @@ -0,0 +1,46 @@ +' Challenge 097 +' +' TASK #2 › Binary Substings +' Submitted by: Mohammad S Anwar +' You are given a binary string $B and an integer $S. +' +' 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 1: +' Input: $B = “101100101”, $S = 3 +' Output: 1 +' +' Binary Substrings: +' "101": 0 flip +' "100": 1 flip to make it "101" +' "101": 0 flip +' Example 2: +' Input $B = “10110111”, $S = 4 +' Output: 2 +' +' Binary Substrings: +' "1011": 0 flip +' "0111": 2 flips to make it "1011" + +function str_flips(a as string, b as string) as integer + dim i as integer, flips as integer + + for i=1 to len(a) + if mid(a,i,1)<>mid(b,i,1) then flips=flips+1 + next i + + str_flips=flips +end function + +function bit_flips(bits as string, n as integer) as integer + dim i as integer, flips as integer + + for i=n+1 to len(bits) step n + flips=flips+str_flips(left(bits,n), mid(bits,i,n)) + next i + + bit_flips=flips +end function + +print trim(str(bit_flips(command(1), val(command(2))))) diff --git a/challenge-097/paulo-custodio/c/ch-1.c b/challenge-097/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..7439af95e8 --- /dev/null +++ b/challenge-097/paulo-custodio/c/ch-1.c @@ -0,0 +1,58 @@ +/* +Challenge 097 + +TASK #1 › Caesar Cipher +Submitted by: Mohammad S Anwar +You are given string $S containing alphabets A..Z only and a number $N. + +Write a script to encrypt the given string $S using Caesar Cipher with left +shift of size $N. + +Example +Input: $S = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG", $N = 3 +Output: "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD" + +Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ +Cipher: XYZABCDEFGHIJKLMNOPQRSTUVW + +Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG +Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <memory.h> +#include <ctype.h> + +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + +// replace in-place by ciphered text +void caeser(int n, char* str) { + for (int i = 0; i < strlen(str); i++) { + if (isalpha(str[i])) + str[i] = ((toupper(str[i])-'A'+26-n)%26)+'A'; + } +} + +int main(int argc, char* argv[]) { + char* str = check_mem(strdup("")); + if (argc > 2) { + int n = atoi(argv[1]); + for (int i = 2; i < argc; i++) { + str = check_mem(realloc(str, strlen(str)+strlen(argv[i])+2)); + strcat(str, argv[i]); + strcat(str, " "); + } + str[strlen(str)-1] = '\0'; + caeser(n, str); + printf("%s\n", str); + } + free(str); +} diff --git a/challenge-097/paulo-custodio/c/ch-2.c b/challenge-097/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..d87bfa0547 --- /dev/null +++ b/challenge-097/paulo-custodio/c/ch-2.c @@ -0,0 +1,51 @@ +/* +Challenge 097 + +TASK #2 › Binary Substings +Submitted by: Mohammad S Anwar +You are given a binary string $B and an integer $S. + +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 1: +Input: $B = “101100101”, $S = 3 +Output: 1 + +Binary Substrings: + "101": 0 flip + "100": 1 flip to make it "101" + "101": 0 flip +Example 2: +Input $B = “10110111”, $S = 4 +Output: 2 + +Binary Substrings: + "1011": 0 flip + "0111": 2 flips to make it "1011" +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +int str_flips(const char* a, const char* b, int n) { + int flips = 0; + for (int i = 0; i < n; i++) { + if (a[i] != b[i]) + flips++; + } + return flips; +} + +int bit_flips(const char* bits, int n) { + int flips = 0; + for (int i = n; i < strlen(bits); i += n) + flips += str_flips(bits, bits+i, n); + return flips; +} + +int main(int argc, char* argv[]) { + if (argc == 3) + printf("%d\n", bit_flips(argv[1], atoi(argv[2]))); +} diff --git a/challenge-097/paulo-custodio/cpp/ch-1.cpp b/challenge-097/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..cddd5df19f --- /dev/null +++ b/challenge-097/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,32 @@ +/* +Challenge 097 + +TASK #1 + +*/ + +#include <iostream> +#include <string> +#include <cctype> + +// replace in-place by ciphered text +void caeser(int n, std::string& str) { + for (auto& c : str) { + if (isalpha(c)) + c = ((toupper(c)-'A'+26-n)%26)+'A'; + } +} + +int main(int argc, char* argv[]) { + std::string str; + if (argc > 2) { + int n = atoi(argv[1]); + for (int i = 2; i < argc; i++) { + str += argv[i]; + str += " "; + } + str.pop_back(); + caeser(n, str); + std::cout << str << std::endl; + } +} diff --git a/challenge-097/paulo-custodio/cpp/ch-2.cpp b/challenge-097/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..9eff10ac52 --- /dev/null +++ b/challenge-097/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,50 @@ +/* +Challenge 097 + +TASK #2 › Binary Substings +Submitted by: Mohammad S Anwar +You are given a binary string $B and an integer $S. + +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 1: +Input: $B = “101100101”, $S = 3 +Output: 1 + +Binary Substrings: + "101": 0 flip + "100": 1 flip to make it "101" + "101": 0 flip +Example 2: +Input $B = “10110111”, $S = 4 +Output: 2 + +Binary Substrings: + "1011": 0 flip + "0111": 2 flips to make it "1011" +*/ + +#include <iostream> +#include <string> + +int str_flips(const char* a, const char* b, int n) { + int flips = 0; + for (int i = 0; i < n; i++) { + if (a[i] != b[i]) + flips++; + } + return flips; +} + +int bit_flips(const std::string& bits, int n) { + int flips = 0; + for (int i = n; i < static_cast<int>(bits.size()); i += n) + flips += str_flips(bits.c_str(), bits.c_str()+i, n); + return flips; +} + +int main(int argc, char* argv[]) { + if (argc == 3) + std::cout << bit_flips(std::string(argv[1]), atoi(argv[2])) << std::endl; +} diff --git a/challenge-097/paulo-custodio/forth/ch-1.fs b/challenge-097/paulo-custodio/forth/ch-1.fs new file mode 100644 index 0000000000..e47986bb41 --- /dev/null +++ b/challenge-097/paulo-custodio/forth/ch-1.fs @@ -0,0 +1,48 @@ +#! /usr/bin/env gforth + +\ Challenge 097 +\ +\ TASK #1 › Caesar Cipher +\ Submitted by: Mohammad S Anwar +\ You are given string $S containing alphabets A..Z only and a number $N. +\ +\ Write a script to encrypt the given string $S using Caesar Cipher with left +\ shift of size $N. +\ +\ Example +\ Input: $S = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG", $N = 3 +\ Output: "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD" +\ +\ Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ +\ Cipher: XYZABCDEFGHIJKLMNOPQRSTUVW +\ +\ Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG +\ Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD + +\ cipher one word +: caeser ( n str-addr str-len -- ) \ outputs coded string + BOUNDS ?DO + I C@ 'A' - \ convert to 0..25 + OVER - 26 + 26 MOD \ cipher + 'A' + EMIT \ convert back to letter + LOOP + DROP + SPACE +; + +\ get shift value and words from args, cipher each word +: caeser-args ( -- ) + NEXT-ARG DUP 0= IF 1 THROW THEN \ get first argument + S>NUMBER? 0= THROW \ convert to number, thow on error + DROP \ drop high part + + BEGIN NEXT-ARG DUP 0> WHILE \ for each argument + >R >R DUP R> R> ( n n addr len ) + caeser \ convert word + REPEAT 2DROP + DROP + CR +; + +caeser-args +BYE diff --git a/challenge-097/paulo-custodio/forth/ch-2.fs b/challenge-097/paulo-custodio/forth/ch-2.fs new file mode 100644 index 0000000000..e19b02f48b --- /dev/null +++ b/challenge-097/paulo-custodio/forth/ch-2.fs @@ -0,0 +1,55 @@ +#! /usr/bin/env gforth + +\ Challenge 097 +\ +\ TASK #2 › Binary Substings +\ Submitted by: Mohammad S Anwar +\ You are given a binary string $B and an integer $S. +\ +\ 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 1: +\ Input: $B = “101100101”, $S = 3 +\ Output: 1 +\ +\ Binary Substrings: +\ "101": 0 flip +\ "100": 1 flip to make it "101" +\ "101": 0 flip +\ Example 2: +\ Input $B = “10110111”, $S = 4 +\ Output: 2 +\ +\ Binary Substrings: +\ "1011": 0 flip +\ "0111": 2 flips to make it "1011" + + +\ count number of flipped chars +: flipped-chars { str1 str2 len -- count } + 0 ( count ) + len 0 ?DO + str1 I + C@ + str2 I + C@ + <> IF 1+ THEN + LOOP +; + +\ count number of flips in sequence +: flipped-sequence { str len n -- count } + 0 str ( count cur-str ) + BEGIN + n + \ advance to next subtring + DUP str len + < + WHILE \ for each substring + str OVER n flipped-chars ( count str delta ) + ROT + SWAP ( count str ) + REPEAT DROP +; + +\ main +NEXT-ARG ( str len ) +NEXT-ARG S>NUMBER? 0= THROW DROP ( str len n ) +flipped-sequence . CR +BYE diff --git a/challenge-097/paulo-custodio/lua/ch-1.lua b/challenge-097/paulo-custodio/lua/ch-1.lua new file mode 100644 index 0000000000..4bb62c085d --- /dev/null +++ b/challenge-097/paulo-custodio/lua/ch-1.lua @@ -0,0 +1,41 @@ +#!/usr/bin/env lua + +--[[ +Challenge 097 + +TASK #1 › Caesar Cipher +Submitted by: Mohammad S Anwar +You are given string $S containing alphabets A..Z only and a number $N. + +Write a script to encrypt the given string $S using Caesar Cipher with left +shift of size $N. + +Example +Input: $S = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG", $N = 3 +Output: "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD" + +Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ +Cipher: XYZABCDEFGHIJKLMNOPQRSTUVW + +Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG +Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD +--]] + +function caesar(n, text) + text = string.upper(text) + local cipher = "" + + for i=1, #text do + local c = string.sub(text,i,i) + if c >= "A" and c <= "Z" then + c = string.char((string.byte(c)-string.byte('A')+26-n)%26 + +string.byte('A')) + end + cipher = cipher..c + end + return cipher +end + +n = arg[1] +text = ""; for i=2,#arg do text = text..arg[i].." "; end +io.write(caesar(n, text),"\n") diff --git a/challenge-097/paulo-custodio/lua/ch-2.lua b/challenge-097/paulo-custodio/lua/ch-2.lua new file mode 100644 index 0000000000..8157c651ed --- /dev/null +++ b/challenge-097/paulo-custodio/lua/ch-2.lua @@ -0,0 +1,50 @@ +#!/usr/bin/env lua + +--[[ +Challenge 097 + +TASK #2 Binary Substings +Submitted by: Mohammad S Anwar +You are given a binary string $B and an integer $S. + +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 1: +Input: $B = 101100101, $S = 3 +Output: 1 + +Binary Substrings: + "101": 0 flip + "100": 1 flip to make it "101" + "101": 0 flip +Example 2: +Input $B = 10110111, $S = 4 +Output: 2 + +Binary Substrings: + "1011": 0 flip + "0111": 2 flips to make it "1011" +--]] + +function str_flips(a, b) + local flips = 0 + for i=1,#a do + if string.sub(a,i,i) ~= string.sub(b,i,i) then + flips = flips+1 + end + end + return flips +end + +function bit_flips(bits, n) + local flips = 0 + for i=n+1,#bits,n do + flips = flips+str_flips(string.sub(bits,1,1+n-1), + string.sub(bits,i,i+n-1)) + end + return flips +end + +io.write(bit_flips(arg[1], arg[2]),"\n") + diff --git a/challenge-097/paulo-custodio/perl/ch-1.pl b/challenge-097/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..adf28c1181 --- /dev/null +++ b/challenge-097/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/perl + +# Challenge 097 +# +# TASK #1 › Caesar Cipher +# Submitted by: Mohammad S Anwar +# You are given string $S containing alphabets A..Z only and a number $N. +# +# Write a script to encrypt the given string $S using Caesar Cipher with left +# shift of size $N. +# +# Example +# Input: $S = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG", $N = 3 +# Output: "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD" +# +# Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ +# Cipher: XYZABCDEFGHIJKLMNOPQRSTUVW +# +# Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG +# Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD + +use strict; +use warnings; +use 5.030; + +@ARGV>=2 or die "Usage: ch-1.pl N WORDS...\n"; +my($n, @words) = @ARGV; +say join " ", caeser($n, @words); + +sub caeser { + my($n, @words) = @_; + my @output; + for my $word (@words) { + $word =~ s/\W//g; + my @codes = map {(ord($_)-ord('A')+26-$n)%26} split //, uc($word); + my @cipher = map {chr($_+ord('A'))} @codes; + push @output, join "", @cipher; + } + return @output; +} diff --git a/challenge-097/paulo-custodio/perl/ch-2.pl b/challenge-097/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..5e4bc9d2f3 --- /dev/null +++ b/challenge-097/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!/usr/bin/perl + +# Challenge 097 +# +# TASK #2 › Binary Substings +# Submitted by: Mohammad S Anwar +# You are given a binary string $B and an integer $S. +# +# 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 1: +# Input: $B = “101100101”, $S = 3 +# Output: 1 +# +# Binary Substrings: +# "101": 0 flip +# "100": 1 flip to make it "101" +# "101": 0 flip +# Example 2: +# Input $B = “10110111”, $S = 4 +# Output: 2 +# +# Binary Substrings: +# "1011": 0 flip +# "0111": 2 flips to make it "1011" + +use strict; +use warnings; +use 5.030; + +@ARGV==2 && $ARGV[0]=~/^[01]+$/ && $ARGV[1]=~/^\d+$/ +&& length($ARGV[0])%$ARGV[1]==0 + or die "Usage: ch-2.pl BITS N\n"; +say bit_flips(@ARGV); + +sub bit_flips { + my($bits, $n) = @_; + my $re = "." x $n; + my @bits = grep {$_ ne ''} split /($re)/, $bits; + + my $flips = 0; + my $b0 = shift @bits; # initial value + $flips += str_flips($b0, $_) for @bits; # flips to initial value + + return $flips; +} + +sub str_flips { + my($a, $b) = @_; + my @a = split //, $a; + my @b = split //, $b; + my $flips = grep {$a[$_] ne $b[$_]} 0..$#a; + return $flips; +} diff --git a/challenge-097/paulo-custodio/python/ch-1.py b/challenge-097/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..374dbc9f5f --- /dev/null +++ b/challenge-097/paulo-custodio/python/ch-1.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python + +# Challenge 097 +# +# TASK #1 › Caesar Cipher +# Submitted by: Mohammad S Anwar +# You are given string $S containing alphabets A..Z only and a number $N. +# +# Write a script to encrypt the given string $S using Caesar Cipher with left +# shift of size $N. +# +# Example +# Input: $S = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG", $N = 3 +# Output: "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD" +# +# Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ +# Cipher: XYZABCDEFGHIJKLMNOPQRSTUVW +# +# Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG +# Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD + +import sys + +def caeser(n, words): + output = [] + for word in words: + codes = [(ord(c)-ord('A')+26-n)%26 for c in word.upper()] + cipher = [chr(i+ord('A')) for i in codes] + output.append("".join(cipher)) + return output + +n = int(sys.argv[1]) +words = sys.argv[2:] +print(" ".join(caeser(n, words))) diff --git a/challenge-097/paulo-custodio/python/ch-2.py b/challenge-097/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..dc0eee00fc --- /dev/null +++ b/challenge-097/paulo-custodio/python/ch-2.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python + +# Challenge 097 +# +# TASK #2 › Binary Substings +# Submitted by: Mohammad S Anwar +# You are given a binary string $B and an integer $S. +# +# 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 1: +# Input: $B = “101100101”, $S = 3 +# Output: 1 +# +# Binary Substrings: +# "101": 0 flip +# "100": 1 flip to make it "101" +# "101": 0 flip +# Example 2: +# Input $B = “10110111”, $S = 4 +# Output: 2 +# +# Binary Substrings: +# "1011": 0 flip |
