User avatar
Forrrge
Posts: 120
Joined: Fri Apr 27, 2012 6:07 am
Location: Gateshead, UK.

Coding Exercises

Fri Dec 05, 2014 11:15 am

I think a lot of people learning to program would find it useful if there was resource of coding exercises in varying difficulties that was easily printed and in a simple format. I've done a bit of search and haven't really found what I was particular after. With that in mind I've started this thread in the hope that some of you coding gurus could add your own exercises using the format below.

Coding Exercises

1. Reversing a string.
Write a function were you enter a text string which is then output in reverse order.

Correct Examples
Input = "RaspberryPi" Output = "iPyrrebpsaR"
Input = "Keep It Simple" Output = "elpmiS tI peeK"


2. Determining the factorial for a given number.
Write a function that accepts a number being passed to it and returns the factorial (i.e. if num = 4, return (4 * 3 * 2 * 1)). For the test cases, the range will be between 1 and 10.

Correct Examples
Input = 4 Output = 24
Input = 8 Output = 40320


3. Determining the largest word in a sentence.
Write a function that accepts a text string parameter being passed and return the largest word in the string. If there are two or more words that are the same length, return the first word from the string with that length. Ignore punctuation and assume the string will not be empty.

Correct Examples
Input = "this is fun" Output = "this"
Input = "I love cake" Output = "love"
Input = "Lets Party!" Output = "Party"

BONUS : Add validation so that an empty string cannot be entered, a message is displayed and the input is requested again.


4. Manipulating characters in a string based off their positions in the alphabet.
Create a function that takes the string parameter being passed and modify it using the following algorithm.
Replace every letter in the string with the letter following it in the alphabet (i.e. c becomes d, z becomes a). Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string.

Correct Examples
Input = "hello*3" Output = "Ifmmp*3"
Input = "fun times!" Output = "gvO Ujnft!"


5. Adding up all the numbers from 1 to a certain argument.
Create a function SimpleAdding(num) to add up all the numbers from 1 to num. For the test cases, the parameter num will be any number from 1 to 1000.

Correct Examples
Input = 12 Output = 78
Input = 140 Output = 9870


6. Determine how many hours and minutes from a value supplied in minutes.
Create a function TimeConvert(num) take the num parameter being passed and return the number of hours and minutes the parameter converts to (ie. if num = 63 then the output should be 1:3). Separate the number of hours and minutes with a colon.

Correct Examples
Input = 126 Output = "2:6"
Input = 45 Output = "0:45"



For info the inspiration and first few examples came from http://coderbyte.com/ but format is not what I was after, please visit for other examples.
Last edited by Forrrge on Fri Dec 05, 2014 12:54 pm, edited 1 time in total.

tufty
Posts: 1456
Joined: Sun Sep 11, 2011 2:32 pm

Re: Coding Exercises

Fri Dec 05, 2014 12:36 pm

… and rosettacode.org doesn't work for you?

User avatar
Forrrge
Posts: 120
Joined: Fri Apr 27, 2012 6:07 am
Location: Gateshead, UK.

Re: Coding Exercises

Fri Dec 05, 2014 12:49 pm

tufty wrote:… and rosettacode.org doesn't work for you?
Thanks, not seen that one before :)

User avatar
aTao
Posts: 1091
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Coding Exercises

Fri Dec 05, 2014 1:03 pm

My 3 standard steps with a new language:

1 Hello World

2 input name
Hello <name>

3 Towers of Hanoi
>)))'><'(((<

User avatar
Jake Nixon
Posts: 16
Joined: Sat Jan 24, 2015 11:27 am
Location: Stoke-on-Trent
Contact: Website

Re: Coding Exercises

Sun Jan 25, 2015 1:26 pm

I've always enjoyed working my way through the problems on Project Euler found at https://projecteuler.net/problems :D
Some of the problems are easier/harder than others but there is a lot to work through on that website :)

User avatar
stephj
Posts: 80
Joined: Thu Jun 21, 2012 1:20 pm
Location: Lancashire, UK

Re: Coding Exercises

Mon Jan 26, 2015 6:12 pm

Jake Nixon wrote:I've always enjoyed working my way through the problems on Project Euler
I'll second that. Here is some of the sort of stuff you will need to master........

Generate prime numbers - Sieve of Eratosthenes?

Reduce a number to its prime factors. 120 = 2*2*2*3*5

Generate the Fibonacci series. Some of these numbers can rapidly overrun long integers or even long longs (64 bit integers). How do you deal with that?

Find the highest cost/lowest cost route across a grid.

Investigate properties of palindromic numbers e.g. 625494526 or repunit numbers e.g. 111 11111 111111111111

There are currently almost 500 puzzles listed. Some will require a fair amount of research. Problem no 1 can be solved by a short program, or on paper if you can see what is behind it.

Good hunting!

User avatar
r3d4
Posts: 980
Joined: Sat Jul 30, 2011 8:21 am
Location: ./

Re: Coding Exercises

Mon Jan 26, 2015 6:46 pm

Forrrge wrote:
tufty wrote:… and rosettacode.org doesn't work for you?
Thanks, not seen that one before :)
in that case literateprograms.org might also be of some use .
Real life is, to most, a long second-best, a perpetual compromise between the ideal and the possible.
-
Meanwhile, the sysadmin who accidentally nuked the data reckons "its best not run anything more with sudo today"
-
what about spike milligan?

User avatar
Jake Nixon
Posts: 16
Joined: Sat Jan 24, 2015 11:27 am
Location: Stoke-on-Trent
Contact: Website

Re: Coding Exercises

Mon Jan 26, 2015 6:53 pm

Forrrge wrote:
tufty wrote:… and rosettacode.org doesn't work for you?
Thanks, not seen that one before :)
I haven't seen Rosetta Code before either, but it looks really useful!

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Coding Exercises

Mon Jan 26, 2015 7:02 pm

stephj wrote:Generate the Fibonacci series. Some of these numbers can rapidly overrun long integers or even long longs (64 bit integers). How do you deal with that?
Use Python ;-)

Advanced maths can be boring for a lot of people. Games are far more fun.

1. Guess the animal
The program learns to guess which animal you are thinking of by asking yes/no questions. When it fails to guess correctly, it asks you for a question that will differentiate between its closest guess and the new animal, so from then on it will know about that one too.

2. Pontoon (blackjack)
This is fairly easy to program. The standard dealer's rules to stick on 18 or above makes for a computer opponent that is simple to program.

3. An adventure game
This is totally open ended. It can be either programmed as a huge mess of code or as a small engine with a large data structure.

Then there are the classic graphics games:

4. Snake
A popular one for the RaspPi.

5. Robots
On the screen there is you and there are robots and mines. You can move around and the robots always move toward your current position. If one touches you you are dead, if anything touches a mine then it and the mine are dead. You win if all the robots are dead and you are not.

6. John Conway's Game of Life
Google for the rules. The trick with this one is to optimise the algorithm so it can run with a large world quickly.
Bonus: have a good edit mode so you can set the word up before it starts running.
Extra bonus: be able to insert gliders, spaceships and the r-pentomino easily.
Interesting task: measure the speed of light in the Game of Life universe.

Return to “General programming discussion”