├── .github ├── code-of-conduct.md ├── contributing.md ├── issue-template.md └── pull-request-template.md ├── algorithms ├── artificial-intelligence │ └── Astar.pm ├── ciphers │ ├── atbash │ │ └── atbash.pl │ ├── autokey │ │ └── autokey.pl │ ├── caesar │ │ └── caesar.pl │ └── vigenere.pl ├── computational-geometry │ └── AreaOfTriangle.pm ├── math │ ├── factorial_iterative.pl │ ├── factorial_recursive.pl │ ├── fibonacci_sequence.pl │ └── levenshtein.pl ├── searches │ ├── binary_search.pl │ └── hill_climbing.pl └── sorting │ ├── bubblesort.pl │ ├── insertionsort.pl │ ├── mergesort.pl │ └── quicksort.pl ├── license ├── readme.md └── src └── .gitkeep /.github/code-of-conduct.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Attribution 36 | 37 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 38 | 39 | [homepage]: http://contributor-covenant.org 40 | [version]: http://contributor-covenant.org/version/1/4/ 41 | -------------------------------------------------------------------------------- /.github/contributing.md: -------------------------------------------------------------------------------- 1 | ## Contributing 2 | 3 | > Please note that this project is released with a [Contributor Code of Conduct](code-of-conduct.md). By participating in this project you agree to abide by its terms. -------------------------------------------------------------------------------- /.github/issue-template.md: -------------------------------------------------------------------------------- 1 | I am creating an issue because... 2 | -------------------------------------------------------------------------------- /.github/pull-request-template.md: -------------------------------------------------------------------------------- 1 | I am creating a pull request for... 2 | 3 | - [ ] New algorithm 4 | - [ ] Update to an algorithm 5 | - [ ] Fix an error 6 | - [ ] Other - *Describe below* -------------------------------------------------------------------------------- /algorithms/artificial-intelligence/Astar.pm: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl 2 | package Astar; # A*: https://en.wikipedia.org/wiki/A*_search_algorithm 3 | use strict; # MinPQ: https://algs4.cs.princeton.edu/24pq/ 4 | use Exporter qw< import >; 5 | our @EXPORT_OK = qw< astar >; 6 | 7 | =pod 8 | 9 | =head1 SYNOPSIS 10 | 11 | use Astar qw< astar >; 12 | 13 | # Arguments, M for Mandatory, O for Optional 14 | my %args = ( 15 | start => $node1, # M, node in your graph 16 | goal => $node2, # M, node in your graph 17 | distance => \&dsub, # M, subref, takes 2 nodes, returns number 18 | successors => \&ssub, # M, subref, takes 1 node, returns nodes list 19 | heuristic => \&dsub, # O, subref like distance, defaults to distance 20 | identifier => \&dsub, # O, subref, takes 1 node, returns id, 21 | # defaults to stringification of input node 22 | ); 23 | 24 | # get a list back 25 | @path = astar(%args); 26 | @path = astar(\%args); # works with reference to hash too 27 | 28 | # get an array reference back, containing the list above 29 | $path = astar(%args); 30 | $path = astar(\%args); # works with reference to hash too 31 | 32 | =cut 33 | 34 | sub astar { 35 | my %args = (@_ && ref($_[0])) ? %{$_[0]} : @_; 36 | my @reqs = qw< start goal distance successors >; 37 | exists($args{$_}) || die "missing parameter '$_'" for @reqs; 38 | my ($start, $goal, $dist, $succs) = @args{@reqs}; 39 | my $h = $args{heuristic} || $dist; 40 | my $id_of = $args{identifier} || sub { return "$_[0]" }; 41 | 42 | my ($id, $gid) = ($id_of->($start), $id_of->($goal)); 43 | my %node_for = ($id => {value => $start, g => 0}); 44 | my $queue = bless ['-', {id => $id, f => 0}], __PACKAGE__; 45 | 46 | while (!$queue->_is_empty) { 47 | my $cid = $queue->_dequeue->{id}; 48 | my $cx = $node_for{$cid}; 49 | next if $cx->{visited}++; 50 | 51 | my $cv = $cx->{value}; 52 | return __unroll($cx, \%node_for) if $cid eq $gid; 53 | 54 | for my $sv ($succs->($cv)) { 55 | my $sid = $id_of->($sv); 56 | my $sx = $node_for{$sid} ||= {value => $sv}; 57 | next if $sx->{visited}; 58 | my $g = $cx->{g} + $dist->($cv, $sv); 59 | next if defined($sx->{g}) && ($g >= $sx->{g}); 60 | @{$sx}{qw< p g >} = ($cid, $g); # p: id of best "previous" 61 | $queue->_enqueue({id => $sid, f => $g + $h->($sv, $goal)}); 62 | } ## end for my $sv ($succs->($cv...)) 63 | } ## end while (!$queue->_is_empty) 64 | 65 | return; 66 | } ## end sub astar 67 | 68 | # Functions implementing a minimal priority queue 69 | sub _dequeue { # includes "sink" 70 | my ($k, $self) = (1, @_); 71 | my $r = ($#$self > 1) ? (splice @$self, 1, 1, pop @$self) : pop @$self; 72 | while ((my $j = $k * 2) <= $#$self) { 73 | ++$j if ($j < $#$self) && ($self->[$j + 1]{f} < $self->[$j]{f}); 74 | last if $self->[$k]{f} < $self->[$j]{f}; 75 | (@{$self}[$j, $k], $k) = (@{$self}[$k, $j], $j); 76 | } 77 | return $r; 78 | } ## end sub _dequeue 79 | 80 | sub _enqueue { # includes "swim" 81 | my ($self, $node) = @_; 82 | push @$self, $node; 83 | my $k = $#$self; 84 | (@{$self}[$k / 2, $k], $k) = (@{$self}[$k, $k / 2], int($k / 2)) 85 | while ($k > 1) && ($self->[$k]{f} < $self->[$k / 2]{f}); 86 | } ## end sub _enqueue 87 | 88 | sub _is_empty { return !$#{$_[0]} } 89 | 90 | sub __unroll { # unroll the path from start to goal 91 | my ($node, $node_for, @path) = ($_[0], $_[1], $_[0]{value}); 92 | while (defined(my $p = $node->{p})) { 93 | $node = $node_for->{$p}; 94 | unshift @path, $node->{value}; 95 | } 96 | return wantarray ? @path : \@path; 97 | } ## end sub __unroll 98 | 99 | 100 | ######################################################################## 101 | # 102 | # What follows is just to show an example usage of astar and can be 103 | # ignored if using this implementation as a module. 104 | # 105 | ######################################################################## 106 | 107 | sub __example_get_field { 108 | my $field_text = <<'END_OF_FIELD'; 109 | ################### 110 | # # # # 111 | # # # # # X# 112 | # # # # # # # 113 | #@ # # # # # 114 | # # # # # # # 115 | # # # # # # 116 | # # # # # 117 | ################### 118 | END_OF_FIELD 119 | my ($player, $target, @field); 120 | for my $line (split m{\s*\n}mxs, $field_text) { 121 | push @field, [split m{}mxs, $line]; 122 | for my $j (0 .. $#{$field[-1]}) { 123 | my $c = $field[-1][$j]; 124 | if ($c eq '@') { 125 | $player = [$#field, $j]; 126 | } 127 | elsif ($c eq 'X') { 128 | $target = [$#field, $j]; 129 | } 130 | } 131 | } 132 | return ($player, $target, \@field); 133 | } 134 | 135 | sub __example_distance { 136 | my ($x, $y) = map { $_->{pos} } @_; 137 | return abs($x->[0] - $y->[0]) + abs($x->[1] + $y->[1]); 138 | } 139 | 140 | sub __example_identifier { 141 | my $x = $_[0]{pos}; 142 | return "($x->[0], $x->[1])"; 143 | } 144 | 145 | sub __example_successors { 146 | my ($I, $J, $field) = (@{$_[0]{pos}}, $_[0]{field}); 147 | my @retval; 148 | for my $i ($I - 1 .. $I + 1) { 149 | next if $i < 0 || $i > $#$field; 150 | for my $j ($J - 1 .. $J + 1) { 151 | next if ($i == $I) && ($j == $J); # avoid same position 152 | next if $j < 0 || $j > $#{$field->[$i]}; 153 | next if $field->[$i][$j] eq '#'; 154 | push @retval, { 155 | pos => [$i, $j], 156 | field => $field, 157 | }; 158 | } 159 | } 160 | return @retval; 161 | } 162 | 163 | sub __example_print_field { 164 | my ($field) = @_; 165 | for my $line (@$field) { 166 | print {*STDOUT} join('', @$line), "\n"; 167 | } 168 | } 169 | 170 | sub __MAIN { 171 | my ($player, $target, $field) = __example_get_field(); 172 | 173 | # Arguments, M for Mandatory, O for Optional 174 | my %args = ( 175 | start => { pos => $player, field => $field }, 176 | goal => { pos => $target, field => $field }, 177 | distance => \&__example_distance, 178 | successors => \&__example_successors, 179 | identifier => \&__example_identifier, 180 | ); 181 | 182 | my @path = astar(%args); 183 | 184 | print {*STDOUT} "initial field (\@: player, X: target):\n"; 185 | __example_print_field($field); 186 | 187 | shift @path; # first position in path is player's position 188 | pop @path; # last position is target's position 189 | my $char = 1; 190 | for my $step (@path) { 191 | my ($i, $j) = @{$step->{pos}}; 192 | $field->[$i][$j] = $char; 193 | if ($char ne '.') { 194 | ++$char; 195 | $char = '.' if $char > 3; 196 | } 197 | } 198 | print {*STDOUT} "\nfield with path (1, 2, 3, ...):\n"; 199 | __example_print_field($field); 200 | } 201 | 202 | ######################################################################## 203 | # 204 | # Restrict running MAIN only to when this file is executed, not when 205 | # it is "use"-d or "require"-d, according to the "Modulino" trick. 206 | # 207 | # See https://gitlab.com/polettix/notechs/snippets/1868370 208 | 209 | exit sub { 210 | __MAIN(@_); 211 | return 0; 212 | }->(@ARGV) unless caller; 213 | 214 | 1; 215 | -------------------------------------------------------------------------------- /algorithms/ciphers/atbash/atbash.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | # 3 | # atbash.pl - implement a simple atbash cipher in Perl 4 | # 5 | # Usage: perl atbash.pl 'text' 6 | # 7 | # text: string to be encrypted/decrypted 8 | ##### 9 | 10 | use strict; 11 | use warnings; 12 | 13 | sub doCrypt { 14 | my($inputText) = @_; 15 | my $inputAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 16 | my $outputAlphabet = "ZYXWVUTSRQPONMLKJIHGFEDCBA"; 17 | my $outputText = ""; 18 | 19 | my $count = 0; 20 | while ($count < length($inputText)) { 21 | my $workChar = substr($inputText,$count,1); 22 | my $workCharIndex = index($inputAlphabet,$workChar); 23 | if ($workCharIndex ne "-1") { 24 | $outputText = $outputText . substr($outputAlphabet,$workCharIndex,1); 25 | } else { 26 | $outputText = $outputText . $workChar; 27 | } 28 | $count++; 29 | } 30 | return $outputText; 31 | } 32 | 33 | my $error = "TRUE"; 34 | my $num_args = $#ARGV + 1; 35 | 36 | if ($num_args == 1) { 37 | my $uppertext = uc($ARGV[0]); 38 | $error = "FALSE"; 39 | print doCrypt($uppertext); 40 | } 41 | 42 | if ($error eq "TRUE") { 43 | print "Usage: perl atbash.pl 'text'\r\n\r\n"; 44 | print "text: string to be encrypted/decrypted, enclose in single quotes\r\n\r\n"; 45 | exit 0; 46 | } 47 | 48 | 49 | exit 0; 50 | -------------------------------------------------------------------------------- /algorithms/ciphers/autokey/autokey.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | # 3 | # vigenere.pl - implement a vignere cipher in Perl 4 | # 5 | # Usage: perl vignere.pl (action) 'keyphrase' 'text' 6 | # 7 | # action: string representing action to be taken, either "encrypt" or "decrypt" 8 | # keyphrase: string to be used as keyphrase, enclosed in single quotes 9 | # text: string to be encrypted/decrypted, enclosed in single quotes 10 | # 11 | # (NOTE: Non alpha characters will be stripped from keyphrase!) 12 | ##### 13 | 14 | use strict; 15 | use warnings; 16 | 17 | sub encrypt { 18 | my($keyphrase,$plaintext) = @_; 19 | my $ciphertext = ""; 20 | 21 | my $count = 0; 22 | while ($count < length($plaintext)) { 23 | my $count2 = 0; 24 | my $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 25 | LOOP: while ($count2 < length($keyphrase)) { 26 | my $workChar = substr($plaintext,$count,1); 27 | my $workCharIndex = index($alphabet,$workChar); 28 | my $shiftedAlphabet = shiftAlphabet($alphabet,index($alphabet,substr($keyphrase,$count2,1),1)); 29 | if ($workCharIndex ne "-1") { 30 | $ciphertext = $ciphertext . substr($shiftedAlphabet,$workCharIndex,1); 31 | } else { 32 | $ciphertext = $ciphertext . $workChar; 33 | $count2--; 34 | } 35 | $count++; 36 | unless ($count < length($plaintext)) { 37 | last LOOP; 38 | } 39 | $count2++; 40 | } 41 | 42 | $keyphrase = $plaintext; 43 | $keyphrase =~ s/[^a-zA-Z]//g; 44 | $count2 = 0; 45 | LOOP: while ($count2 < length($keyphrase)) { 46 | my $workChar = substr($plaintext,$count,1); 47 | my $workCharIndex = index($alphabet,$workChar); 48 | my $shiftedAlphabet = shiftAlphabet($alphabet,index($alphabet,substr($keyphrase,$count2,1),1)); 49 | if ($workCharIndex ne "-1") { 50 | $ciphertext = $ciphertext . substr($shiftedAlphabet,$workCharIndex,1); 51 | } else { 52 | $ciphertext = $ciphertext . $workChar; 53 | $count2--; 54 | } 55 | $count++; 56 | unless ($count < length($plaintext)) { 57 | last LOOP; 58 | } 59 | $count2++; 60 | } 61 | } 62 | return $ciphertext; 63 | } 64 | 65 | sub decrypt { 66 | my($keyphrase,$ciphertext) = @_; 67 | my $plaintext = ""; 68 | 69 | my $count = 0; 70 | while ($count < length($ciphertext)) { 71 | my $count2 = 0; 72 | my $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 73 | LOOP: while ($count2 < length($keyphrase)) { 74 | my $workChar = substr($ciphertext,$count,1); 75 | my $shiftedAlphabet = shiftAlphabet($alphabet,index($alphabet,substr($keyphrase,$count2,1),1)); 76 | my $workCharIndex = index($shiftedAlphabet,$workChar); 77 | if ($workCharIndex ne "-1") { 78 | $plaintext = $plaintext . substr($alphabet,$workCharIndex,1); 79 | } else { 80 | $plaintext = $plaintext . $workChar; 81 | $count2--; 82 | } 83 | $count++; 84 | unless ($count < length($ciphertext)) { 85 | last LOOP; 86 | } 87 | $count2++; 88 | } 89 | 90 | $keyphrase = $plaintext; 91 | $keyphrase =~ s/[^a-zA-Z]//g; 92 | $count2 = 0; 93 | LOOP: while ($count2 < length($keyphrase)) { 94 | my $workChar = substr($ciphertext,$count,1); 95 | my $shiftedAlphabet = shiftAlphabet($alphabet,index($alphabet,substr($keyphrase,$count2,1),1)); 96 | my $workCharIndex = index($shiftedAlphabet,$workChar); 97 | if ($workCharIndex ne "-1") { 98 | $plaintext = $plaintext . substr($alphabet,$workCharIndex,1); 99 | } else { 100 | $plaintext = $plaintext . $workChar; 101 | $count2--; 102 | } 103 | $count++; 104 | unless ($count < length($ciphertext)) { 105 | last LOOP; 106 | } 107 | $keyphrase = $plaintext; 108 | $keyphrase =~ s/[^a-zA-Z]//g; 109 | $count2++; 110 | } 111 | } 112 | return $plaintext; 113 | } 114 | 115 | sub shiftAlphabet { 116 | my($alphabet,$charsToShift) = @_; 117 | my $shiftedAlphabet = substr($alphabet,$charsToShift) . substr($alphabet,0,$charsToShift); 118 | return $shiftedAlphabet; 119 | } 120 | 121 | my $error = "TRUE"; 122 | my $num_args = $#ARGV + 1; 123 | 124 | if ($num_args == 3) { 125 | my $action = uc($ARGV[0]); 126 | my $keyphrase = uc($ARGV[1]); 127 | my $uppertext = uc($ARGV[2]); 128 | $keyphrase =~ s/[^a-zA-Z]//g; 129 | 130 | if ($action eq "ENCRYPT") { 131 | $error = "FALSE"; 132 | print encrypt($keyphrase,$uppertext); 133 | } elsif ($action eq "DECRYPT") { 134 | $error = "FALSE"; 135 | print decrypt($keyphrase,$uppertext); 136 | } 137 | } 138 | 139 | if ($error eq "TRUE") { 140 | print "Usage: perl autokey.pl (action) 'keyphrase' 'text'\r\n\r\n"; 141 | print "action: string representing action to be taken, either \"encrypt\" or \"decrypt\"\r\n"; 142 | print "keyphrase: string to be used as keyphrase, enclosed in single quotes\r\n"; 143 | print "text: string to be encrypted/decrypted, enclosed in single quotes\r\n\r\n"; 144 | print "(NOTE: Non alpha characters will be stripped from keyphrase!)\r\n\r\n"; 145 | exit 0; 146 | } 147 | 148 | exit 0; 149 | -------------------------------------------------------------------------------- /algorithms/ciphers/caesar/caesar.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | # 3 | # caesar.pl - implement a simple caesar cipher in Perl 4 | # 5 | # Usage: perl caesar.pl (action) (chars_to_shift) 'text' 6 | # 7 | # action: string representing action to be taken, either "encrypt" or "decrypt" 8 | # chars_to_shift: integer between 0 and 26 9 | # text: string to be encrypted/decrypted 10 | ##### 11 | 12 | use strict; 13 | use warnings; 14 | 15 | sub encrypt { 16 | my($charsToShift,$plaintext) = @_; 17 | my $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 18 | my $shiftedAlphabet = shiftAlphabet($alphabet,$charsToShift); 19 | my $ciphertext = ""; 20 | 21 | my $count = 0; 22 | while ($count < length($plaintext)) { 23 | my $workChar = substr($plaintext,$count,1); 24 | my $workCharIndex = index($alphabet,$workChar); 25 | if ($workCharIndex ne "-1") { 26 | $ciphertext = $ciphertext . substr($shiftedAlphabet,$workCharIndex,1); 27 | } else { 28 | $ciphertext = $ciphertext . $workChar; 29 | } 30 | $count++; 31 | } 32 | return $ciphertext; 33 | } 34 | 35 | sub decrypt { 36 | my($charsToShift,$ciphertext) = @_; 37 | my $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 38 | my $shiftedAlphabet = shiftAlphabet($alphabet,$charsToShift); 39 | my $count = 0; 40 | my $plaintext = ""; 41 | 42 | while ($count < length($ciphertext)) { 43 | my $workChar = substr($ciphertext,$count,1); 44 | my $workCharIndex = index($shiftedAlphabet,$workChar); 45 | if ($workCharIndex ne "-1") { 46 | $plaintext = $plaintext . substr($alphabet,$workCharIndex,1); 47 | } else { 48 | $plaintext = $plaintext . $workChar; 49 | } 50 | $count++; 51 | } 52 | return $plaintext; 53 | } 54 | 55 | sub shiftAlphabet { 56 | my($alphabet,$charsToShift) = @_; 57 | my $shiftedAlphabet = substr($alphabet,$charsToShift) . substr($alphabet,0,$charsToShift); 58 | return $shiftedAlphabet; 59 | } 60 | 61 | my $error = "TRUE"; 62 | my $num_args = $#ARGV + 1; 63 | 64 | if ($num_args == 3) { 65 | my $action = uc($ARGV[0]); 66 | my $charsToShift = $ARGV[1]; 67 | my $uppertext = uc($ARGV[2]); 68 | 69 | if ($charsToShift =~ /^\d+\z/) { 70 | if ($charsToShift <= 26) { 71 | if ($action eq "ENCRYPT") { 72 | $error = "FALSE"; 73 | print encrypt($charsToShift,$uppertext); 74 | } elsif ($action eq "DECRYPT") { 75 | $error = "FALSE"; 76 | print decrypt($charsToShift,$uppertext); 77 | } 78 | } 79 | } 80 | } 81 | 82 | if ($error eq "TRUE") { 83 | print "Usage: perl caesar.pl (action) (chars_to_shift) 'text'\r\n\r\n"; 84 | print "action: string representing action to be taken, either \"encrypt\" or \"decrypt\"\r\n"; 85 | print "chars_to_shift: integer between 0 and 26\r\n"; 86 | print "text: string to be encrypted/decrypted, enclose in single quotes\r\n\r\n"; 87 | exit 0; 88 | } 89 | 90 | 91 | exit 0; 92 | -------------------------------------------------------------------------------- /algorithms/ciphers/vigenere.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | # 3 | # vigenere.pl - implement a vignere cipher in Perl 4 | # 5 | # Usage: perl vignere.pl (action) 'keyphrase' 'text' 6 | # 7 | # action: string representing action to be taken, either "encrypt" or "decrypt" 8 | # keyphrase: string to be used as keyphrase, enclosed in single quotes 9 | # text: string to be encrypted/decrypted, enclosed in single quotes 10 | # 11 | # (NOTE: Non alpha characters will be stripped from keyphrase!) 12 | ##### 13 | 14 | use strict; 15 | use warnings; 16 | 17 | sub encrypt { 18 | my($keyphrase,$plaintext) = @_; 19 | my $ciphertext = ""; 20 | 21 | my $count = 0; 22 | while ($count < length($plaintext)) { 23 | my $count2 = 0; 24 | LOOP: while ($count2 < length($keyphrase)) { 25 | my $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 26 | my $workChar = substr($plaintext,$count,1); 27 | my $workCharIndex = index($alphabet,$workChar); 28 | my $shiftedAlphabet = shiftAlphabet($alphabet,index($alphabet,substr($keyphrase,$count2,1),1)); 29 | if ($workCharIndex ne "-1") { 30 | $ciphertext = $ciphertext . substr($shiftedAlphabet,$workCharIndex,1); 31 | } else { 32 | $ciphertext = $ciphertext . $workChar; 33 | $count2--; 34 | } 35 | $count++; 36 | unless ($count < length($plaintext)) { 37 | last LOOP; 38 | } 39 | $count2++; 40 | } 41 | } 42 | return $ciphertext; 43 | } 44 | 45 | sub decrypt { 46 | my($keyphrase,$ciphertext) = @_; 47 | my $plaintext = ""; 48 | 49 | my $count = 0; 50 | while ($count < length($ciphertext)) { 51 | my $count2 = 0; 52 | LOOP: while ($count2 < length($keyphrase)) { 53 | my $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 54 | my $workChar = substr($ciphertext,$count,1); 55 | my $shiftedAlphabet = shiftAlphabet($alphabet,index($alphabet,substr($keyphrase,$count2,1),1)); 56 | my $workCharIndex = index($shiftedAlphabet,$workChar); 57 | if ($workCharIndex ne "-1") { 58 | $plaintext = $plaintext . substr($alphabet,$workCharIndex,1); 59 | } else { 60 | $plaintext = $plaintext . $workChar; 61 | $count2--; 62 | } 63 | $count++; 64 | unless ($count < length($ciphertext)) { 65 | last LOOP; 66 | } 67 | $count2++; 68 | } 69 | } 70 | return $plaintext; 71 | } 72 | 73 | sub shiftAlphabet { 74 | my($alphabet,$charsToShift) = @_; 75 | my $shiftedAlphabet = substr($alphabet,$charsToShift) . substr($alphabet,0,$charsToShift); 76 | return $shiftedAlphabet; 77 | } 78 | 79 | my $error = "TRUE"; 80 | my $num_args = $#ARGV + 1; 81 | 82 | if ($num_args == 3) { 83 | my $action = uc($ARGV[0]); 84 | my $keyphrase = uc($ARGV[1]); 85 | my $uppertext = uc($ARGV[2]); 86 | $keyphrase =~ s/[^a-zA-Z]//g; 87 | 88 | if ($action eq "ENCRYPT") { 89 | $error = "FALSE"; 90 | print encrypt($keyphrase,$uppertext); 91 | } elsif ($action eq "DECRYPT") { 92 | $error = "FALSE"; 93 | print decrypt($keyphrase,$uppertext); 94 | } 95 | } 96 | 97 | if ($error eq "TRUE") { 98 | print "Usage: perl vigenere.pl (action) 'keyphrase' 'text'\r\n\r\n"; 99 | print "action: string representing action to be taken, either \"encrypt\" or \"decrypt\"\r\n"; 100 | print "keyphrase: string to be used as keyphrase, enclosed in single quotes\r\n"; 101 | print "text: string to be encrypted/decrypted, enclosed in single quotes\r\n\r\n"; 102 | print "(NOTE: Non alpha characters will be stripped from keyphrase!)\r\n\r\n"; 103 | exit 0; 104 | } 105 | 106 | exit 0; 107 | -------------------------------------------------------------------------------- /algorithms/computational-geometry/AreaOfTriangle.pm: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl 2 | package AreaOfTriangle; 3 | use strict; 4 | use Exporter qw< import >; 5 | our @EXPORT_OK = qw< triangle_area_basic triangle_area_cartesian >; 6 | 7 | =pod 8 | 9 | =head1 SYNOPSIS 10 | 11 | use AreaOfTriangle qw< triangle_area_basic triangle_area_cartesian >; 12 | 13 | my $basic = triangle_area_basic( 14 | 10, # base 15 | 12, # height 16 | ); # result: 60 17 | 18 | my $planar = triangle_area_cartesian( 19 | [1, 1], # vertex A, 2 coordinates 20 | [11, 1], # vertex B, 2 coordinates 21 | [1, 13], # vertex C, 2 coordinates 22 | ); # result: 60 23 | 24 | my $planar_vectorial = triangle_area_cartesian( 25 | [10, 0], # vector v, 2 coordinates 26 | [0, 12], # vector w, 2 coordinates 27 | ); # result: 60 28 | 29 | my $spatial = triangle_area_cartesian( 30 | [1, 1, 1], # vertex A, 3 coordinates 31 | [11, 1, 2], # vertex B, 3 coordinates 32 | [1, 13, 15], # vertex C, 3 coordinates 33 | ); # result: 92.39047... 34 | 35 | my $spatial_vectorial = triangle_area_cartesian( 36 | [10, 0, 1], # vector v, 3 coordinates 37 | [0, 12, 14], # vector w, 3 coordinates 38 | ); # result: 92.39047... 39 | 40 | =cut 41 | 42 | sub triangle_area_basic { 43 | my ($base, $height) = @_; 44 | return $base * $height / 2; 45 | } 46 | 47 | # This combines together into a single access point four variants, derived 48 | # by mixing and matching the following alternatives: 49 | # - provision of three vertices (A, B, and C) or two vectors (v, w) 50 | # - planar (two dimensions) or spatial (three dimensions) 51 | # 52 | # The "vector" alternative can be seen as if A is in the origin and the 53 | # provided inputs are positions of B and C. 54 | sub triangle_area_cartesian { 55 | my ($A, $B, $C) = @_; 56 | ref($A) eq 'ARRAY' && (@$A == 2 || @$A == 3) or die "invalid first point"; 57 | ref($B) eq 'ARRAY' && @$B == @$A or die "invalid second point"; 58 | ($A, $B, $C) = ([(0) x @$A], $A, $B) unless defined $C; 59 | (ref($C) eq 'ARRAY' && @$C == @$A) or die "invalid third point"; 60 | return _triangle_area_plane($A, $B, $C) if @$A == 2; 61 | my $s = 0; 62 | $s += _triangle_area_plane([@{$A}[@$_]], [@{$B}[@$_]], [@{$C}[@$_]]) ** 2 63 | for ([1, 2], [2, 0], [0, 1]); 64 | return sqrt($s); 65 | } 66 | 67 | sub _triangle_area_plane { 68 | my ($v_x, $v_y) = ($_[1][0] - $_[0][0], $_[1][1] - $_[0][1]); 69 | my ($w_x, $w_y) = ($_[2][0] - $_[0][0], $_[2][1] - $_[0][1]); 70 | return ($v_x * $w_y - $v_y * $w_x) / 2; 71 | } 72 | 73 | 74 | ######################################################################## 75 | # 76 | # What follows is just to show an example usage of the different subs 77 | # and is ignored if using this implementation as a module. 78 | # 79 | ######################################################################## 80 | 81 | 82 | sub __MAIN { 83 | if (! defined $_[0]) { 84 | print {*STDERR} "$0 bh \n$0 # or\n$0 \n"; 85 | return 1; 86 | } 87 | if ($_[0] eq 'bh') { 88 | print {*STDOUT} triangle_area_basic(@_[1,2]), "\n"; 89 | return 0; 90 | } 91 | for my $item (@_) { 92 | $item =~ s{[][(){}]}{}gmxs; 93 | $item = [split m{[,; ]+}mxs, $item]; 94 | } 95 | print {*STDOUT} triangle_area_cartesian(@_), "\n"; 96 | return 0; 97 | } 98 | 99 | ######################################################################## 100 | # 101 | # Restrict running MAIN only to when this file is executed, not when 102 | # it is "use"-d or "require"-d, according to the "Modulino" trick. 103 | # 104 | # See https://gitlab.com/polettix/notechs/snippets/1868370 105 | 106 | exit sub { return __MAIN(@_) }->(@ARGV) unless caller; 107 | 108 | 1; 109 | -------------------------------------------------------------------------------- /algorithms/math/factorial_iterative.pl: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env perl 2 | 3 | use strict; 4 | use warnings; 5 | 6 | sub factorial 7 | { 8 | my( $n ) = @_; 9 | 10 | my $r = 1; 11 | while( 0 < $n ) 12 | { 13 | $r *= $n; 14 | --$n; 15 | } 16 | 17 | return $r; 18 | } 19 | 20 | MAIN: 21 | my $n = $ARGV[0]; 22 | print( "$n! = " . factorial( $n ) . "\n" ); 23 | 24 | __END__ 25 | -------------------------------------------------------------------------------- /algorithms/math/factorial_recursive.pl: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env perl 2 | 3 | use strict; 4 | use warnings; 5 | 6 | sub factorial 7 | { 8 | my( $n ) = @_; 9 | 10 | return ( $n <= 0 ) ? 1 : $n * factorial( $n - 1 ); 11 | } 12 | 13 | MAIN: 14 | my $n = $ARGV[0]; 15 | print( "$n! = " . factorial( $n ) . "\n" ); 16 | 17 | __END__ 18 | -------------------------------------------------------------------------------- /algorithms/math/fibonacci_sequence.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | use strict; 4 | use warnings; 5 | 6 | my @fib; 7 | 8 | die "usage: ./fibonacci_sequence.pl \n" 9 | unless scalar @ARGV; 10 | my $terms = shift @ARGV; 11 | 12 | my ($n_1, $n_2) = (0, 1); 13 | 14 | die "Invalid ``$terms''" if $terms <= 0; 15 | print "0\n" and exit 0 if $terms == 1; 16 | 17 | foreach (1 ... $terms) { 18 | push @fib, $n_1; 19 | my $n_th = $n_1 + $n_2; 20 | $n_1 = $n_2; 21 | $n_2 = $n_th; 22 | } 23 | 24 | print join(', ', @fib), "\n"; 25 | -------------------------------------------------------------------------------- /algorithms/math/levenshtein.pl: -------------------------------------------------------------------------------- 1 | # Levenshtein distance calculation. 2 | # The Levenshtein distance is a string metric for measuring the difference between two sequences. 3 | # It is the minimum number of single-character edits (insertions, deletions or substitutions), 4 | # required to change one word into the other. 5 | 6 | use strict; 7 | use warnings; 8 | 9 | sub min { 10 | my $min = undef; 11 | for (@_) { 12 | $min //= $_; 13 | $min = $_ if ($min > $_); 14 | } 15 | return $min; 16 | } 17 | 18 | sub levenshtein { 19 | my ($x, $y) = @_; 20 | 21 | my @A = split //, $x; 22 | my @B = split //, $y; 23 | 24 | my @W = ( 0..@B ); 25 | 26 | my ($cur, $next); 27 | 28 | for my $i ( 0..$#A ) { 29 | $cur = $i+1; 30 | for my $j ( 0..$#B ) { 31 | $next = min ( 32 | $W[$j+1]+1, 33 | $cur+1, 34 | ( $A[$i] ne $B[$j] ) + $W[$j], 35 | ); 36 | $W[$j] = $cur; 37 | $cur = $next; 38 | } 39 | $W[@B] = $next; 40 | } 41 | 42 | return $next; 43 | } 44 | 45 | printf ("%d\n", levenshtein("foo", "bar")); # 3 46 | printf ("%d\n", levenshtein("bar", "baz")); # 1 47 | printf ("%d\n", levenshtein("kitten", "sitting")); # 3 48 | -------------------------------------------------------------------------------- /algorithms/searches/binary_search.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl 2 | use strict; 3 | use warnings; 4 | 5 | # $index = binary_search( \@array, $word ) 6 | # @array is a list of lowercase strings in alphabetical order. 7 | # $word is the target word that might be in the list. 8 | # binary_search() returns the array index such that $array[$index] 9 | # is $word. 10 | 11 | my @names_list = qw (anatole barnabe dalida johnny mickael sally zazie); 12 | my $name_to_find = 'dalida'; 13 | print binary_search( \@names_list, $name_to_find ); 14 | 15 | sub binary_search { 16 | my ($array, $word) = @_; 17 | my ($start, $end) = ( 0, @$array - 1 ); 18 | my $mid; 19 | 20 | while ( $start <= $end ){ 21 | $mid = int( ($start+$end)/2 ); # Get the middle value 22 | if ($array->[$mid] eq $word){ # Check if that value is the correct one 23 | return $mid; # Word found 24 | } 25 | else { 26 | if ( $word gt $array->[$mid] ){ 27 | $start = $mid + 1; # Raise the start value 28 | } 29 | else { 30 | $end = $mid - 1; # Lower the end value 31 | } 32 | } 33 | } 34 | return; # Word not found 35 | } -------------------------------------------------------------------------------- /algorithms/searches/hill_climbing.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl 2 | use strict; 3 | use warnings; 4 | 5 | # Max Value = hill_climbing($initial_node) 6 | # @data_set is a list of value with their F(value) result associated. 7 | # Link to algorithm description in wikipedia: https://en.wikipedia.org/wiki/Hill_climbing 8 | 9 | 10 | my @data_set = ([1,4],[2,6],[3,15],[4,5],[5,3],[6,2],[7,4],[8,5],[9,6],[10,7],[11,8],[12,10],[13,9],[14,8],[15,7],[16,3]); 11 | 12 | my $initial_node = 6; 13 | print "Max Value from starting point $initial_node is ".hill_climbing($initial_node)."\n"; 14 | 15 | 16 | sub hill_climbing { 17 | my $n = shift; 18 | my $n_prime; 19 | 20 | while(1){ 21 | $n_prime = select_successor_node($n); 22 | if (F($n_prime) <= F($n)){ 23 | return $n; 24 | } 25 | $n = $n_prime; 26 | } 27 | } 28 | 29 | sub select_successor_node { 30 | my $current_node = shift; 31 | my $successor_node; 32 | my $left_successor = $current_node - 1 if ($current_node - 1) >= 0; 33 | my $right_successor = $current_node + 1 if ($current_node + 1) <= scalar(@data_set) - 1; 34 | if (defined $left_successor){ 35 | if (defined $right_successor){ 36 | $successor_node = F($left_successor) < F($right_successor) ? $right_successor : $left_successor; 37 | } 38 | else { 39 | $successor_node = $left_successor; 40 | } 41 | } 42 | else { 43 | if (defined $right_successor){ 44 | $successor_node = $right_successor; 45 | } 46 | } 47 | return $successor_node; 48 | } 49 | 50 | sub F { 51 | my $entry_value = shift; 52 | return $data_set[$entry_value-1]->[1]; 53 | } -------------------------------------------------------------------------------- /algorithms/sorting/bubblesort.pl: -------------------------------------------------------------------------------- 1 | use strict; 2 | use warnings; 3 | 4 | sub bubble_sort { 5 | my @a = @_; 6 | 7 | my $n = scalar @a; 8 | for (my $j = 1; $j <= $n; $j++) { 9 | my $f = 0; 10 | my $min = $j; 11 | for (my $i = $j; $i <= $n-$j; $i++) { 12 | if ($a[$i-1] > $a[$i]) { 13 | ($a[$i-1], $a[$i]) = ($a[$i], $a[$i-1]); 14 | $f = 1; 15 | } 16 | $min = $i-1 if ($a[$i-1] < $a[$min]); 17 | } 18 | last if $f == 0; 19 | ($a[$j-1], $a[$min]) = ($a[$min], $a[$j-1]) if ($min != $j); 20 | } 21 | return @a; 22 | } 23 | 24 | my @sorted = bubble_sort ( qw/10 9 8 4 5 6 7 3 2 1/ ); 25 | print "@sorted\n"; 26 | -------------------------------------------------------------------------------- /algorithms/sorting/insertionsort.pl: -------------------------------------------------------------------------------- 1 | use strict; 2 | use warnings; 3 | 4 | sub in_sort { 5 | my @a = @_; 6 | 7 | for (my $i = 1; $i <= $#a; $i++) { 8 | my $n = $i; 9 | while ($a[$n] < $a[$n-1] && $n > 0) { 10 | ($a[$n-1], $a[$n]) = ($a[$n], $a[$n-1]); 11 | $n--; 12 | } 13 | } 14 | return @a; 15 | } 16 | 17 | my @sorted = in_sort ( qw/10 9 8 4 5 6 7 3 2 1/ ); 18 | print "@sorted\n"; 19 | -------------------------------------------------------------------------------- /algorithms/sorting/mergesort.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | use strict; 3 | use warnings; 4 | 5 | sub merge { 6 | my ($s, @b) = @_; 7 | my @a = splice @b, $s; 8 | 9 | return @a, @b unless @a && @b; 10 | my $head = $a[0] < $b[0] ? shift @a : shift @b; 11 | return $head, merge(scalar @a, @a, @b); 12 | } 13 | 14 | sub mergesort { 15 | my $half = int(@_ / 2); 16 | return @_ unless $half; 17 | 18 | return merge($half, mergesort(splice @_, $half), mergesort(@_)); 19 | } 20 | 21 | my @sorted = mergesort(10, 9, 8, 4, 5, 6, 7, 3, 2, 1); 22 | print "@sorted\n"; 23 | -------------------------------------------------------------------------------- /algorithms/sorting/quicksort.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | # Based on Cormen third edition 3 | use strict; 4 | use warnings; 5 | 6 | sub quicksort (\@) {qsort($_[0], 0, $#{$_[0]})} 7 | 8 | sub qsort{ 9 | my ($aref, $p, $r) = @_; 10 | 11 | if($p < $r){ 12 | my $q = partition($aref, $p, $r); 13 | qsort($aref, $p, $q - 1); 14 | qsort($aref, $q + 1, $r); 15 | } 16 | } 17 | 18 | sub partition{ 19 | my ($aref, $p, $r) = @_; 20 | my $x = $aref->[$r]; 21 | my $i = $p - 1; 22 | 23 | for my $j($p .. $r - 1){ 24 | if($aref->[$j] <= $x){ 25 | $i++; 26 | ($aref->[$i], $aref->[$j]) = ($aref->[$j], $aref->[$i]); 27 | } 28 | } 29 | 30 | ($aref->[$i + 1], $aref->[$r]) = ($aref->[$r], $aref->[$i + 1]); 31 | return $i + 1; 32 | } 33 | 34 | my @array = (9, 8, 4, 5, 6, 7, 3, 2, 1); 35 | 36 | quicksort @array; 37 | print "@array\n"; 38 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 All Algorithms and its contributors (allalgorithms.com) 4 | Copyright (c) 2018 Carlos Abraham (abranhe.com) 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | We are accepting all pull requests. [Read More](https://github.com/AllAlgorithms/algorithms/issues/40) 2 | 3 | 47 | 48 | ## See 49 | 50 | - [What is an algorithm](#what-is-an-algorithm) 51 | - [Contributing](https://github.com/AllAlgorithms/algorithms/blob/master/.github/contributing.md) 52 | - [Code of Conduct](https://github.com/AllAlgorithms/algorithms/blob/master/.github/code-of-conduct.md) 53 | - [Stickers and T-Shirts](https://www.redbubble.com/people/abranhe/works/34285088) 54 | - [Twitter](https://twitter.com/AllAlgorithms) 55 | - [Instagram](https://instagram.com/AllAlgorithms) 56 | - [Algorithms Categories](#categories) 57 | - [Maintainers](#maintainers) 58 | - [License](#license) 59 | 60 | 61 | ## What is an algorithm? 62 | 63 | Informally, an algorithm is any well-defined computational procedure that takes 64 | some value, or set of values, as input and produces some value, or set of values, as 65 | output. An algorithm is thus a sequence of computational steps that transform the 66 | input into the output. 67 | 68 | An algorithm should have three important characteristics to be considered valid: 69 | 70 | - **It should be finite**: If your algorithm never ends trying to solve the problem 71 | it was designed to solve then it is useless 72 | - **It should have well defined instructions**: Each step of the algorithm has to 73 | be precisely defined; the instructions should be unambiguously specified for each case. 74 | - **It should be effective**: The algorithm should solve the problem it was designed 75 | to solve. And it should be possible to demonstrate that the algorithm converges with 76 | just a paper and pencil. 77 | 78 | ## Categories 79 | 80 | > Structure of The All ▲lgoritms project 81 | 82 | - [Artificial Intelligence](#artificial-intelligence) 83 | - [Backtracking](#backtracking) 84 | - [Bit Manipulation](#bit-manipulation) 85 | - [Cellular Automaton](#cellular-automaton) 86 | - [Ciphers](#ciphers) 87 | - [Computational Geometry](#computational-geometry) 88 | - [Cryptography](#cryptography) 89 | - [Data Structures](#data-structures) 90 | - [Divide and conquer](#divide-and-conquer) 91 | - [Dynamic Programming](#dynamic-programming) 92 | - [Gaming Theory](#gaming-theory) 93 | - [Graphs](#graphs) 94 | - [Greedy Algorithms](#greedy-algorithms) 95 | - [Math](#math) 96 | - [Networking](#networking) 97 | - [Numerical Analysis](#numerical-analysis) 98 | - [Operating system](#operating-system) 99 | - [Randomized Algorithms](#randomized-algorithms) 100 | - [Searches](#searches) 101 | - [Selections Algorithms](#selections-algorithms) 102 | - [Sorting](#sorting) 103 | - [Strings](#strings) 104 | - [Online Challenges](#online-challenges) 105 | - [Others](#others) 106 | 107 | ## [Artificial Intelligence](artificial-intelligence) 108 | 109 | - [Density-based spatial clustering of applications with noise (DBSCAN Clustering)](https://allalgorithms.com/docs/dbscan) 110 | - [Interactive Self-Organizing Data Analysis Technique yAy! (ISODATA Clustering)](https://allalgorithms.com/docs/isodata) 111 | - [Linear Regression](https://allalgorithms.com/docs/linear-regression) 112 | - [Logistic Regression](https://allalgorithms.com/docs/logistic-regression) 113 | - [Neutral Style Transfer](https://allalgorithms.com/docs/neutral-style-transfer) 114 | - [SATisfiable (SAT)](https://allalgorithms.com/docs/sat) 115 | - [Travelling salesman problem (TSP)](https://allalgorithms.com/docs/tsp) 116 | - [A* (A Star)](https://allalgorithms.com/docs/a-star) 117 | - [Artificial Neutral Network](https://allalgorithms.com/docs/artificial-neutral-network) 118 | - [Convolutional Neutral Network](https://allalgorithms.com/docs/convolutional-neutral-network) 119 | - [Decision Tree](https://allalgorithms.com/docs/decision-tree) 120 | - [Factorization Machines](https://allalgorithms.com/docs/factorization-machines) 121 | - [Gaussian Mixture Model](https://allalgorithms.com/docs/gaussian-mixtrue-model) 122 | - [Gradient Boosting Trees](https://allalgorithms.com/docs/gradient-boostring-trees) 123 | - [Hierachical Clustering](https://allalgorithms.com/docs/hierachical-clustering) 124 | - [Image Processing](https://allalgorithms.com/docs/image-processing) 125 | - [K Nearest Neighbors](https://allalgorithms.com/docs/k-nearest-neighbors) 126 | - [K Means](https://allalgorithms.com/docs/k-means) 127 | - [Minimax](https://allalgorithms.com/docs/minimax) 128 | - [Native Bayes](https://allalgorithms.com/docs/native-bayes) 129 | - [Nearest Sequence Memory](https://allalgorithms.com/docs/nearest-sequence-memory) 130 | - [Neutral Network](https://allalgorithms.com/docs/neutral-network) 131 | - [Perceptron](https://allalgorithms.com/docs/perceptron) 132 | - [Principal Component Analysis](https://allalgorithms.com/docs/principal-component-analysis) 133 | - [Q Learing](https://allalgorithms.com/docs/q-learning) 134 | - [Random Forests](https://allalgorithms.com/docs/random-forest) 135 | - [Restricted Boltzman Machine](https://allalgorithms.com/docs/restricted-boltzman-machine) 136 | 137 | ## [Backtracking](backtracking) 138 | 139 | - [Algorithm X](backtracking/algorithm-x) 140 | - [Crossword Puzzle](backtracking/crossword-Puzzle) 141 | - [Knight Tour](backtracking/knight-tour) 142 | - [M Coloring Problem](backtracking/m-coloring-problem) 143 | - [N Queen](backtracking/n-queen) 144 | - [Number of ways in Maze](backtracking/number-of-ways-in-maze) 145 | - [Partitions of set](backtracking/partitions-of-set) 146 | - [Permutation of Strings](backtracking/permutation-of-strings) 147 | - [Powerset](backtracking/powerset) 148 | - [Rat in maze](backtracking/rat-in-maze) 149 | - [Subset Sum](backtracking/subset-sum) 150 | - [Sudoku Solve](backtracking/sudoku-solve) 151 | 152 | ## [Bit Manipulation](bit-manipulation) 153 | 154 | - [Addition using bits](bit-manipulation/adding-using-bits) 155 | - [Bit divisor](bit-manipulation/bit-divisor) 156 | - [Byte swapper](bit-manipulation/byte-swapper) 157 | - [Convert numbers to binary](bit-manipulation/convert-numbers-to-binary) 158 | - [Count set bits](bit-manipulation/count-set-bits) 159 | - [Flip bits](bit-manipulation/flip-bits) 160 | - [Hamming distance](bit-manipulation/hamming-distace) 161 | - [Invert bit](bit-manipulation/invert-bit) 162 | - [Lonely integer](bit-manipulation/lonely-integer) 163 | - [Magic Number](bit-manipulation/magic-number) 164 | - [Maximum XOR Value](bit-manipulation/maximun-xor-value) 165 | - [Power of 2](bit-manipulation/power-of-2) 166 | - [Subset Generation](bit-manipulation/subset-generation) 167 | - [Sum binary numbers](bit-manipulation/sum-binary-numbers) 168 | - [Sum equals XOR](bit-manipulation/sum-equals-xor) 169 | - [Thrice unique number](bit-manipulation/thrice-unique-number) 170 | - [Twice unique number](bit-manipulation/twice-unique-number) 171 | - [XOR Swap](bit-manipulation/xor-swap) 172 | 173 | ## [Cellular Automaton](cellular-automaton) 174 | 175 | - [Brians Brain](cellular-automaton/brians-brain) 176 | - [Conways Game of life](cellular-automaton/conways-game-of-life) 177 | - [Elementary Cellular Automata](cellular-automaton/elementary-cellular-automata) 178 | - [Generic Algorithm](cellular-automaton/generic-algorithm) 179 | - [Langtons Ant](cellular-automaton/langtons-ant) 180 | - [Nobili Cellular Automata](cellular-automaton/nobili-cellular-automata) 181 | - [Von Neoumann Cellular Automata](cellular-automaton/von-neoumann-cellular-automata) 182 | 183 | ## [Computational Geometry](computational-geometry) 184 | 185 | - [2D Line intersection](computational-geometry/) 186 | - [2D Separating Axis test](computational-geometry/) 187 | - [Area of polygon](computational-geometry/) 188 | - [Area of triangle](computational-geometry/) 189 | - [Axis aligned bounding box collision](computational-geometry/) 190 | - [Bresenham Line](computational-geometry/) 191 | - [Chans Algorithm](computational-geometry/) 192 | - [Cohen Sutherland Lineclip](computational-geometry/) 193 | - [Distance between points](computational-geometry/) 194 | - [Graham Scan](computational-geometry/) 195 | - [Halfplane intersection](computational-geometry/) 196 | - [Jarvis March](computational-geometry/) 197 | - [Quickull](computational-geometry/) 198 | - [Sphere tetrahedron intersection](computational-geometry/) 199 | - [Sutherland Hodgeman clipping](computational-geometry/) 200 | 201 | ## [Cryptography](cryptography) 202 | 203 | - [Affine Cipher](cryptography/) 204 | - [Atbash Cipher](cryptography/) 205 | - [Autokey Cipher](cryptography/) 206 | - [Baconian Cipher](cryptography/) 207 | - [Caesar Cipher](cryptography/) 208 | - [Colummnar Cipher](cryptography/) 209 | - [Vigenere Cipher](cryptography/) 210 | 211 | ## [Data Structures](data-structures) 212 | 213 | - [Bag](data-structures/bag/) 214 | - [Hashes](data-structures/hashes/) 215 | - [Linked List](data-structures/linked-list/) 216 | - [List](data-structures/list/) 217 | - [Queue](data-structures/queue/) 218 | - [Stack](data-structures/stack/) 219 | - [Tree](data-structures/tree/) 220 | 221 | ## [Divide and conquer](divide-and-conquer) 222 | 223 | - [Strassen Matrix Manipulation](divide-and-conquer/) 224 | - [Closest Pair of Point](divide-and-conquer/) 225 | - [Inversion Count](divide-and-conquer/) 226 | - [Karatsuba Multiplication](divide-and-conquer/) 227 | - [Maximum Contiguous subsequence sum](divide-and-conquer/) 228 | - [Merge Sort using divide and conquer](divide-and-conquer/) 229 | - [Quick Sort using divide and conquer](divide-and-conquer/) 230 | - [Tournament Method to find min max](divide-and-conquer/) 231 | - [Warnock Algorithm](divide-and-conquer/) 232 | - [X Power Y](divide-and-conquer/) 233 | 234 | ## [Dynamic Programming](dynamic-programming) 235 | 236 | - [Array Median](dynamic-programming) 237 | - [Optima Binary Search Tree](dynamic-programming) 238 | - [Binomial Coefficient](dynamic-programming) 239 | 240 | ## [Gaming Theory](gaming-theory) 241 | 242 | - [Nim Next Best Move Game](gaming-theory/) 243 | - [Nim Win Loss Game](gaming-theory/) 244 | - [Grundy Numbers Kayle Game](gaming-theory/) 245 | 246 | ## [Graphs](graphs) 247 | 248 | - [Bipartite Check](graphs/) 249 | - [Adjacency Lists graphs representation](graphs/) 250 | - [A* (A Star)](https://allalgorithms.com/docs/a-star) 251 | 252 | ## [Greedy Algorithms](greedy-algorithms) 253 | 254 | - [Activity Selection](greedy-algorithms) 255 | - [Dijkstra Shortest Path](greedy-algorithms) 256 | - [Egyptian Fraction](greedy-algorithms) 257 | 258 | ## [Math](math) 259 | 260 | - [2 Sum](math/) 261 | - [Add Polynomials](math/) 262 | - [Amicable Numbers](math/) 263 | - [Armstrong Numbers](math/) 264 | - [Automorphic Numbers](math/) 265 | - [Average Stream Numbers](math/) 266 | - [Babylonian Method](math/) 267 | - [Binomial Coefficient](math/) 268 | - [Catalan Number](math/) 269 | - [Check is Square](math/) 270 | - [Convolution](math/) 271 | - [Coprime Numbers](math/) 272 | - [Count Digits](math/) 273 | - [Count Trailing Zeroes](math/) 274 | - [Decoding of String](math/) 275 | - [Delannoy Number](math/) 276 | - [Derangements](math/) 277 | - [DFA Division](math/) 278 | - [Diophantine](math/) 279 | - [Divided Differences](math/) 280 | - [Euler Totient](math/) 281 | - [Exponentiation Power](math/) 282 | - [Factorial](math/factorial) 283 | - [Fast Fourier transform](math/) 284 | - [Fast inverse (sqrt) Square Root](math/) 285 | 286 | ## [Networking](networking) 287 | 288 | - [Packet Sniffer](networking/) 289 | - [Determine Endianess](networking/) 290 | - [Validate IP](networking/) 291 | 292 | ## [Numerical Analysis](numerical-analysis) 293 | 294 | - [Integral](numerical-analysis/integral) 295 | - [Monte Carlo](numerical-analysis/monte-carlo) 296 | - [Runge Kutt](numerical-analysis/runge-kutt) 297 | 298 | ## [Operating system](operating-system) 299 | 300 | - [Currency](operating-system/) 301 | - [Deadlocks](operating-system/) 302 | - [Memory Management](operating-system/) 303 | - [Scheduling](operating-system/) 304 | - [Shell](operating-system/) 305 | 306 | ## [Randomized Algorithms](randomized-algorithms) 307 | 308 | - [Birthday Paradox](randomized-algorithms) 309 | - [Karger Minimum Cut Algorithm](randomized-algorithms) 310 | - [Kth Smallest Element Algorithm](randomized-algorithms) 311 | - [Random from Stream](randomized-algorithms) 312 | - [Random Node Linked list](randomized-algorithms) 313 | - [Randomized Quicksort](randomized-algorithms) 314 | - [Reservoir Sampling](randomized-algorithms) 315 | - [Shuffle an Array](randomized-algorithms) 316 | 317 | ## [Searches](searches) 318 | 319 | - [Binary Search](searches) 320 | - [Exponential Search](searches) 321 | - [Fibonacci Search](searches) 322 | - [Fuzzy Search](searches) 323 | - [Interpolation Search](searches) 324 | - [Jump Search](searches) 325 | - [Linear Search](searches) 326 | - [Ternay Search](searches) 327 | 328 | ## [Selections Algorithms](selections-algorithms) 329 | 330 | - [Median of Medians](selections-algorithms) 331 | - [Quick Select](selections-algorithms) 332 | 333 | ## [Sorting](sorting) 334 | 335 | - [Bead Sort](sorting/) 336 | - [Bogo Sort](sorting/) 337 | - [Bubble Sort](sorting/) 338 | - [Bucket Sort](sorting/) 339 | - [Circle Sort](sorting/) 340 | - [Comb Sort](sorting/) 341 | - [Counting Sort](sorting/) 342 | - [Cycle Sort](sorting/) 343 | - [Flash Sort](sorting/) 344 | - [Gnome Sort](sorting/) 345 | - [Heap Sort](sorting/) 346 | - [Insertion Sort](sorting/) 347 | - [Intro Sort](sorting/) 348 | - [Median Sort](sorting/) 349 | - [Merge Sort](sorting/) 350 | - [Pipeonhole Sort](sorting/) 351 | - [Quick Sort](sorting/) 352 | - [Radix Sort](sorting/) 353 | - [Selection Sort](sorting/) 354 | - [Shaker Sort](sorting/) 355 | - [Shell Sort](sorting/) 356 | - [Sleep Sort](sorting/) 357 | - [Stooge Sort](sorting/) 358 | - [Topological Sort](sorting/) 359 | - [Tree Sort](sorting/) 360 | 361 | ## [Strings](strings) 362 | 363 | - [Aho Corasick Algorithm](strings) 364 | - [Anagram Search](strings) 365 | - [Arithmetic on large numbers](strings) 366 | - [Boyer Moore Algorithm](strings) 367 | - [Finite Automata](strings) 368 | - [Kasai Algorithm](strings) 369 | - [Kmp Algorithm](strings) 370 | - [Levenshteing Distance](strings) 371 | - [Lipogram Checker](strings) 372 | 373 | ## [Online Challenges](online-challenges) 374 | 375 | - [Coderbyte](online-challenges/coderbyte) 376 | - [Code Chef](online-challenges/code-chef) 377 | - [Code Eval](online-challenges/code-eval) 378 | - [Hackerearth](online-challenges/hackerearth) 379 | - [Hackerrank](online-challenges/hackerrank) 380 | - [LeetCode](online-challenges/leetcode) 381 | - [Project Euler](online-challenges/project-euler) 382 | - [Rosalind](online-challenges/rosalind) 383 | - [SPOJ](online-challenges/spoj) 384 | - [Top Coder](online-challenges/top-coder)` 385 | 386 | ## [Others](others) 387 | 388 | - [Average](others/) 389 | - [Biggest of n numbers](others/) 390 | - [Biggest Suffix](others/) 391 | - [Fifteen Puzzle](others/) 392 | - [Jaccard Similarity](others/) 393 | - [Jose Phus Problem](others/) 394 | - [Lapindrom Checker](others/) 395 | - [Leap Year](others/) 396 | - [Magic Square](others/) 397 | - [Majority Element](others/) 398 | - [Minimum subarray size with degree](others/) 399 | - [No operator addition](others/) 400 | - [Paint fill](others/) 401 | - [Split list](others/) 402 | - [Tokenizer](others/) 403 | - [Unique number](others/) 404 | 405 | ## License 406 | 407 | This work is released under MIT License. 408 | 409 | To the extent possible under law, [Abraham Hernandez (@abranhe)](https://go.abranhe.com/github) has waived all copyright and related or neighboring rights to this work. 410 | 411 |
412 | 413 | 414 | 415 |
416 |
-------------------------------------------------------------------------------- /src/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllAlgorithms/perl/3a7b3825534857aa326afc203f8420ccfb19c1d6/src/.gitkeep --------------------------------------------------------------------------------
4 |
5 |
6 |
7 |
8 | Algorithms Logo 9 |
10 |
11 |
12 |
13 |
14 |
15 | 16 |

17 | What is an algorithm?    18 | Contributing    19 | Stickers & T-Shirts 20 |

21 | 22 | 23 |

24 | 25 | Twitter 26 |     27 | 28 | Instagram 29 |     30 | 31 | Github 32 |     33 |

34 | 35 |
36 |

37 | Huge collection of All ▲lgorithms implemented in multiple languages 38 |

39 |
40 | 41 | 42 | 43 | 44 | 45 | 46 |