15-08-2009, 14:47
|
#46
|
Remoaner
Cable Forum Team
Join Date: Mar 2004
Posts: 32,719
|
Re: Programming Challenges?
Quote:
Originally Posted by LemonyBrainAid
|
Jeff Atwood is a very good blogger! His latest podcast about COBOL was quite funny.
|
|
|
15-08-2009, 15:18
|
#47
|
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
|
Re: Programming Challenges?
Quote:
Originally Posted by Reedy
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
|
|
|
16-08-2009, 09:16
|
#48
|
Remoaner
Cable Forum Team
Join Date: Mar 2004
Posts: 32,719
|
Re: Programming Challenges?
|
|
|
16-08-2009, 19:14
|
#49
|
cf.mega poster
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
|
Re: Programming Challenges?
Quote:
Originally Posted by LemonyBrainAid
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.
|
|
|
16-08-2009, 19:17
|
#50
|
Remoaner
Cable Forum Team
Join Date: Mar 2004
Posts: 32,719
|
Re: Programming Challenges?
Quote:
Originally Posted by nffc
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.
|
|
|
16-08-2009, 20:21
|
#51
|
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
|
Re: Programming Challenges?
|
|
|
16-08-2009, 20:28
|
#52
|
Remoaner
Cable Forum Team
Join Date: Mar 2004
Posts: 32,719
|
Re: Programming Challenges?
Quote:
Originally Posted by Rob M
|
Then the Terrorists win. Do you want the terrorists to win Rob?
|
|
|
16-08-2009, 20:39
|
#53
|
Inactive
Join Date: Dec 2005
Posts: 23
|
Re: Programming Challenges?
Quote:
Originally Posted by Damien
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
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.
|
|
|
16-08-2009, 20:45
|
#54
|
cf.mega poster
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
|
Re: Programming Challenges?
Quote:
Originally Posted by Damien
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
|
I usually do... Makes it easier for me to read back if nothing else.
|
|
|
16-08-2009, 21:39
|
#55
|
Remoaner
Cable Forum Team
Join Date: Mar 2004
Posts: 32,719
|
Re: Programming Challenges?
Quote:
Originally Posted by Fobic
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:
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.
|
|
|
16-08-2009, 22:19
|
#56
|
Inactive
Join Date: Jun 2003
Location: London way
Age: 49
Services: Sarcasm
Posts: 8,376
|
Re: Programming Challenges?
Quote:
Originally Posted by Damien
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...
|
|
|
16-08-2009, 22:27
|
#57
|
Remoaner
Cable Forum Team
Join Date: Mar 2004
Posts: 32,719
|
Re: Programming Challenges?
Quote:
Originally Posted by Mr_love_monkey
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?
|
|
|
16-08-2009, 22:31
|
#58
|
cf.mega poster
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
|
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!
|
|
|
16-08-2009, 23:22
|
#59
|
cf.mega poser
Join Date: Jun 2003
Posts: 16,687
|
Re: Programming Challenges?
Here's my Monty Hall. It's in LISP again, but nobody speaks LISP
__________________
Remember kids: We are blessed with a listening, caring government.
|
|
|
17-08-2009, 00:39
|
#60
|
Inactive
Join Date: Jun 2009
Posts: 139
|
Re: Programming Challenges?
What the hell is THAT language!!
|
|
|
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
HTML code is Off
|
|
|
All times are GMT +1. The time now is 16:28.
|