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 17-08-2009, 06:54   #61
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 Damien View Post
Any word on that Rob?
No word from me, but then I'm still waiting for the terrorists to win

Seriously though, still thinking about the format - gonna sit and take a look at it today
Raistlin is offline   Reply With Quote
Advertisement
Old 17-08-2009, 07:27   #62
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 danielf View Post
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))))
))

ah..... lisp.....next thing we know we'll have code in Occam and Prolog

---------- Post added at 07:27 ---------- Previous post was at 07:25 ----------

Quote:
Originally Posted by Damien View Post
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

well, again, Sun coding standards say that you should do :

Code:
 if (true) { 
   // something 
} else {
  //something else
}
which is slightly less bulky..
Mr_love_monkey is offline   Reply With Quote
Old 17-08-2009, 08:54   #63
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

well, again, Sun coding standards say that you should do :

Code:
 if (true) { 
   // something 
} else {
  //something else
}
which is slightly less bulky..
I'm not scared of Sun Plus I program in C#
Damien is offline   Reply With Quote
Old 17-08-2009, 12:07   #64
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
I'm not scared of Sun Plus I program in C#
Yeah but C# is just a hacked version of java, so technically sun owns your ass
Mr_love_monkey is offline   Reply With Quote
Old 17-08-2009, 12:37   #65
gazzae
Inactive
 
gazzae's Avatar
 
Join Date: Jun 2003
Location: Belfast
Age: 45
Posts: 4,594
gazzae is cast in bronzegazzae is cast in bronzegazzae is cast in bronzegazzae is cast in bronze
gazzae is cast in bronzegazzae is cast in bronzegazzae is cast in bronzegazzae is cast in bronzegazzae is cast in bronzegazzae is cast in bronzegazzae is cast in bronzegazzae is cast in bronzegazzae is cast in bronzegazzae is cast in bronzegazzae is cast in bronzegazzae is cast in bronzegazzae is cast in bronzegazzae is cast in bronzegazzae is cast in bronzegazzae is cast in bronzegazzae is cast in bronzegazzae is cast in bronzegazzae is cast in bronze
Re: Programming Challenges?

Probably better ways to do it.

Spoiler: 


Code:
BEGIN
DECLARE @i FLOAT --Counter DECLARE @t FLOAT --Three DECLARE @f FLOAT --Five SET @i = 1 WHILE @i <= 100 BEGIN
SET @t = @i /3 SET @f = @i /5 IF CHARINDEX('.',@t) = 0 and CHARINDEX('.', @f) = 0
BEGIN
PRINT CAST(@i AS VARCHAR) + ' is a multiple of 3 and 5'
END
ELSE IF CHARPRINT('.',@t) = 0
BEGIN
PRINT CAST(@i AS VARCHAR) + ' is a multiple of 3'
END
ELSE IF CHARINDEX('.',@f) = 0
BEGIN
PRINT CAST(@i AS VARCHAR) + ' is a multiple of 5'
END
SET @i = @i + 1
END
END
gazzae is offline   Reply With Quote
Old 17-08-2009, 13:04   #66
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 Mr_love_monkey View Post
Yeah but C# is just a hacked version of java, so technically sun owns your ass
What about if he doesn't have a donkey?

Anyway, they're both essentially hacked versions of C...
__________________


nffc is offline   Reply With Quote
Old 17-08-2009, 13:23   #67
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?

I think hacked is a unfair description..
Damien is offline   Reply With Quote
Old 17-08-2009, 13:31   #68
Hugh
laeva recumbens anguis
Cable Forum Team
 
Hugh's Avatar
 
Join Date: Jun 2006
Age: 68
Services: Premiere Collection
Posts: 43,475
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 auraHugh has a golden aura
Re: Programming Challenges?

Quote:
Originally Posted by Mr_love_monkey View Post
Yeah but C# is just a hacked version of java, so technically sun owns your ass
I think you will find that Oracle owns Sun's burro (or very soon will), so you are now all Larry's beyatches....
__________________
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 17-08-2009, 13:34   #69
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?

C#:

Spoiler: 
Code:
for (var i = 1; i < 100; i++)
{
    if (i % 3 && i % 5 == 0)
        Console.WriteLine(i + " is a multiple of both 3 and 5");
    if (i % 3 == 0)
        Console.WriteLine(i + " is a multiple of 3");
    if (i % 5 == 0)
        Console.WriteLine(i + " is a multiple of 5");
}


