Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions archive/p/perl/baklava.pl
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#!/usr/bin/env perl
use strict;
use warnings;
use v5.42;

my $size = 10;

for my $i (1..$size){
print " "x($size + 1 - $i), "*"x($i*2 - 1), "\n";
}
for my $i ( -$size .. $size ) {
my $spaces = abs($i);
my $stars = 2 * ( $size - $spaces ) + 1;

for my $j (0..$size){
print " "x($j), "*"x($size*2 - $j*2 + 1), "\n";
}
say ' ' x $spaces, '*' x $stars;
}
103 changes: 35 additions & 68 deletions archive/p/perl/binary-search.pl
Original file line number Diff line number Diff line change
@@ -1,82 +1,49 @@
#!/usr/bin/env perl
use strict;
use warnings;
use v5.42;

sub handle_error {
print "Usage: please provide a list of sorted integers (\"1, 4, 5, 11, 12\") and the integer to find (\"11\")\n";
exit(0);
}
use feature qw/keyword_any/;
no warnings 'experimental::keyword_any';

sub check {
my ($s) = @_;

# Trim leading and trailing spaces
$s =~ s/^\s+//;
$s =~ s/\s+$//;

# Check if there are spaces in the middle
if ($s =~ /\s/) {
handle_error();
}

# Check if it's a valid integer
if ($s !~ /^-?\d+$/) {
handle_error();
}

return int($s);
sub usage {
say 'Usage: please provide a list of sorted integers ("1, 4, 5, 11, 12") and the integer to find ("11")';
exit;
}

sub convert {
my ($s) = @_;

if (length($s) == 0) {
handle_error();
}

my @v;
my @parts = split(',', $s);

foreach my $part (@parts) {
push @v, check($part);
}

return @v;
}
sub parse_list ($s) {
return undef unless defined $s;

# Main program
if (@ARGV < 2) {
handle_error();
}
my @vals = split /\s*,\s*/, $s;

my @v = convert($ARGV[0]);
my $num = check($ARGV[1]);
return undef unless @vals;
return undef if any { $_ !~ /\A\d+\z/ } @vals;

# Check if array is sorted
for (my $i = 0; $i < @v - 1; $i++) {
if ($v[$i] > $v[$i + 1]) {
handle_error();
}
@vals = map 0 + $_, @vals;

return undef if any { $vals[$_] > $vals[ $_ + 1 ] } 0 .. $#vals - 1;
return \@vals;
}

# Binary search
my $start = 0;
my $end = scalar(@v);
my $ans = "false";
sub binary_search ( $a, $x ) {
my ( $lo, $hi ) = ( 0, $#$a );

while ($start < $end) {
my $mid = int(($start + $end) / 2);

if ($num < $v[$mid]) {
$end = $mid;
}
elsif ($v[$mid] < $num) {
$start = $mid + 1;
}
elsif ($v[$mid] == $num) {
$ans = "true";
last;
while ( $lo <= $hi ) {
my $mid = ( $lo + $hi ) >> 1;

return "true" if $a->[$mid] == $x;
$hi = $mid - 1 if $a->[$mid] > $x;
$lo = $mid + 1 if $a->[$mid] < $x;
}

return "false";
}

print "$ans\n";
my ( $list_s, $num_s ) = @ARGV;

defined $num_s or usage();

my $list = parse_list($list_s) or usage();

usage() unless $num_s =~ /\A\d+\z/;
my $num = 0 + $num_s;

say binary_search( $list, $num );
71 changes: 42 additions & 29 deletions archive/p/perl/bubble-sort.pl
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
#!/usr/bin/perl
$num_args = $#ARGV + 1;
if ($num_args == 0) {
print "Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"";
} else {
$input_string = $ARGV[0];
my @arr = split(',',$input_string);
$n = $#arr + 1;
if ($n <= 1) {
print "Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"";
} else {
for ($i = 0;$i < $n;$i++) {
$arr[$i] = int($arr[$i])
}
for ($i = 0;$i < $n;$i = $i + 1) {
for ($j = 0;$j < $n - $i - 1;$j = $j + 1) {
if ($arr[$j] > $arr[$j + 1]) {
$temp = $arr[$j];
$arr[$j] = $arr[$j + 1];
$arr[$j + 1] = $temp;
}
}
}
for ($i = 0;$i < $n;$i = $i + 1) {
if ($i == 0) {
print "$arr[$i]";
} else {
print ", $arr[$i]";
}
#!/usr/bin/env perl
use v5.42;

use feature qw/keyword_any/;
no warnings 'experimental::keyword_any';

sub usage {
say 'Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5"';
exit;
}

sub parse_list ($s) {
return undef unless defined $s;

my @vals = split /\s*,\s*/, $s;

return undef if @vals < 2;
return undef if any { $_ !~ /\A-?\d+\z/ } @vals;

return [ map 0 + $_, @vals ];
}

sub bubble_sort ($a) {
my $n = @$a;

for my $end ( reverse 1 .. $n - 1 ) {
my $swapped = false;

for my $i ( 0 .. $end - 1 ) {
next if $a->[$i] <= $a->[ $i + 1 ];
( $a->[$i], $a->[ $i + 1 ] ) = ( $a->[ $i + 1 ], $a->[$i] );
$swapped = true;
}

last unless $swapped;
}

return $a;
}

my ($input) = @ARGV;
my $a = parse_list($input) or usage();

bubble_sort($a);
say join ', ', @$a;
17 changes: 8 additions & 9 deletions archive/p/perl/capitalize.pl
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#!/usr/bin/env perl
use strict;
use warnings;
use v5.42;

# accept input as argument
my ($string) = @ARGV;

if (!defined $string || length $string == 0) {
print "Usage: please provide a string\n";
exit;
sub usage {
say "Usage: please provide a string";
exit;
}

print ucfirst $string, "\n";
my ($string) = @ARGV;
usage() unless defined $string && length $string;

say ucfirst $string;
20 changes: 8 additions & 12 deletions archive/p/perl/even-odd.pl
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
#!/usr/bin/env perl
use strict;
use warnings;
use v5.42;

sub usage {
say "Usage: please input a number";
exit;
}

# accept input as argument
my ($number) = @ARGV;

if (!defined $number || $number !~ /^\-?\d+$/) {
print "Usage: please input a number\n";
exit;
}
usage() unless defined $number && $number =~ /\A-?\d+\z/;

if ($number % 2 == 0) {
print "Even\n";
} else {
print "Odd\n";
}
say $number % 2 == 0 ? "Even" : "Odd";
33 changes: 8 additions & 25 deletions archive/p/perl/factorial.pl
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
#!/usr/bin/env perl
use strict;
use warnings;
use v5.42;

# no input
usage() unless @ARGV == 1;

# accept input as argument
my ($number) = @ARGV;

# if not provided, read from standard input
if (!defined $number) {
$number = <STDIN>;
chomp $number;
sub usage {
say "Usage: please input a non-negative integer";
exit;
}

if (!defined $number || $number !~ /^\d+$/ || $number < 0) {
usage();
}
my ($number) = @ARGV;
usage() unless defined $number && $number =~ /\A\d+\z/;

my $factorial = 1;
$factorial *= $_ for 2 .. $number;

for (my $i = 1; $i <= $number; $i++) {
$factorial = $factorial * $i;
}

print "$factorial\n";

sub usage {
print "Usage: please input a non-negative integer\n";
exit;
}
say $factorial;
35 changes: 16 additions & 19 deletions archive/p/perl/fibonacci.pl
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
#!/usr/bin/perl
$num_args = $#ARGV + 1;
if ($num_args == 0) {
print "Usage: please input the count of fibonacci numbers to output\n";
} elsif ($num_args == 1) {
if ($ARGV[0] =~ /[0-9]+/) {
$n = $ARGV[0];
$result = 0,$first = 0,$second = 1;
for ($i = 1;$i <= $n;$i = $i + 1) {
$result = $first + $second;
$first = $second;
$second = $result;
print "$i: $first\n";
}
} else {
print "Usage: please input the count of fibonacci numbers to output\n";
}
} else {
print "Usage: please input the count of fibonacci numbers to output\n";
#!/usr/bin/env perl
use v5.42;

sub usage {
say "Usage: please input the count of fibonacci numbers to output";
exit;
}

my ($n) = @ARGV;
usage() unless defined $n && $n =~ /\A\d+\z/;

my ( $a, $b ) = ( 0, 1 );

for my $i ( 1 .. $n ) {
( $a, $b ) = ( $b, $a + $b );
say "$i: $a";
}
36 changes: 15 additions & 21 deletions archive/p/perl/file-input-output.pl
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
#!/usr/bin/env perl
use v5.42;
use IO::File;

sub Main {
Write("Some arbitrary data.");
Read();
exit(0);
}
my $file = "output.txt";
my $content = <<'EOF';
Perl is cool!
There's more than one way to do it.
EOF

sub Write {
open(my $writing, ">output.txt") || die "File could not be written.\nError: $!";
# Write to file

print $writing "@_"."\n";
my $w = IO::File->new(">$file") or die "Cannot open $file for writing: $!";
$w->print($content) or die "Write failed: $!";
$w->close or die "Cannot close after write: $!";

close($writing) || die "The file could not be closed on write.\nError: $!";
}
# Read from file

sub Read {
open(my $reading, "<output.txt") || die "File could not be readed.\nError: $!";

while (!eof($reading)) {
print <$reading>;
}

close($reading) || die "The file could not be closed on reading.\nError: $!";
}

Main();
my $r = IO::File->new("<$file") or die "Cannot open $file for reading: $!";
print while <$r>;
$r->close or die "Cannot close after read: $!";
Loading
Loading