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)

danielf 14-08-2009 22:46

Re: Programming Challenges?
 
Here's an interesting one, well I think it's interesting if only from a counter-intuitive statistical perspective.

The 'Monty Hall problem'.

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... :)

webcrawler2050 14-08-2009 22:48

Re: Programming Challenges?
 
Quote:

Originally Posted by Rob M (Post 34852615)
Afternoon All,

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

Hugh 14-08-2009 22:57

Re: Programming Challenges?
 
When I were't lad.........

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.....).

Mr_love_monkey 15-08-2009 06:39

Re: Programming Challenges?
 
Quote:

Originally Posted by foreverwar (Post 34854338)
When I were't lad.........

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 (Post 34854321)
If you want a real language to learn and be challenge - PEARL

Is that anything like Perl then? :angel:

I like perl, it's useful for writing code that looks like characters from comics when they swear.

Fred_Smith 15-08-2009 07:39

Re: Programming Challenges?
 
Well when I started in the 1980's I was writing code on ICL Mainframes using COBOL. Can't beat that language.....

Mr_love_monkey 15-08-2009 08:28

Re: Programming Challenges?
 
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") : ""));

Hugh 15-08-2009 09:39

Re: Programming Challenges?
 
Quote:

Originally Posted by Fred_Smith (Post 34854433)
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. ;)

Raistlin 15-08-2009 09:56

Re: Programming Challenges?
 
Quote:

Originally Posted by danielf (Post 34854319)
The statistical conundrum is: should you stick with your choice or change to the other door? Which one has the best odds?



I can answer that question if it will help anybody with the code? :D

Damien 15-08-2009 11:12

Re: Programming Challenges?
 
Quote:

Originally Posted by Mr_love_monkey (Post 34854436)
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

Paddy1 15-08-2009 12:51

Re: Programming Challenges?
 
Apologies for the lack of indentation but I couldn't get it to look right. Anyway, here's my effort -

Spoiler: 

import java.util.Random;

public class MontyHall {

private enum Door { CAR, GOAT }

public static void main(String[] args) {

int switchWins = 0;
int iterations = 100000;


for (int i=1;i<=iterations;i++) {
if (doMontyHall())
switchWins++;
}

System.out.println("Switch wins = " + switchWins +
", No Switch wins = " + (iterations - switchWins) +
", Percentage wins from switching = " + ((float)(switchWins * 100) / iterations));


}

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);

return switchToWin;

}
}


LemonyBrainAid 15-08-2009 12:57

Re: Programming Challenges?
 
Quote:

Originally Posted by webcrawler2050 (Post 34854321)
If you want a real language to learn and be challenge - PEARL

Hmmm... Is that based on your experience with your hosting websites? ;)

Quote:

Originally Posted by Fred_Smith (Post 34854433)
Well when I started in the 1980's I was writing code on ICL Mainframes using COBOL. Can't beat that language.....

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

Jon T 15-08-2009 13:01

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


Reedy 15-08-2009 13:08

Re: Programming Challenges?
 
Excel vba.

Spoiler: 
Code:

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


Raistlin 15-08-2009 13:45

Re: Programming Challenges?
 
I've added spoiler tags to the last two just to maintain the convention and so as not to spoil the fun for anybody else that's playing along :)

Reedy 15-08-2009 14:34

Re: Programming Challenges?
 
Cool ... I didn't know how to do them! :)


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