Cable Forum

Cable Forum (https://www.cableforum.uk/board/index.php)
-   General IT Discussion (https://www.cableforum.uk/board/forumdisplay.php?f=19)
-   -   Programming Challenges? (https://www.cableforum.uk/board/showthread.php?t=33653991)

Damien 15-08-2009 14:47

Re: Programming Challenges?
 
Quote:

Originally Posted by LemonyBrainAid (Post 34854558)
Hmmm... Is that based on your experience with your hosting websites? ;)



I think it's down at the moment, but here's a pretty funny article on COBOL: http://www.codinghorror.com/blog/archives/001294.html

Jeff Atwood is a very good blogger! His latest podcast about COBOL was quite funny.

Raistlin 15-08-2009 15:18

Re: Programming Challenges?
 
Quote:

Originally Posted by Reedy (Post 34854589)
Cool ... I didn't know how to do them! :)

No worries, for reference you just add [spoiler] and [/spoiler] tags around everything you want to put in the spoiler :)

Damien 16-08-2009 09:16

Re: Programming Challenges?
 
Monty Hall Problem:

Spoiler: 

Code:

class Program
    {
        const string CAR = "CAR";
        const string GOAT = "GOAT";

        static void Main(string[] args)
        {
            Random random = new Random();
            Double DidntChangeSuccessRate = 0;
            Double DidChangeSuccessRate = 0;
            Int32 doorToOpen;
            IList<String> doors = new List<String>()
            {
                    GOAT, GOAT, GOAT
            };

            for (Int32 round = 1; round <= 10000; round++)
            {

                doors[random.Next(0, 3)] = CAR;
                doorToOpen = random.Next(3); //Choose a Door
               
                if (round % 2 == 0) //Is Even
                {
                  doors.Remove(GOAT);
                    if (doors[random.Next(2)] == CAR) //Did they get the car
                        DidChangeSuccessRate++;
                    doors.Add(GOAT);
                }
                else
                {
                    if (doors[doorToOpen] == CAR) //Don't change, did they get the car.
                        DidntChangeSuccessRate++;
                }
             
                doors[doors.IndexOf(CAR)] = GOAT; //Reset car door to goat. Faster than creating new list class each time
            }

            Console.WriteLine(String.Format("Didn't Change: {0}%", (DidntChangeSuccessRate / 5000) * 100));
            Console.WriteLine(String.Format("Did Change: {0}%", (DidChangeSuccessRate / 5000) * 100));
            Console.ReadKey(true);
        }

    }


nffc 16-08-2009 19:14

Re: Programming Challenges?
 
Quote:

Originally Posted by LemonyBrainAid (Post 34853736)
Good job with the if statements though - I like how you ignored the curly braces as they're not necessary when you only have the one line of code being executed.

personally, i wouldnt recommend leaving the {}s out even for a single line. It can cause issues if someone decides to add other lines, and with nested if statements. Most tutorials I've read also recommend always using them.

Damien 16-08-2009 19:17

Re: Programming Challenges?
 
Quote:

Originally Posted by nffc (Post 34855506)
personally, i wouldnt recommend leaving the {}s out even for a single line. It can cause issues if someone decides to add other lines, and with nested if statements. Most tutorials I've read also recommend always using them.


If someone wants to add lines they can easily add them. It's just personal choice. Neither is better than the other.

Raistlin 16-08-2009 20:21

Re: Programming Challenges?
 
Doesn't anybody comment their code any more :erm: :D :D

Damien 16-08-2009 20:28

Re: Programming Challenges?
 
Quote:

Originally Posted by Rob M (Post 34855555)
Doesn't anybody comment their code any more :erm: :D :D

Then the Terrorists win. Do you want the terrorists to win Rob?

Fobic 16-08-2009 20:39

Re: Programming Challenges?
 
Quote:

Originally Posted by Damien (Post 34855113)
Monty Hall Problem:

Interesting code: made me think .... always a good thing.
Liked your shortcut for resetting the list.

However, i dont think the code works the way it should for the case when the contestant changes their choice.

Quote:

Originally Posted by Damien (Post 34855113)
Code:

                {
                  doors.Remove(GOAT);
                    if (doors[random.Next(2)] == CAR) //Did they get the car
                        DidChangeSuccessRate++;
                    doors.Add(GOAT);
                }


You seem to be removing the first door you come across with a Goat behind it (which may even be the contestant's chosen door) and then choosing at random from the 2 remaining doors, rather than swapping to the unchosen door.

I suspect this will give results of 33% and 50% in your output which is not the correct answer.

nffc 16-08-2009 20:45

Re: Programming Challenges?
 
Quote:

Originally Posted by Damien (Post 34855510)
If someone wants to add lines they can easily add them. It's just personal choice. Neither is better than the other.

Well, I can't see the extra 2 characters making much difference performance wise but heh, it was just something I have always seen recommended as good practice.
Quote:

Originally Posted by Rob M (Post 34855555)
Doesn't anybody comment their code any more :erm: :D :D

I usually do... Makes it easier for me to read back if nothing else. :D

Damien 16-08-2009 21:39

Re: Programming Challenges?
 
Quote:

Originally Posted by Fobic (Post 34855564)
Interesting code: made me think .... always a good thing.
Liked your shortcut for resetting the list.

However, i dont think the code works the way it should for the case when the contestant changes their choice.



You seem to be removing the first door you come across with a Goat behind it (which may even be the contestant's chosen door) and then choosing at random from the 2 remaining doors, rather than swapping to the unchosen door.

I suspect this will give results of 33% and 50% in your output which is not the correct answer.

I think 33% and 50% are the correct answers. If they choose to change they have a 50% chance of winning the car

Wait just figured it out. Tis Wrong.

---------- Post added at 21:39 ---------- Previous post was at 21:00 ----------

The CORRECT Answer:

Spoiler: 
Code:

class Program
    {
        const string CAR = "CAR";
        const string GOAT = "GOAT";

        static void Main(string[] args)
        {
            Random random = new Random();
            Double DidntChangeSuccessRate = 0;
            Double DidChangeSuccessRate = 0;
            Int32 doorToOpen;
            Int32 IndexOfCar;
            Int32 doorForHostToOpen;

            IList<String> doors = new List<String>()
            {
                    GOAT, GOAT, GOAT
            };

            for (Int32 round = 1; round <= 10000; round++)
            {

                doors[random.Next(0, 3)] = CAR; //Randomly assign a car door (thus killing a goat. Happy Times)
                IndexOfCar = doors.IndexOf(CAR); //The Host needs to know which is the car door
                doorToOpen = random.Next(3); //Choose a Door
                doorForHostToOpen = random.Next(3); //Host Chooses door to open

              //Now a door has been chosen Noel 'I believe in rubbish' Edmonds needs to remove a door
                //Choose the one that is not a car and not one the user has chosen.
                while (doorForHostToOpen == IndexOfCar || doorForHostToOpen == doorToOpen)
                    doorForHostToOpen = random.Next(3);


                if (round % 2 == 0) //Is Even do a switch, otherwise do a didn't switch
                {
                    //Switch door. So choose the door that hasn't been opened and was not the
                    //previously chosen one.
                    Int32 SwitchedDoor = random.Next(3);
                    while (SwitchedDoor == doorForHostToOpen || SwitchedDoor == doorToOpen)
                        SwitchedDoor = random.Next(3);

                    if (doors[SwitchedDoor] == CAR) //Did they get the car
                        DidChangeSuccessRate++;

                }
                else
                {
                    if (doors[doorToOpen] == CAR) //Don't change, did they get the car.
                        DidntChangeSuccessRate++;
                }

                //Reset car door to goat. Faster than creating new list class each time
                doors[doors.IndexOf(CAR)] = GOAT;
            }

            Console.WriteLine(String.Format("Didn't Change: {0}%", (DidntChangeSuccessRate / 5000) * 100));
            Console.WriteLine(String.Format("Did Change: {0}%", (DidChangeSuccessRate / 5000) * 100));
            Console.ReadKey(true); //And so we end our magical story. The Code goes on.
        }



Didn't change: 33.5% chance of winning
Did Change: 66.78% chance of winning

Also got to remove the nasty loop adding/removing. Bonus points for spotting the Beatles reference.

Mr_love_monkey 16-08-2009 22:19

Re: Programming Challenges?
 
Quote:

Originally Posted by Damien (Post 34855510)
If someone wants to add lines they can easily add them. It's just personal choice. Neither is better than the other.

With java the sun coding standards say you should have the braces even if it's just one line.

Though that said they also say you should avoid the compound if statement because some programmers find it hard to understand - my opinion is if you can't understand it then you shouldn't be a programmer...

Damien 16-08-2009 22:27

Re: Programming Challenges?
 
Quote:

Originally Posted by Mr_love_monkey (Post 34855625)
With java the sun coding standards say you should have the braces even if it's just one line.

Though that said they also say you should avoid the compound if statement because some programmers find it hard to understand - my opinion is if you can't understand it then you shouldn't be a programmer...

I find it's a space thing, if it's one line it's just ugly.

Code:

if (ProductType == ProductType.Software)
{
    // do something

}
else
{
  //do something else
}

or

Code:

if (ProductType == ProductType.Software)
    // do something
 else
    //do something else

Anyhoo, The Monty Hall Problem was good. I think one like that a month would be good for a monthly challenge. Any word on that Rob?

nffc 16-08-2009 22:31

Re: Programming Challenges?
 
I tend to prefer

Code:

if (this == whatever) {
  do this;
}
else {
  meh2u2;
}

TCL is really anal about the positions of the brackets with the else - the } needs to be on the same line!

danielf 16-08-2009 23:22

Re: Programming Challenges?
 
Here's my Monty Hall. It's in LISP again, but nobody speaks LISP :(

Spoiler: 
(defun monty-hall (times)
(let ((change-hits 0)
(change-misses 0)
(no-change-hits 0)
(no-change-misses 0)
(n-change-trials 0)
(doors '(nil nil nil)))
(dotimes (n times)
(dotimes (x 3)
(setf (nth x doors) 0))
(setf (nth (random 3) doors) 1)
(let* ((choice (random 3))
(choice-set (remove choice '(0 1 2)))
(alternative nil)
(change-trial (random 2)))
(if (equal (nth choice doors) 1)
(setf alternative (nth (random 2) choice-set))
(progn
(if (equal (nth (first choice-set) doors) 0)
(setf alternative (second choice-set))
(setf alternative (first choice-set)))))
(if (equal change-trial 1)
(progn
(incf n-change-trials)
(if (equal (nth alternative doors) 1)
(incf change-hits)
(incf change-misses)))
(if (equal (nth choice doors) 1)
(incf no-change-hits)
(incf no-change-misses)))))
(format t "~% change-hits ~A ~A" change-hits (float (/ change-hits n-change-trials)))
(format t "~% no-change-hits ~A ~A" no-change-hits (float (/ no-change-hits (- times n-change-trials))))
))

Reedy 17-08-2009 00:39

Re: Programming Challenges?
 
What the hell is THAT language!!:confused:


All times are GMT +1. The time now is 03:40.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
All Posts and Content are © Cable Forum