Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Programming Pushups part II

Name: Anonymous 2015-01-05 12:04

Write a program to compare two version strings.

Example comparisons:
1.05.00.0156 > 1.0.221.9289
1 < 1.0.1
1.0.1 < 1.0.2
1.0.2 < 1.0.3
1.0.3 < 1.1
1.1 < 1.1.1
1.1.1 < 1.1.2
1.1.2 < 1.2

Name: Anonymous 2015-01-05 12:13

It's called lexicographic ordering, you dolt.

Name: Anonymous 2015-01-05 12:17

let xs = (str1 # split "." >>> map (read :: String -> Int)) `zipWith` (str1 # split "." >>> map (read :: String -> Int))
in
and (\x -> fst x >= snd x) xs

Name: Anonymous 2015-01-05 12:19

repnz cmpsb

Name: Anonymous 2015-01-05 12:20

>>44
dubz chckm

Name: Anonymous 2015-01-05 12:22

#!/usr/bin/python3

import sys

def version_string_to_integer(vstr):
return int(''.join(s.lsplit('0') for s in vstr.split('.')))

a, b = map(version_string_to_integer, sys.argv[1:3])
if a < b:
print('lt')
elif a > b:
print('gt')
else:
print('eq')


I didn't bother to try if it works since it's so trivial program

Name: Anonymous 2015-01-05 12:36

>>6
That feel when a language is so retarded that not everything in it is an expression.

Name: Anonymous 2015-01-05 15:48

>>3
that's pretty awful

Name: Anonymous 2015-01-06 10:05

>>8
That was pretty much muh purpose. But if you can do better, why don't you post it here?

Name: Anonymous 2015-01-06 17:54

>>9
import Data.List.Split

newtype Version = V [Int] deriving (Eq, Ord)

mkV :: String -> Version
mkV = V . map read . splitOn "."

Name: Anonymous 2015-01-06 23:14

function c(a,b)
local x,y = a:gmatch('%d+'), b:gmatch('%d+')
for i=1,math.max(#a,#b) do
local a = tonumber(x() or 0)
local b = tonumber(y() or 0)
if a ~= b then
return a-b
end
end
return 0
end

Name: Anonymous 2015-01-07 2:43

Io> Version[1.0.0] == Version[1]
==> true
Io> // etc.

// Version.io (in cwd)
Version := Object clone do(
version ::= list()
fromString := method(s,
self clone setVersion(s split(".") \
map(asNumber) \
map(x, if(x isNan or(x isNil), 0, x))))

size := method(self version size)
at := method(x, self version at(x))
push := method(x, self version push(x))

cmp := method(other,
o := other clone
s := self clone
while(s size > o size, o push(0))
while(o size > s size, s push(0))

s version foreach(i,v,
if(v > o at(i), return 1, if(v < o at(i), return -1)))
0)
)

Version setSlot(">", method(o, self cmp(o) > 0))
Version setSlot(">=", method(o, self cmp(o) >= 0))
Version setSlot("<", method(o, self cmp(o) < 0))
Version setSlot("<=", method(o, self cmp(o) <= 0))
Version setSlot("==", method(o, self cmp(o) == 0))

Name: Anonymous 2015-01-07 3:02

Perl 5.
[code]
#!/usr/bin/env perl
use strict;
use warnings;

use 5.10.0;

# The subroutine itself
sub cmpver {
my $alen = my @a = split /\./, $_[0];
my $blen = my @b = split /\./, $_[1];

push @a, (0) x ($blen - $alen);
push @b, (0) x ($alen - $blen);

for (0 .. @a - 1) {
return +1 if $a[$_] > $b[$_];
return -1 if $a[$_] < $b[$_];
}

0;
}

# Testing
say cmpver('1.157.2821.9289', '1.05.00.0156.789'); # 1 (bigger)
say cmpver('1.09.1' ,'1.0.2'); # 1 (bigger)
say cmpver('1.5.3' ,'1.1'); # 1 (bigger)
say cmpver('0.1.8' ,'1.1.1'); # -1 (smaller)
say cmpver('1.1.1' ,'1.1.1'); # undef (equal)
[code]

Name: Anonymous 2015-01-07 3:20

>>13
Or you can just say fuck the rules.

#!/usr/bin/env perl
use strict;
use warnings;

use 5.10.0;

sub cmpver { no warnings; $_[0] <=> $_[1] }

# Testing
say cmpver('1.157.2821.9289', '1.05.00.0156.789'); # 1 (bigger)
say cmpver('1.09.1' ,'1.0.2'); # 1 (bigger)
say cmpver('1.5.3' ,'1.1'); # 1 (bigger)
say cmpver('0.1.8' ,'1.1.1'); # -1 (smaller)
say cmpver('1.1.1' ,'1.1.1'); # undef (equal)

Name: Anonymous 2015-01-07 3:23

Or don't even use warnings, if you are a son of a bitch. Here it is, the perl version.

sub cmpver { $_[0] <=> $_[1] }

Fuck, you don't even neet a function for this shit. Just code it directly:

print '1.157.2821.9289' <=> '1.05.00.0156.789', "\n"; # will print 1

Name: Anonymous 2015-01-07 8:11

>>13-15
Larry Wall please go.

Name: Anonymous 2015-01-07 9:03

v :: String -> [Integer]
v = map read . words . map sub

sub '.' = ' '
sub c = c

tests =
[ v "1.05.00.0156" > v "1.0.221.9289"
, v "1" < v "1.0.1"
, v "1.0.1" < v "1.0.2"
, v "1.0.2" < v "1.0.3"
, v "1.0.3" < v "1.1"
, v "1.1" < v "1.1.1"
, v "1.1.1" < v "1.1.2"
, v "1.1.2" < v "1.2"
]

-- *Main> tests
-- [True,True,True,True,True,True,True,True]

Name: Anonymous 2015-01-07 9:52

>>17
v ... > v ...
v ... > v ...
v ... > v ...

Optimize you are broilerplate.

Name: Anonymous 2015-01-07 9:58

>>6
You're actually retarded, by the way.

def parse_version(s):
return tuple(map(int, s.split('.')))

def test(s):
for s in s.split('\n'):
s1, rel, s2 = s.split()
v1, v2 = parse_version(s1), parse_version(s2)
assert rel == '=><'[cmp(v1, v2)]
print 'OK'

test('''1.05.00.0156 > 1.0.221.9289
1 < 1.0.1
1.0.1 < 1.0.2
1.0.2 < 1.0.3
1.0.3 < 1.1
1.1 < 1.1.1
1.1.1 < 1.1.2
1.1.2 < 1.2''')

Name: Cudder !MhMRSATORI 2015-01-07 13:25

int cmpver(char *v1, char *v2) {
int i = 0, j = 0;
while(v1 && v2 && (i=atoi(v1)) == (j=atoi(v2))) {
v1=strchr(v1, '.'); v1 = v1 ? v1 + 1 : v1;
v2=strchr(v2, '.'); v2 = v2 ? v2 + 1 : v2;
if(!v1 || !v2) return (v1 > v2) - (v2 > v1);
}
return (i > j) - (j > i);
}

Name: Anonymous 2015-01-08 14:26

Come on, people, we need to push towards >>44 in order to check 'em.

Name: Anonymous 2015-01-08 16:52


Don't change these.
Name: Email:
Entire Thread Thread List