Skip to content

Commit

Permalink
new files and changes
Browse files Browse the repository at this point in the history
  • Loading branch information
trizen committed Jul 24, 2015
1 parent 4cef601 commit bfb281f
Show file tree
Hide file tree
Showing 18 changed files with 584 additions and 2,073 deletions.
668 changes: 17 additions & 651 deletions Analyzers/perl_code_analyzer.pl

Large diffs are not rendered by default.

132 changes: 132 additions & 0 deletions Finders/fsf.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/usr/bin/perl

# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 23 July 2015
# http://github.com/trizen

# Find files which have almost the same content.

use 5.014;
use strict;
use warnings;

use Math::BigInt (try => 'GMP');

use File::Find qw(find);
use Getopt::Long qw(GetOptions);

sub help {
my ($code) = @_;

print <<"HELP";
usage: $0 [options] /my/path [...]
Options:
-w --whitespaces! : remove whitespaces (default: false)
-u --unique! : don't include a file in more groups (default: false)
-h --help : print this message and exit
Example:
$0 -w ~/Documents
HELP

exit($code // 0);
}

my $strip_spaces = 0; # bool
my $unique = 0; # bool

GetOptions(
'w|whitespaces!' => \$strip_spaces,
'u|unique!' => \$unique,
'h|help' => \&help,
)
or die("Error in command line arguments");

sub hash ($) {
my ($str) = @_;

$strip_spaces
and $str =~ s/\s+//g;

state $ten = Math::BigInt->new(10);

my $hash1 = Math::BigInt->new(0);
my $pow = Math::BigInt->new(1);

state $chars = {};
my @chars = map { $chars->{$_} //= Math::BigInt->new($_) } unpack("C*", $str);

foreach my $char (@chars) {
$hash1->badd($pow->copy->bmul($char));
$pow->bmul($ten);
}

return $hash1;
}

sub hash_file ($) {
my ($file) = @_;
open my $fh, '<:raw', $file;
hash(
do { local $/; <$fh> }
);
}

sub alike_hashes ($$) {
my ($h1, $h2) = @_;

my $pow = abs($h1->copy->blog(10) - $h2->copy->blog(10));

my $ratio = ($h2 > $h1 ? ($h2 / $h1) : ($h1 / $h2));
my $limit = 10**$pow;

$ratio == $limit;
}

sub find_similar_files (&@) {
my $code = shift;

my @files;
find {
wanted => sub {
(-f)
&& push @files,
{
hash => hash_file($File::Find::name),
name => $File::Find::name,
};
}
} => @_;

my %dups;
foreach my $i (0 .. $#files - 1) {
for (my $j = $i + 1 ; $j <= $#files ; $j++) {
if (alike_hashes($files[$i]{hash}, $files[$j]{hash})) {
push @{$dups{$files[$i]{name}}},
(
$unique
? ${splice @files, $j--, 1}{name}
: $files[$j]{name}
);
}
}
}

while (my ($fparent, $fdups) = each %dups) {
$code->(sort $fparent, @{$fdups});
}

return 1;
}

{
@ARGV || help(1);
local $, = "\n";
find_similar_files {
say @_, "-" x 80 if @_;
}
@ARGV;
}
12 changes: 6 additions & 6 deletions Finders/fsfn.pl
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ ($$)
($d[-1][-1] // $min) <= $diff ? 0 : 1;
}

sub find_duplicated_files (&@) {
sub find_similar_filenames (&@) {
my $code = shift;

my %files;
Expand All @@ -145,8 +145,8 @@ (&@)
},
real_name => $File::Find::name,
};
}
} => @_;
}
} => @_;

foreach my $files (values %files) {

Expand Down Expand Up @@ -177,9 +177,9 @@ (&@)
}

{
(my @dirs = grep { -d } @ARGV) || help(1);
@ARGV || help(1);
local $, = "\n";
find_duplicated_files {
find_similar_filenames {

say @_, "-" x 80 if @_;

Expand All @@ -191,5 +191,5 @@ (&@)
unlink $_[$i] or warn "[error]: Can't delete: $!\n";
}
}
@dirs;
@ARGV;
}
62 changes: 62 additions & 0 deletions GD/factorial_turtles.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/perl

# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 20 July 2015
# Website: https://github.com/trizen

