diff options
Diffstat (limited to 'challenge-191')
| -rw-r--r-- | challenge-191/mohammad-anwar/java/theweeklychallenge/TwiceLargest.java | 52 | ||||
| -rw-r--r-- | challenge-191/mohammad-anwar/perl/ch-1.pl | 40 | ||||
| -rw-r--r-- | challenge-191/mohammad-anwar/python/ch-1.py | 41 | ||||
| -rw-r--r-- | challenge-191/mohammad-anwar/raku/ch-1.raku | 39 | ||||
| -rw-r--r-- | challenge-191/mohammad-anwar/swift/ch-1.swift | 88 |
5 files changed, 260 insertions, 0 deletions
diff --git a/challenge-191/mohammad-anwar/java/theweeklychallenge/TwiceLargest.java b/challenge-191/mohammad-anwar/java/theweeklychallenge/TwiceLargest.java new file mode 100644 index 0000000000..5ee4cdde29 --- /dev/null +++ b/challenge-191/mohammad-anwar/java/theweeklychallenge/TwiceLargest.java @@ -0,0 +1,52 @@ +package theweeklychallenge; + +/* + +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. + +Compile and Run: + + mohammad-anwar/java$ javac theweeklychallenge/TwiceLargest.java + mohammad-anwar/java$ java theweeklychallenge.TwiceLargest + +*/ + +import java.util.Arrays; +import junit.framework.TestCase; +import static junit.framework.Assert.*; + +public class TwiceLargest extends TestCase { + + public static void main(String[] args) { + junit.textui.TestRunner.run( + theweeklychallenge.TwiceLargest.class + ); + } + + public void testTwiceLargest() { + assertEquals(twiceLargest(new int[]{1, 2, 3, 4}), -1); + assertEquals(twiceLargest(new int[]{1, 2, 0, 5}), 1); + assertEquals(twiceLargest(new int[]{2, 6, 3, 1}), 1); + assertEquals(twiceLargest(new int[]{4, 5, 2, 3}), -1); + } + + public static int twiceLargest(int[] array) { + Arrays.sort(array); + int max = array[array.length-1]; + for(int i : array) { + if (i == max) { continue; } + if (i * 2 > max) { return -1; }; + } + + return 1; + } +} diff --git a/challenge-191/mohammad-anwar/perl/ch-1.pl b/challenge-191/mohammad-anwar/perl/ch-1.pl new file mode 100644 index 0000000000..8f479dd3fb --- /dev/null +++ b/challenge-191/mohammad-anwar/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/perl + +=head1 + +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. + +=cut + +use v5.36; +use Test2::V0; + +is twice_largest(1, 2, 3, 4), -1, 'Example 1'; +is twice_largest(1, 2, 0, 5), 1, 'Example 2'; +is twice_largest(2, 6, 3, 1), 1, 'Example 3'; +is twice_largest(4, 5, 2, 3), -1, 'Example 4'; + +done_testing; + +# +# +# METHOD + +sub twice_largest(@list) { + my $max = [ sort @list ]->[-1]; + foreach (@list) { + next if $_ == $max; + ($_ * 2 > $max) and return -1; + } + + return 1; +} 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() diff --git a/challenge-191/mohammad-anwar/raku/ch-1.raku b/challenge-191/mohammad-anwar/raku/ch-1.raku new file mode 100644 index 0000000000..3110dbc44b --- /dev/null +++ b/challenge-191/mohammad-anwar/raku/ch-1.raku @@ -0,0 +1,39 @@ +#!/usr/bin/env raku + +=begin pod + +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. + +=end pod + +use Test; + +is twice-largest(<1 2 3 4>), -1, 'Example 1'; +is twice-largest(<1 2 0 5>), 1, 'Example 2'; +is twice-largest(<2 6 3 1>), 1, 'Example 3'; +is twice-largest(<4 5 2 3>), -1, 'Example 4'; + +done-testing; + +# +# +# METHOD + +sub twice-largest(@list --> Int) { + my Int $max = @list.sort.tail; + for @list -> $i { + next if $i == $max; + ($i * 2 > $max) and return -1; + } + + return 1; +} diff --git a/challenge-191/mohammad-anwar/swift/ch-1.swift b/challenge-191/mohammad-anwar/swift/ch-1.swift new file mode 100644 index 0000000000..2047992cdd --- /dev/null +++ b/challenge-191/mohammad-anwar/swift/ch-1.swift @@ -0,0 +1,88 @@ +import Foundation + +/* + +Week 191: + + https://perlweeklychallenge.org/blog/perl-weekly-challenge-1910 + +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. + +ACTION: + + $ swift ch-1.swift "1, 2, 3, 4" + $ swift ch-1.swift "1, 2, 0, 5" + $ swift ch-1.swift "2, 6, 3, 1" + $ swift ch-1.swift "4, 5, 2, 3" + +*/ + +enum ParamError: Error { + case missingList + case invalidList +} + +do { + let paramCount:Int = Int(CommandLine.argc) + + if paramCount <= 1 { + throw ParamError.missingList + } + + let list:String = CommandLine.arguments[1] + if isValidList(list) { + let array = list.components(separatedBy: ", ") + var intArray = array.map { Int($0)! } + let size = intArray.count + + iintArray.sort() + let max = (intArray.last)! + + var found = 1 + for i in 0...size-1 { + if intArray[i] == max { + continue + } + if intArray[i] * 2 > max { + found = -1 + break; + } + } + print(found) + } + else { + throw ParamError.invalidList + } +} +catch ParamError.missingList { + print("Missing list.") +} +catch ParamError.invalidList { + print("Invalid list.") +} +catch let error { + print(error) +} + +// +// +// Function + +func isValidList(_ list:String) -> Bool { + + let pattern = "^[\\-?\\d\\,?\\s?]+$" + let regex = try! NSRegularExpression(pattern: pattern) + let range = NSRange(location: 0, length: list.utf16.count) + + if regex.firstMatch(in: list, options: [], range: range) != nil { + return true + } + else { + return false + } +} |
