add perl6 code for chapters 1-4 (#49)

This commit is contained in:
ryn1x
2018-01-04 07:46:31 -07:00
committed by Aditya Bhargava
parent dd0100e471
commit 7d48d29139
11 changed files with 194 additions and 0 deletions

28
.gitignore vendored
View File

@@ -1 +1,29 @@
.DS_Store .DS_Store
*.swp
index.data
*~
html/*.html
html/css/style.css
html/css/style.css.map
html/perl6.xhtml
html/routine/
html/type/
html/op/
html/language/
html/programs/
html/syntax/
html/images/type-graph*
html/js/search.js
.precomp
precompiled
assets/assetpack.db
assets/cache
.sass-cache/
html/css/style.css.map
html/links.txt
xt/aspell.pws
highlights/node_modules
**/npm-debug.log
highlights/atom-language-perl6/
.DS_store
highlights/package-lock.json

View File

@@ -0,0 +1,35 @@
#!/usr/bin/env perl6
use v6.c;
sub binary-search(@arr, $item){
# low and high keep track of which part of the array you'll search in.
my $low = 0;
my $high = @arr.elems - 1;
# While you haven't narrowed it down to one element ...
while $low <= $high {
# ... check the middle element
my $mid = ($low + $high) div 2;
my $guess = @arr[$mid];
# Found the item.
if $guess ~~ $item {
return $mid;
}
# The guess was too high.
if $guess > $item {
$high = $mid - 1;
}
# The guess was too low.
else {
$low = $mid + 1;
}
}
# Item doesn't exist
return Nil;
}
my @arr = 1, 3, 5, 7, 9;
say binary-search(@arr, 3); # => 1
# We use 'Nil' to indicate that the item wasn't found.
say binary-search(@arr, -1); # => Nil

View File

@@ -0,0 +1,31 @@
#!/usr/bin/env perl6
use v6.c;
# Finds the smallest value in an array
sub findSmallest(@arr) {
# Stores the smallest value
my $smallest = @arr[0];
# Stores the index of the smallest value
my $smallest-index = 0;
for 1..^@arr.elems {
if @arr[$_] < $smallest {
$smallest = @arr[$_];
$smallest-index = $_;
}
}
return $smallest-index;
}
# Sort array
sub selectionSort(@arr) {
my @newArr;
for ^@arr.elems {
# Finds the smallest element in the array and adds it to the new array
my $smallest = findSmallest(@arr);
@newArr.append: @arr.splice($smallest, 1);
}
return @newArr;
}
my @unsorted-array = 5, 3, 6, 2, 10;
say selectionSort(@unsorted-array);

View File

@@ -0,0 +1,13 @@
#!/usr/bin/env perl6
use v6.c;
# base case
multi countdown(0) { say 0 }
# recursive case
multi countdown($i where { $i > 0 }) {
say $i;
countdown $i-1;
}
countdown 5;

19
03_recursion/perl6/02_greet.p6 Executable file
View File

@@ -0,0 +1,19 @@
#!/usr/bin/env perl6
use v6.c;
sub greet2($name) {
say "how are you, $name?";
}
sub bye() {
say "ok bye!";
}
sub greet($name) {
say "hello, $name!";
greet2 $name;
say "getting ready to say bye...";
bye;
}
greet "adit";

View File

@@ -0,0 +1,7 @@
#!/usr/bin/env perl6
use v6.c;
multi fact(1) { 1 }
multi fact($x where { $x > 1 }) { $x * fact($x-1) }
say fact(5);

View File

@@ -0,0 +1,13 @@
#!/usr/bin/env perl6
use v6.c;
sub sum(@arr) {
my $total = 0;
for @arr {
$total += $_;
}
return $total;
}
my @arr = 1, 2, 3, 4;
say sum(@arr);

View File

@@ -0,0 +1,8 @@
#!/usr/bin/env perl6
use v6.c;
multi sum([]) { 0 };
multi sum(@arr) { @arr[0] + sum(@arr[1..*]) }
my @arr = 1,2,3,4,5;
say sum(@arr);

View File

@@ -0,0 +1,8 @@
#!/usr/bin/env perl6
use v6.c;
multi count([]) { 0 }
multi count(@arr) { 1 + count(@arr[1..*]) }
my @arr = 1,2,3,4,5;
say count(@arr);

View File

@@ -0,0 +1,11 @@
#!/usr/bin/env perl6
use v6.c;
sub my-max(@arr) {
if @arr.elems == 2 { return (@arr[0] > @arr[1]) ?? @arr[0] !! @arr[1] }
my $sub-max = my-max(@arr[1..*]);
return (@arr[0] > $sub-max) ?? @arr[0] !! $sub-max;
}
my @arr = 1,2,3,4,5;
say my-max(@arr);

View File

@@ -0,0 +1,21 @@
#!/usr/bin/env perl6
use v6.c;
sub quicksort(@arr) {
if @arr.elems < 2 {
# base case, arrays with 0 or 1 element are already "sorted"
return @arr;
}
else {
# recursive case
my $pivot = @arr[0];
# sub-array of all the elements less than the pivot
my @less = (gather for @arr[1..*] { take $_ if $_ <= $pivot });
# sub-array of all the elements greater than the pivot
my @greater = (gather for @arr[1..*] { take $_ if $_ > $pivot });
return (|quicksort(@less), $pivot, |quicksort(@greater));
}
}
my @arr = 10, 5, 2, 3;
say quicksort(@arr);