# An image generator based on the following formula: n!/(n-1)!, n!/(n-2)!, ... n!/(n-n)!

# Simplified as:
# n!/(n-1)! = n
# n!/(n-2)! = n * (n-1)
# n!/(n-3)! = n * (n-1) * (n-2)

use 5.010;
use strict;
use warnings;
use GD::Simple;

use bignum (try => 'GMP');
use File::Spec::Functions qw(catfile);

my $beg = 3; # start point
my $end = 30; # end point
my $dir = 'Factorial turtles'; # where to output the images

if (not -d $dir) {
mkdir($dir)
or die "Can't mkdir `$dir': $!";
}

foreach my $n ($beg .. $end) {

{
local $| = 1;
printf("[%3d of %3d]\r", $n, $end);
}

my $img = 'GD::Simple'->new(5000, 5000);
$img->moveTo(2500, 2500);
$img->fgcolor('red');

my @values;
my $p = 1;
foreach my $j (0 .. $n - 1) {
$p *= $n - $j;
push @values, $p;
}

for my $i (1 .. 100) {
foreach my $value (@values) {
$img->line($i);
$img->turn($value);
}
}

my $image_name = catfile($dir, sprintf('%03d.png', $n));

open my $fh, '>:raw', $image_name or die $!;
print {$fh} $img->png;
close $fh;
}
56 changes: 56 additions & 0 deletions GD/fibonacci_spirals.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/perl

# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 18 July 2015
# https://github.com/trizen

#
## Generate a Fibonacci cluster of spirals.
#

use 5.010;
use strict;
use warnings;
use GD::Simple;

my $img = 'GD::Simple'->new(8000, 8000);
$img->moveTo(3500, 3500);

sub t($) {
$img->turn(shift);
}

sub l($) {
$img->line(shift);
}

sub c($) {
$img->fgcolor(shift);
}

sub fibonacci(&$) {
my ($callback, $n) = @_;
my @fib = (1, 1);
for (1 .. $n - 2) {
$callback->($fib[0]);
@fib = ($fib[-1], $fib[-1] + $fib[-2]);
}
$callback->($_) for @fib;
}

c 'red';
for my $i (1 .. 180) {
fibonacci {
l $_[0]**(1 / 11);
t $i;
}
$i;
t 0;
}

my $image_name = 'fibonacci_spirals.png';

open my $fh, '>:raw', $image_name or die $!;
print {$fh} $img->png;
close $fh;
4 changes: 1 addition & 3 deletions GD/mathematical_butt.pl
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ ($)

my $image_name = 'turtle.png';

open my $fh, '>', $image_name or die $!;
open my $fh, '>:raw', $image_name or die $!;
print {$fh} $img->png;
close $fh;

system "gliv", $image_name;
2 changes: 1 addition & 1 deletion GD/random_abstract_art.pl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# create a new image
my $img = GD::Simple->new($limit * 3, $limit * 3);

# move to center
# move to the center
$img->moveTo($limit * 1.5, $limit * 1.5);

my $i = 1;
Expand Down
2 changes: 1 addition & 1 deletion GD/random_abstract_art_2.pl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# create a new image
my $img = GD::Simple->new($limit * 3, $limit * 3);

# move to right
# move to the center
$img->moveTo($limit * 1.5, $limit * 1.5);

my $i = 1;
Expand Down
45 changes: 45 additions & 0 deletions GD/random_machinery_art.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/perl

# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 21 July 2015
# http://github.com/trizen

#
## Generate a complex machine-like art based on simple mathematics.
#

use 5.010;
use strict;
use warnings;
use GD::Simple;
use List::Util qw(shuffle);

my $max = 1_000_000;
my $limit = int(sqrt($max));

say "Possible combinations: $limit!";

# create a new image
my $img = GD::Simple->new($limit * 3, $limit * 3);

# move to the center
$img->moveTo($limit * 1.5, $limit * 1.5);

my $i = 1;
my $j = 1;

for my $m (shuffle(1 .. $limit)) {

for my $n ($j .. $i**2) {
$img->line(1);
$img->turn($n * $i + $m);
++$j;
}

++$i;
}

open my $fh, '>:raw', "random_machinery.png";
print $fh $img->png;
close $fh;
Loading

0 comments on commit bfb281f

Please sign in to comment.