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.
Today's challenge (if you're interested) is to implement the conditions: there's a Ferrari hidden randomly behind one of 3 doors, and a goat behind the other two. You want the Ferrari. You choose one door, and the host then eliminates one of the goats (the host knows where the Ferrari is). So now there's two choices left. The statistical conundrum is: should you stick with your choice or change to the other door? Which one has the best odds?
Run the program in both conditions (switch/no switch) for a decent number of trials, and record the probabilities of getting the car.
You'll be surprised.
BTW: I've done this one before, and it really is quite simple. My code is about 30 lines, but a third of that is variable declaration and printing. It prints "Hello World" as well...
__________________
Remember kids: We are blessed with a listening, caring government.
I could do with getting some programming practice, and (whilst I'm at it) gaining at least a passing familiarity with a few different programming languages.
I know that there are a few sites out there that have daily/weekly/monthly programming challenges, but are there any that you would recommend?
Also, anybody got any thoughts on some little programming type tasks that might give me a little practice?
If you want a real language to learn and be challenge - PEARL
My first programming language in 1980 was IBM Mainframe Assembler, with 2, 4, and 6 byte instructions, and if you weren't careful, you could overwrite the instruction, which would result in "interesting" outputs (and the code was input in punch cards.....).
__________________ Thank you for calling the Abyss.
If you have called to scream, please press 1 to be transferred to the Void, or press 2 to begin your stare. If my post is in bold and this colour, it's a Moderator Request.
My first programming language in 1980 was IBM Mainframe Assembler, with 2, 4, and 6 byte instructions, and if you weren't careful, you could overwrite the instruction, which would result in "interesting" outputs (and the code was input in punch cards.....).
Wow - they had computers back then?????
---------- Post added at 06:39 ---------- Previous post was at 06:37 ----------
Quote:
Originally Posted by webcrawler2050
If you want a real language to learn and be challenge - PEARL
Is that anything like Perl then?
I like perl, it's useful for writing code that looks like characters from comics when they swear.
My java version of the 3&5 divisor in one line (ignoring all the usual boilerplate java
Spoiler:
for (int i=0;i<=100;System.out.print(++i%3 == 0 && i%5 ==0 ? (i+" is a multiple of 3 and 5\n") : i%5 == 0 ? (i+" is a multiple of 5\n") : i%3 == 0 ? (i+" is a multiple of 3\n") : ""));
Well when I started in the 1980's I was writing code on ICL Mainframes using COBOL. Can't beat that language.....
Unless you used RPG2 - we had a in-house competition, writing a number of programmes in Cobol and RPG2; we could usually write the RPG2 programmes (from the same spec, using programmers with the same experience in both languages) in about a quarter to a fifth of the time.
__________________ Thank you for calling the Abyss.
If you have called to scream, please press 1 to be transferred to the Void, or press 2 to begin your stare. If my post is in bold and this colour, it's a Moderator Request.
My java version of the 3&5 divisor in one line (ignoring all the usual boilerplate java
Spoiler:
for (int i=0;i<=100;System.out.print(++i%3 == 0 && i%5 ==0 ? (i+" is a multiple of 3 and 5\n") : i%5 == 0 ? (i+" is a multiple of 5\n") : i%3 == 0 ? (i+" is a multiple of 3\n") : ""));
Nice use of the conditional operator.
I should really do the sorting challenge and the Monty Hall Problem one. (P.S It's a pretty famous one, you always change). If you program all week it's tiring :P
public static boolean doMontyHall() {
Door[] doors = new Door[3];
doors[0] = Door.GOAT;
doors[1] = Door.GOAT;
doors[2] = Door.GOAT;
Random r = new Random();
int carPosition = r.nextInt(3);
doors[carPosition] = Door.CAR;
int contestantSelection = r.nextInt(3);
boolean switchToWin = false;
if (doors[contestantSelection] == Door.GOAT)
switchToWin = true;
// This next step is not at all necessary to establish whether or not switching is beneficial
// and illustrates the point that once a door has been selected, the problem becomes a
// simple issue of only picking the correct door 1/3 times and the whole Host opening
// a door and asking is actually just a charade that serves to confuse people into thinking
// they now have a 50/50 chance of picking the correct door, which, of course they do, but
// 2/3 is always better than 50/50.
int hostSelection = r.nextInt(3);
while (hostSelection == contestantSelection || doors[hostSelection] == Door.CAR)
hostSelection = r.nextInt(3);
System.out.println("Contestant picked door " + contestantSelection +
", Host opened door " + hostSelection +
", Car was in door " + carPosition + ", Switch to win = " + switchToWin);
Services: Virgin Media Telephone and 100Mb broadband, Sky Q
Posts: 1,994
Re: Programming Challenges?
Quick and dirty 3 and 5 divisibility test in vbscript
Spoiler:
Code:
Option Explicit
Dim intCount,intDividedBy3,intDividedBy5,strDecPoint
For intCount=1 To 100
intDividedBy3=intCount/3
intDividedBy5=intCount/5
strDecPoint="."
If InStr(intDividedBy3,strDecPoint)=0 And InStr(intDividedBy5,strDecPoint)=0 Then
WScript.Echo intCount & " is divisible by 3 and 5"
ElseIf InStr(intDividedBy3,strDecPoint)=0 Then WScript.Echo intCount & " is divisible by 3"
ElseIf InStr(intDividedBy5,strDecPoint)=0 Then WScript.Echo IntCount & " is divisible by 5"
End If
Next
Set intCount=Nothing
Set intDividedBy3=Nothing
Set intDividedBy5=Nothing
Set strDecPoint=Nothing
WScript.Quit
Sub test()
Cells(1, 1).Select
For i = 1 To 100
three = i / 3
five = i / 5
If (three - Int(three)) = 0 And (five - Int(five)) = 0 Then
ActiveCell.Value = "div by 3 and 5 - " & i
ActiveCell.Offset(1, 0).Select
ElseIf (three - Int(three)) = 0 Then
ActiveCell.Value = "div by 3 - " & i
ActiveCell.Offset(1, 0).Select
ElseIf (five - Int(five)) = 0 Then
ActiveCell.Value = "div by 5 - " & i
ActiveCell.Offset(1, 0).Select
End If
Next i
Cells(1, 1).Select
End Sub