VB.NET:
Spoiler: 
Code:
Dim i As Integer

For i = 1 To 100
    If i Mod 3 AndAlso i Mod 5 = 0 Then
        Console.WriteLine(i + " is a multiple of both 3 and 5")
    End If
    If i Mod 3 = 0 Then
        Console.WriteLine(i + " is a multiple of both 3")
    End If
    If i Mod 5 = 0 Then
        Console.WriteLine(i + " is a multiple of both 3")
    End If
Next i


PHP:
Spoiler: 

PHP Code:
<?php
for ($i=1$i<100$i++)
  {
    if (
$i == 0)
    {
        if (
$i == 0)
        {
            echo(
$i." is a multiple of both 3 and 5<br />");
        }
        else
        {
            echo(
$i." is a multiple of 3<br />");
        }
    }
    if (
$i == 0)
    {
        if (
$i == 0)
        {
        echo(
$i." is a multiple of both 3 and 5<br />");
        }
        else
        {
        echo(
$i." is a multiple of 5<br />");
        }
    }
  }
?>

BASIC: (Not actually 100% this works, hah!)
Spoiler: 
Code:
10 FOR I$ = 1 TO 100
20 IF I$ % 3 AND I$ % 5 = 0 THEN GOTO 60
30 IF I$ % 3 = 0 THEN GOTO 70
40 IF I$ % 5 = 0 THEN GOTO 80
50 NEXT I$
60 PRINT I$ "is a multiple of both 3 and 5"
70 PRINT I$ "is a multiple of 3"
80 PRINT I$ "is a multiple of 5"
90 END
LemonyBrainAid is offline   Reply With Quote
Old 17-08-2009, 14:00   #70
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?

We need another challenge, of the Monty Hall Type...
Damien is offline   Reply With Quote
Old 17-08-2009, 14:06   #71
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?

Quote:
Originally Posted by Damien View Post
We need another challenge, of the Monty Hall Type...
I've got another one. It's a mortgage calculator. Say you have a mortgage of £100,000. Specify an interest rate and monthly repayment amount. What you want to determine is the number of months required to pay off the balance, keeping in mind that you will accrue interest on the outstanding balance. My solution is 8 lines long. You can check your solution against the calculators floating around on the web.
__________________
Remember kids: We are blessed with a listening, caring government.
danielf is offline   Reply With Quote
Old 17-08-2009, 14:12   #72
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 danielf View Post
I've got another one. It's a mortgage calculator. Say you have a mortgage of £100,000. Specify an interest rate and monthly repayment amount. What you want to determine is the number of months required to pay off the balance, keeping in mind that you will accrue interest on the outstanding balance. My solution is 8 lines long. You can check your solution against the calculators floating around on the web.
Cool. I'll do that soonish. Is this flat rate interest or is this ongoing? So is the interest calculated by a percentage of the total amount? (i.e 10% of 100,000)?
Damien is offline   Reply With Quote
Old 17-08-2009, 18:13   #73
punky
Inactive
 
Join Date: Jun 2003
Age: 44
Posts: 14,750
punky has a golden aurapunky has a golden aura
punky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aura
Re: Programming Challenges?

BTW, Here's my attempt at the Monty Hall solution. I did via Object-Orientated programming, but its probably a bit more cumbersome than Damien's attempt. Still at least its good simple intro it using it as most of the important base principals are used. Its not pure OO though but thereabouts

Spoiler: 
Code:
namespace PCTestMontyHall
{

    //class to represent the door
    class Door
    {
        public int doorNumber;
        public Boolean open = false;
        public String prize;

        //constructor, this happens when a door is created
        public Door(int doorNumber, String prize)
        {
            this.doorNumber = doorNumber;
            this.prize = prize;
        }

        //this is called when we Console.WriteLine a door. We have to override the default ToString() method
        public override String ToString()
        {
            //conditional shorthand. If open is true then it sets "Open" to status. If not it sets "Closed" to status
            String status = open ? "Open" : "Closed";
            return "Door " + doorNumber + ": " + prize + " (" + status + ")";
        }
    }

    class Program
    {
        //create a random number generator
        public static Random random = new Random();

