Fibonacci sequence

#include <stdio.h>

void fibonacci(int n);

int main()
  int n;
  printf("Number of elements in the Fibonacci sequence: ");
  scanf("%d", &n);
  return 0;

void fibonacci(int n)
  int a = 0;
  int b = 1;
  int i, sum;
  for (i = 0; i < n; i++)
    printf("%d\n", a);
    sum = a + b;
    a = b;
    b = sum;
Posted in Programming | Tagged | Leave a comment

Dice roller

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
  int n, i, x, d;

  printf("Number of tosses: ");
  scanf("%d", &n);

  for (i = 0; i < n; i++)
    x = rand();
    d = x % 6 + 1;
    printf("%d\n", d);

  return 0;
Posted in Programming | Tagged | Leave a comment

Remove rows with NA values from data frame

Sometimes you need to drop rows that contain specific number of NA values from your dataset. Below is a simple function to do that. It’s partially inspired by inpute.knn() function from impute package. maxmiss argument is maximal proportion of NA values allowed in any row.

DropRows <- function(x, maxmiss=0.15) {
  pn <- dim(x)
  n <- as.integer(pn[2])
  imiss <-
  irmiss <- drop(imiss %*% rep(1, n))
  imax <- trunc(maxmiss * n)
  imax <- irmiss > imax
  simax <- sum(imax)
  irmiss <- irmiss[!imax]
  imiss.omit <- imiss[imax, , drop=FALSE]
  imiss <- imiss[!imax, ]
  xomit <- x[imax, , drop=FALSE]
  x <- x[!imax, ]
  discards <- seq(imax)[imax]
  return(list(data.frame=x, discards=discards))
Posted in Statistics | Tagged | Leave a comment

Convert your file to Unicode UTF-8 with BOM

Pajek now supports Unicode UTF-8 files, but only if they start with a proper byte order mark (BOM) Unicode character. The simplest way for converting plain ASCII files to Unicode UTF-8 files with BOM is through a shell.

Let’s first create simple plain text file using the bash echo command.

echo "Lorem ipsum dolor sit amet." > test.txt

To determine the character set used in the created file we can use file command.

file test.txt

Now we should create second plain text file. The sequence ‘\xEF\xBB\xBF’ stands fot UTF-8 representation of the BOM.

echo -ne '\xEF\xBB\xBF' > test_utf8.txt

Finally we use iconv program for converting ASCII file to UTF-8 file. We use two output redirect symbols ‘>>’ to indicate that we want to append a file, not overwrite it.

iconv -f ASCII -t UTF-8 test.txt >> test_utf8.txt

That’ all. Happy converting.

Posted in Programming | Leave a comment

Anonymous comments in Microsoft Word for Mac 2011

I was asked to referee a manuscript for Horizons of Psychology. Here are some notes how to force Microsoft Word for Mac 2011 to hide author information in comment boxes.

  1. Go to Word > Preferences.
  2. Under Personal Settings click Security.
  3. In Privacy Options check Remove personal information from this file on save.
  4. Save your file and all comments should now appear anonymously when you reopen the document.

Actually, the procedure above only change the label that appears in comment boxes from your real name to “Author”. To show only pure comments, without any further information, you should also do the following.

  1. Go to Word > Preferences again.
  2. Under Output and Sharing click Track Changes.
  3. In Balloons uncheck Include reviewer, time stamp, and action buttons.

Happy reviewing!

Posted in Writing | Tagged , | Leave a comment

Align checkboxes or radio buttons with their labels in XHTML

Vertically align checkboxes or radio buttons with their labels is one of the CSS problems that makes me nervous constantly. I waste a lot of time on this issue during this Christmas season when I code forms in my recent Facebook applications. Looking around the Web, I found many interesting ideas how others do that. A good review of various solutions could be found on Stack Overflow.

Unfortunately, no one works for me. Code that works on Firefox, doesn’t work on Chrome (and vice versa). Code that aligns properly on MS Windows, could be totally misaligned on Mac OS X. I was so disappointed, that I almost did my coding in Flex Builder.

Inspired by this article, I finally found the proper solution. The answer lies in three values for the CSS display property, which are all self-descriptive: table, table-row, and table-cell. Applying these values to page elements causes the elements to behave like their HTML table equivalents.

The final code chunks (XHTML and CSS) are pasted below. A real world application which implements described approach could be found here (a login to Facebook is required).

  <legend>Example legend</legend>
  <div class="row">
    <div class="cell">
      <input id="rb1" type="radio" />
    <div class="cell">
      <label for="rb1">Label A</label>
  <div class="row">
    <div class="cell">
      <input id="rb2" type="radio" />
    <div class="cell">
      <label for="rb2">Label B</label>
.row {
  display: table-row;
.cell {
  display: table-cell;
  vertical-align: middle;
Posted in Programming | Tagged , | Leave a comment

Export Facebook network into Pajek format

More and more applications are now available on Facebook so I decided to spend some time to build my own. Of course, my primary objective was to study some core concepts behind Facebook platform (e.g., Graph API which provides a simple way to read and write data to Facebook).

I started with simple API queries and continue with more advanced programs. Finally, I decided to develop an application which allows someone to export his/her personal Facebook network into Pajek .net file. Google search found no previous solutions so I decided to build it from scratch.

Creating network file using simple Graph API calls could take very long time. Fortunately, there is Facebook Query Language (FQL). FQL is a SQL-style query language that allows querying Facebook data without using the API. It provides batching multiple queries into a single call. Below is a core FQL statement that query the friend table to determine whether all possible pairs of user’s friends are linked together as friends. Note that $uid is a PHP variable, which stores user’s Facebook ID.

SELECT uid1, uid2 FROM friend WHERE uid1 IN (SELECT uid2 FROM friend WHERE uid1 = $uid) AND uid2 IN (SELECT uid2 FROM friend WHERE uid1 = $uid)

Actually, all relations between friends are mutual so the is no need to count them twice. All what we need to do is to add a simple constraint into the query.

SELECT uid1, uid2 FROM friend WHERE uid1 IN (SELECT uid2 FROM friend WHERE uid1 = $uid) AND uid2 IN (SELECT uid2 FROM friend WHERE uid1 = $uid) AND uid1 < uid2

friends2pajek application was programmed in PHP with some jQuery support and it offers a very simple interface. User only needs to select type of vertex label and end-of-line character (LF or CRLF). Character encoding is set to UTF-8 by default. It’s very convenient that Unicode is now supported in Pajek (thanks Andrej and Vlado).

Posted in Programming | Tagged , , , , | Leave a comment