aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-05-01 11:23:29 +0100
committerGitHub <noreply@github.com>2020-05-01 11:23:29 +0100
commit4a7c0bdf1cefe3be29d2ca4ba041af849f069ff5 (patch)
tree259f235efd9c2390fcf67dfa1c4d515f30334f44
parent6aa5a8b48c485e49cd75c5188828cc7016d6d13f (diff)
parente9e74bac39e7227afb6590a01e6152c272a2660f (diff)
downloadperlweeklychallenge-club-4a7c0bdf1cefe3be29d2ca4ba041af849f069ff5.tar.gz
perlweeklychallenge-club-4a7c0bdf1cefe3be29d2ca4ba041af849f069ff5.tar.bz2
perlweeklychallenge-club-4a7c0bdf1cefe3be29d2ca4ba041af849f069ff5.zip
Merge pull request #1654 from manfredi/challenge-058
Added solution Task #1
-rwxr-xr-xchallenge-058/manfredi/bash/ch-1.sh32
-rwxr-xr-xchallenge-058/manfredi/perl/ch-1.pl53
-rwxr-xr-xchallenge-058/manfredi/python/ch-1.py45
3 files changed, 130 insertions, 0 deletions
diff --git a/challenge-058/manfredi/bash/ch-1.sh b/challenge-058/manfredi/bash/ch-1.sh
new file mode 100755
index 0000000000..ea8373b14d
--- /dev/null
+++ b/challenge-058/manfredi/bash/ch-1.sh
@@ -0,0 +1,32 @@
+#!/bin/bash
+set -e
+
+compare() {
+ v1=${1//_/-}
+ v2=${2//_/-}
+ [[ $1 == $2 ]] && cmp="0"
+ [[ $v1 > $v2 ]] && cmp="1"
+ [[ $v1 < $v2 ]] && cmp="-1"
+ echo $cmp
+}
+
+declare -A sig=( ['0']='=' ['1']='>' ['-1']='<')
+
+declare -a data=(
+ "0.1 1.1"
+ "2.0 1.2"
+ "1.2 1.2_5"
+ "1.2.1 1.2_1"
+ "1.2.1 1.2.1"
+)
+
+
+printf "%10s %10s %s\n" 'v1' 'v2' 'Result'
+printf "%10s %10s %s\n" `printf '%.s-' $(seq 10)` `printf '%.s-' $(seq 10)` `printf '%.s-' $(seq 6)`
+
+for i in "${data[@]}"
+do
+ declare -a v=($(echo "$i" | tr -s ' ' '\n'))
+ cmp=$(compare $i)
+ printf "%10s %s %10s %5s\n" ${v[0]} ${sig[$cmp]} ${v[1]} $cmp
+done
diff --git a/challenge-058/manfredi/perl/ch-1.pl b/challenge-058/manfredi/perl/ch-1.pl
new file mode 100755
index 0000000000..f196582adb
--- /dev/null
+++ b/challenge-058/manfredi/perl/ch-1.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+
+sub compare($$) {
+ my ($v1, $v2) = @_;
+ my $cmp = 0;
+
+ return $cmp if $v1 eq $v2;
+
+ # To have lower precedence for underscore, use ascii code smaller than dot's ascii code
+ $v1 =~ tr|_|-|;
+ $v2 =~ tr|_|-|;
+
+ # version with ascii number codes
+ #my @v1 = map { ord } split '', $v1;
+ #my @v2 = map { ord } split '', $v2;
+
+ # Fills with zero until same length
+ #push @v1, 0 while @v1 < @v2;
+ #push @v2, 0 while @v2 < @v1;
+
+ #while (@v1) {
+ # if ( $cmp = shift(@v1) <=> shift(@v2) ) {
+ # last;
+ # }
+ #}
+
+ # version with bare string compare
+ $cmp = $v1 cmp $v2;
+
+ $cmp;
+}
+
+my %sig = ( '0' => '=', '1' => '>', '-1' => '<');
+printf "%10s %10s %s\n", 'v1', 'v2', 'Result';
+printf "%10s %10s %s\n", '-' x 10, '-' x 10, '-' x 6;
+
+while (<DATA>) {
+ chomp;
+ my ($v1, $v2) = split /\s+/;
+ my $cmp = compare($v1, $v2);
+ printf "%10s %s %10s %5s\n", $v1, $sig{$cmp}, $v2, $cmp;
+}
+
+
+__DATA__
+0.1 1.1
+2.0 1.2
+1.2 1.2_5
+1.2.1 1.2_1
+1.2.1 1.2.1
diff --git a/challenge-058/manfredi/python/ch-1.py b/challenge-058/manfredi/python/ch-1.py
new file mode 100755
index 0000000000..b000a54d56
--- /dev/null
+++ b/challenge-058/manfredi/python/ch-1.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python3
+
+def compare(v1, v2):
+ cmp = 0
+ if v1 is v2: return cmp
+
+ # To have lower precedence for underscore, use ascii code smaller than dot's ascii code
+ v1 = v1.replace('_', '-')
+ v2 = v2.replace('_', '-')
+
+ a1 = [ord(c) for c in v1]
+ a2 = [ord(c) for c in v2]
+
+ # Fills with zero until same length
+ while len(a1) < len(a2): a1.append(0)
+ while len(a2) < len(a1): a2.append(0)
+
+ while True:
+ if len(a1) is 0: break
+ t1 = a1.pop(0)
+ t2 = a2.pop(0)
+ cmp = (t1 > t2) - (t1 < t2) # simulate cmp() for python 3.x
+ if cmp is not 0: break
+
+ return cmp
+
+data = '''\
+0.1 1.1
+2.0 1.2
+1.2 1.2_5
+1.2.1 1.2_1
+1.2.1 1.2.1
+'''
+
+items = data.splitlines()
+
+sig = {'0': '=', '1': '>', '-1': '<'}
+
+print("%10s %10s %s" % ('v1', 'v2', 'Result'))
+print("%10s %10s %s" % ('-' * 10, '-' * 10, '-' * 6))
+
+for item in items:
+ v1, v2 = item.split()
+ ret = compare(v1, v2)
+ print("%10s %s %10s %5s" % (v1, sig[str(ret)], v2, ret))