diff options
Diffstat (limited to 'challenge-243/ianrifkin/python')
| -rw-r--r-- | challenge-243/ianrifkin/python/ch-1.py | 40 | ||||
| -rw-r--r-- | challenge-243/ianrifkin/python/ch-2.py | 38 |
2 files changed, 78 insertions, 0 deletions
diff --git a/challenge-243/ianrifkin/python/ch-1.py b/challenge-243/ianrifkin/python/ch-1.py new file mode 100644 index 0000000000..2dc99b736c --- /dev/null +++ b/challenge-243/ianrifkin/python/ch-1.py @@ -0,0 +1,40 @@ +#!/usr/local/bin/python3 +import sys, argparse + +# Challenge 243, Task 1: Reverse Pairs + +# You are given an array of integers. +# Write a script to return the number of reverse pairs in the given array. +# A reverse pair is a pair (i, j) where: a) 0 <= i < j < nums.length and b) nums[i] > 2 * nums[j]. +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-243/#TASK1 for more information on this challenge + +def main(argv): + argParser = argparse.ArgumentParser() + argParser.add_argument("-n", "--nums", nargs='+', type=int, help="space seperated list of integers e.g. -n 10 30 4 5") + args = argParser.parse_args() + + if args.nums: + nums = args.nums + print( reverse_pairs(nums) ) + else: + #Example 1 + nums = (1, 3, 2, 3, 1) + print( reverse_pairs(nums) ) + #Example 2 + nums = (2, 4, 3, 5, 1) + print( reverse_pairs(nums) ) + +def reverse_pairs(nums): + pairs_found = 0 + for i, inum in enumerate(nums): + for j, jnum in enumerate(nums): + if i >= j: + pass + else: + if inum > jnum * 2: + pairs_found = pairs_found + 1 + return pairs_found + + +if __name__ == "__main__": + main(sys.argv[1:]) diff --git a/challenge-243/ianrifkin/python/ch-2.py b/challenge-243/ianrifkin/python/ch-2.py new file mode 100644 index 0000000000..f12c1dcfc1 --- /dev/null +++ b/challenge-243/ianrifkin/python/ch-2.py @@ -0,0 +1,38 @@ +#!/usr/local/bin/python3 +import sys, argparse +import math + +# Challenge 243, Task 2: Floor Sum + +# You are given an array of positive integers (>=1). +# Write a script to return the sum of floor(nums[i] / nums[j]) where 0 <= i,j < nums.length. The floor() function returns the integer part of the division. +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-243/#TASK2 for more information on this challenge + +# else set default values from example if no cmd line input + +def main(argv): + argParser = argparse.ArgumentParser() + argParser.add_argument("-n", "--nums", nargs='+', type=int, help="space seperated list of positive integers e.g. -n 10 30 4 5") + args = argParser.parse_args() + + if args.nums: + nums = args.nums + print( sum_floors(nums) ) + else: + #Example 1 + nums = (2, 5, 9) + print( sum_floors(nums) ) + #Example 2 + nums = (7, 7, 7, 7, 7, 7, 7) + print( sum_floors(nums) ) + +def sum_floors(nums): + sum_of_floors = 0 + for i, inum in enumerate(nums): + for j, jnum in enumerate(nums): + sum_of_floors = sum_of_floors + math.floor(inum / jnum) + return sum_of_floors + + +if __name__ == "__main__": + main(sys.argv[1:]) |
