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)

LemonyBrainAid 14-08-2009 10:46

Re: Programming Challenges?
 
Quote:

Originally Posted by Damien (Post 34853752)
I prefer using Int32 because I prefer the highlighting in Visual Studio for it ;). As for var, there is little point in this case because I know the type of the variable, for the sake of clear code it is easier to explicitly declare it. Anyone reading the code will be very clear on what data type this is. (Also, Not everyone uses C# 3.5, which introduced Implicitly typed variables)

At the end of the day, it's your code, your choice, this is just how I've been taught by SAMS, Microsoft and tutorials across the web ;)

The main advantage of using var is that unlike VB, it doesn't mean 'Variant' - CSC actually determines the most appropriate type for it based on your code - meaning less error margin. (i.e. say I used an integer then later in the code parsed values of over 2,147,483,648, it would auto assign it to be a double)

And I think you're getting your version numbers mixed, .NET 3.0 introduced C# 3.0 and .NET 3.5 is the most current version.

Quote:

Originally Posted by Damien (Post 34853752)
My example was writing to a String Builder which had a debug at the end. I changed it to console at the end. Probably should have added that.

That's fair enough :)

---------- Post added at 10:46 ---------- Previous post was at 10:44 ----------

Quote:

Originally Posted by danielf (Post 34853756)
For large Ns, try quicksort.

Spoiler: 

(defun quick-sort (a-list)
(let ((pivot nil)
(lesser nil)
(greater nil))
(when a-list
(cond ((= (length a-list) 1)
(list (first a-list)))
(t
(setf pivot (first a-list))
(dolist (item (rest a-list))
(if (<= item pivot)
(push item lesser)
(push item greater)))
(append (quick-sort lesser) (list pivot) (quick-sort greater))
)))))


http://en.wikipedia.org/wiki/Quicksort

Yeah - I chose Bubble Sort because from the basic programming challenges that were requested it's one of the simplest algorithms to use. By far not the fastest, but simple and easy to understand.

Damien 14-08-2009 10:48

Re: Programming Challenges?
 
Quote:

And I think you're getting your version numbers mixed, .NET 3.0 introduced C# 3.0 and .NET 3.5 is the most current version.
Yeah Sorry. C# 3.0. Either way, The latest release.

punky 14-08-2009 10:51

Re: Programming Challenges?
 
Quote:

Originally Posted by LemonyBrainAid (Post 34853736)
Seeing as you're only using 10 random numbers, I suggest you actually create your own code for a bubble sort algorithm - http://en.wikipedia.org/wiki/Bubble_sort

I remember doing a bubble sort at university. Its very inefficient. The way I did it at university (and last night) was on usually about twice as efficient. I did it both ways for fun last night anyway. I'll post both later if people want.

Quote:

Originally Posted by heero_yuy (Post 34853747)
Try coding the divisibility test without using the "%" operator.

Excellent idea for test #3. :tu:

BTW remember my post on this before: http://www.cableforum.co.uk/board/34528914-post11.html

Damien 14-08-2009 10:54

Re: Programming Challenges?
 
Quote:

I remember doing a bubble sort at university. Its very inefficient. The way I did it at university (and last night) was on usually about twice as efficient. I did it both ways for fun last night anyway. I'll post both later if people want.
We should probably decide on the size of the array and measure the performance for each different implementation.

Raistlin 14-08-2009 11:02

Re: Programming Challenges?
 
Keep these coming guys, they're really helping my enthusiasm :)

Have to confess that I'm starting small and just going through the basics with some course materials that I've got to get myself back up to speed - I haven't even tried the challenges that you're posting above :)

I'm tempted to suggest a monthly coding challenge, but I'm not sure how we would judge who the winner was (there are so many 'correct' answers to these sorts of things.....answers on a postcard please :)

Damien 14-08-2009 11:05

Re: Programming Challenges?
 
Quote:

Originally Posted by Rob M (Post 34853767)
I'm tempted to suggest a monthly coding challenge, but I'm not sure how we would judge who the winner was (there are so many 'correct' answers to these sorts of things.....answers on a postcard please :)

Could either have a vote, or simply just get feedback from other people.

I mean the following requirements are all important:

  • Readability (Can I understand it?)
  • Performance (How quick is it compared to other implementations?)
  • Elegance (Is it frakking Cool?)
  • Do they Support Spurs (Automatic Point Deduction)

punky 14-08-2009 11:13

Re: Programming Challenges?
 
As I have my code I might as well post it now. I've not beautified or robustified it yet:

Creating the initial random array:

Spoiler: 
Code:

           
            int[] unsorted = new int[10];
            Random random = new Random();

            for (int i = 0; i < 10; i++)
            {
                unsorted[i] = random.Next(0, 10);
            }



The way I did it at university and how it initially last night:

Spoiler: 
Code:

            int[] sorted = new int[10];
            int temp = 99;
            int loc = 0;


            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    if (unsorted[j] < temp)
                    {
                        temp = unsorted[j];
                        loc = j;
                    }
                }
                sorted[i] = temp;
                unsorted[loc] = 99;
                temp = 99;
            }



And my bubble sort:

Spoiler: 
Code:

            int[] sorted = (int[])unsorted.Clone();
            Boolean swapped;
            int temp = 0;
            int i = 0;

            do
            {
                swapped = false;
                i = 0;
                while (!swapped && i < 10 - 1)
                {

                    if (sorted[i] > sorted[i + 1])
                    {
                        temp = sorted[i];
                        sorted[i] = sorted[i + 1];
                        sorted[i + 1] = temp;
                        swapped = true;
                    }
                    else
                    {
                        i++;
                    }
                }

            } while (swapped);



---------- Post added at 11:13 ---------- Previous post was at 11:08 ----------

Quote:

Originally Posted by Damien (Post 34853768)
Could either have a vote, or simply just get feedback from other people.

I mean the following requirements are all important:

  • Readability (Can I understand it?)
  • Performance (How quick is it compared to other implementations?)
  • Elegance (Is it frakking Cool?)

That's decent criteria. We could also get an experienced developer to act as a judge but maybe rotate the position so everyone gets a go.

LemonyBrainAid 14-08-2009 11:15

Re: Programming Challenges?
 
Quote:

Originally Posted by Rob M (Post 34853767)
Keep these coming guys, they're really helping my enthusiasm :)

Have to confess that I'm starting small and just going through the basics with some course materials that I've got to get myself back up to speed - I haven't even tried the challenges that you're posting above :)

I'm tempted to suggest a monthly coding challenge, but I'm not sure how we would judge who the winner was (there are so many 'correct' answers to these sorts of things.....answers on a postcard please :)

This sounds like a great idea and I like the ideas that Damien outlined, here are a few other suggestions:

  1. Some people think code is the best if it can get the job done in the least amount of lines - this isn't always the case, so that's not a great one.
  2. Elegance is one I really like - if it's a new piece of code - using cutting edge technology such as LINQ in C# - then that's great and should be given more marks. Other examples: Boo, Spark, etc
  3. Voting would be a good idea - Perhaps having a programming challenge and then also giving the permission to expand on some previous code or develop new, intuitive ideas for it.
  4. Perhaps having a beginner, intermediate and advanced classes for the challenge would be a good one? :)
Seems like we actually have a nice number of developers on CF, this could turn out quite nicely!

danielf 14-08-2009 11:23

Re: Programming Challenges?
 
Quote:

Originally Posted by Damien (Post 34853768)
Could either have a vote, or simply just get feedback from other people.

I mean the following requirements are all important:

  • Readability (Can I understand it?)
  • Performance (How quick is it compared to other implementations?)
  • Elegance (Is it frakking Cool?)
  • Do they Support Spurs (Automatic Point Deduction)

Those are decent criteria, but the problem with the first one is that we may not all be using the same language (My quick-sort was in LISP; I don't do C). The second is best judged by runnning the different implementations on one machine, which again might cause compatibility issues. Three is cool. Four: what's Spurs?

Raistlin 14-08-2009 11:25

Re: Programming Challenges?
 
Ok, this thread appears to be deviating somewhat from my original intention, but that's cool as I think it's moving in an interesting direction.

With regards to the 'judging' how about if it were up to the person that set the task for that month to decide on the winner? Obviously they wouldn't be able to take part in their own task, but that's not always a bad thing..... A poll would be another good option, peer review is always a good way of telling how well you're doing. Given that a poll can be added to a thread at any time this could be added after a specified time has passed and then people just vote on the submissions (also saves cluttering everything up with loads of threads). Once the voting's over and the winners announced we could then allow alternative solutions, or discussions of the solutions that have been posted.

On that note, how would the submissions be handled? If they were posted up as people put them in using the [spoiler][/spoiler] tags then other people could potentially copy the entries.

Also, there's the issue of people submitting stuff that isn't actually their work.

I like the idea of multiple classes (might give me a chance of winning something :LOL:), but fear it could be complicated to organise and judge.

LemonyBrainAid 14-08-2009 11:31

Re: Programming Challenges?
 
Quote:

Originally Posted by Rob M (Post 34853785)
Ok, this thread appears to be deviating somewhat from my original intention, but that's cool as I think it's moving in an interesting direction.

With regards to the 'judging' how about if it were up to the person that set the task for that month to decide on the winner? Obviously they wouldn't be able to take part in their own task, but that's not always a bad thing..... A poll would be another good option, peer review is always a good way of telling how well you're doing. Given that a poll can be added to a thread at any time this could be added after a specified time has passed and then people just vote on the submissions (also saves cluttering everything up with loads of threads). Once the voting's over and the winners announced we could then allow alternative solutions, or discussions of the solutions that have been posted.

On that note, how would the submissions be handled? If they were posted up as people put them in using the [spoiler][/spoiler] tags then other people could potentially copy the entries.

Also, there's the issue of people submitting stuff that isn't actually their work.

I like the idea of multiple classes (might give me a chance of winning something :LOL:), but fear it could be complicated to organise and judge.


Submissions could be PM'd to the judge or a neutral member. Multiple classes wouldn't be too hard and we could simply have one experienced developer judge them all, or voting.

You're never going to get around the submitting other people's work, but you can however tell if they've entered a beginner contest, they aren't going to be able to push out the same standard as advanced, if we go with that idea.

Damien 14-08-2009 11:43

Re: Programming Challenges?
 
We should:

  • Instead of 3 levels just have two. Beginner and Advanced. The Same Judge for both?
  • Guest Judges who set and judge the competition. A Judge must provide feedback on their decision process. A Calendar Month to do the code, 10 days for judging while the next judge starts the next one.
Maybe:
  • Contest will be to create something practical? A combination of elements rather than a quick puzzle? Mostly because programming is a combination of skills in which a quick puzzle is but one.

punky 14-08-2009 12:14

Re: Programming Challenges?
 
TBH this competition thing should only be a bit of fun away. I'd only be interested in doing it casually, if it gets too regimented then it might not be worth it. We can't turn this into Google's Summer Of Code or something.

Damien 14-08-2009 12:21

Re: Programming Challenges?
 
Quote:

Originally Posted by punky (Post 34853808)
TBH this competition thing should only be a bit of fun away. I'd only be interested in doing it casually, if it gets too regimented then it might not be worth it. We can't turn this into Google's Summer Of Code or something.

Well a Judge sets the two competitions and judges them. It will be akin to the Photo contents.

punky 14-08-2009 16:02

Re: Programming Challenges?
 
I've had a bash at the modulus one:

Spoiler: 

Code:

        static void Main(string[] args)
        {
            for (int i = 1; i <= 100; i++)
            {
                if (Modulus(i, 3) == 0 && Modulus(i, 5) == 0)
                    Console.WriteLine(i + " is a multiple of both 3 AND 5!");
                else if (Modulus(i, 3) == 0)
                    Console.WriteLine(i + " is a multiple of 3");
                else if (Modulus(i, 5) == 0)
                    Console.WriteLine(i + " is a multiple of 5");
                else
                    Console.WriteLine(i + " is not a multiple of 3 (remainder " + Modulus(i, 3) + ") or a multiple of 5 (remainder " + Modulus(i, 5) + ")");
            }


            Console.In.ReadLine();
        }

        public static int Modulus(int num, int divisor)
        {
            while (num > divisor)
            {
                num -= divisor;
            }

            if (num == divisor)
                num = 0;

            return num;
        }

Which produces:

1 is not a multiple of 3 (remainder 1) or a multiple of 5 (remainder 1)
2 is not a multiple of 3 (remainder 2) or a multiple of 5 (remainder 2)
3 is a multiple of 3
4 is not a multiple of 3 (remainder 1) or a multiple of 5 (remainder 4)
5 is a multiple of 5
6 is a multiple of 3
7 is not a multiple of 3 (remainder 1) or a multiple of 5 (remainder 2)
8 is not a multiple of 3 (remainder 2) or a multiple of 5 (remainder 3)
9 is a multiple of 3
10 is a multiple of 5
11 is not a multiple of 3 (remainder 2) or a multiple of 5 (remainder 1)
12 is a multiple of 3
13 is not a multiple of 3 (remainder 1) or a multiple of 5 (remainder 3)
14 is not a multiple of 3 (remainder 2) or a multiple of 5 (remainder 4)
15 is a multiple of both 3 AND 5!
...


Speaking of operators, in some OO (Object-Orrientated) languages like C# and Java you can actually overload operators (re-code them to do some different). I had to do this in university once - write a Matrix class (a matrix is like a grid/table of values) including defining the + - * / operations for them.

So there's another good educational challenge if someone wants. Don't complain, you're getting a programming degree for free in thread :p:


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