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 12-08-2009 18:22

Programming Challenges?
 
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?

Damien 12-08-2009 18:35

Re: Programming Challenges?
 
What language?

I'll start with an easy one. (Don't look it up online ;))

Program something that will print to console/throw an alert for every multiple of 5 and 3 between 1 and 100. For numbers that are both multiples of 3 and 5 do something special. It's a common test.

Raistlin 12-08-2009 18:37

Re: Programming Challenges?
 
Any language really, doesn't matter.

Will have a go at your challenge tomorrow at work, thanks for that :tu:

Damien 12-08-2009 18:41

Re: Programming Challenges?
 
I gonna try to do it in C# later in the shortest/most elegant way I can. I'll wack it here in spoiler tags.

Raistlin 12-08-2009 18:46

Re: Programming Challenges?
 
Cool, thanks.

I'll probably start with Python or something like that.

Damien 13-08-2009 21:00

Re: Programming Challenges?
 
Spoiler: 

for (Int32 i = 1; i <= 100; i++)
{
if (i % 3 == 0 && 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");

}

haydnwalker 13-08-2009 21:13

Re: Programming Challenges?
 
You could try PERL, C-sharp (just noticed my Mac doesn't have a hash key :() or JAVA as they seem to be popular languages lately.

punky 13-08-2009 21:13

Re: Programming Challenges?
 
I did more or less the same. Just one suggestion i'd make regarding your code Damien:

Spoiler: 
I'd use else ifs instead of ifs otherwise you'd get unnecessary repitition.

for (Int32 i = 1; i <= 100; i++)
{
if (i % 3 == 0 && i % 5 == 0)
Console.WriteLine(i + " is a multiple of both 3 AND 5!");
else if (i % 3 == 0)
Console.WriteLine(i + " is a multiple of 3");
else if (i % 5 == 0)
Console.WriteLine(i + " is a multiple of 5");

}

i.e you get:

12 is a multiple of 3
15 is a multiple of both 3 AND 5!
18 is a multiple of 3

instead of this:

12 is a multiple of 3
15 is a multiple of both 3 AND 5!
15 is a multiple of 3
15 is a multiple of 5
18 is a multiple of 3

Ben B 13-08-2009 21:19

Re: Programming Challenges?
 
Quote:

Originally Posted by haydnwalker (Post 34853484)
(just noticed my Mac doesn't have a hash key :()

Alt + 3 ;)

punky 13-08-2009 21:27

Re: Programming Challenges?
 
BTW the absolute classic programming challenge is to generate an array of random numbers and then sort it manually. Once you do it, then you can refine it to make it as efficient as possible.

I guarantee every programming course has that exercise.

Damien 13-08-2009 21:29

Re: Programming Challenges?
 
Quote:

Originally Posted by punky (Post 34853485)
I did more or less the same. Just one suggestion i'd make regarding your code Damien:

Spoiler: 
I'd use else ifs instead of ifs otherwise you'd get unnecessary repitition.

for (Int32 i = 1; i <= 100; i++)
{
if (i % 3 == 0 && i % 5 == 0)
Console.WriteLine(i + " is a multiple of both 3 AND 5!");
else if (i % 3 == 0)
Console.WriteLine(i + " is a multiple of 3");
else if (i % 5 == 0)
Console.WriteLine(i + " is a multiple of 5");

}

i.e you get:

12 is a multiple of 3
15 is a multiple of both 3 AND 5!
18 is a multiple of 3

instead of this:

12 is a multiple of 3
15 is a multiple of both 3 AND 5!
15 is a multiple of 3
15 is a multiple of 5
18 is a multiple of 3

Opps! Miss that. Indeed.

---------- Post added at 21:29 ---------- Previous post was at 21:28 ----------

Quote:

Originally Posted by punky (Post 34853497)
BTW the absolute classic programming challenge is to generate an array of random numbers and then sort it manually. Once you do it, then you can refine it to make it as efficient as possible.

I guarantee every programming course has that exercise.

Let's do that one next then. An array of 10 random numbers should be fine...

I'll try tomorrow.

haydnwalker 14-08-2009 09:13

Re: Programming Challenges?
 
Quote:

Originally Posted by Ben B (Post 34853490)
Alt + 3 ;)

Thanks for that!! Will be much use in the coming Mac usage years :D

LemonyBrainAid 14-08-2009 10:06

Re: Programming Challenges?
 
Quote:

Originally Posted by punky (Post 34853485)
I did more or less the same. Just one suggestion i'd make regarding your code Damien:

Spoiler: 
I'd use else ifs instead of ifs otherwise you'd get unnecessary repitition.

for (Int32 i = 1; i <= 100; i++)
{
if (i % 3 == 0 && i % 5 == 0)
Console.WriteLine(i + " is a multiple of both 3 AND 5!");
else if (i % 3 == 0)
Console.WriteLine(i + " is a multiple of 3");
else if (i % 5 == 0)
Console.WriteLine(i + " is a multiple of 5");

}

i.e you get:

12 is a multiple of 3
15 is a multiple of both 3 AND 5!
18 is a multiple of 3

instead of this:

12 is a multiple of 3
15 is a multiple of both 3 AND 5!
15 is a multiple of 3
15 is a multiple of 5
18 is a multiple of 3

I'll just point out a few things:

Int32 is actually the structure that contains "int" - so you can just as easily use 'int' or actually use an implicitly typed local variable - 'var' as it will actually pick up that your variable type is an integer.
Int32 is generally used to parse or switch another variable type into the integer type (Int32.Parse) or set the maximum or minimum value of a specified integer.

Good job with the if statements though - I like how you ignored the curly braces as they're not necessary when you only have the one line of code being executed.

However - your code will simply run through and then close when it's finished calculating all of the multiples, what you may want to do is stick
Code:

Console.ReadKey(true);
after your for loop - this will then only exit on keypress.

---------- Post added at 10:06 ---------- Previous post was at 09:54 ----------

Quote:

Originally Posted by Damien (Post 34853499)
Opps! Miss that. Indeed.

---------- Post added at 21:29 ---------- Previous post was at 21:28 ----------



Let's do that one next then. An array of 10 random numbers should be fine...

I'll try tomorrow.


For this, you can just use, in C#:
Code:

Array.Sort(yourArray);
but... that's cheating!

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

Damien 14-08-2009 10:24

Re: Programming Challenges?
 
Quote:

Originally Posted by LemonyBrainAid (Post 34853736)
Int32 is actually the structure that contains "int" - so you can just as easily use 'int' or actually use an implicitly typed local variable - 'var' as it will actually pick up that your variable type is an integer.

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)

Quote:

However - your code will simply run through and then close when it's finished calculating all of the multiples, what you may want to do is stick
Code:

Console.ReadKey(true);
after your for loop - this will then only exit on keypress.
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.

danielf 14-08-2009 10:40

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

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


All times are GMT +1. The time now is 05:32.

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