-
Notifications
You must be signed in to change notification settings - Fork 0
/
soduku.pl
59 lines (55 loc) · 1.4 KB
/
soduku.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
####################################################
# script name : soduku.pl
# description :
# author : Dai Nguyen-Van
# email : dainv1989@gmail.com
# creation date : 25/10/2013
####################################################
#!/usr/bin/perl
# open file in read-only mode
# read content of input file
# remove all end of line character (EOL)
open (DATA, "<", $ARGV[0]) or die $!;
@content = <DATA>;
chomp(@content);
foreach (@content)
{
# if line is not empty
if ($_ ne "")
{
# remove all whitespace
$_ =~ s/\s//g;
# remove all non-parentheses characters
$_ =~ s/[^\(\)\{\}\[\]]//g;
@chars = split("", $_);
if($#chars % 2 == 0)
{
print "False\n";
}
else
{
my @invalid = ();
for(my $i = 0; $i <= $#chars; $i++)
{
push(@invalid, $chars[$i]);
my $pair = join '',$invalid[$#invalid-1],$invalid[$#invalid];
if ($pair eq '()' ||
$pair eq '[]' ||
$pair eq '{}')
{
pop(@invalid);
pop(@invalid);
}
}
if(@invalid == 0)
{
print "True\n";
}
else
{
print "False\n";
}
}
}
}
close(DATA);