הלינקייה: מגזין חודשי למפתחים

רוצה לשמוע על כל האירועים, המדריכים, הקורסים והמאמרים שנכתבו החודש ?
הלינקייה הינו מגזין חופשי בעברית שמשאיר אותך בעניינים.
בלי ספאם. בלי שטויות. פעם בחודש אצלך בתיבה.

Modules Exercise

Part One

Write the modules to make the following code work:

use v5.08;
use warnings;
use strict;
use Test::More tests => 4;
use MyStack;
	
MyStack::add_item(10);
MyStack::add_item(20);
MyStack::add_item(22, 33);
	
# prints 33
is(MyStack::pop_item(), 33, "Pop first item");
	
# prints 22
is(MyStack::pop_item(), 22, "Pop second item");
	
# prints 2
is(MyStack::count_items(), 2, "Item count == 2");
	
while ( MyStack::pop_item() ) {
}

is(MyStack::count_items(), 0, "Item count == 0");

use v5.08;
use strict;
use warnings;

use Contacts;
use Test::More tests => 3;
use List::Util qw/first/;

my $home_book = Contacts::init();
my $work_book = Contacts::init();
	
Contacts::add_contact( $home_book, 'Tom', { lives_in => 'USA', email => 'tomthecat@gmail.com' });
Contacts::add_contact( $home_book, 'Bob', { lives_in => 'USA', email => 'bob@gmail.com' });
	
Contacts::add_contact( $work_book, 'Mike', { lives_in => 'Mars', email => 'mike@gmail.com' });

my @result = Contacts::contacts_by_country( $home_book, 'USA' );
ok( first { $_ eq 'Tom' } @result, "Tom is in \@result");
ok( first { $_ eq 'Bob' } @result, "Bob is in \@result");
is( @result, 2, "Result has 2 items");

use v5.08;
use strict;
use warnings;

use My::Geometry qw/rectangle_area circle_area/;
use Test::More tests => 2;

# rectangle_area takes (x,y,width,height) and
# calculates the area
my $area = rectangle_area(10, 10, 30, 30);
	
# prints 900
is($area, 900, "Rectangle area == 900");
	
# takes a radius and calculate the area of a circle
$area = circle_area( 5 );
	
# prints 78.53981633974483
my $PI = 4 * atan2(1, 1);

is($area, $PI * 5 * 5 , "Circle area == PI * R^2");
		
use v5.14;
use strict;
use warnings;

use My::Speller qw/is_real_word suggested/;
use Test::More tests => 2;

ok( is_real_word('hello'), "Hello is a real word" );

#### BONUS ######
is_deeply( [ suggested('helo') ],
           [ 'hello', 'help' ],
           "suggested(helo) == hello, help" );

Part Two

  1. Write a module to find anagrams in a words file. Two words form an anagram if they have the same letters in different order. For example, dad and add.
    The module should provide the following methods:

    1. read_file Takes an input file with words (one word per line), and read the file into memory
    2. get_random_anagram Print out a random anagram
    3. get_anagrams_for_word Takes a word and print out all other words that have the same letters
  2. Test your work by loading the dictionary file: /usr/share/dict/words

Part Three

  1. Write A Series::Fibonacci module with the following functionality:
    • sub is_series(@numbers): returns true if @numbers forms a Fibonacci series.
    • sub get_series_in_range($start, $end): returns a list of all the numbers in the series between index $start and index $end
    • sub next_in_series: takes a series of numbers which forms a Fibonacci series, and calculates the next value in the series. If the series does not form a legal Fibonacci, throw an exception
  2. Write A Series::ArithmeticProgression module with the following functionality:
    • sub is_series(@numbers): returns true if @numbers forms a Fibonacci series.
    • sub get_series_in_range($start, $end, $step): returns a list of all the numbers in the series between index $start and index $end.
    • sub next_in_series:: takes a series of numbers which forms a ArithmeticProgression series, and calculates the next value in the series. If the series does not form a legal ArithmeticProgression, throw an exception

Part Four

  1. Write A Logger module that offers the following functions:
    • init - takes a hash ref of options (such as log file name and minimum log level) and inits the log
    • log - takes a priority and a message and writes that message to the log
    • debug, info, warn, error, fatal - functions that write a message to the log with a given priority
  2. Write a Soduko::Solver module. It should provide two functions:
    • solve - takes a puzzle (as a 9x9 matrix) and returns it solved if possible
    • hint - takes a puzzle (as a 9x9 matrix) and returns a "next move" that will take us closer to the solution
course: