Back to homepage

Fun exerises for programmers

I think, that it's useful to take some easy exercises, for better understanding of algorithnms, and programming ways. So, i like to make some exercises for yourself, when I am learn something, and here is some of them:

Algorithms

Day of the week

You are need to make some function, that takes argument 'day', and return array with days of the week, starting from given day. Example:

	def dayOfTheWeek(day: 'string'):
		week = ["Monday", "Tuesday", "Wednsday", "Thursday",
				 "Friday", "Saturday", "Sunday"]
		index = week.index(day)
		
		# Days from index to end of array + Days from start of array to index 
		
		return week[index:] + week[:index] 
	
	# ['Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednsday', 'Thursday']	
	print(dayOfTheWeek('Friday')) 
		

Sometimes it's really useful algorithm, like it's used in my web-application for weather.

SQL

SQL is not necessary for today's programming. Anyway, it's funny, that this language first of all being created for average users of computers, that work in companies, but not programmers. So it's also good to try make some things with it.

You can learn a basic SQL on Python Docs and SQLite Docs.

  1. Create a simple table, and give it name, that you want. Make at lease three rows - id (autoincrement), name (string), and status (string). You also can make some more fields, if you want.
  2. Add the main user, with status "Owner", and a few other users, with different status.
  3. Now, when you have a little database, think about, how would you do to use it, and where. Try to make some ideas, like banking, etc...
  4. Select all users, and sort them, or take only users with "user" status, take only the users, whose id are not greater, than 5.
  5. Make some program, that uses SQL (The easiest way - use SQLite) as it's engine for data processing. Take care about security, like split passwords by hash and salt, and store them in separated field.

Regexp

It's also can be good to just understand, how RegExp works. You can find good tutorial on Python HOWTO, and Wikipedia.

  1. Find all in text "acatdcatocatgc", exclude "cat".

CSV Reader

CSV - it's simple tables format, and i think, it's a good thing to try create your own reader. Example CSV file looks like this:

  ID,Product,Price,Notes,
  0,Banan,15,,
  1,Pineapple,30,,
  2,Mango,25,,
  3,"Plush Toy",70,,
  4,Tea,100,"The best drink in the world!",
	
Quotes and delimiter can be replaced by any other character, like tabulation, underscore, and etc...
In result, you should have output like this:
  ['ID', 'Product', 'Price', 'Notes']
  ['0', 'Banan', '15', '']
  ['1', 'Pineapple', '30', '']
  ['2', 'Mango', '25', '']
  ['3', 'Plush Toy', '70', '']
  ['4', 'Tea', '100', 'The best drink in the world!']
	
As bonus, you can create function, that converts data from this file to SQL database.