diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-09-03 21:38:53 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-09-03 21:38:53 +0100 |
| commit | 0ea24ad3f2e62e9531e778ee31b582d73efe4b77 (patch) | |
| tree | 72f6f4d626a3cdbea98d71ad60d9c08a8b83b3e7 | |
| parent | 198cd4bdf0fc36df387cf2a636fadbfb74f9a169 (diff) | |
| download | perlweeklychallenge-club-0ea24ad3f2e62e9531e778ee31b582d73efe4b77.tar.gz perlweeklychallenge-club-0ea24ad3f2e62e9531e778ee31b582d73efe4b77.tar.bz2 perlweeklychallenge-club-0ea24ad3f2e62e9531e778ee31b582d73efe4b77.zip | |
- Added solutions to week 180.
24 files changed, 2569 insertions, 1995 deletions
diff --git a/challenge-180/mohammad-anwar/java/theweeklychallenge/FirstUniqueCharacter.java b/challenge-180/mohammad-anwar/java/theweeklychallenge/FirstUniqueCharacter.java new file mode 100644 index 0000000000..34bf56bc6a --- /dev/null +++ b/challenge-180/mohammad-anwar/java/theweeklychallenge/FirstUniqueCharacter.java @@ -0,0 +1,72 @@ +package theweeklychallenge; + +/* + +Week 180: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-180 + +Task #1: First Unique Character + + You are given a string, $s. + + Write a script to find out the first unique character in the + given string and print its index (0-based). + +Compile and Run: + + mohammad-anwar/java$ javac theweeklychallenge/FirstUniqueCharacter.java + mohammad-anwar/java$ java theweeklychallenge.FirstUniqueCharacter + +*/ + +import java.util.Map; +import java.util.TreeMap; +import java.util.ArrayList; +import junit.framework.TestCase; +import static junit.framework.Assert.*; + +public class FirstUniqueCharacter extends TestCase { + + public static void main(String[] args) { + junit.textui.TestRunner.run( + theweeklychallenge.FirstUniqueCharacter.class); + } + + public void testDammAlgorithm() { + assertEquals(firstUniqueCharacter("Perl Weekly Challenge"), 0); + assertEquals(firstUniqueCharacter("Long Live Perl"), 1); + } + + public static int firstUniqueCharacter(String str) { + char[] chars = str.toCharArray(); + ArrayList<Character> chars_array = new ArrayList<Character>(); + Map<Character, Integer> chars_map = new TreeMap<Character, Integer>(); + int j = 0; + for (int i = 0; i < chars.length; i++) { + char c = chars[i]; + if (Character.compare(c, ' ') == 0) { + continue; + } + c = Character.toLowerCase(c); + if (chars_map.containsKey(c)) { + chars_map.put(c, chars_map.get(c) + 1); + } + else { + chars_array.add(c); + chars_map.put(c, 1); + j++; + } + } + + int k = 0; + for (char c : chars_array) { + if (chars_map.get(c) == 1) { + break; + } + k++; + } + + return k; + } +} diff --git a/challenge-180/mohammad-anwar/java/theweeklychallenge/TrimList.java b/challenge-180/mohammad-anwar/java/theweeklychallenge/TrimList.java new file mode 100644 index 0000000000..711ab8ec74 --- /dev/null +++ b/challenge-180/mohammad-anwar/java/theweeklychallenge/TrimList.java @@ -0,0 +1,63 @@ +package theweeklychallenge; + +/* + +Week 180: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-180 + +Task #2: Trim List + + You are given list of numbers, @n and an integer $i. + + Write a script to trim the given list where element is less than + or equal to the given integer. + +Compile and Run: + + mohammad-anwar/java$ javac theweeklychallenge/TrimList.java + mohammad-anwar/java$ java theweeklychallenge.TrimList + +*/ + +import java.util.Map; +import java.util.TreeMap; +import java.util.Arrays; +import java.util.ArrayList; +import junit.framework.TestCase; +import static junit.framework.Assert.*; + +public class TrimList extends TestCase { + + public static void main(String[] args) { + junit.textui.TestRunner.run( + theweeklychallenge.TrimList.class); + } + + public void testTrimListExample1() { + int i = 3; + Integer[] n = {1, 4, 2, 3, 5}; + Integer[] got = trimList(i, n); + Integer[] exp = {4, 5}; + assertEquals(Arrays.toString(exp), Arrays.toString(got)); + } + + public void testTrimListExample2() { + int i = 4; + Integer[] n = {9, 0, 6, 2, 3, 8, 5}; + Integer[] got = trimList(i, n); + Integer[] exp = {9, 6, 8, 5}; + assertEquals(Arrays.toString(exp), Arrays.toString(got)); + } + + public static Integer[] trimList(int i, Integer[] n) { + ArrayList<Integer> list = new ArrayList<Integer>(); + for (int j : n) { + if (j > i) { + list.add(j); + } + } + + return list.toArray(new Integer[list.size()]); + } +} diff --git a/challenge-180/mohammad-anwar/perl/ch-1.pl b/challenge-180/mohammad-anwar/perl/ch-1.pl new file mode 100644 index 0000000000..4dfb6f2dc9 --- /dev/null +++ b/challenge-180/mohammad-anwar/perl/ch-1.pl @@ -0,0 +1,52 @@ +#!/usr/bin/perl + +=head1 + +Week 180: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-180 + +Task #1: First Unique Character + + You are given a string, $s. + + Write a script to find out the first unique character in the + given string and print its index (0-based). + +=cut + +use v5.36; +use Test2::V0; + +is first_unique_character('Perl Weekly Challenge'), 0, 'Example 1'; +is first_unique_character('Long Live Perl'), 1, 'Example 2'; + +done_testing; + +# +# +# METHOD + +sub first_unique_character($str) { + my @c = (); + my %c = (); + foreach my $c (split //, $str) { + next if $c eq ' '; + $c = lc $c; + if (exists $c{$c}) { + $c{$c}++; + } + else { + push @c, $c; + $c{$c} = 1; + } + } + + my $i = 0; + foreach my $c (@c) { + if ($c{$c} == 1) { + return $i; + } + $i++; + } +} diff --git a/challenge-180/mohammad-anwar/perl/ch-2.pl b/challenge-180/mohammad-anwar/perl/ch-2.pl new file mode 100644 index 0000000000..b0008e98e5 --- /dev/null +++ b/challenge-180/mohammad-anwar/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl + +=head1 + +Week 180: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-180 + +Task #1: Trim List + + You are given list of numbers, @n and an integer $i. + + Write a script to trim the given list where element is less than + or equal to the given integer. + +=cut + +use v5.36; +use Test2::V0; + +is trim_list(3, [1, 4, 2, 3, 5]), [4, 5], 'Example 1'; +is trim_list(4, [9, 0, 6, 2, 3, 8, 5]), [9, 6, 8, 5], 'Example 2'; + +done_testing; + +# +# +# METHOD + +sub trim_list($i, $n) { + my @l = (); + foreach (@$n) { + push @l, $_ if $_ > $i; + } + + return \@l; +} diff --git a/challenge-180/mohammad-anwar/python/ch-1.py b/challenge-180/mohammad-anwar/python/ch-1.py new file mode 100644 index 0000000000..5ce058ecf5 --- /dev/null +++ b/challenge-180/mohammad-anwar/python/ch-1.py @@ -0,0 +1,52 @@ +#!/usr/bin/python3 + +''' + +Week 180: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-180 + +Task #1: First Unique Character + + You are given a string, $s. + + Write a script to find out the first unique character in the + given string and print its index (0-based). + +''' + +import unittest + +def firstUniqueCharacter(str) -> int: + char_list = [] + char_hash = {} + for c in list(str): + if c == ' ': + next + c = c.lower() + + if c in char_hash: + char_hash[c] += 1 + else: + char_list.append(c) + char_hash[c] = 1 + + i = 0 + for c in char_list: + if char_hash[c] == 1: + return i + + i = i + 1 + + return i + +# +# +# Unit test class + +class TestFirstUniqueCharacter(unittest.TestCase): + def test_FirstUniqueCharacter(self): + self.assertEqual(firstUniqueCharacter('Perl Weekly Challenge'), 0) + self.assertEqual(firstUniqueCharacter('Long Live Perl'), 1) + +unittest.main() diff --git a/challenge-180/mohammad-anwar/python/ch-2.py b/challenge-180/mohammad-anwar/python/ch-2.py new file mode 100644 index 0000000000..127805093f --- /dev/null +++ b/challenge-180/mohammad-anwar/python/ch-2.py @@ -0,0 +1,37 @@ +#!/usr/bin/python3 + +''' + +Week 180: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-180 + +Task #2: Trim List + + You are given list of numbers, @n and an integer $i. + + Write a script to trim the given list where element is less than + or equal to the given integer. + +''' + +import unittest + +def trimList(i, n): + trim_list = [] + for j in n: + if j > i: + trim_list.append(j) + + return trim_list + +# +# +# Unit test class + +class TestTrimList(unittest.TestCase): + def test_TrimList(self): + self.assertEqual(trimList(3, [1, 4, 2, 3, 5]), [4, 5]) + self.assertEqual(trimList(4, [9, 0, 6, 2, 3, 8, 5]), [9, 6, 8, 5]) + +unittest.main() diff --git a/challenge-180/mohammad-anwar/raku/ch-1.raku b/challenge-180/mohammad-anwar/raku/ch-1.raku new file mode 100644 index 0000000000..d7cacedc0b --- /dev/null +++ b/challenge-180/mohammad-anwar/raku/ch-1.raku @@ -0,0 +1,52 @@ +#!/usr/bin/env raku + +=begin pod + +Week 180: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-180 + +Task #1: First Unique Character + + You are given a string, $s. + + Write a script to find out the first unique character in the + given string and print its index (0-based). + +=end pod + +use Test; + +is first-unique-character('Perl Weekly Challenge'), 0, 'Example 1'; +is first-unique-character('Long Live Perl'), 1, 'Example 2'; + +done-testing; + +# +# +# METHOD + +sub first-unique-character(Str $str --> Int) { + my Str @c = (); + my %c = (); + for $str.comb { + my $c = $_; + next if $c eq ' '; + $c = $c.lc; + if %c{$c}:exists { + %c{$c}++; + } + else { + @c.push: $c; + %c{$c} = 1; + } + } + + my Int $i = 0; + for @c -> $c { + if %c{$c} == 1 { + return $i; + } + $i++; + } +} diff --git a/challenge-180/mohammad-anwar/raku/ch-2.raku b/challenge-180/mohammad-anwar/raku/ch-2.raku new file mode 100644 index 0000000000..9e0b047792 --- /dev/null +++ b/challenge-180/mohammad-anwar/raku/ch-2.raku @@ -0,0 +1,36 @@ +#!/usr/bin/env raku + +=begin pod + +Week 180: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-180 + +Task #2: Trim List + + You are given list of numbers, @n and an integer $i. + + Write a script to trim the given list where element is less than + or equal to the given integer. + +=end pod + +use Test; + +is trim-list(3, [1, 4, 2, 3, 5]), [4, 5], 'Example 1'; +is trim-list(4, [9, 0, 6, 2, 3, 8, 5]), [9, 6, 8, 5], 'Example 2'; + +done-testing; + +# +# +# METHOD + +sub trim-list(Int $i, @n --> Array[Int]) { + my Int @l = (); + for @n -> $l { + @l.push: $l if $l > $i; + } + + return @l; +} diff --git a/challenge-180/mohammad-anwar/swift/ch-1.swift b/challenge-180/mohammad-anwar/swift/ch-1.swift new file mode 100644 index 0000000000..180f2f0d5b --- /dev/null +++ b/challenge-180/mohammad-anwar/swift/ch-1.swift @@ -0,0 +1,70 @@ +import Foundation + +/* + +Week 180: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-180 + +Task #1: First Unique Character + + You are given a string, $s. + + Write a script to find out the first unique character in the + given string and print its index (0-based). + +ACTION: + + $ swift ch-1.swift 'Perl Weekly Challenge' + $ swift ch-1.swift 'Long Live Perl' + +*/ + +enum ParamError: Error { + case missingString +} +do { + let paramCount:Int = Int(CommandLine.argc) + + if paramCount <= 1 { + throw ParamError.missingString + } + + let str: String = CommandLine.arguments[1] + var chars: [Character] = [Character]() + var dicts = [Character:Int]() + for var char: Character in Array(str) { + if char == " " { + continue + } + char = char.toLower() + if let _: Int = dicts[char] { + dicts[char]! += 1 + } + else { + chars.append(char) + dicts[char] = 1 + } + } + + var i:Int = 0; + for char in chars { + if dicts[char]! == 1 { + print(i) + break + } + i += 1 + } +} +catch ParamError.missingString { + print("Missing string.") +} +catch let error { + print(error) +} + +extension Character { + func toLower() -> Character { + return String(self).lowercased().first! + } +} diff --git a/challenge-180/mohammad-anwar/swift/ch-2.swift b/challenge-180/mohammad-anwar/swift/ch-2.swift new file mode 100644 index 0000000000..4855d93f1b --- /dev/null +++ b/challenge-180/mohammad-anwar/swift/ch-2.swift @@ -0,0 +1,84 @@ +import Foundation + +/* + +Week 180: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-180 + +Task #1: Trim List + + You are given list of numbers, @n and an integer $i. + + Write a script to trim the given list where element is less than + or equal to the given integer. + +ACTION: + + $ swift ch-2.swift 3 "1, 4, 2, 3, 5" + $ swift ch-2.swift 4 "9, 0, 6, 2, 3, 8, 5" + +*/ + +enum ParamError: Error { + case missingNumber + case missingList + case invalidList +} +do { + let paramCount:Int = Int(CommandLine.argc) + + if paramCount <= 2 { + throw ParamError.missingNumber + } + + if paramCount <= 1 { + throw ParamError.missingList + } + + let num: Int = Int(CommandLine.arguments[1])! + let list: String = CommandLine.arguments[2] + + if isValidList(list) { + let array = list.components(separatedBy: ", ") + var trimList = [Int]() + for i in array { + if (Int(i)! > num) { + trimList.append(Int(i)!) + } + } + print(trimList) + } + else { + throw ParamError.invalidList; + } +} +catch ParamError.missingNumber { + print("Missing number.") +} +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 + } +} diff --git a/stats/pwc-current.json b/stats/pwc-current.json index fde8932a56..f2cb2b4a3c 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -2,8 +2,35 @@ "chart" : { "type" : "column" }, + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, + "plotOptions" : { + "series" : { + "dataLabels" : { + "enabled" : 1, + "format" : "{point.y}" + }, + "borderWidth" : 0 + } + }, + "subtitle" : { + "text" : "[Champions: 29] Last updated at 2022-09-03 20:36:41 GMT" + }, + "tooltip" : { + "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>", + "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>", + "followPointer" : 1 + }, + "xAxis" : { + "type" : "category" + }, "series" : [ { + "name" : "The Weekly Challenge - 180", + "colorByPoint" : 1, "data" : [ { "name" : "Ali Moradi", @@ -11,14 +38,14 @@ "drilldown" : "Ali Moradi" }, { - "drilldown" : "Arne Sommer", + "y" : 5, "name" : "Arne Sommer", - "y" : 5 + "drilldown" : "Arne Sommer" }, { "drilldown" : "Aut0exec", - "name" : "Aut0exec", - "y" : 2 + "y" : 2, + "name" : "Aut0exec" }, { "name" : "Bejoy Mathews", @@ -31,18 +58,18 @@ "drilldown" : "Ben Davies" }, { + "drilldown" : "Cheok-Yin Fung", "y" : 3, - "name" : "Cheok-Yin Fung", - "drilldown" : "Cheok-Yin Fung" + "name" : "Cheok-Yin Fung" }, { "drilldown" : "Dario Mazzeo", - "y" : 2, - "name" : "Dario Mazzeo" + "name" : "Dario Mazzeo", + "y" : 2 }, { - "name" : "Dave Jacoby", "y" : 2, + "name" : "Dave Jacoby", "drilldown" : "Dave Jacoby" }, { @@ -51,9 +78,9 @@ "drilldown" : "E. Choroba" }, { - "drilldown" : "Flavio Poletti", + "name" : "Flavio Poletti", "y" : 6, - "name" : "Flavio Poletti" + "drilldown" : "Flavio Poletti" }, { "y" : 2, @@ -61,14 +88,14 @@ "drilldown" : "Humberto Massa" }, { - "drilldown" : "James Smith", "y" : 3, - "name" : "James Smith" + "name" : "James Smith", + "drilldown" : "James Smith" }, { - "drilldown" : "Jorg Sommrey", "y" : 2, - "name" : "Jorg Sommrey" + "name" : "Jorg Sommrey", + "drilldown" : "Jorg Sommrey" }, { "drilldown" : "Julien Fiegehenn", @@ -76,105 +103,88 @@ "name" : "Julien Fiegehenn" }, { - "drilldown" : "Kueppo Wesley", "y" : 2, - "name" : "Kueppo Wesley" + "name" : "Kueppo Wesley", + "drilldown" : "Kueppo Wesley" }, { - "drilldown" : "Laurent Rosenfeld", + "name" : "Laurent Rosenfeld", "y" : 5, - "name" : "Laurent Rosenfeld" + "drilldown" : "Laurent Rosenfeld" }, { - "drilldown" : "Mark Anderson", + "y" : 2, "name" : "Mark Anderson", - "y" : 2 + "drilldown" : "Mark Anderson" }, { - "y" : 2, + "drilldown" : "Marton Polgar", "name" : "Marton Polgar", - "drilldown" : "Marton Polgar" + "y" : 2 }, { - "y" : 2, "name" : "Matthew Neleigh", + "y" : 2, "drilldown" : "Matthew Neleigh" }, { + "y" : 4, + "name" : "Mohammad S Anwar", + "drilldown" : "Mohammad S Anwar" + }, + { "drilldown" : "Niels van Dijke", "y" : 2, "name" : "Niels van Dijke" }, { - "drilldown" : "Robert DiCicco", + "y" : 4, "name" : "Robert DiCicco", - "y" : 4 + "drilldown" : "Robert DiCicco" }, { - "drilldown" : "Robert Ransbottom", "name" : "Robert Ransbottom", - "y" : 2 + "y" : 2, + "drilldown" : "Robert Ransbottom" }, { - "drilldown" : "Roger Bell_West", + "name" : "Roger Bell_West", "y" : 5, - "name" : "Roger Bell_West" + "drilldown" : "Roger Bell_West" }, { - "name" : "Simon Proctor", + "drilldown" : "Simon Proctor", "y" : 2, - "drilldown" : "Simon Proctor" + "name" : "Simon Proctor" }, { - "name" : "Solathian", + "drilldown" : "Solathian", "y" : 2, - "drilldown" : "Solathian" + "name" : "Solathian" }, { - "drilldown" : "Stephen G Lynn", + "name" : "Stephen G Lynn", "y" : 5, - "name" : "Stephen G Lynn" + "drilldown" : "Stephen G Lynn" }, { + "drilldown" : "Ulrich Rieke", "name" : "Ulrich Rieke", - "y" : 4, - "drilldown" : "Ulrich Rieke" + "y" : 4 }, { - "drilldown" : "W. Luis Mochan", "y" : 3, - "name" : "W. Luis Mochan" + "name" : "W. Luis Mochan", + "drilldown" : "W. Luis Mochan" } - ], - "colorByPoint" : 1, - "name" : "The Weekly Challenge - 180" + ] } ], - "yAxis" : { - "title" : { - "text" : "Total Solutions" - } - }, - "xAxis" : { - "type" : "category" - }, - "legend" : { - "enabled" : 0 - }, - "plotOptions" : { - "series" : { - "dataLabels" : { - "enabled" : 1, - "format" : "{point.y}" - }, - "borderWidth" : 0 - } - }, "drilldown" : { "series" : [ { - "name" : "Ali Moradi", "id" : "Ali Moradi", + "name" : "Ali Moradi", "data" : [ [ "Perl", @@ -188,7 +198,6 @@ }, { "name" : "Arne Sommer", - "id" : "Arne Sommer", "data" : [ [ "Perl", @@ -202,11 +211,12 @@ "Blog", 1 ] - ] + ], + "id" : "Arne Sommer" }, { - "name" : "Aut0exec", "id" : "Aut0exec", + "name" : "Aut0exec", "data" : [ [ "Perl", @@ -215,13 +225,13 @@ ] }, { + "name" : "Bejoy Mathews", "data" : [ [ "Perl", 2 ] ], - "name" : "Bejoy Mathews", "id" : "Bejoy Mathews" }, { @@ -235,8 +245,6 @@ ] }, { - "id" : "Cheok-Yin Fung", - "name" : "Cheok-Yin Fung", "data" : [ [ "Perl", @@ -246,11 +254,13 @@ "Raku", 1 ] - ] + ], + "name" : "Cheok-Yin Fung", + "id" : "Cheok-Yin Fung" }, { - "name" : "Dario Mazzeo", "id" : "Dario Mazzeo", + "name" : "Dario Mazzeo", "data" : [ [ "Perl", @@ -259,8 +269,8 @@ ] }, { - "name" : "Dave Jacoby", "id" : "Dave Jacoby", + "name" : "Dave Jacoby", "data" : [ [ "Perl", @@ -279,6 +289,7 @@ "id" : "E. Choroba" }, { + "id" : "Flavio Poletti", "data" : [ [ "Perl", @@ -293,21 +304,19 @@ 2 ] ], - "name" : "Flavio Poletti", - "id" : "Flavio Poletti" + "name" : "Flavio Poletti" }, { + "id" : "Humberto Massa", + "name" : "Humberto Massa", "data" : [ [ "Raku", 2 ] - ], - "id" : "Humberto Massa", - "name" : "Humberto Massa" + ] }, { - "name" : "James Smith", "id" : "James Smith", "data" : [ [ @@ -318,27 +327,28 @@ "Blog", 1 ] - ] + ], + "name" : "James Smith" }, { - "id" : "Jorg Sommrey", "name" : "Jorg Sommrey", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "Jorg Sommrey" }, { + "id" : "Julien Fiegehenn", "data" : [ [ "Perl", 1 ] ], - "name" : "Julien Fiegehenn", - "id" : "Julien Fiegehenn" + "name" : "Julien Fiegehenn" < |
