Forum Articles
  Welcome back Join CF
You are here You are here: Home | Forum | Programming Challenges?

You are currently viewing our boards as a guest which gives you limited access to view most of the discussions, articles and other free features. By joining our Virgin Media community you will have full access to all discussions, be able to view and post threads, communicate privately with other members (PM), respond to polls, upload your own images/photos, and access many other special features. Registration is fast, simple and absolutely free so please join our community today.


Welcome to Cable Forum
Go Back   Cable Forum > Computers & IT > General IT Discussion
Register FAQ Community Calendar

Programming Challenges?
Reply
 
Thread Tools
Old 15-08-2009, 14:47   #46
Damien
Remoaner
Cable Forum Team
 
Damien's Avatar
 
Join Date: Mar 2004
Posts: 32,719
Damien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver bling
Damien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver bling
Re: Programming Challenges?

Quote:
Originally Posted by LemonyBrainAid View Post
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.
Damien is offline   Reply With Quote
Advertisement
Old 15-08-2009, 15:18   #47
Raistlin
Inactive
 
Join Date: Feb 2004
Location: There's no place like 127.0.0.1
Services: Depends on the person and the price they're offering
Posts: 12,384
Raistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered stars
Raistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered stars
Re: Programming Challenges?

Quote:
Originally Posted by Reedy View Post
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
Raistlin is offline   Reply With Quote
Old 16-08-2009, 09:16   #48
Damien
Remoaner
Cable Forum Team
 
Damien's Avatar
 
Join Date: Mar 2004
Posts: 32,719
Damien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver bling
Damien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver bling
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);
        }

    }
Damien is offline   Reply With Quote
Old 16-08-2009, 19:14   #49
nffc
cf.mega poster
 
nffc's Avatar
 
Join Date: Jul 2004
Location: chavvy Nottingham
Age: 41
Services: Freeview, Sky+, 100 Mb/s VM BB, mega i7 PC, iPhone 13, Macbook Air
Posts: 7,411
nffc has a nice shiny star
nffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny star
Re: Programming Challenges?

Quote:
Originally Posted by LemonyBrainAid View Post
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.
__________________


nffc is offline   Reply With Quote
Old 16-08-2009, 19:17   #50
Damien
Remoaner
Cable Forum Team
 
Damien's Avatar
 
Join Date: Mar 2004
Posts: 32,719
Damien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver bling
Damien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver bling
Re: Programming Challenges?

Quote:
Originally Posted by nffc View Post
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.
Damien is offline   Reply With Quote
Old 16-08-2009, 20:21   #51
Raistlin
Inactive
 
Join Date: Feb 2004
Location: There's no place like 127.0.0.1
Services: Depends on the person and the price they're offering
Posts: 12,384
Raistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered stars
Raistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered starsRaistlin is seeing silvered stars
Re: Programming Challenges?

Doesn't anybody comment their code any more
Raistlin is offline   Reply With Quote
Old 16-08-2009, 20:28   #52
Damien
Remoaner
Cable Forum Team
 
Damien's Avatar
 
Join Date: Mar 2004
Posts: 32,719
Damien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver bling
Damien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver bling
Re: Programming Challenges?

Quote:
Originally Posted by Rob M View Post
Doesn't anybody comment their code any more
Then the Terrorists win. Do you want the terrorists to win Rob?
Damien is offline   Reply With Quote
Old 16-08-2009, 20:39   #53
Fobic
Inactive
 
Join Date: Dec 2005
Posts: 23
Fobic is a jewel in the roughFobic is a jewel in the roughFobic is a jewel in the roughFobic is a jewel in the roughFobic is a jewel in the rough
Re: Programming Challenges?

Quote:
Originally Posted by Damien View Post
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 View Post
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.
Fobic is offline   Reply With Quote
Old 16-08-2009, 20:45   #54
nffc
cf.mega poster
 
nffc's Avatar
 
Join Date: Jul 2004
Location: chavvy Nottingham
Age: 41
Services: Freeview, Sky+, 100 Mb/s VM BB, mega i7 PC, iPhone 13, Macbook Air
Posts: 7,411
nffc has a nice shiny star
nffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny star
Re: Programming Challenges?

Quote:
Originally Posted by Damien View Post
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 View Post
Doesn't anybody comment their code any more
I usually do... Makes it easier for me to read back if nothing else.
__________________


nffc is offline   Reply With Quote
Old 16-08-2009, 21:39   #55
Damien
Remoaner
Cable Forum Team
 
Damien's Avatar
 
Join Date: Mar 2004
Posts: 32,719
Damien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver bling
Damien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver bling
Re: Programming Challenges?

Quote:
Originally Posted by Fobic View Post
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.
Damien is offline   Reply With Quote
Old 16-08-2009, 22:19   #56
Mr_love_monkey
Inactive
 
Mr_love_monkey's Avatar
 
Join Date: Jun 2003
Location: London way
Age: 49
Services: Sarcasm
Posts: 8,376
Mr_love_monkey has a pair of shiny starsMr_love_monkey has a pair of shiny starsMr_love_monkey has a pair of shiny starsMr_love_monkey has a pair of shiny starsMr_love_monkey has a pair of shiny starsMr_love_monkey has a pair of shiny stars
Mr_love_monkey has a pair of shiny starsMr_love_monkey has a pair of shiny starsMr_love_monkey has a pair of shiny starsMr_love_monkey has a pair of shiny starsMr_love_monkey has a pair of shiny stars
Re: Programming Challenges?

Quote:
Originally Posted by Damien View Post
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...
Mr_love_monkey is offline   Reply With Quote
Old 16-08-2009, 22:27   #57
Damien
Remoaner
Cable Forum Team
 
Damien's Avatar
 
Join Date: Mar 2004
Posts: 32,719
Damien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver bling
Damien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver bling
Re: Programming Challenges?

Quote:
Originally Posted by Mr_love_monkey View Post
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?
Damien is offline   Reply With Quote
Old 16-08-2009, 22:31   #58
nffc
cf.mega poster
 
nffc's Avatar
 
Join Date: Jul 2004
Location: chavvy Nottingham
Age: 41
Services: Freeview, Sky+, 100 Mb/s VM BB, mega i7 PC, iPhone 13, Macbook Air
Posts: 7,411
nffc has a nice shiny star
nffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny starnffc has a nice shiny star
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!
__________________


nffc is offline   Reply With Quote
Old 16-08-2009, 23:22   #59
danielf
cf.mega poser
 
danielf's Avatar
 
Join Date: Jun 2003
Posts: 16,687
danielf has a golden auradanielf has a golden auradanielf has a golden aura
danielf has a golden auradanielf has a golden auradanielf has a golden auradanielf has a golden auradanielf has a golden auradanielf has a golden auradanielf has a golden auradanielf has a golden auradanielf has a golden auradanielf has a golden auradanielf has a golden auradanielf has a golden auradanielf has a golden auradanielf has a golden auradanielf has a golden aura
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))))
))
__________________
Remember kids: We are blessed with a listening, caring government.
danielf is offline   Reply With Quote
Old 17-08-2009, 00:39   #60
Reedy
Inactive
 
Join Date: Jun 2009
Posts: 139
Reedy is just really niceReedy is just really niceReedy is just really niceReedy is just really niceReedy is just really niceReedy is just really nice
Re: Programming Challenges?

What the hell is THAT language!!
Reedy is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 21:39.


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