diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-04-20 09:56:31 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-04-20 09:56:31 +0200 |
| commit | ad4831edfeb0d7763cf9280a42246c06d154ed30 (patch) | |
| tree | 3623b839b7fbd38ba28b1ef16b1aab2bf5b5bb4c | |
| parent | 67be0f9ab853d4740b3c1d6d3954a46cc63afa13 (diff) | |
| download | perlweeklychallenge-club-ad4831edfeb0d7763cf9280a42246c06d154ed30.tar.gz perlweeklychallenge-club-ad4831edfeb0d7763cf9280a42246c06d154ed30.tar.bz2 perlweeklychallenge-club-ad4831edfeb0d7763cf9280a42246c06d154ed30.zip | |
Challenge 213 LK Perl Python
| -rw-r--r-- | challenge-213/lubos-kolouch/perl/ch-1.pl | 16 | ||||
| -rw-r--r-- | challenge-213/lubos-kolouch/perl/ch-2.pl | 46 | ||||
| -rw-r--r-- | challenge-213/lubos-kolouch/python/ch-1.py | 17 | ||||
| -rw-r--r-- | challenge-213/lubos-kolouch/python/ch-2.py | 48 |
4 files changed, 127 insertions, 0 deletions
diff --git a/challenge-213/lubos-kolouch/perl/ch-1.pl b/challenge-213/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..cf3e705ec2 --- /dev/null +++ b/challenge-213/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub fun_sort { + my @list = @_; + my @even = sort { $a <=> $b } grep { $_ % 2 == 0 } @list; + my @odd = sort { $a <=> $b } grep { $_ % 2 == 1 } @list; + return (@even, @odd); +} + +my @input = (1, 2, 3, 4, 5, 6); +my @output = fun_sort(@input); +print "Input: \@list = (", join(",", @input), ")\n"; +print "Output: (", join(",", @output), ")\n"; + diff --git a/challenge-213/lubos-kolouch/perl/ch-2.pl b/challenge-213/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..8701741f6a --- /dev/null +++ b/challenge-213/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub shortest_route { + my ($routes, $source, $destination) = @_; + my %graph; + + for my $route (@$routes) { + for my $i (0 .. @$route - 2) { + push @{$graph{$route->[$i]}}, $route->[$i + 1]; + push @{$graph{$route->[$i + 1]}}, $route->[$i]; + } + } + + my @queue = ([$source]); + my %visited = ($source => 1); + + while (@queue) { + my $path = shift @queue; + my $node = $path->[-1]; + + return @$path if $node == $destination; + + for my $neighbor (@{$graph{$node}}) { + unless ($visited{$neighbor}) { + $visited{$neighbor} = 1; + push @queue, [@$path, $neighbor]; + } + } + } + return -1; +} + +my @routes = ([1, 2, 3], [4, 5, 6], [3, 8, 9], [7, 8]); +my $source = 1; +my $destination = 7; + +my @result = shortest_route(\@routes, $source, $destination); +print "Output: "; +if (@result > 0) { + print "(", join(",", @result), ")\n"; +} else { + print "-1\n"; +} + diff --git a/challenge-213/lubos-kolouch/python/ch-1.py b/challenge-213/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..c70bb43c4f --- /dev/null +++ b/challenge-213/lubos-kolouch/python/ch-1.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def fun_sort(numbers: List[int]) -> List[int]: + even = sorted([num for num in numbers if num % 2 == 0]) + odd = sorted([num for num in numbers if num % 2 == 1]) + return even + odd + + +if __name__ == "__main__": + input_numbers = [1, 2, 3, 4, 5, 6] + output_numbers = fun_sort(input_numbers) + print(f"Input: @list = {input_numbers}") + print(f"Output: {output_numbers}") diff --git a/challenge-213/lubos-kolouch/python/ch-2.py b/challenge-213/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..4ed7324af9 --- /dev/null +++ b/challenge-213/lubos-kolouch/python/ch-2.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List, Union + + +def shortest_route(routes: List[List[int]], source: int, + destination: int) -> Union[int, List[int]]: + graph = {} + + for route in routes: + for i in range(len(route) - 1): + if route[i] not in graph: + graph[route[i]] = [] + if route[i + 1] not in graph: + graph[route[i + 1]] = [] + graph[route[i]].append(route[i + 1]) + graph[route[i + 1]].append(route[i]) + + queue = [[source]] + visited = {source: True} + + while queue: + path = queue.pop(0) + node = path[-1] + + if node == destination: + return path + + for neighbor in graph[node]: + if neighbor not in visited: + visited[neighbor] = True + queue.append(path + [neighbor]) + + return -1 + + +if __name__ == "__main__": + routes = [[1, 2, 3], [4, 5, 6], [3, 8, 9], [7, 8]] + source = 1 + destination = 7 + + result = shortest_route(routes, source, destination) + print("Output: ", end="") + if isinstance(result, list): + print("(", ", ".join(map(str, result)), ")", sep="") + else: + print("-1") |
