Programming Competition With Prize

U

Uri Guttman

c> Wizard Solutions is now hosting it's first programming competition:
c> http://www.wizardsolutionsusa.com/forum/showthread.php?p=2688#post2688
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.

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
 
A

Anno Siegel

Uri Guttman said:
c> Wizard Solutions is now hosting it's first programming competition:
c> http://www.wizardsolutionsusa.com/forum/showthread.php?p=2688#post2688
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
 
U

usenet

Uri said:
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).
 
A

Anno Siegel

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
 
A

Anno Siegel

[...]
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
 
X

Xicheng

Uri said:
c> Wizard Solutions is now hosting it's first programming competition:
c> http://www.wizardsolutionsusa.com/forum/showthread.php?p=2688#post2688
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
 
X

Xicheng

Abigail said:
Uri Guttman ([email protected]) wrote on MMMMDXLVIII September MCMXCIII
in <URL:--
-- c> Wizard Solutions is now hosting it's first programming competition:
-- c> http://www.wizardsolutionsusa.com/forum/showthread.php?p=2688#post2688
-- 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
 
A

Anno Siegel

Xicheng said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top