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 14-08-2009, 22:46   #31
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 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...
__________________
Remember kids: We are blessed with a listening, caring government.
danielf is offline   Reply With Quote
Advertisement
Old 14-08-2009, 22:48   #32
webcrawler2050
Inactive
 
Join Date: Feb 2008
Location: Swindon
Services: TiVo 110MB BB Phone Line
Posts: 3,087
webcrawler2050 has reached the bronze age
webcrawler2050 has reached the bronze agewebcrawler2050 has reached the bronze agewebcrawler2050 has reached the bronze agewebcrawler2050 has reached the bronze agewebcrawler2050 has reached the bronze agewebcrawler2050 has reached the bronze agewebcrawler2050 has reached the bronze age
Send a message via MSN to webcrawler2050
Re: Programming Challenges?

Quote:
Originally Posted by Rob M View Post
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
webcrawler2050 is offline   Reply With Quote
Old 14-08-2009, 22:57   #33
Hugh
laeva recumbens anguis
Cable Forum Team
 
Hugh's Avatar
 
Join Date: Jun 2006
Age: 68
Services: Premiere Collection
Posts: 43,472
Hugh has a golden auraHugh has a golden auraHugh has a golden aura
Hugh has a golden auraHugh has a golden auraHugh has a golden auraHugh has a golden auraHugh has a golden auraHugh has a golden auraHugh has a golden auraHugh has a golden aura
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.....).
__________________
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.
Hugh is offline   Reply With Quote
Old 15-08-2009, 06:39   #34
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 foreverwar View Post
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 View Post
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.
Mr_love_monkey is offline   Reply With Quote
Old 15-08-2009, 07:39   #35
Fred_Smith
Inactive
 
Join Date: Jun 2003
Posts: 267
Fred_Smith is on a distinguished roadFred_Smith is on a distinguished road
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.....
Fred_Smith is offline   Reply With Quote
Old 15-08-2009, 08:28   #36
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?

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") : ""));
Mr_love_monkey is offline   Reply With Quote
Old 15-08-2009, 09:39   #37
Hugh
laeva recumbens anguis
Cable Forum Team
 
Hugh's Avatar
 
Join Date: Jun 2006
Age: 68
Services: Premiere Collection
Posts: 43,472
Hugh has a golden auraHugh has a golden auraHugh has a golden aura
Hugh has a golden auraHugh has a golden auraHugh has a golden auraHugh has a golden auraHugh has a golden auraHugh has a golden auraHugh has a golden auraHugh has a golden aura
Re: Programming Challenges?

Quote:
Originally Posted by Fred_Smith View Post
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.
Hugh is offline   Reply With Quote
Old 15-08-2009, 09:56   #38
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 danielf View Post
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?
Raistlin is offline   Reply With Quote
Old 15-08-2009, 11:12   #39
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
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
Damien is offline   Reply With Quote
Old 15-08-2009, 12:51   #40
Paddy1
cf.addict
 
Join Date: Jul 2004
Posts: 350
Paddy1 has a reputation beyond reputePaddy1 has a reputation beyond reputePaddy1 has a reputation beyond reputePaddy1 has a reputation beyond reputePaddy1 has a reputation beyond reputePaddy1 has a reputation beyond reputePaddy1 has a reputation beyond reputePaddy1 has a reputation beyond reputePaddy1 has a reputation beyond reputePaddy1 has a reputation beyond reputePaddy1 has a reputation beyond reputePaddy1 has a reputation beyond reputePaddy1 has a reputation beyond repute
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;

}
}

Paddy1 is offline   Reply With Quote
Old 15-08-2009, 12:57   #41
LemonyBrainAid
Inactive
 
Join Date: Jul 2004
Location: 127.0.0.1
Services: 50MB Virgin w/ TiVo 1TB
Posts: 1,255
LemonyBrainAid has a bronze arrayLemonyBrainAid has a bronze arrayLemonyBrainAid has a bronze array
LemonyBrainAid has a bronze arrayLemonyBrainAid has a bronze arrayLemonyBrainAid has a bronze arrayLemonyBrainAid has a bronze array
Re: Programming Challenges?

Quote:
Originally Posted by webcrawler2050 View Post
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 View Post
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
LemonyBrainAid is offline   Reply With Quote
Old 15-08-2009, 13:01   #42
Jon T
cf.mega poster
 
Join Date: Jun 2003
Location: Mansfield, Notts
Age: 45
Services: Virgin Media Telephone and 100Mb broadband, Sky Q
Posts: 1,994
Jon T has reached the bronze age
Jon T has reached the bronze ageJon T has reached the bronze ageJon T has reached the bronze ageJon T has reached the bronze ageJon T has reached the bronze ageJon T has reached the bronze ageJon T has reached the bronze ageJon T has reached the bronze ageJon T has reached the bronze ageJon T has reached the bronze ageJon T has reached the bronze ageJon T has reached the bronze ageJon T has reached the bronze ageJon T has reached the bronze ageJon T has reached the bronze ageJon T has reached the bronze ageJon T has reached the bronze ageJon T has reached the bronze ageJon T has reached the bronze age
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
Jon T is offline   Reply With Quote
Old 15-08-2009, 13:08   #43
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?

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
Reedy is offline   Reply With Quote
Old 15-08-2009, 13:45   #44
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?

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
Raistlin is offline   Reply With Quote
Old 15-08-2009, 14:34   #45
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?

Cool ... I didn't know how to do them!
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 05:24.


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