forked from manwar/perlweeklychallenge-club
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request manwar#9448 from jeanluc2020/jeanluc-253
Add solution 253.
- Loading branch information
Showing
6 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-253-1.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-253-2.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/env perl | ||
# https://theweeklychallenge.org/blog/perl-weekly-challenge-253/#TASK1 | ||
# | ||
# Task 1: Split Strings | ||
# ===================== | ||
# | ||
# You are given an array of strings and a character separator. | ||
# | ||
# Write a script to return all words separated by the given character excluding | ||
# empty string. | ||
# | ||
# Example 1 | ||
# | ||
# Input: @words = ("one.two.three","four.five","six") | ||
# $separator = "." | ||
# Output: "one","two","three","four","five","six" | ||
# | ||
# Example 2 | ||
# | ||
# Input: @words = ("$perl$$", "$$raku$") | ||
# $separator = "$" | ||
# Output: "perl","raku" | ||
# | ||
############################################################ | ||
## | ||
## discussion | ||
## | ||
############################################################ | ||
# | ||
# This is a classic one-liner problem: join the words into a single string, | ||
# then split this string into an array of strings at the separator, then | ||
# only keep the non-empty strings by grepping for strings that contain one | ||
# character. | ||
# | ||
|
||
use strict; | ||
use warnings; | ||
|
||
split_strings(".", "one.two.three","four.five","six"); | ||
split_strings('$', '$perl$$', '$$raku$'); | ||
|
||
sub split_strings { | ||
my ($separator, @words) = @_; | ||
print "Input: (\"" . join("\", \"", @words) . "\"), '$separator'\n"; | ||
my @output = grep /./, split /\Q$separator\E/, join($separator, @words); | ||
print "Output: (\"" . join("\", \"", @output) . "\")\n"; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#!/usr/bin/env perl | ||
# https://theweeklychallenge.org/blog/perl-weekly-challenge-253/#TASK2 | ||
# | ||
# Task 2: Weakest Row | ||
# =================== | ||
# | ||
# You are given an m x n binary matrix i.e. only 0 and 1 where 1 always appear before 0. | ||
# | ||
# A row i is weaker than a row j if one of the following is true: | ||
# | ||
## a) The number of 1s in row i is less than the number of 1s in row j. | ||
## b) Both rows have the same number of 1 and i < j. | ||
# | ||
# Write a script to return the order of rows from weakest to strongest. | ||
# | ||
## Example 1 | ||
## | ||
## Input: $matrix = [ | ||
## [1, 1, 0, 0, 0], | ||
## [1, 1, 1, 1, 0], | ||
## [1, 0, 0, 0, 0], | ||
## [1, 1, 0, 0, 0], | ||
## [1, 1, 1, 1, 1] | ||
## ] | ||
## Output: (2, 0, 3, 1, 4) | ||
## | ||
## The number of 1s in each row is: | ||
## - Row 0: 2 | ||
## - Row 1: 4 | ||
## - Row 2: 1 | ||
## - Row 3: 2 | ||
## - Row 4: 5 | ||
# | ||
## Example 2 | ||
## | ||
## Input: $matrix = [ | ||
## [1, 0, 0, 0], | ||
## [1, 1, 1, 1], | ||
## [1, 0, 0, 0], | ||
## [1, 0, 0, 0] | ||
## ] | ||
## Output: (0, 2, 3, 1) | ||
## | ||
## The number of 1s in each row is: | ||
## - Row 0: 1 | ||
## - Row 1: 4 | ||
## - Row 2: 1 | ||
## - Row 3: 1 | ||
# | ||
############################################################ | ||
## | ||
## discussion | ||
## | ||
############################################################ | ||
# | ||
# First, we create a second array which contains the number of 1's | ||
# in each row and the row's index, then we sort that array by the | ||
# number of ones and the index | ||
# | ||
|
||
use strict; | ||
use warnings; | ||
|
||
weakest_row([ [1, 1, 0, 0, 0], [1, 1, 1, 1, 0], [1, 0, 0, 0, 0], [1, 1, 0, 0, 0], [1, 1, 1, 1, 1] ]); | ||
weakest_row([ [1, 0, 0, 0], [1, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 0] ]); | ||
|
||
sub weakest_row { | ||
my $matrix = shift; | ||
print "Input: [\n"; | ||
foreach my $array (@$matrix) { | ||
print " [" . join(", ", @$array) . "\,\n"; | ||
} | ||
print " ]\n"; | ||
my @sort_by_me = (); | ||
my $i = 0; | ||
foreach my $array (@$matrix) { | ||
my $num = number_of_ones(@$array); | ||
my $pos = $i; | ||
$i++; | ||
push @sort_by_me, [$num, $pos]; | ||
} | ||
my @sorted = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @sort_by_me; | ||
print "Output: (" . join(", ", map { $_->[1] } @sorted) . ")\n"; | ||
} | ||
|
||
sub number_of_ones { | ||
my @array = @_; | ||
my $output = 0; | ||
foreach my $value (@array) { | ||
$output++ if $value; | ||
} | ||
return $output; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/usr/bin/python3 | ||
# https://theweeklychallenge.org/blog/perl-weekly-challenge-253/#TASK1 | ||
# | ||
# Task 1: Split Strings | ||
# ===================== | ||
# | ||
# You are given an array of strings and a character separator. | ||
# | ||
# Write a script to return all words separated by the given character excluding | ||
# empty string. | ||
# | ||
# Example 1 | ||
# | ||
# Input: @words = ("one.two.three","four.five","six") | ||
# $separator = "." | ||
# Output: "one","two","three","four","five","six" | ||
# | ||
# Example 2 | ||
# | ||
# Input: @words = ("$perl$$", "$$raku$") | ||
# $separator = "$" | ||
# Output: "perl","raku" | ||
# | ||
############################################################ | ||
## | ||
## discussion | ||
## | ||
############################################################ | ||
# | ||
# This is a classic one-liner problem: join the words into a single string, | ||
# then split this string into an array of strings at the separator, then | ||
# only keep the non-empty strings by grepping for strings that contain one | ||
# character. | ||
# | ||
|
||
import re | ||
|
||
def split_strings(separator: str, words: list) -> None: | ||
print("Input: (\"", "\", \"".join(words), "\"), '", separator, "'", sep="") | ||
output = [] | ||
for value in separator.join(words).split(separator): | ||
if len(value) > 0: | ||
output.append(value) | ||
print("Output: (\"", "\", \"".join(output), "\")", sep="") | ||
|
||
|
||
split_strings(".", ["one.two.three","four.five","six"]) | ||
split_strings('$', ['$perl$$', '$$raku$']) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#!/usr/bin/python3 | ||
# https://theweeklychallenge.org/blog/perl-weekly-challenge-253/#TASK2 | ||
# | ||
# Task 2: Weakest Row | ||
# =================== | ||
# | ||
# You are given an m x n binary matrix i.e. only 0 and 1 where 1 always appear before 0. | ||
# | ||
# A row i is weaker than a row j if one of the following is true: | ||
# | ||
## a) The number of 1s in row i is less than the number of 1s in row j. | ||
## b) Both rows have the same number of 1 and i < j. | ||
# | ||
# Write a script to return the order of rows from weakest to strongest. | ||
# | ||
## Example 1 | ||
## | ||
## Input: $matrix = [ | ||
## [1, 1, 0, 0, 0], | ||
## [1, 1, 1, 1, 0], | ||
## [1, 0, 0, 0, 0], | ||
## [1, 1, 0, 0, 0], | ||
## [1, 1, 1, 1, 1] | ||
## ] | ||
## Output: (2, 0, 3, 1, 4) | ||
## | ||
## The number of 1s in each row is: | ||
## - Row 0: 2 | ||
## - Row 1: 4 | ||
## - Row 2: 1 | ||
## - Row 3: 2 | ||
## - Row 4: 5 | ||
# | ||
## Example 2 | ||
## | ||
## Input: $matrix = [ | ||
## [1, 0, 0, 0], | ||
## [1, 1, 1, 1], | ||
## [1, 0, 0, 0], | ||
## [1, 0, 0, 0] | ||
## ] | ||
## Output: (0, 2, 3, 1) | ||
## | ||
## The number of 1s in each row is: | ||
## - Row 0: 1 | ||
## - Row 1: 4 | ||
## - Row 2: 1 | ||
## - Row 3: 1 | ||
# | ||
############################################################ | ||
## | ||
## discussion | ||
## | ||
############################################################ | ||
# | ||
# First, we create a second array which contains the number of 1's | ||
# in each row and the row's index, then we sort that array by the | ||
# number of ones and the index - which means we only sort by the | ||
# number of ones since python's sort is stable, keeping the order | ||
# of elements intact if the values are the same, and the index is | ||
# therefore already sorted after sorting by number of ones. | ||
# | ||
|
||
from operator import itemgetter | ||
|
||
def number_of_ones( array: list) -> int: | ||
output = 0 | ||
for value in array: | ||
if value > 0: | ||
output += 1 | ||
return output | ||
|
||
def weakest_row(matrix: list) -> None: | ||
print("Input: [") | ||
for array in matrix: | ||
print(" [", ", ".join([str(x) for x in array]), "],") | ||
print(" ]") | ||
sort_by_me = [] | ||
i = 0 | ||
for array in matrix: | ||
num = number_of_ones(array) | ||
pos = i | ||
i += 1 | ||
sort_by_me.append([num, pos]) | ||
sorted_array = sorted(sort_by_me, key=itemgetter(0)) | ||
print("Output: (", ", ".join([str(x[1]) for x in sorted_array]), ")", sep=None) | ||
|
||
|
||
weakest_row([ [1, 1, 0, 0, 0], [1, 1, 1, 1, 0], [1, 0, 0, 0, 0], [1, 1, 0, 0, 0], [1, 1, 1, 1, 1] ]) | ||
weakest_row([ [1, 0, 0, 0], [1, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 0] ]) | ||
|