aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-05-04 00:22:43 +0100
committerGitHub <noreply@github.com>2020-05-04 00:22:43 +0100
commitd8c3b120fa8ebef9abfebad6f79508f2caa78178 (patch)
tree4b05470548587cd44b528e38282aad1c8a1717b0
parent068dd9fc3486625d78923a8919eb220147f8a387 (diff)
parentf39eac74410157ab1a35a0a18d2353923e4ebe80 (diff)
downloadperlweeklychallenge-club-d8c3b120fa8ebef9abfebad6f79508f2caa78178.tar.gz
perlweeklychallenge-club-d8c3b120fa8ebef9abfebad6f79508f2caa78178.tar.bz2
perlweeklychallenge-club-d8c3b120fa8ebef9abfebad6f79508f2caa78178.zip
Merge pull request #1661 from kevincolyer/branch-058
Branch 058
-rw-r--r--challenge-058/kevin-colyer/raku/ch-1.p663
l---------challenge-058/kevincolyer1
2 files changed, 64 insertions, 0 deletions
diff --git a/challenge-058/kevin-colyer/raku/ch-1.p6 b/challenge-058/kevin-colyer/raku/ch-1.p6
new file mode 100644
index 0000000000..bc501c9106
--- /dev/null
+++ b/challenge-058/kevin-colyer/raku/ch-1.p6
@@ -0,0 +1,63 @@
+#!perl6
+# Task 1 Challenge 058 Solution by kevincolyer
+
+# TASK #1 › Compare Version
+# Reviewed by Ryan Thompson
+# Compare two given version number strings v1 and v2 such that:
+#
+# If v1 > v2 return 1
+# If v1 < v2 return -1
+# Otherwise, return 0
+# The version numbers are non-empty strings containing only digits, and the dot (“.”) and underscore (“_”) characters. (“_” denotes an alpha/development version, and has a lower precedence than a dot, “.”). Here are some examples:
+#
+# v1 v2 Result
+# ------ ------ ------
+# 0.1 < 1.1 -1
+# 2.0 > 1.2 1
+# 1.2 < 1.2_5 -1
+# 1.2.1 > 1.2_1 1
+# 1.2.1 = 1.2.1 0
+# Version numbers may also contain leading zeros. You may handle these how you wish, as long as it’s consistent.
+
+# compares versions numbers of type major,minor,revision.
+# Assumes max of 500 revisions and 1000 minor versions.
+sub compare-versions(Str $v1 is copy, Str $v2 is copy) returns Int {
+
+ # convert maj to maj,min,revision
+ $v1 ~= ".0.0" if $v1 ~~ /^ \d+ $/;
+ $v2 ~= ".0.0" if $v2 ~~ /^ \d+ $/;
+ # convert maj,min to maj,min,revision
+ $v1 ~= ".0" if $v1 ~~ /^ \d+ \. \d+ $/;
+ $v2 ~= ".0" if $v2 ~~ /^ \d+ \. \d+ $/;
+
+ # flag if development version
+ my $dev1 = $v1 ~~ /_/ ?? 1 !! 0;
+ my $dev2 = $v2 ~~ /_/ ?? 1 !! 0;
+
+ # convert maj, min, revision a numeric value
+ # double revision value and reduce by 1 if development version
+ my ($a,$b,$c)=$v1.split(/ \. | _ /);
+ $c=0 if ! $c.defined;
+ my $val1=($a*1000+$b)*1000+$c*2-$dev1;
+
+ ($a,$b,$c) = $v2.split(/ \. | _ /);
+ $c=0 if ! $c.defined;
+ my $val2=($a*1000+$b)*1000+$c*2-$dev2;
+
+ return 1 if $val1 > $val2;
+ return -1 if $val1 < $val2;
+ return 0;
+}
+
+use Test;
+is compare-versions( "0.1","1.1" ) ,-1, " 0.1 < 1.1 ";
+is compare-versions( "2.0","1.2" ) , 1, " 2.0 > 1.2 ";
+is compare-versions( "1.2","1.2_5") ,-1, " 1.2 < 1.2_5";
+is compare-versions( "1.2.1","1.2_1") , 1, " 1.2.1 > 1.2_1";
+is compare-versions( "1.2.1","1.2.1") , 0, " 1.2.1 = 1.2.1";
+# edge case
+is compare-versions( "2","1.2.1") , 1 , "edge case 2 > 1.2.1";
+is compare-versions( "2","1.2_1") , 1 , "edge case 2 > 1.2_1";
+
+is compare-versions( "2.1","2_1") , 1 , "edge case 2.1 > 2_1";
+is compare-versions( "1","2") ,-1 , "edge case 1 < 2";
diff --git a/challenge-058/kevincolyer b/challenge-058/kevincolyer
new file mode 120000
index 0000000000..8fc47c15c2
--- /dev/null
+++ b/challenge-058/kevincolyer
@@ -0,0 +1 @@
+kevin-colyer \ No newline at end of file