From b5e40df31c953c3c811d5ba462ff9b8bdf93be93 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 16 Nov 2022 21:22:26 +0000 Subject: - Added solutions to the task "Twice Largest" of week 191. --- challenge-191/mohammad-anwar/python/ch-1.py | 41 +++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 challenge-191/mohammad-anwar/python/ch-1.py (limited to 'challenge-191/mohammad-anwar/python/ch-1.py') diff --git a/challenge-191/mohammad-anwar/python/ch-1.py b/challenge-191/mohammad-anwar/python/ch-1.py new file mode 100644 index 0000000000..d8b6032c40 --- /dev/null +++ b/challenge-191/mohammad-anwar/python/ch-1.py @@ -0,0 +1,41 @@ +#!/usr/bin/python3 + +''' + +Week 191: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-191 + +Task #1: Twice Largest + + You are given list of integers, @list. + + Write a script to find out whether the largest item in the list + is at least twice as large as each of the other items. + +''' + +import unittest + +def twiceLargest(array) -> int: + m = sorted(array).pop() + for i in array: + if i == m: + continue + if i * 2 > m: + return -1 + + return 1 + +# +# +# Unit test class + +class TestTwiceLargest(unittest.TestCase): + def test_twiceLargest(self): + self.assertEqual(twiceLargest([1, 2, 3, 4]), -1, 'Example 1') + self.assertEqual(twiceLargest([1, 2, 0, 5]), 1, 'Example 2') + self.assertEqual(twiceLargest([2, 6, 3, 1]), 1, 'Example 3') + self.assertEqual(twiceLargest([4, 5, 2, 3]), -1, 'Example 4') + +unittest.main() -- cgit