diff options
| author | Solathian <horvath6@gmail.com> | 2023-03-26 13:53:23 +0200 |
|---|---|---|
| committer | Solathian <horvath6@gmail.com> | 2023-03-26 13:53:23 +0200 |
| commit | 0eb4e17ee09eeae2f6a7ca5694fbcb45ccef660f (patch) | |
| tree | c29ff2338be3b10fa26b1e52c51a97445375a1bf | |
| parent | e654b9a8e0b8cb51c472c474d80c9b71affb892a (diff) | |
| download | perlweeklychallenge-club-0eb4e17ee09eeae2f6a7ca5694fbcb45ccef660f.tar.gz perlweeklychallenge-club-0eb4e17ee09eeae2f6a7ca5694fbcb45ccef660f.tar.bz2 perlweeklychallenge-club-0eb4e17ee09eeae2f6a7ca5694fbcb45ccef660f.zip | |
Added file challenge 209
| -rw-r--r-- | challenge-209/solathian/perl/ch-1.pl | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-209/solathian/perl/ch-1.pl b/challenge-209/solathian/perl/ch-1.pl new file mode 100644 index 0000000000..8a7e61d6fe --- /dev/null +++ b/challenge-209/solathian/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!usr/bin/perl +use v5.36; + +# Challenge 209 - 1 - Special Bit Characters +# You are given an array of binary bits that ends with 0. + +# Valid sequences in the bit string are: + +# [0] -decodes-to-> "a" +# [1, 0] -> "b" +# [1, 1] -> "c" + + +# bits(); # error: array is empty +# bits(1); # error: array is not ending with 0 + +bits(0, 0, 0); # 1 +bits(1, 0, 0); # 1 +bits(1, 1, 1, 0); # 0 +bits(0, 1, 1, 1, 0); # 0 +bits(0, 1, 0, 1, 1, 0); # 1 +bits(0, 1, 0, 1, 1, 0, 0); # 1 +bits(1, 0, 1, 1, 0, 0); # 1 +bits(1, 0, 0, 1, 1, 0); # 1 + + +sub bits(@array) +{ + + die"Array is empty" if(@array == 0); + die"Array is not ending with 0" if( $array[-1] != 0); + + + while(@array > 1) + { + # shift once if the first character is 0, shift twice if it is a two bit character + shift @array if( $array[0] == 1 ); + shift @array; + } + + say "0" if(@array == 0); + say "1" if(@array == 1); +}
\ No newline at end of file |
