diff options
| -rw-r--r-- | challenge-282/paulo-custodio/perl/ch-1.pl | 3 | ||||
| -rw-r--r-- | challenge-282/paulo-custodio/perl/ch-2.pl | 2 | ||||
| -rw-r--r-- | challenge-282/paulo-custodio/python/ch-1.py | 39 | ||||
| -rw-r--r-- | challenge-282/paulo-custodio/python/ch-2.py | 43 | ||||
| -rw-r--r-- | challenge-282/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-283/paulo-custodio/perl/ch-1.pl | 3 | ||||
| -rw-r--r-- | challenge-283/paulo-custodio/python/ch-1.py | 45 | ||||
| -rw-r--r-- | challenge-283/paulo-custodio/python/ch-2.py | 49 |
8 files changed, 186 insertions, 3 deletions
diff --git a/challenge-282/paulo-custodio/perl/ch-1.pl b/challenge-282/paulo-custodio/perl/ch-1.pl index 739c9c0216..7e83acc3a2 100644 --- a/challenge-282/paulo-custodio/perl/ch-1.pl +++ b/challenge-282/paulo-custodio/perl/ch-1.pl @@ -7,7 +7,8 @@ # # You are given a positive integer, $int, having 3 or more digits. # -# Write a script to return the Good Integer in the given integer or -1 if none found. +# Write a script to return the Good Integer in the given integer or -1 +# if none found. # # A good integer is exactly three consecutive matching digits. # diff --git a/challenge-282/paulo-custodio/perl/ch-2.pl b/challenge-282/paulo-custodio/perl/ch-2.pl index 7aa10cd168..8b13aaa670 100644 --- a/challenge-282/paulo-custodio/perl/ch-2.pl +++ b/challenge-282/paulo-custodio/perl/ch-2.pl @@ -9,7 +9,7 @@ # # Write a script to find the number of times user had to change the key to type # the given string. Changing key is defined as using a key different from the -# last used key. The shift and caps lock keys won’t be counted. +# last used key. The shift and caps lock keys won't be counted. # # Example 1 # diff --git a/challenge-282/paulo-custodio/python/ch-1.py b/challenge-282/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..c64a2229d4 --- /dev/null +++ b/challenge-282/paulo-custodio/python/ch-1.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +# Challenge 282 +# +# Task 1: Good Integer +# Submitted by: Mohammad Sajid Anwar +# +# You are given a positive integer, $int, having 3 or more digits. +# +# Write a script to return the Good Integer in the given integer or -1 +# if none found. +# +# A good integer is exactly three consecutive matching digits. +# +# Example 1 +# +# Input: $int = 12344456 +# Output: 444 +# +# Example 2 +# +# Input: $int = 1233334 +# Output: -1 +# +# Example 3 +# +# Input: $int = 10020003 +# Output: 000 + +import re +import sys + +found = re.search(r'((\d)\2\2+)', sys.argv[1]) +if found is None: + print(-1) +elif len(found.group(1)) != 3: + print(-1) +else: + print(found.group(1)) diff --git a/challenge-282/paulo-custodio/python/ch-2.py b/challenge-282/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..aa1f15080a --- /dev/null +++ b/challenge-282/paulo-custodio/python/ch-2.py @@ -0,0 +1,43 @@ +#!/usr/bin/env perl + +# Challenge 282 +# +# Task 2: Changing Keys +# Submitted by: Mohammad Sajid Anwar +# +# You are given an alphabetic string, $str, as typed by user. +# +# Write a script to find the number of times user had to change the key to type +# the given string. Changing key is defined as using a key different from the +# last used key. The shift and caps lock keys won't be counted. +# +# Example 1 +# +# Input: $str = 'pPeERrLl' +# Ouput: 3 +# +# p -> P : 0 key change +# P -> e : 1 key change +# e -> E : 0 key change +# E -> R : 1 key change +# R -> r : 0 key change +# r -> L : 1 key change +# L -> l : 0 key change +# +# Example 2 +# +# Input: $str = 'rRr' +# Ouput: 0 +# +# Example 3 +# +# Input: $str = 'GoO' +# Ouput: 1 + +import re +import sys + +str = sys.argv[1].upper() +str, count = re.subn(r'(.)\1*', r'\1', str) + +print(len(str)-1) diff --git a/challenge-282/paulo-custodio/t/test-1.yaml b/challenge-282/paulo-custodio/t/test-1.yaml index 3b9ddc2d61..508f04e0b2 100644 --- a/challenge-282/paulo-custodio/t/test-1.yaml +++ b/challenge-282/paulo-custodio/t/test-1.yaml @@ -13,3 +13,8 @@ args: 10020003 input: output: 000 +- setup: + cleanup: + args: 1002003 + input: + output: -1 diff --git a/challenge-283/paulo-custodio/perl/ch-1.pl b/challenge-283/paulo-custodio/perl/ch-1.pl index a1d4008c9c..13b0aab2a5 100644 --- a/challenge-283/paulo-custodio/perl/ch-1.pl +++ b/challenge-283/paulo-custodio/perl/ch-1.pl @@ -5,7 +5,8 @@ # Task 1: Unique Number # Submitted by: Mohammad Sajid Anwar # -# You are given an array of integers, @ints, where every elements appears more than once except one element. +# You are given an array of integers, @ints, where every elements appears more +# than once except one element. # # Write a script to find the one element that appears exactly one time. # Example 1 diff --git a/challenge-283/paulo-custodio/python/ch-1.py b/challenge-283/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..2cedb20d5d --- /dev/null +++ b/challenge-283/paulo-custodio/python/ch-1.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +# Challenge 283 +# +# Task 1: Unique Number +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints, where every elements appears more +# than once except one element. +# +# Write a script to find the one element that appears exactly one time. +# Example 1 +# +# Input: @ints = (3, 3, 1) +# Output: 1 +# +# Example 2 +# +# Input: @ints = (3, 2, 4, 2, 4) +# Output: 3 +# +# Example 3 +# +# Input: @ints = (1) +# Output: 1 +# +# Example 4 +# +# Input: @ints = (4, 3, 1, 1, 1, 4) +# Output: 3 + +import sys + +ints = [int(x) for x in sys.argv[1:]] +count = {} +for x in ints: + if x in count: + count[x] += 1 + else: + count[x] = 1 +unique = list(filter(lambda x:count[x]==1, ints)) +if len(unique) == 0: + print(-1) +else: + print(unique[0]) diff --git a/challenge-283/paulo-custodio/python/ch-2.py b/challenge-283/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..f325700e3b --- /dev/null +++ b/challenge-283/paulo-custodio/python/ch-2.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 + +# Challenge 283 +# +# Task 2: Digit Count Value +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of positive integers, @ints. +# +# Write a script to return true if for every index i in the range 0 <= i < size +# of array, the digit i occurs exactly the $ints[$i] times in the given array +# otherwise return false. +# Example 1 +# +# Input: @ints = (1, 2, 1, 0) +# Ouput: true +# +# $ints[0] = 1, the digit 0 occurs exactly 1 time. +# $ints[1] = 2, the digit 1 occurs exactly 2 times. +# $ints[2] = 1, the digit 2 occurs exactly 1 time. +# $ints[3] = 0, the digit 3 occurs 0 time. +# +# Example 2 +# +# Input: @ints = (0, 3, 0) +# Ouput: false +# +# $ints[0] = 0, the digit 0 occurs 2 times rather than 0 time. +# $ints[1] = 3, the digit 1 occurs 0 time rather than 3 times. +# $ints[2] = 0, the digit 2 occurs exactly 0 time. + +import sys + +def digit_count_true(ints): + count = {} + for i in range(len(ints)): + count[i] = 0 + for x in ints: + if x in count: + count[x] += 1 + else: + count[x] = 1 + for i in range(len(ints)): + if count[i] != ints[i]: + return False + return True + +ints = [int(x) for x in sys.argv[1:]] +print('true' if digit_count_true(ints) else 'false') |
