Skip to content

Commit c59ffb1

Browse files
authored
Merge pull request #12785 from jaldhar/challenge-203
Challenge 203 by Jaldhar H. Vyas.
2 parents 9506417 + 9681237 commit c59ffb1

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/perl
2+
use 5.030;
3+
use warnings;
4+
5+
sub combinations {
6+
my @list = @{$_[0]};
7+
my $length = $_[1];
8+
9+
if ($length <= 1) {
10+
return map [$_], @list;
11+
}
12+
13+
my @combos;
14+
15+
for (my $i = 0; $i + $length <= scalar @list; $i++) {
16+
my $val = $list[$i];
17+
my @rest = @list[$i + 1 .. $#list];
18+
for my $c (combinations(\@rest, $length - 1)) {
19+
push @combos, [$val, @{$c}] ;
20+
}
21+
}
22+
23+
return @combos;
24+
}
25+
26+
my @args = @ARGV;
27+
my @quadruplets;
28+
29+
for my $combo (combinations(\@args, 4)) {
30+
my @sorted = sort {$a <=> $b} @{$combo};
31+
if ($sorted[0] + $sorted[1] + $sorted[2] == $sorted[3]) {
32+
push @quadruplets, \@sorted;
33+
}
34+
}
35+
36+
say scalar @quadruplets;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/perl
2+
use 5.038;
3+
use warnings;
4+
use File::Find;
5+
use File::Spec;
6+
7+
my ($source, $target) = @ARGV;
8+
if (@ARGV != 2) {
9+
die "Usage: $0 source_dir target_dir\n";
10+
}
11+
12+
unless (-d $target) {
13+
mkdir $target;
14+
}
15+
16+
my @dirs;
17+
18+
find(
19+
{
20+
wanted => sub {
21+
if (-d) {
22+
push @dirs, $File::Find::name;
23+
}
24+
},
25+
no_chdir => 1,
26+
},
27+
$source
28+
);
29+
30+
for my $dir (@dirs) {
31+
if ($dir eq $source) {
32+
next;
33+
}
34+
35+
my $relPath = File::Spec->abs2rel($dir, $source);
36+
my $newDir = File::Spec->catdir($target, $relPath);
37+
38+
mkdir $newDir;
39+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/raku
2+
3+
sub MAIN(
4+
*@args
5+
) {
6+
my @quadruplets;
7+
8+
for @args.combinations(4) -> $combo {
9+
my @sorted = @$combo.sort({$^a <=> $^b});
10+
if @sorted[0] + @sorted[1] + @sorted[2] == @sorted[3] {
11+
@quadruplets.push(@sorted);
12+
}
13+
}
14+
15+
@quadruplets.elems.say;
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/raku
2+
use File::Find;
3+
4+
sub MAIN(
5+
Str $source, #= source directory path
6+
Str $target #= target directory path
7+
) {
8+
my $sio = $source.IO;
9+
my $tio = $target.IO;
10+
11+
unless $tio.d {
12+
mkdir $target;
13+
}
14+
15+
my @dirs = find(dir => $sio, type => 'dir');
16+
17+
for @dirs -> $dir {
18+
19+
my $relPath = $dir.relative($sio);
20+
my $newDir = $tio.add($relPath);
21+
22+
mkdir $newDir;
23+
}
24+
}
25+

0 commit comments

Comments
 (0)