From 25a6c66bd9295780ab16bfccc08bff59ace14891 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 2 May 2020 13:30:24 +0200 Subject: Challenge 1 LK --- challenge-058/lubos-kolouch/perl/ch-1.pl | 52 ++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 challenge-058/lubos-kolouch/perl/ch-1.pl diff --git a/challenge-058/lubos-kolouch/perl/ch-1.pl b/challenge-058/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..5e132040d8 --- /dev/null +++ b/challenge-058/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,52 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-1.pl +# +# USAGE: ./ch-1.pl +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-058/ +# +# Compare two given version number strings v1 and v2 such that: +# +# If v1 > v2 return 1 +# If v1 < v2 return -1 +# Otherwise, return 0 +# +# +# OPTIONS: --- +# REQUIREMENTS: --- +# BUGS: --- +# NOTES: --- +# AUTHOR: Lubos Kolouch +# ORGANIZATION: +# VERSION: 1.0 +# CREATED: 05/02/2020 12:57:10 PM +# REVISION: --- +#=============================================================================== + +use strict; +use warnings; +use Perl::Version; + +sub compare_versions { + my ($ver1, $ver2) = @_; + + my $v1 = version->new($ver1); + my $v2 = version->new($ver2); + + return $v1 <=> $v2; +} + +# TESTS + +use Test::More; + +is(compare_versions('0.1','1.1'),-1); +is(compare_versions('2.0','1.2'),1); +is(compare_versions('1.2','1.2_5'),-1); + +# There is likely bug in the module, I have reported it +# https://rt.cpan.org/Ticket/Display.html?id=132481& +is(compare_versions('1.2.1','1.2_1'),1); +is(compare_versions('1.2.1','1.2.1'),0); -- cgit From 402d939f06e708782f632e0e6476a9f62177803d Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 2 May 2020 13:50:15 +0200 Subject: Challenge 1 Python --- challenge-058/lubos-kolouch/python/ch-1.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100755 challenge-058/lubos-kolouch/python/ch-1.py diff --git a/challenge-058/lubos-kolouch/python/ch-1.py b/challenge-058/lubos-kolouch/python/ch-1.py new file mode 100755 index 0000000000..aac84f1fbf --- /dev/null +++ b/challenge-058/lubos-kolouch/python/ch-1.py @@ -0,0 +1,29 @@ +#! /usr/bin/env python +""" Perl weekly challenge 058 + https://perlweeklychallenge.org/blog/perl-weekly-challenge-058/ """ + +from packaging import version + + +def compare_versions(ver_1, ver_2): + """ Compare versions and return the result""" + + v_1 = version.parse(ver_1) + v_2 = version.parse(ver_2) + + if v_1 > v_2: + return 1 + + if v_1 < v_2: + return -1 + + return 0 + + +assert compare_versions('0.1', '1.1') == -1 +assert compare_versions('2.0', '1.2') == 1 + +# This fails, reported : https://github.com/pypa/packaging/issues/299 +assert compare_versions('1.2', '1.2_5') == -1 +assert compare_versions('1.2.1', '1.2_1') == 1 +assert compare_versions('1.2.1', '1.2.1') == 0 -- cgit From e256989a080c9b0fb6cde2a02ec687040e762b77 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 2 May 2020 16:46:22 +0200 Subject: It works with Perl::Version but not version --- challenge-058/lubos-kolouch/perl/ch-1.pl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/challenge-058/lubos-kolouch/perl/ch-1.pl b/challenge-058/lubos-kolouch/perl/ch-1.pl index 5e132040d8..269ac6bf8a 100644 --- a/challenge-058/lubos-kolouch/perl/ch-1.pl +++ b/challenge-058/lubos-kolouch/perl/ch-1.pl @@ -27,7 +27,6 @@ use strict; use warnings; -use Perl::Version; sub compare_versions { my ($ver1, $ver2) = @_; @@ -47,6 +46,6 @@ is(compare_versions('2.0','1.2'),1); is(compare_versions('1.2','1.2_5'),-1); # There is likely bug in the module, I have reported it -# https://rt.cpan.org/Ticket/Display.html?id=132481& +# https://rt.cpan.org/Ticket/Display.html?id=132482 is(compare_versions('1.2.1','1.2_1'),1); is(compare_versions('1.2.1','1.2.1'),0); -- cgit