Created
October 6, 2011 09:40
-
-
Save shardiwal/1266972 to your computer and use it in GitHub Desktop.
Indian T20 Practice Schedule
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# Have a look this for the problem | |
#https://docs.google.com/document/d/1TyPFlDh--yp5q_NPPHX_Yd1iuUhIoZOH0hHSTabqAZw/edit?hl=en_GB&pli=1 | |
use strict; | |
use warnings; | |
use Getopt::Std; | |
use DateTime; | |
use Date::Calc | |
qw( Delta_Days Add_Delta_Days Day_of_Week Day_of_Week_Abbreviation ); | |
my %opt; | |
my $opt_string = 'y:s:m:p:d:'; | |
getopts( $opt_string, \%opt ); | |
if ( !$opt{y} || !$opt{s} || !$opt{m} || !$opt{p} || !$opt{d} ) { | |
usage(); | |
} | |
my $result = guess_schedule( \%opt ); | |
if ($result) { | |
print "Total $result days required to make the schedule \n" | |
} | |
sub guess_schedule { | |
my ($opt) = @_; | |
my $weekdays = calculate_weekdays($opt); | |
return days_req_to_make_schedule( $opt, $weekdays ); | |
} | |
sub days_req_to_make_schedule { | |
my ( $opt, $weekdays ) = @_; | |
my @alldays = sort keys %$weekdays; | |
my @days = @alldays; | |
my $result_found = 0; | |
foreach my $key (@alldays) { | |
my $no_of_practic = 0; | |
my $prev_days = []; | |
if ( !$result_found ) { | |
shift @days; | |
foreach my $key1 (@days) { | |
$no_of_practic += $weekdays->{$key1}; | |
push( @$prev_days, $key1 ); | |
if ( $no_of_practic == $opt{d} ) { | |
return scalar @$prev_days; | |
$prev_days = []; | |
$no_of_practic = 0; | |
$result_found = 1; | |
last; | |
} | |
if ( $no_of_practic > $opt{d} ) { | |
last; | |
} | |
} | |
} | |
} | |
return 0; | |
} | |
sub calculate_weekdays { | |
my ($opt) = @_; | |
my $start_date = get_date_obj( $opt{y}, $opt{s}, 0 ); | |
my $end_date = get_date_obj( $opt{y}, $opt{m}, 1 ); | |
my $week_days = get_days_difference( $start_date, $end_date ); | |
return $week_days; | |
} | |
# Get the number of days between two dates. | |
# Prepare a hash to count the number of weekdays. | |
# Ex. | |
# { | |
# '1-Mon' => 5, | |
# '2-Tue' => 5, | |
# '3-Wed' => 5, | |
# '4-Thu' => 4, | |
# '5-Fri' => 4, | |
# '6-Sat' => 5, | |
# '7-Sun' => 5, | |
# }; | |
sub get_days_difference { | |
my ( $start_date, $end_date ) = @_; | |
my @start = ( $start_date->year, $start_date->month, $start_date->day ); | |
my @stop = ( $end_date->year, $end_date->month, $end_date->day ); | |
my $weekdays; | |
my $days_difference = Delta_Days( @start, @stop ); | |
for ( my $i = 0 ; $i <= $days_difference ; $i++ ) { | |
my @date = Add_Delta_Days( @start, $i ); | |
my $day_week = Day_of_Week(@date); | |
my $weekday = Day_of_Week_Abbreviation($day_week); | |
$weekdays->{"$day_week-$weekday"} += 1; | |
} | |
return $weekdays; | |
} | |
# Get DateTime object for all the entered date | |
# So the manipulation will be easy. | |
sub get_date_obj { | |
my ( $year, $date, $exclude_last_day ) = @_; | |
my @give_date = ( $date =~ m/../g ); | |
if ( $give_date[0] > 12 || $give_date[1] > 31 ) { | |
die "Wrong Date format it should be MMDD"; | |
} | |
my $month = $give_date[0]; | |
my $day = $give_date[1]; | |
$day -= 1 if $exclude_last_day; | |
return DateTime->new( | |
year => $year, | |
month => $month, | |
day => $day, | |
); | |
} | |
sub usage { | |
print STDERR << "EOF"; | |
usage: $0 [-y session year ] [-s start date] [-m match date] [-p minimum practic] [-d max practic] | |
-y : Session year | |
-s : Start Date (MMDD) | |
-m : Match Date (MMDD) | |
-p : Minimum practic | |
-d : Maximum paractic | |
EOF | |
exit; | |
} | |
# Example | |
# Run this script like | |
# perl schedule-t20.pl -y 2012 -s 0901 -m 1004 -p 12 -d 14 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment