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

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-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

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