Stefano Tommesani

  • Increase font size
  • Default font size
  • Decrease font size

An object-oriented approach to FizzBuzz

E-mail Print PDF

FizzBuzz is described as:

Write a program that prints the integers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz"

It takes a couple of minutes to write a few lines of C++ code that satisfy the requirements:

#include <iostream>
using namespace std;
int main () {
 int i;
 for (i = 0; i <= 100; i++) {
 if ((i % 15) == 0)
 cout << "FizzBuzz" << endl;
 else if ((i % 3) == 0)
 cout << "Fizz" << endl;
 else if ((i % 5) == 0)
 cout << "Buzz" << endl;
 cout << i << endl;
 return 0;

But then the examiner may ask you to add some more checks, such as printing a different string when the number is a multiple of 11 or any other number, and the code quickly becomes ugly. In fact, the major mistake that you can make with FizzBuzz is using the wrong sequence of checks, for example checking for modulus of 3 before that of 15, so that FizzBuzz is never printed; this is obvious with only 3 simple checks, but gets more complicated with a longer list of more complicated checks.

So why not spend a few minutes more than those required to write a trivial solution, and propose a fully object-oriented solution that is designed for extensibility?

Last Updated on Monday, 27 May 2013 18:51

Improving web speed with Arduino

E-mail Print PDF

The Flash library by Mikal Hart is a wonderful piece of software for improving simple web server apps built on the Arduino: the main problem when designing such apps is that the limited number of RAM available forces the designer either to use an SD card to store the files that are part of the web site, and/or store some HTML fragments in the PROGMEM area, and copy them back to normal RAM to modify and include them in the server HTML packets. The Flash library greatly simplifies the second technique, as the following code fragment highlights:

FLASH_STRING(WebPageHeader, "HTML source code");
Client& client = web_server.get_client();

The FLASH_STRING macro declares a string contained in PROGMEM, so that it does not decrease the available RAM. Streaming that string is as easy as calling the print method and passing the Client connection of the web server as the parameter. Using this technique, dynamic generation of web pages includes both static parts stored in PROGMEM as FLASH_STRINGs (for example, page headers and footers, top menus, etc.) and dynamic parts that are generated by code (for example, tables containing data values coming from attached devices).

Last Updated on Thursday, 21 March 2013 23:18

Test video for CCTV motion analysis

E-mail Print PDF

CCTVTestVideoHere is video footage from a real-life scene: it's a windy day, so the camera is slightly shaking, and there is a lot of action happening in the road below. This video sequence is useful for the following tests:

  • - checking the direction of moving vehicles (there are bikes moving in the wrong direction)
  • - classifying moving objects (cars, buses, motorcycles, bikes etc.)
  • - rejecting false alarms due to unrelevant movements (the shadow of the trees is costantly moving, but it should not trigger an alarm, while people walking and running should)

You can download this video sequence (running time: 9m 42s) from Dropbox clicking on this link. (if clicking on the link starts a video player, right-click on the link and choose Save Link As.. from the popup menu)

Last Updated on Thursday, 13 September 2012 15:53

Slow blits with latest nVidia drivers

E-mail Print PDF

An application of mine uses DirectDraw to draw video frames on multiple screens, and so far the visualization pipeline used a group of off-screen YUV surfaces that, at the end of the process, are drawn into the primary surface. So this is the code that creates the primary surface:

ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(DDSURFACEDESC);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps    = DDSCAPS_PRIMARYSURFACE;
if ((ddrval = lpDD->CreateSurface(&ddsd, &lpDDSPrimary, NULL)) != DD_OK)

and this is the code that creates an off-screen YUV surface:

Last Updated on Friday, 27 July 2012 11:12

Easy multi-thread programming with Delphi

E-mail Print PDF

In the AltaPixShare software app, when the user drags a group of images to the target location, the following code iterates over the list of images, and for each image creates the destination file:

for i := 0 to Pred(ActiveImages.Count) do
CurrentImage := TProcessedImage(ActiveImages[i]);
CurrentImage := nil;
if Assigned(CurrentImage)
then begin

Given that applying a filter to a large image and then writing the output image into a JPEG file is a time-consuming task, this is a clear case for multi-threaded optimization, so that multiple images are processed at the same time on different CPU cores. And using the amazing AsyncCalls library, we can achieve this result with only a few lines of code. These are the steps required to turn this serial code into a multi-threaded one:

Last Updated on Saturday, 20 April 2013 17:19

Using Amazon SimpleDB from Delphi

E-mail Print PDF


Table of contents 1 - Introduction to Amazon SimpleDB 2 - SimpleDB and Delphi 3 - Extending Data.Cloud.AmazonAPI

Download_32 Download SimpleDBExplorer project files and executable

Introduction to Amazon SimpleDB

The best description of the Amazon SimpleDB service is given by Amazon itself here:

Amazon SimpleDB is a highly available and flexible non-relational data store that offloads the work of database administration. Developers simply store and query data items via web services requests and Amazon SimpleDB does the rest. Unbound by the strict requirements of a relational database, Amazon SimpleDB is optimized to provide high availability and flexibility, with little or no administrative burden. Behind the scenes, Amazon SimpleDB creates and manages multiple geographically distributed replicas of your data automatically to enable high availability and data durability. The service charges you only for the resources actually consumed in storing your data and serving your requests. You can change your data model on the fly, and data is automatically indexed for you. With Amazon SimpleDB, you can focus on application development without worrying about infrastructure provisioning, high availability, software maintenance, schema and index management, or performance tuning.

Amazon stresses that SimpleDB is optimized for high availability, thaks to distributed replicas of data, and low administrative costs, as there are no schemas and indexes to manage. With SimpleDB, you don't have to define a schema for each database table before you can use it, and you don't have to change that schema before storing your data in a different way. As there is no schema, there are no data types, as all data values are treated as variable length char data. So, if you want to add a new field to an existing database, you just data the new field to the data items that require it, and there is no enforcing that all data items contains the same fields. The drawback of having no schema is that you have to handle formatting and type conversions in your code, and this has a serious impact on queries.

The terminology used in SimpleDB is different from the usual DBMS one, so the following table shows how the different terms are related:

DBMS Amazon SimpleDB
table domain
row item
column attribute

The domain is analogous to a database table, but a domain has no schema, so the only parameter you can set is the name of the domain. All the data stored in a domain has the form of name-value attribute pairs, and each attributes pair belongs to an item, which is similar to a table row. The attribute name is similar to a table column, but each item can have different attribute names, so you can store data with different layouts in the same domain, and add new fields to items without restructuring the table. It is also possible to have, for a given attribute, not just one value, but an array of values, so when you add an attribute name-value pair to an item, you can choose if SimpleDB should replace the existing pair with the same attribute pair, or add the new value to the existing pair.

Queries on SimpleDB are mostly done with a key-value approach, where you retrieve an item and its attributes from the item's name. But you can also use a SQL-style query language to issue queries over the scope of a single domain. When designing queries, you should be aware that all the data stored in SimpleDB is treated as plain string data (be careful with numerical data!), and that all values are automatically indexed.

Last Updated on Friday, 27 July 2012 11:12

Page 8 of 10

Latest Articles

Easily upload videos of security cameras to YouTube
In this example, we will import video from a Yi security camera into YouTube. The same process, with eventual adjustment to the naming of directories in the SD card used by the camera to record videos, will also apply to other
A software to stand out 27 January 2018, 14.35 Web
A software to stand out
Standing out of the pack starts by being visible, and being noticed by the right group of professionals. No matter how good your profile is, it is lost in a sea of similar profiles, so you need to show up and start attracting
Web page scraping, the easy way 07 January 2018, 00.46 Web
Web page scraping, the easy way
There are many ways to extract data elements from web pages, almost all of them prettier and cooler than the method proposed here, but as we are in an hurry, let's get that data quickly, ok? Suppose we have to extract the
Scraping dynamic page content 06 January 2018, 23.57 Web
Scraping dynamic page content
One of the most common roadblocks when scraping the content of web sites is getting the full contents of the page, including JS-generated data elements (probably, the ones you are looking for). So, when using CEFSharp to scrape
Unit-testing file I/O 26 November 2017, 12.09 Testing
Unit-testing file I/O
Two good news: file I/O is unit-testable, and it is surprisingly easy to do. Let's see how it works! A software no-one asked for First, we need a piece of software that deals with files and that has to be unit-tested. The