diff options
| -rw-r--r-- | challenge-097/paulo-custodio/basic/ch-1.bas | 43 | ||||
| -rw-r--r-- | challenge-097/paulo-custodio/basic/ch-2.bas | 46 | ||||
| -rw-r--r-- | challenge-097/paulo-custodio/c/ch-1.c | 58 | ||||
| -rw-r--r-- | challenge-097/paulo-custodio/c/ch-2.c | 51 | ||||
| -rw-r--r-- | challenge-097/paulo-custodio/cpp/ch-1.cpp | 32 | ||||
| -rw-r--r-- | challenge-097/paulo-custodio/cpp/ch-2.cpp | 50 | ||||
| -rw-r--r-- | challenge-097/paulo-custodio/forth/ch-1.fs | 48 | ||||
| -rw-r--r-- | challenge-097/paulo-custodio/forth/ch-2.fs | 55 | ||||
| -rw-r--r-- | challenge-097/paulo-custodio/lua/ch-1.lua | 41 | ||||
| -rw-r--r-- | challenge-097/paulo-custodio/lua/ch-2.lua | 50 | ||||
| -rw-r--r-- | challenge-097/paulo-custodio/perl/ch-1.pl | 40 | ||||
| -rw-r--r-- | challenge-097/paulo-custodio/perl/ch-2.pl | 55 | ||||
| -rw-r--r-- | challenge-097/paulo-custodio/python/ch-1.py | 34 | ||||
| -rw-r--r-- | challenge-097/paulo-custodio/python/ch-2.py | 55 | ||||
| -rw-r--r-- | challenge-097/paulo-custodio/test.pl | 64 |
15 files changed, 722 insertions, 0 deletions
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 +# "0111": 2 flips to make it "1011" + +import sys +import re + +def bit_flips(bitstring, n): + + def str_flips(a, b): + a = [c for c in a] + b = [c for c in b] + flips = 0; + for i in range(0, len(a)): + if a[i] != b[i]: + flips += 1 + return flips + + bits = [bitstring[i:i+n] for i in range(0, len(bitstring), n)] + flips = 0 + for i in range(1, len(bits)): + flips += str_flips(bits[0], bits[i]) + return flips + +# main +assert len(sys.argv) == 3, "Usage: ch-2.py BITS N" +assert re.match(r"^[01]+$", sys.argv[1]), "BITS must be a bit string" +assert re.match(r"^\d+$", sys.argv[2]), "N must be a number" +assert len(sys.argv[1]) % int(sys.argv[2]) == 0, "N must be multiple of size of BITS" + +bits, n = sys.argv[1], int(sys.argv[2]) +print(bit_flips(bits,n)) diff --git a/challenge-097/paulo-custodio/test.pl b/challenge-097/paulo-custodio/test.pl new file mode 100644 index 0000000000..d09b489e04 --- /dev/null +++ b/challenge-097/paulo-custodio/test.pl @@ -0,0 +1,64 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use 5.030; +use Test::More; + +# hack so that output redirection works in msys +my $LUA = $^O eq "msys" ? "lua.exe" : "lua"; + +run("gcc c/ch-1.c -o c/ch-1"); +run("g++ cpp/ch-1.cpp -o cpp/ch-1"); +run("fbc basic/ch-1.bas -o basic/ch-1"); + +for (["THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" + => "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD"]) { + my($in, $out) = @$_; + + is capture( "perl perl/ch-1.pl 3 $in "), "$out\n"; + is capture( "perl perl/ch-1.pl -3 $out"), "$in\n"; + is capture( "$LUA lua/ch-1.lua 3 $in "), "$out\n"; + is capture( "$LUA lua/ch-1.lua -3 $out"), "$in\n"; + is capture( "gforth forth/ch-1.fs 3 $in "), "$out\n"; + is capture( "gforth forth/ch-1.fs -3 $out"), "$in\n"; + is capture( "python python/ch-1.py 3 $in "), "$out\n"; + is capture( "python python/ch-1.py -3 $out"), "$in\n"; + is capture( "c/ch-1 3 $in "), "$out\n"; + is capture( "c/ch-1 -3 $out"), "$in\n"; + is capture( "cpp/ch-1 3 $in "), "$out\n"; + is capture( "cpp/ch-1 -3 $out"), "$in\n"; + is capture( "basic/ch-1 3 $in "), "$out\n"; + is capture( "basic/ch-1 -3 $out"), "$in\n"; +} + +run("gcc c/ch-2.c -o c/ch-2"); +run("g++ cpp/ch-2.cpp -o cpp/ch-2"); +run("fbc basic/ch-2.bas -o basic/ch-2"); + +for (["101100101 3" => "1"], + ["10110111 4" => "2"]) { + my($in, $out) = @$_; + + is capture( "perl perl/ch-2.pl $in"), "$out\n"; + is capture( "$LUA lua/ch-2.lua $in"), "$out\n"; + is capture( "gforth forth/ch-2.fs $in"), "$out\n"; + is capture("python python/ch-2.py $in"), "$out\n"; + is capture( "c/ch-2 $in"), "$out\n"; + is capture( "cpp/ch-2 $in"), "$out\n"; + is capture( "basic/ch-2 $in"), "$out\n"; +} + +done_testing; + +sub capture { + my($cmd) = @_; + my $out = `$cmd`; + $out =~ s/[ \r\t]*\n/\n/g; + return $out; +} + +sub run { + my($cmd) = @_; + ok 0==system($cmd), $cmd; +} |
