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)

Raistlin 17-08-2009 06:54

Re: Programming Challenges?
 
Quote:

Originally Posted by Damien (Post 34855632)
Any word on that Rob?

No word from me, but then I'm still waiting for the terrorists to win :D

Seriously though, still thinking about the format - gonna sit and take a look at it today :)

Mr_love_monkey 17-08-2009 07:27

Re: Programming Challenges?
 
Quote:

Originally Posted by danielf (Post 34855679)
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 (Post 34855632)
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..

Damien 17-08-2009 08:54

Re: Programming Challenges?
 
Quote:

Originally Posted by Mr_love_monkey (Post 34855787)

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 :D Plus I program in C#

Mr_love_monkey 17-08-2009 12:07

Re: Programming Challenges?
 
Quote:

Originally Posted by Damien (Post 34855810)
I'm not scared of Sun :D Plus I program in C#

Yeah but C# is just a hacked version of java, so technically sun owns your ass :)

gazzae 17-08-2009 12:37

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


nffc 17-08-2009 13:04

Re: Programming Challenges?
 
Quote:

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

Damien 17-08-2009 13:23

Re: Programming Challenges?
 
I think hacked is a unfair description..

Hugh 17-08-2009 13:31

Re: Programming Challenges?
 
Quote:

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

LemonyBrainAid 17-08-2009 13:34

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


Damien 17-08-2009 14:00

Re: Programming Challenges?
 
We need another challenge, of the Monty Hall Type...

danielf 17-08-2009 14:06

Re: Programming Challenges?
 
Quote:

Originally Posted by Damien (Post 34855952)
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.

Damien 17-08-2009 14:12

Re: Programming Challenges?
 
Quote:

Originally Posted by danielf (Post 34855955)
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)?

punky 17-08-2009 18:13

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.

Damien 17-08-2009 19:40

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

Druchii 17-08-2009 19:58

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";
}



All times are GMT +1. The time now is 03:39.

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