[Code snippets] A short prime number checker in CoffeeScript

It has been so, so long now! I have not really written much in a while, mainly because I have not had anything to write about either. I feel it is about time I put something new up! And what better, than something about CoffeeScript? I love coffee. Like, a lot. Thus, writing in CoffeeScript should come just natural. Really, it is because I might be working with it early next year, but hey. Coffee!

Continue reading “[Code snippets] A short prime number checker in CoffeeScript”

[Code snippets] A short prime number checker in CoffeeScript

[Academic / How to] How to write research papers

This post will be a brief explanation of how I go about writing my papers for university, specifically the second research paper we had a little while ago, which can be found here. I write this on request from Lucia, although I do not think this was what she expected. Sadly I will not provide any references in this post, as this is just a blog post written in my spare time; I do base most of the following on what I was taught in previous English and Swedish classes.

Continue reading “[Academic / How to] How to write research papers”

[Academic / How to] How to write research papers

[Problem solving] A note on the power of observation

I have not posted anything public lately as I have been busy contemplating my choices in life, eating massive amounts of chocolate and been overall busy with different projects. The purpose of this post is to give some insight on how information somewhat hidden or hard to find actually can be found.

Obviously, this is not an answer to every question on finding hidden information, but it may give some pointers.


Currently, I am in the starting stages of developing an application for my university – in this very moment I am compiling my thoughts on what I believe we need to get started, from whom we may get it and what to do with it. Kind of. And to properly write such a piece, it is my belief some sort of research of some areas is important. Also, I did not actually finish the write-up as it by the time of writing is around 01:15 AM, and I should have gone to bed earlier. This post will be published later today (24/11/2014). Continue reading “[Problem solving] A note on the power of observation”

[Problem solving] A note on the power of observation

[Academic] FOSDIC and my thoughts and opinions on it

The purpose of this post is to bring attention to the device that was, or actually still is – FOSDIC.


Am I weird for sending an email to Herb Johnson saying thank you for his write-up on FOSDIC? The reason I did this is because there is in essence nothing on the subject. As I have said previously, it is not even mentioned on Wikipedia.

I get it, that joke is old by now.

Anyway, I thought I would shed some light on this thing, since pretty much no one seems to know about FOSDIC.


So, what is this… FOSDIC?

FOSDIC is the abbreviation of Film-Optical Sensing Device for Input to Computer, which essentially is an old scanner type thingy for automated input of census data, among other things. It was developed in co-operation by the U.S. Census Bureau (then U.S. Bureau of the Census) and the National Bureau of Standards (wherein “National” means American) in the early 1950’s and was first used in for the American 1960 census.

The first FOSDIC used by the Census Bureau was not completely automated – staff still had to manually make photocopies on negative microfilm for the device to read. Later versions automated this process as well, speeding up processing times significantly, which also was the main reason for developing FOSDIC. The first generation saved around $6 million on input costs and reduced the time it took to around an eighth or a seventh compared to the census ten years before (that being the 1950 census).

If you want to read my full report on FOSDIC, you can do so here. I linked the revisited version because I wanted to.


In a foreseeable future I will also write a post on the art of googling, as there seems to be a major lack of how to use that search engine properly. But for now I already have two posts planned for the next couple of days.


Thank you for reading.

[Academic] FOSDIC and my thoughts and opinions on it

[Code snippets] How to “refresh” a value in a C# Console Application

In this post I will be discussing how to refresh a value in C# Console applications.


While still in bed this morning,  I was skimming /r/csharp on Reddit and because it is so easy to just keep scrolling while on the phone, I happened to find a quite old subject. By old I mean six days with the latest reply being two days ago. That is old on the internet.

Anyway, the question was in essence if it is possible to refresh a value in a C# Console application. This got me intrigued, as I thus far have not worked too much with Console applications in this language, and did not know if it was possible. I read the answers, and because of the nature of /r/csharp, the preferred way to answer is not to spoon feed, but to give hints or link to places were one could get a push to figure out the answer oneself.

However, the original poster did not seem to get how this was to be done, so I kind of wanted to see if I could do it, because I like a challenge. Though it really was not a challenge, it was quite easy actually, it was kind of enjoyable, so I decided to play around a little with it.


As a programmer, generally the first thing you want to is to query ones favourite search engine, mine being Google (which I soon will write a blog post on how to properly use Google). The rule is, shorten your question into key terms and search away. So, I figured these were “refresh value console c#“, and sure enough, the first link was a StackedOverflow question that answered the question.

Since I did this after reading the answers on Reddit, I thought I would try to refresh a value in a Console application using the links provided by /u/TrikkyMakk and /u/wydok, both of which gave excellent answers. I will start of with the latter, as that was the shorter and slightly easier one, but also a little more restrictive.


/u/wydok’s answer was to use Console.Write() instead of Console.WriteLine(), because it does not automatically create a new line, but just keeps writing on the same line until it is told otherwise.

Because of how a C# Console application works, if it starts writing where other characters are, it simply writes over it – kind of like writing while having Insert on in for example Word. So, by starting the string inside the brackets with \r, the expression for Carriage Return, whatever is written in the Console will be overwritten by what is inside the string after \r. Here is an example using a Timer to automatically update the value.