        static void Main(string[] args)
        {
            //set initial counters and parameters
            int ferraris = 0; 
            int goats = 0;
            int gamesToPlay = 1000;
            bool contestantSwapping = false;

            //play the game
            for (int i = 0;i<gamesToPlay;i++)
            {
                //true parameter means contestant will swap, false means he doesn't.
                string result = PlayGame(contestantSwapping);

                if (result == "Ferrari")
                {
                    ferraris++;
                }
                else
                {
                    goats++;
                }
            }

            //print out statistics
            Console.WriteLine("Contestant swapping? " + contestantSwapping);
            float ferrariPercentage = ((float)ferraris / gamesToPlay) * 100;
            float goatPercentage = ((float)goats / gamesToPlay) * 100;
            Console.WriteLine(String.Format("\n\nFerraris: {0} {1}%\nGoats: {2} {3}%", ferraris, ferrariPercentage, goats, goatPercentage));
            
            //Hit enter to quit
            Console.ReadLine();


        }

        //the actual game
        static string PlayGame(Boolean swapChoice)
        {

            //create an array to hold the doors
            Door[] doors = new Door[3];

            //set contestant's choice of door randomly
            int contestantsChoice = random.Next(0, 3);

            //add door objects to the array of doors
            for (int i = 0;i < doors.Length; i++)
            {
                doors[i] = new Door(i+1,"Goat");
            }

            //choose which door a Ferrari should be behind
            int ferrari = random.Next(0, 3);
            doors[ferrari].prize = "Ferrari";

            //host has to remove a choice
            int removeChoice;
            //keep chosing a door at random until the door contains a goat and isn't the same as the contestants choice
            do
            {
                removeChoice = random.Next(0, 3);
            } while ((doors[removeChoice].prize == "Ferrari")|| (removeChoice == contestantsChoice));

            //"open" the door
            doors[removeChoice].open = true;

            //if contestant is swapping, then choose the other door with a goat
            int oldChoice = contestantsChoice;
            if (swapChoice)
            {

                do
                {
                    contestantsChoice = random.Next(0, 3);
                } while ((doors[contestantsChoice].open == true) || (contestantsChoice == oldChoice));


            }

            //return the prize as a result
            return doors[contestantsChoice].prize;

        }

        //print out the status of the doors. I added this as a debug whilst writing but illustrates overriding the default ToString method
        static void ShowDoors(Door[] doors)
        {
            Console.WriteLine("Doors:");
            for (int i = 0; i < doors.Length; i++)
            {
                Console.WriteLine(doors[i]);
            }
        }

    }
}
Result:

Contestant swapping? False


Ferraris: 330 33%
Goats: 670 67%

Contestant swapping? True


Ferraris: 671 67.1%
Goats: 329 32.9%


I'll have a go at the mortgage calculator tonight probably.
punky is offline   Reply With Quote
Old 17-08-2009, 19:40   #74
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?

Nice Punky. It's only not 'proper' OO because it's limited in scope (i.e the project is too small) to use many features. OO becomes more central to your application structure in bigger applications anyway. I mean all you can do is make door an object really...(which you did, smart).

You probably know this but when accessing a variable outside of it's containing class you should make it a property..
Damien is offline   Reply With Quote
Old 17-08-2009, 19:58   #75
Druchii
cf.mega poster
 
Druchii's Avatar
 
Join Date: Mar 2006
Location: Oslo, Norway.
Age: 36
Services: Canal Digital: 50/10
Posts: 7,577
Druchii has a nice shiny star
Druchii has a nice shiny starDruchii has a nice shiny starDruchii has a nice shiny starDruchii has a nice shiny starDruchii has a nice shiny starDruchii has a nice shiny starDruchii has a nice shiny starDruchii has a nice shiny starDruchii has a nice shiny starDruchii has a nice shiny starDruchii has a nice shiny starDruchii has a nice shiny starDruchii has a nice shiny starDruchii has a nice shiny starDruchii has a nice shiny starDruchii has a nice shiny starDruchii has a nice shiny starDruchii has a nice shiny star
Re: Programming Challenges?

Got this so far, it appears to work...

Spoiler: 
Code:
//c++
#include <iostream>
using namespace std;

int main() {
float interest;
float mortgage=100000;
float repayment;
long i=1;
cout << "Please enter the interest rate in number format sans % mark.\n";
cin >> interest;
cout << "Ok, Interest rate is set to " << interest << "% \nPlease enter the Mortgage payback amount per month sans delimiters.\n";
cin >> repayment;
do {
mortgage = mortgage - repayment;
if (i % 12 == 0){
mortgage = mortgage*(1+(interest/100));
}
++i;
} while (mortgage > 0);
cout << "100,000 repaid at " << repayment << " per month would take " << i << " months to pay off";
}
Druchii 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 00:34.


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