So, the above the library.. now follows the main loop (read: my testing code):# Each hour in a week (with days from 7am to 7pm) gets its own # unique bit in an 8-byte string. # Mon 7-8 is the 0th bit, Mon 6-7pm is 11, ... Fri 6-7 (pm) is 60th. use strict; my %base_hours = ( mon => 0, tue => 12, wed => 24 , thu => 36, fri => 48 ); my (%students,%profs,%courses); sub prof_read_file { my ($filename) = @_; my ($line, $curr_prof); open (F, $filename) || die "Could not open $filename"; while ($line = <F>) { chomp($line); next if $line =~ /^\s*$/; next if $line =~ /^-*$/; if ($line =~ /^id.*:\s*(.*)/) { $profs{$1} = $curr_prof = {}; } elsif ($line =~ m/^name\s*:\s*(.*)/) { $curr_prof->{name} = $1; } elsif ($line =~ /^Office Hours.*:\s*(.*)/) { $curr_prof->{Office_Hours} = interval_parse($1); } elsif ($line =~ /^Courses.*:\s*(.*)/) { my (@courses_taught) = split(/[\s,]+/, $1); $curr_prof->{Courses} = \@courses_taught; } } } sub interval_parse { my ($interval_sequence) = @_; #contains "Mon 3-5, Tue 2-6" print "[debug] interval_sequence = $interval_sequence\n" ; my ($time_range) = ""; foreach my $day_hours (split /,/, $interval_sequence) { # $day_hours contains "Mon 3-5" etc. my ($day, $from, $to) = ($day_hours =~ /([A-Za-z]+).*(\d+)-(\d+)/); # if $from or $to is less than 7, it must be afternoon. Normali +ze # it by adding 12. Then reduce it to a zero base by subtracting + 7 # (that is, 7 hrs to 19 hrs becomes 0 - 12. Finally, # normalize each hour in a day with respect to weekly hours, # by adding in the day's "base hour" #$to = 19 if $to == 7; $from += 12 if $from < 7 ; $to += 12 if $to <= 7; my $base = $base_hours{lc $day}; $from += $base - 7; $to += $base - 7; # At this point Tue 7a.m ==> 12 and Tue 4 p.m => 21 for (my $i = $from; $i < $to; $i++) { # Set the corresponding bit vec($time_range, $i, 1) = 1; } } $time_range; } sub course_get_hours { my $course = shift; my $result_string = `grep -2 $course courses.dat|tail -1`; return interval_parse($1) if ($result_string =~ /^Class Hours.*:\ +s*(.*)/); return undef; } sub prof_check_constraints { my ($prof) = @_; my $r_prof = $profs{$prof}; # %profs created by prof_read_file my $office_hours = $r_prof->{Office_Hours}; my $rl_courses = $r_prof->{Courses}; for my $i (0 .. $#$rl_courses) { my $course_taught = $rl_courses->[$i]; my $course_hours = course_get_hours($course_taught); print "Prof. ", $r_prof->{name}, " Office hours conflict with c +ourse ".$course_taught."\n" if interval_conflicts($office_hours, $course_hours); for my $j ($i + 1 .. $#$rl_courses) { my ($other_course_hours) = course_get_hours($rl_courses->[$ +j]); if (interval_conflicts ($course_hours, $other_course_hours) +){ print "Prof. ", $r_prof->{name}, ": Course conflict: ", + $rl_courses->[$i], " with ", $rl_courses->[$j], "\n" } else { print "[debug] ",$rl_courses->[$i], " = ", unpack("b*", +$course_hours), "\n", "[debug] ",$rl_courses->[$j], " = ", unpack("b*",$ +other_course_hours),"\n"; } } } } sub interval_conflicts { my ($t1, $t2) = @_; my ($combined) = $t1 & $t2; # $combined will have at least one bit set if there's a conflict my $offset = length($combined) * 8; # start counting down from last bit, and see if any is set while (--$offset >= 0) { return 1 if vec($combined,$offset,1); } return 0; }
Now, I think I understand everything well... but there is one output which seems to bug the crap out of me...prof_read_file("professors.dat"); print "Searching for overlappings: \n"; print "--------------------------\n"; prof_check_constraints("00001"); prof_check_constraints("00002"); prof_check_constraints("00003"); print "--------------------------\nSearch done.\n"; print "Current enrolled teachers:\n"; print "-------------------------\n"; while (my($key,$value)=each(%profs)) { next if not defined $profs{$key}; print "Name: ".$profs{$key}->{name}."\n"; foreach my $course (@{$profs{$key}->{Courses}}){ print "\t-$course \tSchedule: (",unpack("b*",course_get_hours +($course)),")\n" if defined $course; } print "Office Hours: ",unpack("%b*",$profs{$key}->{Office_Hours}), +"\tSchedule: (",unpack("b*",$profs{$key}->{Office_Hours}),")\n"; }
and this is the courses.dat file:id : 00001 name : John Smith Office Hours : Mon 2-7, Tue 4-7, Fri 7-1 Courses : AB200, CD300 ------------------------------------------------- id : 00002 name : Cynthia Bitch Office Hours : Mon 7-12, Tue 8-7, Fri 1-5 Courses : CD200, EF400 ------------------------------------------------- id : 00003 name : Dick Ass Office Hours : Wed 8-5, Thu 10-3 Courses : YW600, UZ400
Now, the bit representation about of the course YW600 seems to beid: AB200 Description: A Beautiful Class Class Hours: Mon 5-7, Tue 8-9, Thu 8-10 ------------------------------------------------- id: CD300 Description: Experienced Users Class Hours: Mon 5-7, Fri 3-7 ------------------------------------------------- id: CD200 Description: Basic Users Class Hours: Mon 8-9, Tue 4-6, Fri 8-11 ------------------------------------------------- id: EF400 Description: Ey Fucking 4 Zeroes Class Hours: Tue 8-10, Thu 8-10, Fri 1-5 ------------------------------------------------- id: YW600 Description: Why Would 6 Zeroes Fail Class Hours: Mon 11-2, Wed 10-12, Fri 11-3 ------------------------------------------------- id: UZ400 Description: You Suck 6 Zeroes Class Hours: Tue 11-12, Wed 3-6, Thu 3-7
00000010000000000000000000000000000000000000000000000011
but according to my courses.dat file, there should be a class on monday between 11am and 2pm.. but you can't see that for this course. All the other courses have a bit representation is logical (in the sense that the enabled bits correspondent with day/hour in the schedule)Janitored by Arunbear - added readmore tags, as per Monastery guidelines
Sorry Arunbear - I forgot to read that, since I was to eager to post.. yes, a young kid... :-/
In reply to Weird bitmap output by insaniac
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |