# Programming Competition With Prize

Discussion in 'Perl Misc' started by csheppard91, Feb 12, 2006.

1. ### csheppard91Guest

csheppard91, Feb 12, 2006

2. ### Uri GuttmanGuest

>>>>> "c" == csheppard91 <> writes:

c> Wizard Solutions is now hosting it's first programming competition:
c> Test your skills against other elite programmers and have fun. The
c> prize includes money, reputation points,and a top coder ribbon on the
c> forum.

follows: Replace each digit by ((digit +7)mod 10).Then,swap the
first digit with the thir d,swap the second digit with the
fourth,and print the encrypted integer. An example session:

Enter a Four Digit Number:6254
The Encrypted Number is:2139

trivial in perl:

perl -lne 'print +(map {(\$_ + 7) %10 } /./g)[2,3,0,1]'

not something he will likely see in c++ or java. and when the golfers
get to work on it, he will be bamboozled.

i am not in the mood to work on the other 2 problems. so here they are
(i also didn't care to login to the forum site to even bother submitting
that solution):

Peter the postman became bored one night and, to break the
monotony of the night shift, he carried out the following
experiment with a row of mailboxes in the post office. These
mailboxes were numbered 1 through n, and beginning with mailbox
2, he opened the doors of all the even-numbered mailboxes,
leaving the other closed. Next, beginning with mailbox 3, he
went to every third mail box, opening its door if it were
closed, and closing it if it were open. Then he repeated this
procedure with every fourth mailbox, then every fifth mailbox,
and so on. When he finished, he was surprised at the
distribution of closed mailboxes. Write a program to determine
which mailboxes these were.

Input: The number of mailboxes, 99 < n < 1001;
Output number of closed mailboxes

A Magic square is an N <D7> N array of numbers consisting of the
positive integers from 1 to N 2 arranged such that each integer
is used exactly once, and the s um of the numbers in each
horizontal, vertical and corner-to-corner line is the same. You
will be given the size of the array (N) and an N <D7> N array of
integers. One cell in each row will be marked as a
<91>0<92>. You must determine, if possible, whether or not the
<91>0<92> values can be replaced by p ositive integers so that
the array is a Magic square. The input data begins with a line
containing an integer N representing the numbe r of rows in a
square (1 < N <= 6). The next N lines represent the array of
valu es, N integers per row. If a solution is found, output the
square array (N numbers per line). If no solu tion is found,
output <93>no solution<94>.

<i dunno where the wacko formating shit is from. i just downloaded the
files and they look like that. the guy who posted these problems is a
moron for calling it a .txt file but it is butchered with binary format
markup. calling themselves wizard solutions seems to be a false self
impression IMO>

Sample Input:

5
24 8 17 0 15
0 5 14 23 7
13 22 0 20 4
10 0 3 12 21
2 11 25 9 0

Sample Output:

24 8 17 1 15
16 5 14 23 7
13 22 6 20 4
10 19 3 12 21
2 11 25 9 18

so have at those if you care and slam him with perl that will amaze him.

uri

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Uri Guttman, Feb 12, 2006

3. ### Anno SiegelGuest

Uri Guttman <> wrote in comp.lang.perl.misc:
> >>>>> "c" == csheppard91 <> writes:

>
> c> Wizard Solutions is now hosting it's first programming competition:
> c> Test your skills against other elite programmers and have fun. The
> c> prize includes money, reputation points,and a top coder ribbon on the
> c> forum.
>
> Your program should read a four-digit integer and encrypt it as
> follows: Replace each digit by ((digit +7)mod 10).Then,swap the
> first digit with the thir d,swap the second digit with the
> fourth,and print the encrypted integer. An example session:
>
> Enter a Four Digit Number:6254
> The Encrypted Number is:2139
>
> trivial in perl:
>
> perl -lne 'print +(map {(\$_ + 7) %10 } /./g)[2,3,0,1]'
>
> not something he will likely see in c++ or java. and when the golfers
> get to work on it, he will be bamboozled.

Hmm... so we get

sub encode {
return join '', (map {(\$_ + 7) %10 } /./g)[2,3,0,1] for shift;
}

That encoding isn't worth very much cryptographically:

sub decode {
my \$n = shift;
\$n = encode( \$n) for 1 .. 9;
\$n;
}

(Nine additional encodings work as a decoder.) Proof left as an exercise.

Anno
--
\$_='Just another Perl hacker'; print +( join( '', map { eval \$_; \$@ }
'use warnings FATAL => "all"; printf "%-1s", "\n"', 'use strict; a',
'use warnings FATAL => "all"; "@x"', '1->m') =~
m|\${ s/(.)/(\$1).*/g; \ \$_ }|is),',';

Anno Siegel, Feb 12, 2006
4. ### Guest

Uri Guttman wrote:
> i am not in the mood to work on the other 2 problems. so here they are
>
> Peter the postman became bored one night and, to break the
> monotony of the night shift, he carried out the following
> experiment with a row of mailboxes in the post office. These
> mailboxes were numbered 1 through n, and beginning with mailbox
> 2, he opened the doors of all the even-numbered mailboxes,
> leaving the other closed. Next, beginning with mailbox 3, he
> went to every third mail box, opening its door if it were
> closed, and closing it if it were open. Then he repeated this
> procedure with every fourth mailbox, then every fifth mailbox,
> and so on. When he finished, he was surprised at the
> distribution of closed mailboxes. Write a program to determine
> which mailboxes these were.
>
> Input: The number of mailboxes, 99 < n < 1001;
> Output number of closed mailboxes

OK, I'll have a hack at that one (but I won't create an account on the
contest site):

#!/usr/bin/perl
use warnings; use strict;

my %mailbox = ();
my \$n = 100; #or whatever...
for my \$start (2..\$n) {
for (my \$mailbox = \$start; \$n >= \$mailbox; \$mailbox += \$start) {
\$mailbox{\$mailbox} = not \$mailbox{\$mailbox};
}
}
print "\$_\t\$mailbox{\$_}\n" for (1..\$n);

__END__

I varied a bit from the requirements (which were to simply show the
total number of closed mailboxes) because that won't show what Peter
found interesting about the DISTRIBUTION of the outcome (whereas my
script will show the reader what is interesting - or at least those

--
http://DavidFilmer.com

, Feb 12, 2006
5. ### Anno SiegelGuest

<> wrote in comp.lang.perl.misc:
> Uri Guttman wrote:
> > i am not in the mood to work on the other 2 problems. so here they are
> >
> > Peter the postman became bored one night and, to break the
> > monotony of the night shift, he carried out the following
> > experiment with a row of mailboxes in the post office. These
> > mailboxes were numbered 1 through n, and beginning with mailbox
> > 2, he opened the doors of all the even-numbered mailboxes,
> > leaving the other closed. Next, beginning with mailbox 3, he
> > went to every third mail box, opening its door if it were
> > closed, and closing it if it were open. Then he repeated this
> > procedure with every fourth mailbox, then every fifth mailbox,
> > and so on. When he finished, he was surprised at the
> > distribution of closed mailboxes. Write a program to determine
> > which mailboxes these were.
> >
> > Input: The number of mailboxes, 99 < n < 1001;
> > Output number of closed mailboxes

>
> OK, I'll have a hack at that one (but I won't create an account on the
> contest site):
>
> #!/usr/bin/perl
> use warnings; use strict;
>
> my %mailbox = ();
> my \$n = 100; #or whatever...
> for my \$start (2..\$n) {
> for (my \$mailbox = \$start; \$n >= \$mailbox; \$mailbox += \$start) {
> \$mailbox{\$mailbox} = not \$mailbox{\$mailbox};
> }
> }
> print "\$_\t\$mailbox{\$_}\n" for (1..\$n);
>
> __END__
>
> I varied a bit from the requirements (which were to simply show the
> total number of closed mailboxes) because that won't show what Peter
> found interesting about the DISTRIBUTION of the outcome (whereas my
> script will show the reader what is interesting - or at least those
> readers who are real squares).

I'm not going to give a proof, but clearly the time box n is toggled
on round k exactly if k divides n. Only squares have every prime factor
an even number of times. This is going somewhere...

Taking the proof as given, the answer to the original question (how many
closed mailboxes) is the number of perfect squares <= n:

sub n_of_open_boxes { int( sqrt( \$n) . '') }

Anno
--
\$_='Just another Perl hacker'; print +( join( '', map { eval \$_; \$@ }
'use warnings FATAL => "all"; printf "%-1s", "\n"', 'use strict; a',
'use warnings FATAL => "all"; "@x"', '1->m') =~
m|\${ s/(.)/(\$1).*/g; \ \$_ }|is),',';

Anno Siegel, Feb 12, 2006
6. ### Anno SiegelGuest

Abigail <> wrote in comp.lang.perl.misc:

[...]

> my (\$d1, \$d2);
> for my \$i (0 .. \$N - 1) {
> my \$s = 0; \$s += \$\$_ [\$i] for @square;
> \$s = \$row_total or do {print "no solution\n"; exit};

^
That should be ==

> \$d1 += \$square [\$i] [\$i];
> \$d2 += \$square [\$i] [\$N - 1 - \$i];
> }

Anno
--
\$_='Just another Perl hacker'; print +( join( '', map { eval \$_; \$@ }
'use warnings FATAL => "all"; printf "%-1s", "\n"', 'use strict; a',
'use warnings FATAL => "all"; "@x"', '1->m') =~
m|\${ s/(.)/(\$1).*/g; \ \$_ }|is),',';

Anno Siegel, Feb 12, 2006
7. ### XichengGuest

Uri Guttman wrote:
> >>>>> "c" == csheppard91 <> writes:

>
> c> Wizard Solutions is now hosting it's first programming competition:
> c> Test your skills against other elite programmers and have fun. The
> c> prize includes money, reputation points,and a top coder ribbon on the
> c> forum.
>
> Your program should read a four-digit integer and encrypt it as
> follows: Replace each digit by ((digit +7)mod 10).Then,swap the
> first digit with the thir d,swap the second digit with the
> fourth,and print the encrypted integer. An example session:
>
> Enter a Four Digit Number:6254
> The Encrypted Number is:2139
>
> trivial in perl:
>
> perl -lne 'print +(map {(\$_ + 7) %10 } /./g)[2,3,0,1]'

perl -le 'print @{[ map { (\$_ + 7) % 10 } 6,2,5,4] }[2,3,0,1]'
perl -le 'print+(map { (\$_ + 7) % 10 } 6,2,5,4)[2,3,0,1]'

Xicheng

>
> not something he will likely see in c++ or java. and when the golfers
> get to work on it, he will be bamboozled.
>
> i am not in the mood to work on the other 2 problems. so here they are
> (i also didn't care to login to the forum site to even bother submitting
> that solution):
>
> Peter the postman became bored one night and, to break the
> monotony of the night shift, he carried out the following
> experiment with a row of mailboxes in the post office. These
> mailboxes were numbered 1 through n, and beginning with mailbox
> 2, he opened the doors of all the even-numbered mailboxes,
> leaving the other closed. Next, beginning with mailbox 3, he
> went to every third mail box, opening its door if it were
> closed, and closing it if it were open. Then he repeated this
> procedure with every fourth mailbox, then every fifth mailbox,
> and so on. When he finished, he was surprised at the
> distribution of closed mailboxes. Write a program to determine
> which mailboxes these were.
>
> Input: The number of mailboxes, 99 < n < 1001;
> Output number of closed mailboxes
>
>
> A Magic square is an N <D7> N array of numbers consisting of the
> positive integers from 1 to N 2 arranged such that each integer
> is used exactly once, and the s um of the numbers in each
> horizontal, vertical and corner-to-corner line is the same. You
> will be given the size of the array (N) and an N <D7> N array of
> integers. One cell in each row will be marked as a
> <91>0<92>. You must determine, if possible, whether or not the
> <91>0<92> values can be replaced by p ositive integers so that
> the array is a Magic square. The input data begins with a line
> containing an integer N representing the numbe r of rows in a
> square (1 < N <= 6). The next N lines represent the array of
> valu es, N integers per row. If a solution is found, output the
> square array (N numbers per line). If no solu tion is found,
> output <93>no solution<94>.
>
> <i dunno where the wacko formating shit is from. i just downloaded the
> files and they look like that. the guy who posted these problems is a
> moron for calling it a .txt file but it is butchered with binary format
> markup. calling themselves wizard solutions seems to be a false self
> impression IMO>
>
> Sample Input:
>
> 5
> 24 8 17 0 15
> 0 5 14 23 7
> 13 22 0 20 4
> 10 0 3 12 21
> 2 11 25 9 0
>
>
> Sample Output:
>
> 24 8 17 1 15
> 16 5 14 23 7
> 13 22 6 20 4
> 10 19 3 12 21
> 2 11 25 9 18
>
>
> so have at those if you care and slam him with perl that will amaze him.
>
> uri
>
> --
> Uri Guttman ------ -------- http://www.stemsystems.com
> --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
> Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Xicheng, Feb 12, 2006
8. ### XichengGuest

Abigail wrote:
> Uri Guttman () wrote on MMMMDXLVIII September MCMXCIII
> in <URL:news:>:
> -- >>>>> "c" == csheppard91 <> writes:
> --
> -- c> Wizard Solutions is now hosting it's first programming competition:
> -- c> Test your skills against other elite programmers and have fun. The
> -- c> prize includes money, reputation points,and a top coder ribbon on the
> -- c> forum.
> --
> -- Your program should read a four-digit integer and encrypt it as
> -- follows: Replace each digit by ((digit +7)mod 10).Then,swap the
> -- first digit with the thir d,swap the second digit with the
> -- fourth,and print the encrypted integer. An example session:
> --
> -- Enter a Four Digit Number:6254
> -- The Encrypted Number is:2139
> --
> -- trivial in perl:
> --
> -- perl -lne 'print +(map {(\$_ + 7) %10 } /./g)[2,3,0,1]'
> --
> -- not something he will likely see in c++ or java. and when the golfers
> -- get to work on it, he will be bamboozled.
>
> It's still trivial in C and Java as well.
>
> -- i am not in the mood to work on the other 2 problems. so here they are
> -- (i also didn't care to login to the forum site to even bother submitting
> -- that solution):
> --
> -- Peter the postman became bored one night and, to break the
> -- monotony of the night shift, he carried out the following
> -- experiment with a row of mailboxes in the post office. These
> -- mailboxes were numbered 1 through n, and beginning with mailbox
> -- 2, he opened the doors of all the even-numbered mailboxes,
> -- leaving the other closed. Next, beginning with mailbox 3, he
> -- went to every third mail box, opening its door if it were
> -- closed, and closing it if it were open. Then he repeated this
> -- procedure with every fourth mailbox, then every fifth mailbox,
> -- and so on. When he finished, he was surprised at the
> -- distribution of closed mailboxes. Write a program to determine
> -- which mailboxes these were.
> --
> -- Input: The number of mailboxes, 99 < n < 1001;
> -- Output number of closed mailboxes
>
> Trivial, and it doesn't require much programming.
>
> sub closed_mail_boxes {int sqrt shift}
>
> -- A Magic square is an N <D7> N array of numbers consisting of the
> -- positive integers from 1 to N 2 arranged such that each integer
> -- is used exactly once, and the s um of the numbers in each
> -- horizontal, vertical and corner-to-corner line is the same. You
> -- will be given the size of the array (N) and an N <D7> N array of
> -- integers. One cell in each row will be marked as a
> -- <91>0<92>. You must determine, if possible, whether or not the
> -- <91>0<92> values can be replaced by p ositive integers so that
> -- the array is a Magic square. The input data begins with a line
> -- containing an integer N representing the numbe r of rows in a
> -- square (1 < N <= 6). The next N lines represent the array of
> -- valu es, N integers per row. If a solution is found, output the
> -- square array (N numbers per line). If no solu tion is found,
> -- output <93>no solution<94>.
> --
> -- <i dunno where the wacko formating shit is from. i just downloaded the
> -- files and they look like that. the guy who posted these problems is a
> -- moron for calling it a .txt file but it is butchered with binary format
> -- markup. calling themselves wizard solutions seems to be a false self
> -- impression IMO>
> --
> -- Sample Input:
> --
> -- 5
> -- 24 8 17 0 15
> -- 0 5 14 23 7
> -- 13 22 0 20 4
> -- 10 0 3 12 21
> -- 2 11 25 9 0
> --
> --
> -- Sample Output:
> --
> -- 24 8 17 1 15
> -- 16 5 14 23 7
> -- 13 22 6 20 4
> -- 10 19 3 12 21
> -- 2 11 25 9 18
> --
>
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my \$N = <DATA>; chomp \$N;
>
> my @seen_numbers = (0) x \$N ** 2;
> my \$row_total = (\$N ** 3 + \$N) / 2;
> my @square;

== ==
> while (<DATA>) {
> my @numbers = /\d+/g;
> my \$s = 0; \$s += \$_ for @numbers;
> push @square => [map {\$_ || \$row_total - \$s} @numbers];
> \$seen_numbers [\$_] ++ for @{\$square [-1]};
> }

== ==
I didnt check your code, IMO, to fill the missing number in each row
and split the results to a 2*2 array, you might also do it this way:

while(<DATA>) {
s/ /-/g; s/\b0\b/"\$row_total-\$_"/ee;
push @square, [ split '-' ];
}

=====
Best,
Xicheng

> shift @seen_numbers;
> \$_ == 1 or do {print "no solution\n"; exit} for @seen_numbers;
>
> my (\$d1, \$d2);
> for my \$i (0 .. \$N - 1) {
> my \$s = 0; \$s += \$\$_ [\$i] for @square;
> \$s = \$row_total or do {print "no solution\n"; exit};
> \$d1 += \$square [\$i] [\$i];
> \$d2 += \$square [\$i] [\$N - 1 - \$i];
> }
> \$_ == \$row_total or do {print "no solution\n"; exit} for \$d1, \$d2;
>
> print "@\$_\n" for @square;
>
> __DATA__
> 5
> 24 8 17 0 15
> 0 5 14 23 7
> 13 22 0 20 4
> 10 0 3 12 21
> 2 11 25 9 0
> --
> perl -wle 'print "Prime" if (1 x shift) !~ /^1?\$|^(11+?)\1+\$/'

Xicheng, Feb 12, 2006
9. ### Anno SiegelGuest

Xicheng <> wrote in comp.lang.perl.misc:
> Abigail wrote:

> == ==
> > while (<DATA>) {
> > my @numbers = /\d+/g;
> > my \$s = 0; \$s += \$_ for @numbers;
> > push @square => [map {\$_ || \$row_total - \$s} @numbers];
> > \$seen_numbers [\$_] ++ for @{\$square [-1]};
> > }

> == ==
> I didnt check your code, IMO, to fill the missing number in each row
> and split the results to a 2*2 array, you might also do it this way:
>
> while(<DATA>) {
> s/ /-/g; s/\b0\b/"\$row_total-\$_"/ee;
> push @square, [ split '-' ];
> }

That's very tricky, but not exactly sound programming praxis. It would
break under a slight format change, like separating columns by two blanks
instead of one. "s/ +/-/g", or indeed "tr/ /-/s" would make it more
robust in that respect, but still...

Anno
--
\$_='Just another Perl hacker'; print +( join( '', map { eval \$_; \$@ }
'use warnings FATAL => "all"; printf "%-1s", "\n"', 'use strict; a',
'use warnings FATAL => "all"; "@x"', '1->m') =~
m|\${ s/(.)/(\$1).*/g; \ \$_ }|is),',';

Anno Siegel, Feb 13, 2006