Exercises, examples and other material relating to training module Q110. This topic is presented on public course
This section provide you with links to (and
comments on) pieces of code to performs certain
task - algorithms - that you can study if you
need to roll your own, or lift from us if you
|Articles and tips on this subject||updated|
|4707||Some gems from an introduction to Python|
This week, I ran a private Introduction to Python course - using Python 2.7 at customer request - and here are some of the code snippets I wrote in front of the class - little things I may not have blogged about in the past. Complete examples being emailed to the delegates!
# Using&nbs ...
|4656||Identifying the first and last records in a sequence|
When programming to analyse event or log files, you'll often find it fairly easy to identify the initial or opening record of a linked series, but harder to spot the closing one until your program has gone well past it. Take this data, for example:
16:15:47 +00 INF: ===================Move ...
|4652||Testing new algorithms in PHP|
During algorithm development, code testing, experiments and spike solutions often account for far more code than the final working code - a classic example of that today where I was writing / testing a loop for Lisa.
Scenario - our booking database holds flags for each of our five hotel bedrooms for ...
|4410||A good example of recursion - a real use in Python|
Recursion is where a named block of code calls itself. The "classic" demonstration is the code to generate a factorial number:
if n ...
|4402||Finding sum, minimum, maximum and average in Python (and Ruby)|
A fresh example (in Python) from today's "learning to program in" course ... finding the number, sum, minimum and maximum of a series of numbers typed in. See [here].
We start off with zero for the count and sum to date, but we do not initialise the minimum or the maximum as we don't know what they ...
|4401||Selecting RECENT and POPULAR news and trends for your web site users|
How do we give people recent news, or talk about recent trends or popular posts>?
The obvious answer is to take data for the last "n" days or months and analyse that. But whilst that's the obvious answer, it's also obviously a fairly crude measure as it gives undue influence as it changes to what happened ...
|4325||Learning to program - what are algorithms and design patterns?|
There are common themes for how programming statements are put together to give complete section of code to perform combined tasks. And typically these are putting together building blocks in a similar way to we would do things if we were working something out by hand.
Looking for the maximum value ...
|3620||Finding the total, average, minimum and maximum in a program|
There are a number of programming techniques which the experienced coder takes for granted, but which aren't necessarily intuitive for the newcomer. Call them "design techniques" or "design patterns" that need to be learned, if you want some fashionable buzzwords.
• To produce the sum of a stream ...
|3662||Finding all the unique lines in a file, using Python or Perl|
A question - how do I process all the unique lines from a file in Python? Asked by a delegate today, solved neatly and easily using a generator which means that there's no need to store all the data - unique values can be passed back and processed onwards as they're found. This is fantastic news if ...
|3451||Why would you want to use a Perl hash?|
"What would I use one of THOSE for?" - a question often asked by newcomers to Perl when I first introduce hashes. Well - in writing a script to count up how many articles I've written about each subject we teach on, I came up with a perfect example.
A hash is described as an "unordered collection" ...
|3102||AND and OR operators - what is the difference between logical and bitwise varieties?|
Many modern programming languages have two operators for "or" and two for "and" - described as "bitwise" and "logical" operators. And you need to choose the right one in the right circumstances ... otherwise your code won't behave quite as you expect.
Bitwise OR takes the internal bit pattern of the ...
|3093||How many toilet rolls - hotel inventory and useage|
Running Well House Manor, we buy a lot of hotel supplies - ranging from sheets and towels when they are to be replaced, through guest soaps and toilet rolls, salt, sugar and jams which are ordered from time to time, to weekly or twice weekly orders such as fruit and fresh products and ultimately the ...
|3072||Finding elements common to many lists / arrays|
"What do these lists have in common". That's a common question - but what does the person asking it actually mean? Is (s)he looking for items that occur in all of the lists, but perhaps at different positions? For items that occur in two or more of the lists? For items that occur at the same position ...
|3042||Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl|
Imagine you have a tree - a series of leaves which join together as you head towards the root into bigger and bigger branches. The "Least Common Ancestor" is the point at which the branches from two leaves you have chosen come together. In computing, such a tree structure is very common - files and ...
|2993||Arrays v Lists - what is the difference, why use one or the other|
If you want a program to run quickly through a data set (that's the sort of thing you'll be doing in heavy scientific work), you'll want the data loaded into successive memory locations - but that means that you have to know how much space to allocate before you set the data up. Otherwise, you'll find ...
|2951||Lots of way of converting 3 letter month abbreviations to numbers|
There are certain programming tasks that sound simple but end up fascinating ... they sound straightforward (and probably are), but it turns out there are lots of ways of doing them and you could spend a lot of time working out which was is best. One of those is converting three letter abbreviations ...
|2894||Sorting people by their names|
Please sort into order:
Uncle Tom Cobbley
What would you do?
Uncle Tom Cobbley
That's not a standard sort order for strings of text ... and if you've got ...
|2617||Comparing floating point numbers - a word of caution and a solution|
"Think of a number between 5 and 15.
Take away the number you first thought of.
Is the result 7?"
We all played games like that as children, thinking of the number 0 ourselves so that we could do an easy calculation and impress our friends. And the result was 7 ... ...
|2586||And and Or illustrated by locks|
Lisa and I came across this impressive array of locks on what we have since learned is probably the back gate to the National Grid's Lacock Switching Station, about 3 miles North of Melksham. [picture/details]
Our first reaction was "wow - that must be REALLY secure - what are they doing in there?" ...
|2509||A life lesson from the accuracy of numbers in Excel and Lua|
Floating point numbers aren't held EXACTLY in a computer's memory - they're held to a certain accuracy (number of significant figures or binary digits to be more accurante) after which they are truncated or rounded. And this means that at time 1.0 / 3.0 * 3.0 doesn't go full circle and come back to ...
|2259||Grouping rows for a summary report - MySQL and PHP|
It's quite quick and easy to write a loop that goes through a mysql result set and displays the content on an HTML page (but remember security of data, size of resulting HTML page, and the need to consider special characters such as <). But what if you want a shorter report, grouping records together ...
|2189||Matching disparate referencing systems (MediaWiki, PHP, also Tcl)|
Yes, we are Well House CONSULTANTS and do a bit of specialist coding ...
I have a requirement on my plate at present to write a piece of code for a customer that recognises cross reference codes within a document and turns them into links. And what makes the task quite difficult is that the references ...
|1949||Nuclear Physics comes to our web site|
One of the major projects that we'll be undertaking in the near future is aimed at improving the findability of information on our site - you shouldn't need to know that you need a blog article dated 2nd Feb 2007 to find about the difference between a comparator and comparable in Java, nor to look up ...
|1840||Validating Credit Card Numbers|
It's standard practise for on line bookings these days to take credit or debit card details as a booking security, and we're no exception at Well House Manor - our hotel for business visitors to Melksham, Wiltshire. There are very many security issues involved, and I am not going to describe what we ...
|1391||Ordnance Survey Grid Reference to Latitude / Longitude|
I've already written about conversion of postal addresses to latitude and longitude using Google's interface - but what is I want to convert the Ordinance Survey's grid references into latitude and longitude? Google's interface doesn't support the OS units - and in any case it's "just" a a calculation. ...
|227||Bellringing and Programming and Objects and Perl|
For the second time this year (and it's only the second month), I find that I have a keen bell-ringer on my programming course ... he's off each evening to meet up with fellow bell-ringers in church towers in Wiltshire, and having a really good time outside course hours as well as during the day. I understand ...
|1157||Speed Networking - a great evening and how we arranged it|
What an excellent evening! Our Speed Networking evening, organised on behalf of the Federation of Small Businesses, drew in around a dozen inividual and a coupe of pairs with a disparate range of products to sell, and a wide variety of requirements to meet - some by using other's products and services.
|1187||Updating a page strictly every minute (PHP, Perl)|
For the Clock exercise that I mentioned in my last entry, I need to update the page once a minute. Exactly once a minute. At the start of the minute.
I'm using client pull, where the browser asks for the next page - so my basic request is:
<meta http-equiv="refresh" content="60; url=/clock/now.php">
|202||Searching for numbers|
If I'm searching for a 50kg bag of cement, and the online store only offers 48kg bags, will their search engine find this product and say "is this what you want"? Our own site searches do clever things with alphabetic searches but we're rarely had to do a "near number" hunt on our own behalf ... but ...
Mean time between failure.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre.