aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvinodk89 <vinodkk89@gmail.com>2025-10-20 16:09:03 +0530
committervinodk89 <vinodkk89@gmail.com>2025-10-20 16:09:03 +0530
commit63e3ee96dfa3635caaac007a48f48379be095666 (patch)
tree5639e7d8987a0783d4eb44617305706378e30703
parent0089e40542c8edad54c99aad1b7e01bfe7050231 (diff)
downloadperlweeklychallenge-club-63e3ee96dfa3635caaac007a48f48379be095666.tar.gz
perlweeklychallenge-club-63e3ee96dfa3635caaac007a48f48379be095666.tar.bz2
perlweeklychallenge-club-63e3ee96dfa3635caaac007a48f48379be095666.zip
Solutions for Challenge-344
-rw-r--r--challenge-344/vinod-k/perl/ch-1.pl59
-rw-r--r--challenge-344/vinod-k/perl/ch-2.pl127
-rw-r--r--challenge-344/vinod-k/python/ch-1.py58
-rw-r--r--challenge-344/vinod-k/python/ch-2.py103
4 files changed, 347 insertions, 0 deletions
diff --git a/challenge-344/vinod-k/perl/ch-1.pl b/challenge-344/vinod-k/perl/ch-1.pl
new file mode 100644
index 0000000000..bed5d4538a
--- /dev/null
+++ b/challenge-344/vinod-k/perl/ch-1.pl
@@ -0,0 +1,59 @@
+use strict;
+use warnings;
+use feature 'say';
+
+sub add_to_array_form {
+ my ($ints_ref, $x) = @_;
+
+ my $int_form = join('', @$ints_ref);
+ my $sum = $int_form + $x;
+ my @result_array = split //, $sum;
+
+ return @result_array;
+}
+
+#Testings
+#Example 1
+my @ints1 = (1, 2, 3, 4);
+my $x1 = 12;
+my @output1 = add_to_array_form(\@ints1, $x1);
+
+say "Input Array: (". (join ', ', @ints1) . ")";
+say "Input X: $x1";
+say "Output Array: (". (join ', ', @output1) . ")\n";
+
+#Example 2
+my @ints2 = (2, 7, 4);
+my $x2 = 181;
+my @output2 = add_to_array_form(\@ints2, $x2);
+
+say "Input Array: (". (join ', ', @ints2) . ")";
+say "Input X: $x2";
+say "Output Array: (". (join ', ', @output2) . ")\n";
+
+#Example 3
+my @ints3 = (9, 9, 9);
+my $x3 = 1;
+my @output3 = add_to_array_form(\@ints3, $x3);
+
+say "Input Array: (". (join ', ', @ints3) . ")";
+say "Input X: $x3";
+say "Output Array: (". (join ', ', @output3) . ")\n";
+
+#Example 4
+my @ints4 = (1, 0, 0, 0, 0);
+my $x4 = 9999;
+my @output4 = add_to_array_form(\@ints4, $x4);
+
+say "Input Array: (". (join ', ', @ints4) . ")";
+say "Input X: $x4";
+say "Output Array: (". (join ', ', @output4) . ")\n";
+
+#Example 5
+my @ints5 = (0);
+my $x5 = 1000;
+my @output5 = add_to_array_form(\@ints5, $x5);
+
+say "Input Array: (". (join ', ', @ints5) . ")";
+say "Input X: $x5";
+say "Output Array: (". (join ', ', @output5) . ")\n";
diff --git a/challenge-344/vinod-k/perl/ch-2.pl b/challenge-344/vinod-k/perl/ch-2.pl
new file mode 100644
index 0000000000..b55bbf6efa
--- /dev/null
+++ b/challenge-344/vinod-k/perl/ch-2.pl
@@ -0,0 +1,127 @@
+use strict;
+use warnings;
+use feature 'say';
+
+sub can_build_target {
+ my ($source_ref, $target_ref) = @_;
+
+ my $target_string = join('', @$target_ref);
+
+ my $source_count = scalar @$source_ref;
+ my %used_source_indices = ();
+ my @used_order = ();
+
+ while (length $target_string) {
+ my $match_found_in_cycle = 0;
+
+ for my $i (0 .. $source_count - 1) {
+ next if exists $used_source_indices{$i};
+
+ my $sub_array_ref = $source_ref->[$i];
+ my $source_pattern = join('', @$sub_array_ref);
+
+ if ($target_string =~ s/^\Q$source_pattern\E//) {
+ $used_source_indices{$i} = 1;
+ push @used_order, $sub_array_ref;
+ $match_found_in_cycle = 1;
+ last;
+ }
+ }
+
+ unless ($match_found_in_cycle) {
+ return (0, []);
+ }
+ }
+
+ return (1, \@used_order);
+}
+
+sub format_array_of_arrays {
+ my $arr_ref = shift;
+ return "(" . (join ', ', map { "[" . (join ',', @$_) . "]" } @$arr_ref) . ")";
+}
+sub format_array {
+ my $arr_ref = shift;
+ return "(" . (join ', ', @$arr_ref) . ")";
+}
+
+
+# Example 1
+my @source1 = ([2, 3], [1], [4]);
+my @target1 = (1, 2, 3, 4);
+
+my ($result1, $order_ref1) = can_build_target(\@source1, \@target1);
+my @order1 = @$order_ref1;
+
+say "Input: \@source = " . format_array_of_arrays(\@source1);
+say "Input: \@target = " . format_array(\@target1);
+say "Output: " . ($result1 ? 'true' : 'false');
+
+if ($result1) {
+ say "Use in the order: " . format_array_of_arrays(\@order1);
+}
+say "\n";
+
+# Example 2
+my @source2 = ([1,3], [2,4]);
+my @target2 = (1, 2, 3, 4);
+
+my ($result2, $order_ref2) = can_build_target(\@source2, \@target2);
+my @order2 = @$order_ref2;
+
+say "Input: \@source = " . format_array_of_arrays(\@source2);
+say "Input: \@target = " . format_array(\@target2);
+say "Output: " . ($result2 ? 'true' : 'false');
+
+if ($result2) {
+ say "Use in the order: " . format_array_of_arrays(\@order2);
+}
+say "\n";
+
+# Example 3
+my @source3 = ([9,1], [5,8], [2]);
+my @target3 = (5, 8, 2, 9, 1);
+
+my ($result3, $order_ref3) = can_build_target(\@source3, \@target3);
+my @order3 = @$order_ref3;
+
+say "Input: \@source = " . format_array_of_arrays(\@source3);
+say "Input: \@target = " . format_array(\@target3);
+say "Output: " . ($result3 ? 'true' : 'false');
+
+if ($result3) {
+ say "Use in the order: " . format_array_of_arrays(\@order1);
+}
+say "\n";
+
+# Example 4
+my @source4 = ([1], [3]);
+my @target4 = (1, 2, 3);
+
+my ($result4, $order_ref4) = can_build_target(\@source4, \@target4);
+my @order4 = @$order_ref4;
+
+say "Input: \@source = " . format_array_of_arrays(\@source4);
+say "Input: \@target = " . format_array(\@target4);
+say "Output: " . ($result4 ? 'true' : 'false');
+
+if ($result4) {
+ say "Use in the order: " . format_array_of_arrays(\@order4);
+}
+say "\n";
+
+# Example 5
+my @source5 = ([7, 4, 6]);
+my @target5 = (7, 4, 6);
+
+my ($result5, $order_ref5) = can_build_target(\@source5, \@target5);
+my @order5 = @$order_ref5;
+
+say "Input: \@source = " . format_array_of_arrays(\@source5);
+say "Input: \@target = " . format_array(\@target5);
+say "Output: " . ($result5 ? 'true' : 'false');
+
+if ($result5) {
+ say "Use in the order: " . format_array_of_arrays(\@order5);
+}
+
diff --git a/challenge-344/vinod-k/python/ch-1.py b/challenge-344/vinod-k/python/ch-1.py
new file mode 100644
index 0000000000..244429f7e8
--- /dev/null
+++ b/challenge-344/vinod-k/python/ch-1.py
@@ -0,0 +1,58 @@
+from typing import List
+
+def add_to_array_form(ints: List[int], x: int) -> List[int]:
+ int_string = "".join(map(str, ints))
+ int_form = int(int_string)
+
+ sum_result = int_form + x
+
+ sum_string = str(sum_result)
+ result_array = [int(digit) for digit in sum_string]
+
+ return result_array
+
+#Tests
+#Example 1
+ints1 = [1, 2, 3, 4]
+x1 = 12
+output1 = add_to_array_form(ints1, x1)
+
+print(f"Input Array: {ints1}")
+print(f"Input X: {x1}")
+print(f"Output Array: {output1}\n")
+
+#Example 2
+ints2 = [2, 7, 4]
+x2 = 181
+output2 = add_to_array_form(ints2, x2)
+
+print(f"Input Array: {ints2}")
+print(f"Input X: {x2}")
+print(f"Output Array: {output2}\n")
+
+#Example 3
+ints3 = [9, 9, 9]
+x3 = 1
+output3 = add_to_array_form(ints3, x3)
+
+print(f"Input Array: {ints3}")
+print(f"Input X: {x3}")
+print(f"Output Array: {output3}\n")
+
+#Example 4
+ints4 = [1, 0, 0, 0, 0]
+x4 = 9999
+output4 = add_to_array_form(ints4, x4)
+
+print(f"Input Array: {ints4}")
+print(f"Input X: {x4}")
+print(f"Output Array: {output4}\n")
+
+#Example 5
+ints5 = [0]
+x5 = 1000
+output5 = add_to_array_form(ints5, x5)
+
+print(f"Input Array: {ints5}")
+print(f"Input X: {x5}")
+print(f"Output Array: {output5}\n") \ No newline at end of file
diff --git a/challenge-344/vinod-k/python/ch-2.py b/challenge-344/vinod-k/python/ch-2.py
new file mode 100644
index 0000000000..618a653970
--- /dev/null
+++ b/challenge-344/vinod-k/python/ch-2.py
@@ -0,0 +1,103 @@
+from typing import List, Tuple
+
+def can_build_target(source: List[List[int]], target: List[int]) -> Tuple[bool, List[List[int]]]:
+ target_string = "".join(map(str, target))
+ source_used = [False] * len(source)
+ used_order = []
+
+ while target_string:
+ match_found_in_cycle = False
+
+ for i in range(len(source)):
+ if source_used[i]:
+ continue
+
+ sub_list = source[i]
+ source_pattern = "".join(map(str, sub_list))
+
+ if target_string.startswith(source_pattern):
+ target_string = target_string[len(source_pattern):]
+ source_used[i] = True
+ used_order.append(sub_list)
+ match_found_in_cycle = True
+
+ if not match_found_in_cycle:
+ return False, []
+
+ return True, used_order
+
+def format_list_of_lists(arr_list: List[List[int]]) -> str:
+ return "(" + ", ".join([f"[{', '.join(map(str, sub))}]" for sub in arr_list]) + ")"
+
+def format_list(arr: List[int]) -> str:
+ return f"({', '.join(map(str, arr))})"
+
+#Example 1
+source1 = [[2, 3], [1], [4]]
+target1 = [1, 2, 3, 4]
+
+result1, order1 = can_build_target(source1, target1)
+
+print(f"Input: source = {format_list_of_lists(source1)}")
+print(f"Input: target = {format_list(target1)}")
+print(f"Output: {str(result1).lower()}")
+
+if result1:
+ print(f"Use in the order: {format_list_of_lists(order1)}")
+print("\n")
+
+#Example 2
+source2 = [[1,3], [2,4]]
+target2 = [1, 2, 3, 4]
+
+result2, order2 = can_build_target(source2, target2)
+
+print(f"Input: source = {format_list_of_lists(source2)}")
+print(f"Input: target = {format_list(target2)}")
+print(f"Output: {str(result2).lower()}")
+
+if result2:
+ print(f"Use in the order: {format_list_of_lists(order2)}")
+print("\n")
+
+#Example 3
+source3 = [[9,1], [5,8], [2]]
+target3 = [5, 8, 2, 9, 1]
+
+result3, order3 = can_build_target(source3, target3)
+
+print(f"Input: source = {format_list_of_lists(source3)}")
+print(f"Input: target = {format_list(target3)}")
+print(f"Output: {str(result3).lower()}")
+
+if result3:
+ print(f"Use in the order: {format_list_of_lists(order3)}")
+print("\n")
+
+#Example 4
+source4 = [[1], [3]]
+target4 = [1, 2, 3]
+
+result4, order4 = can_build_target(source4, target4)
+
+print(f"Input: source = {format_list_of_lists(source4)}")
+print(f"Input: target = {format_list(target4)}")
+print(f"Output: {str(result4).lower()}")
+
+if result4:
+ print(f"Use in the order: {format_list_of_lists(order4)}")
+print("\n")
+
+#Example 5
+source5 = [[7,4,6]]
+target5 = [7, 4, 6]
+
+result5, order5 = can_build_target(source5, target5)
+
+print(f"Input: source = {format_list_of_lists(source5)}")
+print(f"Input: target = {format_list(target5)}")
+print(f"Output: {str(result5).lower()}")
+
+if result5:
+ print(f"Use in the order: {format_list_of_lists(order5)}")
+print("\n") \ No newline at end of file