diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-26 10:59:45 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-26 10:59:45 +0100 |
| commit | 264e19ac3adc1f3e04c8d6ab42505d4e1bb64b66 (patch) | |
| tree | 4ecbc55de4f2bdfd1b32566b2bfe8c9332b40c81 | |
| parent | 06fa96309a44869a97c7ff851bbce30508b0b0a8 (diff) | |
| parent | 0ec9f9ca94e7123281705df933dd09e8d44cac66 (diff) | |
| download | perlweeklychallenge-club-264e19ac3adc1f3e04c8d6ab42505d4e1bb64b66.tar.gz perlweeklychallenge-club-264e19ac3adc1f3e04c8d6ab42505d4e1bb64b66.tar.bz2 perlweeklychallenge-club-264e19ac3adc1f3e04c8d6ab42505d4e1bb64b66.zip | |
Merge pull request #7786 from spadacciniweb/PWC-209
PWC-209
| -rw-r--r-- | challenge-209/spadacciniweb/go/ch-1.go | 66 | ||||
| -rw-r--r-- | challenge-209/spadacciniweb/perl/ch-1.pl | 46 | ||||
| -rw-r--r-- | challenge-209/spadacciniweb/python/ch-1.py | 46 | ||||
| -rw-r--r-- | challenge-209/spadacciniweb/ruby/ch-1.rb | 47 |
4 files changed, 205 insertions, 0 deletions
diff --git a/challenge-209/spadacciniweb/go/ch-1.go b/challenge-209/spadacciniweb/go/ch-1.go new file mode 100644 index 0000000000..cc9b60fb7b --- /dev/null +++ b/challenge-209/spadacciniweb/go/ch-1.go @@ -0,0 +1,66 @@ +/* +Task 1: Special Bit Characters +Submitted by: Mohammad S Anwar + +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" +Write a script to print 1 if the last character is an “a” otherwise print 0. + +Example 1 +Input: @bits = (1, 0, 0) +Output: 1 + +The given array bits can be decoded as 2-bits character (10) followed by 1-bit character (0). + +Example 2 +Input: @bits = (1, 1, 1, 0) +Output: 0 + +Possible decode can be 2-bits character (11) followed by 2-bits character (10) i.e. the last character is not 1-bit character. +*/ + +package main + +import ( + "fmt" + "log" + "os" + "strconv" +) + +func main() { + arrStr := os.Args[1:] + if len(arrStr) < 1 { + log.Fatal("input error") + } + arrInt := make([]int, 0) + for i := 0; i <= len(arrStr)-1; i++ { + value, err := strconv.Atoi(arrStr[i]) + if (err != nil) { + log.Fatal(err) + } + arrInt = append(arrInt, value) + } + + i := 0 + for i < len(arrInt)-1 { + if arrInt[i] == 1 { + i++ + } + i++ + } + + if len(arrInt)-1 == i && arrInt[i] == 1 { + log.Fatal("input error") + } + if len(arrInt) > i { + fmt.Println("1"); + } else { + fmt.Println("0"); + } +} diff --git a/challenge-209/spadacciniweb/perl/ch-1.pl b/challenge-209/spadacciniweb/perl/ch-1.pl new file mode 100644 index 0000000000..d39e237736 --- /dev/null +++ b/challenge-209/spadacciniweb/perl/ch-1.pl @@ -0,0 +1,46 @@ +#!/usr/bin/env perl + +# Task 1: Special Bit Characters +# Submitted by: Mohammad S Anwar +# +# 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" +# Write a script to print 1 if the last character is an “a” otherwise print 0. +# +# Example 1 +# Input: @bits = (1, 0, 0) +# Output: 1 +# +# The given array bits can be decoded as 2-bits character (10) followed by 1-bit character (0). +# +# Example 2 +# Input: @bits = (1, 1, 1, 0) +# Output: 0 +# +# Possible decode can be 2-bits character (11) followed by 2-bits character (10) i.e. the last character is not 1-bit character. + +use strict; +use warnings; + +my @input = @ARGV; +die "Input error\n" + if scalar @input < 1 + or + (scalar map { $_ =~ /[^01]/ ? 1 : () } + @input) != 0; + +while (scalar(@input) > 1) { + my $char = shift @input; + shift @input + if $char; +} + +die "Input error\n" + if scalar @input and $input[0] == 1; + +printf "%d\n", (scalar @input) ? 1 : 0; diff --git a/challenge-209/spadacciniweb/python/ch-1.py b/challenge-209/spadacciniweb/python/ch-1.py new file mode 100644 index 0000000000..88e7289f52 --- /dev/null +++ b/challenge-209/spadacciniweb/python/ch-1.py @@ -0,0 +1,46 @@ +# Task 1: Special Bit Characters +# Submitted by: Mohammad S Anwar +# +# 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" +# Write a script to print 1 if the last character is an “a” otherwise print 0. +# +# Example 1 +# Input: @bits = (1, 0, 0) +# Output: 1 +# +# The given array bits can be decoded as 2-bits character (10) followed by 1-bit character (0). +# +# Example 2 +# Input: @bits = (1, 1, 1, 0) +# Output: 0 +# +# Possible decode can be 2-bits character (11) followed by 2-bits character (10) i.e. the last character is not 1-bit character. + +import re +import sys + +if __name__ == "__main__": + input = sys.argv[1:] + if (len(input) < 1 + or + len(list(filter(lambda x: re.search(r'[^01]', x), input))) > 0): + sys.exit("Input error") + + i = 0 + while i < len(input)-1: + if (input[i] == '1'): + i += 1 + i += 1 + if len(input)-1 == i and input[i] == '1': + sys.exit("Input error") + + if (len(input) > i): + print(1) + else: + print(0) diff --git a/challenge-209/spadacciniweb/ruby/ch-1.rb b/challenge-209/spadacciniweb/ruby/ch-1.rb new file mode 100644 index 0000000000..8ce9e634b0 --- /dev/null +++ b/challenge-209/spadacciniweb/ruby/ch-1.rb @@ -0,0 +1,47 @@ +# Task 1: Special Bit Characters +# Submitted by: Mohammad S Anwar +# +# 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" +# Write a script to print 1 if the last character is an “a” otherwise print 0. +# +# Example 1 +# Input: @bits = (1, 0, 0) +# Output: 1 +# +# The given array bits can be decoded as 2-bits character (10) followed by 1-bit character (0). +# +# Example 2 +# Input: @bits = (1, 1, 1, 0) +# Output: 0 +# +# Possible decode can be 2-bits character (11) followed by 2-bits character (10) i.e. the last character is not 1-bit character. + +def main(input) + if input.length < 1 \ + or \ + input.select{ |i| i[/[^01]/] }.length > 0 + then + puts "Input error" + exit(0) + end + + while input.length > 1 + c = input.shift + if c == '1' + input.shift + end + end + if input.length > 0 and input[0] == '1' + puts "Input error" + exit(0) + end + printf "%d\n", (input.length > 0) ? 1 : 0; +end + +main ARGV |
