diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-02-13 04:45:49 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-13 04:45:49 +0000 |
| commit | ad9c3ff124f680e647b6314aef31bb7821832da1 (patch) | |
| tree | 2eba911535789c1d49f323992d51329d5d469921 /challenge-203/spadacciniweb/python/ch-1.py | |
| parent | ac7c4feb8921204c597449a6b9efec3b847c9bfe (diff) | |
| parent | b27a88ca431bba4fe5036ab54348367b45bc9cd3 (diff) | |
| download | perlweeklychallenge-club-ad9c3ff124f680e647b6314aef31bb7821832da1.tar.gz perlweeklychallenge-club-ad9c3ff124f680e647b6314aef31bb7821832da1.tar.bz2 perlweeklychallenge-club-ad9c3ff124f680e647b6314aef31bb7821832da1.zip | |
Merge pull request #7540 from spadacciniweb/branch-for-challenge-203
PWC 203 - Perl, Python, Go, Bash, Korn shell
Diffstat (limited to 'challenge-203/spadacciniweb/python/ch-1.py')
| -rw-r--r-- | challenge-203/spadacciniweb/python/ch-1.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/challenge-203/spadacciniweb/python/ch-1.py b/challenge-203/spadacciniweb/python/ch-1.py new file mode 100644 index 0000000000..bd7860c779 --- /dev/null +++ b/challenge-203/spadacciniweb/python/ch-1.py @@ -0,0 +1,51 @@ +# Task 1: Special Quadruplets +# Submitted by: Mohammad S Anwar +# +# You are given an array of integers. +# Write a script to find out the total special quadruplets for the given array. +# +# Special Quadruplets are such that satisfies the following 2 rules. +# 1) nums[a] + nums[b] + nums[c] == nums[d] +# 2) a < b < c < d +# +# +# Example 1 +# Input: @nums = (1,2,3,6) +# Output: 1 +# +# Since the only special quadruplets found is $nums[0] + $nums[1] + $nums[2] == $nums[3]. +# +# Example 2 +# Input: @nums = (1,1,1,3,5) +# Output: 4 +# +# $nums[0] + $nums[1] + $nums[2] == $nums[3] +# $nums[0] + $nums[1] + $nums[3] == $nums[4] +# $nums[0] + $nums[2] + $nums[3] == $nums[4] +# $nums[1] + $nums[2] + $nums[3] == $nums[4] +# +# Example 3 +# Input: @nums = (3,3,6,4,5) +# Output: 0 + +import re +import sys + +if __name__ == "__main__": + input = sys.argv[1:] + if (len(input) < 4 or + len(list(filter(lambda x: re.search(r'\D', x), input))) > 0 ): + sys.exit("Input error\n") + + input = list(map(int, input)) + output = 0 + + quadruplets = [] + for i in range(0, len(input)-3): + for j in range(i+1, len(input)-2): + for k in range(j+1, len(input)-1): + for z in range(k+1, len(input)): + if input[i] + input[j] + input[k] == input[z]: + quadruplets.append([i,j,k,z]) + + print(len(quadruplets)); |
