From c3edc9149c2c1d435eecf347139ca83094e1d86e Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 21 Oct 2024 15:58:03 +0100 Subject: Add solutions --- challenge-279/paulo-custodio/python/ch-1.py | 40 ++++++++++++ challenge-279/paulo-custodio/python/ch-2.py | 37 +++++++++++ challenge-292/paulo-custodio/Makefile | 2 + challenge-292/paulo-custodio/perl/ch-1.pl | 36 +++++++++++ challenge-292/paulo-custodio/perl/ch-2.pl | 98 +++++++++++++++++++++++++++++ challenge-292/paulo-custodio/python/ch-1.py | 34 ++++++++++ challenge-292/paulo-custodio/python/ch-2.py | 92 +++++++++++++++++++++++++++ challenge-292/paulo-custodio/t/test-1.yaml | 10 +++ challenge-292/paulo-custodio/t/test-2.yaml | 15 +++++ 9 files changed, 364 insertions(+) create mode 100644 challenge-279/paulo-custodio/python/ch-1.py create mode 100644 challenge-279/paulo-custodio/python/ch-2.py create mode 100644 challenge-292/paulo-custodio/Makefile create mode 100644 challenge-292/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-292/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-292/paulo-custodio/python/ch-1.py create mode 100644 challenge-292/paulo-custodio/python/ch-2.py create mode 100644 challenge-292/paulo-custodio/t/test-1.yaml create mode 100644 challenge-292/paulo-custodio/t/test-2.yaml diff --git a/challenge-279/paulo-custodio/python/ch-1.py b/challenge-279/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..66871bc885 --- /dev/null +++ b/challenge-279/paulo-custodio/python/ch-1.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +# Challenge 279 +# +# Task 1: Sort Letters +# Submitted by: Mohammad Sajid Anwar +# +# You are given two arrays, @letters and @weights. +# +# Write a script to sort the given array @letters based on the @weights. +# Example 1 +# +# Input: @letters = ('R', 'E', 'P', 'L') +# @weights = (3, 2, 1, 4) +# Output: PERL +# +# Example 2 +# +# Input: @letters = ('A', 'U', 'R', 'K') +# @weights = (2, 4, 1, 3) +# Output: RAKU +# +# Example 3 +# +# Input: @letters = ('O', 'H', 'Y', 'N', 'P', 'T') +# @weights = (5, 4, 2, 6, 1, 3) +# Output: PYTHON + +import sys + +if len(sys.argv) > 2: + letters = sys.argv[1] + weights = list(map(int, sys.argv[2:])) + letters_list = list(letters) + + sorted_letters = sorted(zip(letters_list, weights), key=lambda x: x[1]) + result = ''.join(letter for letter, weight in sorted_letters) + print(result) +else: + raise SystemExit("Usage: {} LETTERS WEIGHTS...".format(sys.argv[0])) diff --git a/challenge-279/paulo-custodio/python/ch-2.py b/challenge-279/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..02565ebcec --- /dev/null +++ b/challenge-279/paulo-custodio/python/ch-2.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +# Challenge 279 +# +# Task 2: Split String +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string, $str. +# +# Write a script to split the given string into two containing exactly same +# number of vowels and return true if you can otherwise false. +# +# Example 1 +# +# Input: $str = "perl" +# Ouput: false +# +# Example 2 +# +# Input: $str = "book" +# Ouput: true +# +# Two possible strings "bo" and "ok" containing exactly one vowel each. +# +# Example 3 +# +# Input: $str = "good morning" +# Ouput: true +# +# Two possible strings "good " and "morning" containing two vowels each or +# "good m" and "orning" containing two vowels each. + +import sys + +str_input = " ".join(sys.argv[1:]) +vowels = sum(1 for char in str_input if char in "aeiouAEIOU") +print("true" if vowels % 2 == 0 else "false") diff --git a/challenge-292/paulo-custodio/Makefile b/challenge-292/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-292/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-292/paulo-custodio/perl/ch-1.pl b/challenge-292/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..d816c84886 --- /dev/null +++ b/challenge-292/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl + +# Challenge 292 +# +# Task 1: Twice Largest +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints, where the largest integer is unique. +# +# Write a script to find whether the largest element in the array is at least twice as big as every element in the given array. If it is return the index of the largest element or return -1 otherwise. +# Example 1 +# +# Input: @ints = (2, 4, 1, 0) +# Output: 1 +# +# The largest integer is 4. +# For every other elements in the given array is at least twice as big. +# The index value of 4 is 1. +# +# Example 2 +# +# Input: @ints = (1, 2, 3, 4) +# Output: -1 +# +# The largest integer is 4. +# 4 is less than twice the value of 3, so we return -1. + +use Modern::Perl; + +my @nums = sort {$b->[1] <=> $a->[1]} map {[$_, $ARGV[$_]]} 0..$#ARGV; +if (@nums >= 2 && $nums[0][1] >= 2*$nums[1][1]) { + say $nums[0][0]; +} +else { + say -1; +} diff --git a/challenge-292/paulo-custodio/perl/ch-2.pl b/challenge-292/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..ddc9f289ff --- /dev/null +++ b/challenge-292/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,98 @@ +#!/usr/bin/env perl + +# Challenge 292 +# +# Task 2: Zuma Game +# Submitted by: Mohammad Sajid Anwar +# +# You are given a single row of colored balls, $row and a random number of +# colored balls in $hand. +# +# Here is the variation of Zuma game as your goal is to clear all of the balls +# from the board. Pick any ball from your hand and insert it in between two +# balls in the row or on either end of the row. If there is a group of three +# or more consecutive balls of the same color then remove the group of balls +# from the board. If there are no more balls on the board then you win the game. +# Repeat this process until you either win or do not have any more balls in +# your hand. +# +# Write a script to minimum number of balls you have to insert to clear all +# the balls from the board. If you cannot clear all the balls from the board +# using the balls in your hand, return -1. +# Example 1 +# +# Input: $board = "WRRBBW", $hand = "RB" +# Output: -1 +# +# It is impossible to clear all the balls. The best you can do is: +# - Insert 'R' so the board becomes WRRRBBW. WRRRBBW -> WBBW. +# - Insert 'B' so the board becomes WBBBW. WBBBW -> WW. +# There are still balls remaining on the board, and you are out of balls to insert. +# +# Example 2 +# +# Input: $board = "WWRRBBWW", $hand = "WRBRW" +# Output: 2 +# +# To make the board empty: +# - Insert 'R' so the board becomes WWRRRBBWW. WWRRRBBWW -> WWBBWW. +# - Insert 'B' so the board becomes WWBBBWW. WWBBBWW -> WWWW -> empty. +# 2 balls from your hand were needed to clear the board. +# +# Example 3 +# +# Input: $board = "G", $hand = "GGGGG" +# Output: 2 +# +# To make the board empty: +# - Insert 'G' so the board becomes GG. +# - Insert 'G' so the board becomes GGG. GGG -> empty. +# 2 balls from your hand were needed to clear the board. + +use Modern::Perl; + +sub draw { + my($board, $hand) = @_; + + my %not_yet; + for my $ball (split //, $board) { + if ($board =~ /$ball[^$ball]+$ball/) { + $not_yet{$ball} = 1; + } + } + + while ($board =~ /(.)\1/g) { + my $ball = $1; + if (!$not_yet{$ball} && $hand =~ /$ball/) { + $board =~ s/$ball{2,}//; + $board =~ s/(.)\1\1+//g; + $hand =~ s/$ball//; + return (1, $board, $hand); + } + } + + for my $ball (split //, $board) { + if ($hand =~ /$ball/) { + $board =~ s/$ball/$ball$ball/; + $board =~ s/(.)\1\1+//g; + $hand =~ s/$ball//; + return (1, $board, $hand); + } + } + + return (0, $board, $hand); +} + +sub play { + my($board, $hand) = @_; + my $rounds = 0; + for (;;) { + return $rounds if $board eq ''; + return -1 if $hand eq ''; + (my $moved, $board, $hand) = draw($board, $hand); + return -1 if !$moved; + $rounds++; + } +} + +say play(@ARGV); diff --git a/challenge-292/paulo-custodio/python/ch-1.py b/challenge-292/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..7a578d625e --- /dev/null +++ b/challenge-292/paulo-custodio/python/ch-1.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +# Challenge 292 +# +# Task 1: Twice Largest +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints, where the largest integer is unique. +# +# Write a script to find whether the largest element in the array is at least twice as big as every element in the given array. If it is return the index of the largest element or return -1 otherwise. +# Example 1 +# +# Input: @ints = (2, 4, 1, 0) +# Output: 1 +# +# The largest integer is 4. +# For every other elements in the given array is at least twice as big. +# The index value of 4 is 1. +# +# Example 2 +# +# Input: @ints = (1, 2, 3, 4) +# Output: -1 +# +# The largest integer is 4. +# 4 is less than twice the value of 3, so we return -1. + +import sys + +nums = sorted([[i-1, int(sys.argv[i])] for i in range(1, len(sys.argv))], key=lambda x: x[1], reverse=True) +if len(nums) >= 2 and nums[0][1] >= 2 * nums[1][1]: + print(nums[0][0]) +else: + print(-1) diff --git a/challenge-292/paulo-custodio/python/ch-2.py b/challenge-292/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..f0ef3e715e --- /dev/null +++ b/challenge-292/paulo-custodio/python/ch-2.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 + +# Challenge 292 +# +# Task 2: Zuma Game +# Submitted by: Mohammad Sajid Anwar +# +# You are given a single row of colored balls, $row and a random number of +# colored balls in $hand. +# +# Here is the variation of Zuma game as your goal is to clear all of the balls +# from the board. Pick any ball from your hand and insert it in between two +# balls in the row or on either end of the row. If there is a group of three +# or more consecutive balls of the same color then remove the group of balls +# from the board. If there are no more balls on the board then you win the game. +# Repeat this process until you either win or do not have any more balls in +# your hand. +# +# Write a script to minimum number of balls you have to insert to clear all +# the balls from the board. If you cannot clear all the balls from the board +# using the balls in your hand, return -1. +# Example 1 +# +# Input: $board = "WRRBBW", $hand = "RB" +# Output: -1 +# +# It is impossible to clear all the balls. The best you can do is: +# - Insert 'R' so the board becomes WRRRBBW. WRRRBBW -> WBBW. +# - Insert 'B' so the board becomes WBBBW. WBBBW -> WW. +# There are still balls remaining on the board, and you are out of balls to insert. +# +# Example 2 +# +# Input: $board = "WWRRBBWW", $hand = "WRBRW" +# Output: 2 +# +# To make the board empty: +# - Insert 'R' so the board becomes WWRRRBBWW. WWRRRBBWW -> WWBBWW. +# - Insert 'B' so the board becomes WWBBBWW. WWBBBWW -> WWWW -> empty. +# 2 balls from your hand were needed to clear the board. +# +# Example 3 +# +# Input: $board = "G", $hand = "GGGGG" +# Output: 2 +# +# To make the board empty: +# - Insert 'G' so the board becomes GG. +# - Insert 'G' so the board becomes GGG. GGG -> empty. +# 2 balls from your hand were needed to clear the board. + +import sys +import re + +def draw(board, hand): + not_yet = {} + for ball in board: + if re.search(rf'{ball}[^{ball}]+{ball}', board): + not_yet[ball] = 1 + + if re.search(r'(.)\1', board): + it = re.finditer(r'(.)\1', board) + while m := next(it): + ball = m.group(1) + if ball not in not_yet and ball in hand: + board = re.sub(rf'{ball}{{2,}}', '', board) + board = re.sub(r'(.)\1\1+', '', board) + hand = hand.replace(ball, '', 1) + return 1, board, hand + + for ball in board: + if ball in hand: + board = re.sub(rf'{ball}', f'{ball}{ball}', board, 1) + board = re.sub(r'(.)\1\1+', '', board) + hand = hand.replace(ball, '', 1) + return 1, board, hand + + return 0, board, hand + +def play(board, hand): + rounds = 0 + while True: + if board == '': + return rounds + if hand == '': + return -1 + moved, board, hand = draw(board, hand) + if not moved: + return -1 + rounds += 1 + +print(play(sys.argv[1], sys.argv[2])) diff --git a/challenge-292/paulo-custodio/t/test-1.yaml b/challenge-292/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..c06d2eebe8 --- /dev/null +++ b/challenge-292/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 2 4 1 0 + input: + output: 1 +- setup: + cleanup: + args: 1 2 3 4 + input: + output: -1 diff --git a/challenge-292/paulo-custodio/t/test-2.yaml b/challenge-292/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..f591fb1a3f --- /dev/null +++ b/challenge-292/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: WRRBBW RB + input: + output: -1 +- setup: + cleanup: + args: WWRRBBWW WRBRW + input: + output: 2 +- setup: + cleanup: + args: G GGGGG + input: + output: 2 -- cgit