Stop Microsoft

Miscellaneous => Programming & Networking => Topic started by: TheQuirk on 19 August 2005, 17:02

Title: Re: Let's play a game, pals.
Post by: TheQuirk on 19 August 2005, 17:02
I figured that no one cared for this thread. It seems that I was incorrect. Next challenge:

Fill an n by n array in the following manner:

(http://img377.imageshack.us/img377/591/chal39cl.png)

For example, suppose that n = 3. Your program should create the following array:

[[1 2 3]
 [8 9 4]
 [7 6 5]]
Title: Re: Let's play a game, pals.
Post by: solemnwarning on 19 August 2005, 19:42
Code: [Select]
#!/usr/bin/perl

print "n? ";
$n = ;
chomp($n);

if($n == "3") {
  open(TEMP, ">/tmp/array");
  print TEMP "1 2 3\n";
  print TEMP "8 9 4\n";
  print TEMP "7 6 5";
  close(TEMP);
  open(TEMP, "  @array = ;
  close(TEMP);
}

#The array is @array

is this what you mean?
Title: Re: Let's play a game, pals.
Post by: KernelPanic on 19 August 2005, 20:01
Quote from: solemnwarning

is this what you mean?


 :rolleyes:
Title: Re: Let's play a game, pals.
Post by: worker201 on 19 August 2005, 22:19
for any positive integer x,

x + (x+1) = (x+1)^2 - x^2

example:
6 + 7 = 49 - 36

try it, it works.

I made this up all by myself.
Title: Re: Let's play a game, pals.
Post by: worker201 on 19 August 2005, 22:23
I don't understand how to navigate through the square maze :(

Making an array is easy enough, I guess.  But I don't see the pattern in the circular motion.
Title: Re: Let's play a game, pals.
Post by: Laukev7 on 19 August 2005, 23:19
It's simple, worker201.

 
Code: [Select]
1 -> 2 -> 3
    |
    V
 8 -> 9 4
 ^ |
  | V
 7 <- 6 <- 5
 


 or for n=4

 
Code: [Select]
1 -> 2 -> 3 -> 4
  |
    V
12-> 13-> 14       5
  ^  | |
  | V V
 11       16<- 15       6
 ^  |
  |  V
 10<- 9 <- 8 <- 7
Title: Re: Let's play a game, pals.
Post by: worker201 on 19 August 2005, 23:34
That's even more confusing (an ascii art issue)
Title: Re: Let's play a game, pals.
Post by: Laukev7 on 19 August 2005, 23:49
Fuck.

 (http://illhostit.com/thumbnails/1405442234078754/sq3.bmp) illhostit.com - sq3.bmp (http://illhostit.com/files/1405442234078754/sq3.bmp)
Title: Re: Let's play a game, pals.
Post by: worker201 on 20 August 2005, 00:29
Oh, ok.  I kinda get it now.

Nobody post an answer to this, because I want to take a couple days to figure it out.
Title: Re: Let's play a game, pals.
Post by: KernelPanic on 20 August 2005, 00:37
Quote from: worker201
Oh, ok.  I kinda get it now.

Nobody post an answer to this, because I want to take a couple days to figure it out.


Yes, me too.
Title: Re: Let's play a game, pals.
Post by: worker201 on 20 August 2005, 01:08
learning a language to create and populate the arrays looks like the tricky part...
Title: Re: Let's play a game, pals.
Post by: Pathos on 20 August 2005, 04:57
It shouldn't take longer than 2 mins to work out the strategy to do it.
It can be very tricky so just set small goals that show your strategy is working.

Think Recursively... What it being repeated?
Then you just need to work out the base steps and how to implement the recursion.

I didn't use an actual 2d dimensional array, I simulated one with a 1d array (n*n) if that helps.
Title: Re: Let's play a game, pals.
Post by: solemnwarning on 20 August 2005, 17:02
i already posted answer
Title: Re: Let's play a game, pals.
Post by: muzzy on 20 August 2005, 18:21
oh c'mon. there are lots of trivial solutions to this spiral thing. you can just write code to do it the way you'd do it on paper.

i suppose i could write a solution for you, but i don't want to spoil the fun if you want to solve it on your own :)

also, recursion isn't the best approach imo, i'd go for a simple state machine with iteration.
Title: Re: Let's play a game, pals.
Post by: KernelPanic on 21 August 2005, 00:29
This is very kludgy, and done in bash. (Doesn't format the results properly)
Please don't laugh!  :rolleyes:


When I finish my degree I will be better.
Honest. :D

[verwijderd door de beheerder]
Title: Re: Let's play a game, pals.
Post by: worker201 on 21 August 2005, 00:41
Quote from: solemnwarning
i already posted answer

Except yours only works when n=3.  What if n=8?

I tried Tux's with n=6, and it worked.  Right on!

I really don't have the skills with initializing and populating arrays at this time, so I failed the challenge.  Currently, I am working through the first chapter of K&R, relearning for loops.  The last programming language I used (besides awk and JavaScript) was C++ in '97.  So I kinda have to relearn.
Title: Re: Let's play a game, pals.
Post by: KernelPanic on 21 August 2005, 00:46
Quote from: worker201
Except yours only works when n=3.  What if n=8?

I tried Tux's with n=6, and it worked.  Right on!

I really don't have the skills with initializing and populating arrays at this time, so I failed the challenge.  Currently, I am working through the first chapter of K&R, relearning for loops.  The last programming language I used (besides awk and JavaScript) was C++ in '97.  So I kinda have to relearn.


Yup I know how you feel.
I used to do a bit of C++ but now feel absolutely retarded when faced with a problem like this. :(
Time to brush up on things.
Title: Re: Let's play a game, pals. Challenge 2.
Post by: Pathos on 21 August 2005, 08:42
recursion is always the best solution :)

Anyway, in this excerise a recursive solution is pretty much the same as the iterative.
Title: Re: Let's play a game, pals. Challenge 2.
Post by: KernelPanic on 23 August 2005, 01:42
I was bored so I edited my script to format the results properly,
again it is kludgy.
Attached as matrix.txt :)


Also, I want one of you fuckers to demonstrate a solution in C/C++ or maybe Pascal so I can learn from it.
Preferably using function recursion but anything is good. :D

[verwijderd door de beheerder]
Title: Re: Let's play a game, pals. Challenge 2.
Post by: worker201 on 23 August 2005, 03:20
Nice work, I have verified your correctness using n=8.