From 7d48d29139db3aeb17c69c0f090b828857c5a1e2 Mon Sep 17 00:00:00 2001 From: ryn1x Date: Thu, 4 Jan 2018 07:46:31 -0700 Subject: [PATCH] add perl6 code for chapters 1-4 (#49) --- .gitignore | 28 +++++++++++++++ .../perl6/01_binary_search.p6 | 35 +++++++++++++++++++ 02_selection_sort/perl6/01_selection_sort.p6 | 31 ++++++++++++++++ 03_recursion/perl6/01_countdown.p6 | 13 +++++++ 03_recursion/perl6/02_greet.p6 | 19 ++++++++++ 03_recursion/perl6/03_factorial.p6 | 7 ++++ 04_quicksort/perl6/01_loop_sum.p6 | 13 +++++++ 04_quicksort/perl6/02_recursive_sum.p6 | 8 +++++ 04_quicksort/perl6/03_recursive_count.p6 | 8 +++++ 04_quicksort/perl6/04_recursive_max.p6 | 11 ++++++ 04_quicksort/perl6/05_quicksort.p6 | 21 +++++++++++ 11 files changed, 194 insertions(+) create mode 100755 01_introduction_to_algorithms/perl6/01_binary_search.p6 create mode 100755 02_selection_sort/perl6/01_selection_sort.p6 create mode 100755 03_recursion/perl6/01_countdown.p6 create mode 100755 03_recursion/perl6/02_greet.p6 create mode 100755 03_recursion/perl6/03_factorial.p6 create mode 100755 04_quicksort/perl6/01_loop_sum.p6 create mode 100755 04_quicksort/perl6/02_recursive_sum.p6 create mode 100755 04_quicksort/perl6/03_recursive_count.p6 create mode 100755 04_quicksort/perl6/04_recursive_max.p6 create mode 100755 04_quicksort/perl6/05_quicksort.p6 diff --git a/.gitignore b/.gitignore index e43b0f9..4be9cb4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,29 @@ .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 diff --git a/01_introduction_to_algorithms/perl6/01_binary_search.p6 b/01_introduction_to_algorithms/perl6/01_binary_search.p6 new file mode 100755 index 0000000..e59e5d4 --- /dev/null +++ b/01_introduction_to_algorithms/perl6/01_binary_search.p6 @@ -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 diff --git a/02_selection_sort/perl6/01_selection_sort.p6 b/02_selection_sort/perl6/01_selection_sort.p6 new file mode 100755 index 0000000..e70597a --- /dev/null +++ b/02_selection_sort/perl6/01_selection_sort.p6 @@ -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); diff --git a/03_recursion/perl6/01_countdown.p6 b/03_recursion/perl6/01_countdown.p6 new file mode 100755 index 0000000..2d3ba73 --- /dev/null +++ b/03_recursion/perl6/01_countdown.p6 @@ -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; diff --git a/03_recursion/perl6/02_greet.p6 b/03_recursion/perl6/02_greet.p6 new file mode 100755 index 0000000..6f3bbdf --- /dev/null +++ b/03_recursion/perl6/02_greet.p6 @@ -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"; diff --git a/03_recursion/perl6/03_factorial.p6 b/03_recursion/perl6/03_factorial.p6 new file mode 100755 index 0000000..2957fe1 --- /dev/null +++ b/03_recursion/perl6/03_factorial.p6 @@ -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); diff --git a/04_quicksort/perl6/01_loop_sum.p6 b/04_quicksort/perl6/01_loop_sum.p6 new file mode 100755 index 0000000..9864c61 --- /dev/null +++ b/04_quicksort/perl6/01_loop_sum.p6 @@ -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); diff --git a/04_quicksort/perl6/02_recursive_sum.p6 b/04_quicksort/perl6/02_recursive_sum.p6 new file mode 100755 index 0000000..34feb4d --- /dev/null +++ b/04_quicksort/perl6/02_recursive_sum.p6 @@ -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); diff --git a/04_quicksort/perl6/03_recursive_count.p6 b/04_quicksort/perl6/03_recursive_count.p6 new file mode 100755 index 0000000..f03ae69 --- /dev/null +++ b/04_quicksort/perl6/03_recursive_count.p6 @@ -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); diff --git a/04_quicksort/perl6/04_recursive_max.p6 b/04_quicksort/perl6/04_recursive_max.p6 new file mode 100755 index 0000000..8ce38d7 --- /dev/null +++ b/04_quicksort/perl6/04_recursive_max.p6 @@ -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); diff --git a/04_quicksort/perl6/05_quicksort.p6 b/04_quicksort/perl6/05_quicksort.p6 new file mode 100755 index 0000000..032f83a --- /dev/null +++ b/04_quicksort/perl6/05_quicksort.p6 @@ -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);