diff options
| -rw-r--r-- | challenge-058/lubos-kolouch/perl/ch-1.pl | 51 | ||||
| -rwxr-xr-x | challenge-058/lubos-kolouch/python/ch-1.py | 29 |
2 files changed, 80 insertions, 0 deletions
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..269ac6bf8a --- /dev/null +++ b/challenge-058/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/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; + +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=132482 +is(compare_versions('1.2.1','1.2_1'),1); +is(compare_versions('1.2.1','1.2.1'),0); 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 |