// Declare an object called timer and the integer.
private static Timer timer;
private static int i = 0;
static void Main(string[] args)
{
// Set the timer to be a new Timer with an interval of 2 seconds.
timer = new Timer(2000);
// Everytime that interval has been reached, fire off this event.
timer.Elapsed += OnTimedEvent;
// The timer is now enabled and will start printing stuff.
timer.Enabled = true;
// Print the "first" value
Console.Write("{0}", i);
// This one waits for user input, as long as none is given, the progam will go on forever.
// (I.E. a key is not pressed)
Console.ReadKey();
// Will just flash by as the program will be closing.
Console.WriteLine("Terminating the application…");
}
private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
// Increment the integer
i++;
// \r makes the console go back all the way on the row,
// and {0} prints out whatever the first value is on the other side of the comma (which in this case is the integer i).
Console.Write("\r{0}", i);
}

Note: If the new string is shorter than the old one, the new will only write over as much as it can, and leave the rest. For example, if the original string is “abcdefg” and the new is “\rABC” the final string will be “ABCdefg”.


 

While the above is cool and all, it might not be specifically what the original poster needs, so I also tried the answer that /u/TrikkyMakk gave, with great success. Since the answer was a little on the short side – being nothing but a link – I opened the link and followed the steps provided by Microsoft in said link, which included a very cool method using Console.SetCursorPosition(), which allows the programmer to specify where the Console should write next (as long as it is on the screen).

After doing that, I played around a little myself and decided to go for a Timer again, some Randoms and a list of all the letters in the alphabet. Because of how the Random object works, it is crucial only one Random object is used for all the random operations, otherwise, if two were to be used, both are highly likely to produce the same number, which is not really what is wanted.

What I got is a weird Console application that write random numbers in a 6 x 6 square.


// Declaring the integers used for positioning.
protected static int origingalRow;
protected static int originalColoumn;
protected static void WriteAt(string s, int x, int y)
{
// If an exception (error) is thrown insde the catch,
// the program will, in fact, not crash and whatever code inside catch will be run.
try
{
// Sets the position to write on and then writes there.
Console.SetCursorPosition(originalColoumn + x, origingalRow + y);
Console.Write(s);
}
catch (ArgumentOutOfRangeException e)
{
// Clear the Console and write the exception message.
Console.Clear();
Console.WriteLine(e.Message);
}
}
// For the fun of it, let's create a Timer.
protected static Timer timer;
static void Main(string[] args)
{
// Clear screen, then save the top and left coordinates,
// which should be top of the screen at the moment
Console.Clear();
originalColoumn = Console.CursorLeft;
origingalRow = Console.CursorTop;
// Instantiate timer with an interval of 0.2 seconds.
timer = new Timer(200);
// Everytime that interval's been reached, fire off this event.
timer.Elapsed += advancedTimer_Elapsed;
// Enable the timer to start printing stuff.
timer.Enabled = true;
// This one waits for user input, as long as none is given, the progam will go on forever.
// (I.E. a key is not pressed)
Console.ReadKey();
// Will just flash by as the program will be closing.
Console.WriteLine("Terminating the application…");
}
// Create a list of all the letters in the English alphabet
protected static List<string> englishAlphabet = new List<string>()
{
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k","l","m", "n", "o","p","q","r","s","t","u", "v", "x", "y", "z"
};
private static void advancedTimer_Elapsed(object sender, ElapsedEventArgs e)
{
// Instantiate a Random object. Only one, though!
Random random = new Random();
// A letter somewhere from the English alphabet.
string letter = englishAlphabet[random.Next(0, englishAlphabet.Count)];
// Get the coordinates
int x = random.Next(0, 5);
int y = random.Next(0, 5);
// Print the letter at x and y.
WriteAt(letter, x, y);
}


In conclusion, it is possible to refresh a value in a C# Console application. If a whole row is to be refreshed, and that is the last and only row to be altered, one can use Console.Write(“\rWrites over whatever else was here.”). If a more complex overwrite or refresh has to be done, one can use the Console.SetCursor()-method to specify where to write.

Happy coding!

 

[Code snippets] How to “refresh” a value in a C# Console Application

[How to] OMG I LOST MY CARD

No, I did not. But what if it happened?

In the incident of you ever forgetting your card at home, and there is no chance in actually going to get it before class starts, you should do the following:

Go to Borough Road Building and ask for a temporary card at the reception. Worth noting here is, you can only get three one-day passes, so do not make a habit of forgetting it!

If you by chance would happen to actually lose your card, it gets even worse! Mostly. This is what to do:

Go to Bourough Road Building and tell the staff you have lost your card. In my head the staff then laughs at you, but I believe they act far more professional than that, and tells you what to do. The place to go from there is room LR102, which is the security office, where you once again should say you have lost your card (this should be done immediately!). To issue a new card you will be charged £10.

In the highly unlikely event of someone not getting why this card is of importance, I will now tell you why.

IT IS VERY HARD TO GET ANYWHERE WITHIN THE FACILITIES WITHOUT A STUDENT ID CARD! 

With that said, it is used to access most of the rooms and other points of interest by the university, to open the gates. Of course you can simply jump over the gates, but that is why we have guards at them as well.

If you still would manage to get to a session, you also need your card to log your attendance, and use the library and probably some other cool stuff I have missed.

With that said, keep your Student ID Card with you on all times, it will significantly benefit you in your studies.

Best of luck!

Here is my main source of information for this post (not really, but it does confirm what is written).

[How to] OMG I LOST MY CARD