Wednesday, 29 January 2014

Digital conversion using Python

Python can be used to solve a number of mathematical functions. This post will cover some basic in-built Python functions for digital conversion. Let's see some examples.

To convert an integer into a binary string, use the function bin(x)

Eg:

>>> bin(10)
'0b1010'

To convert an integer into a floating number, use the function float(x)

Eg:

>>> float(15)
15.0

To convert an integer into a hexadecimal string, use the function hex(x)

Eg:

>>> hex(51)
'0x33'

To convert a floating point number or decimal number to an integer, use the function int(x)

Eg:

>>> int(8.94943)
8

To convert an integer into an octal string, use the function oct(x)

Eg:

>>> oct(65)
'0101'

Python on Mac OSX

All Apple Macbooks come pre-loaded with Python. On Mac OSX, the Python interpreter is located at /usr/bin/python

You can enter the python prompt (denoted by >>>) by just typing 'python' in the terminal.

Also, in order to quit from the python interpreter (>>>) type quit() or press CTRL-D

Note that Python is case sensitive, so if you type QUIT or Quit instead of quit(), you would get the following error :

>>> Quit
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Quit' is not defined

Youtube Hidden Game Trick

Check out this awesome hidden Youtube easter egg trick. Do the following:

Play any video on Youtube. While watching, click on right side of the video and immediately type 1980 on the keyboard. Viola!


Congrats. You just unlocked a hidden online game.

Tuesday, 28 January 2014

HBO's True Detective - First Impressions

A few months ago, somewhere during the Breaking Bad's finale, I was checking the series' IMDB page to view the whole list of casts and characters. It was then, when I figured the poster of True Detective in the recommendations list. I quickly checked the True Detective's page only to find out that it was an upcoming crime thriller series starring superstars Woody Harrelson & Matthew McConaughey. I was instantly hooked up and added it to my Watchlist.




Now that the show has started running, with 3 episodes already on air, I'd like make some crisp notes about it's first impressions. The show is about 2 detectives investigating the gruesome ritualistic murder of a young girl in the Louisiana countryside. And when Hollywood biggies Woody Harrelson & Matthew McConaughey come together, it definitely ups the curiosity and expectation levels.

The opening title song Far From Any Road by The Handsome Family is a soothing soulful song with relaxing TexMex texture. The song has the sounds you'd normally listen in a small town Mexican bar. Also the opening credits are accompanied by beautiful abstract art imagery, which really goes well with the mystery-themed drama. It's like the classic delicious steak-smooth wine pairing :)

Apparently, the story doesn't quite seem to be groundbreaking fresh. I mean, so far, you must have seen a dozen films centered around a serial killer. This also is a serial killer hunt drama. At first glance, it might feel like "PPffh, I've seen it all". So what exactly makes this series different ? Let's try to analyse.

Right from the first episode, Harrelson and McConaughey make their presence felt. They add tremendous weight to their characters. Both of them pair up as detectives Hart & Cohle, together trying to crack the case of a serial killer. But both of them portray contrasting personalities, quite evidently. The resultant blend gives a unique taste to the juice.





Harrelson stars as a father of two little girls, having Michelle Monaghan as his wife. The trouble with him is that he is adulterous and cheats on his wife. He sees another girl Lisa (played by Alexandra Daddario). She's like the forbidden fruit, which Woody is tempted to eat. Infact, the episode 2 has both of them in a steamy nude scene. This causes visible friction between Woody and Michelle. Michelle plays a loyal home-loving devoted wife.





McConaughey on the other hand, is a detective who currently lives alone. He portrays quite a serious edgy personality who doesn't believe in Christianity (a total contrast to his Tropic Thunder character). He's shown to have lost his kid and his marriage didn't last after that. Perhaps the forthcoming episodes might shed a light on his past personal life. He occasionally goes high on substance, whereby he hallucinates. At office, he rubs shoulders roughly with other Louisiana authorities. Harrelson tries to play a balancing mediator here. McConaughey is also shown to be an astute observant, which gets them crucial leads in the case. On certain occasions, the intensity that he glows is comparable to his character in The Lincoln Lawyer.





These rich contrasting colorful characters add to the depth of the script and storyline. The narration goes back and forth to 2012 and various times in past during their investigation. Perhaps indicating that the case is not solved yet. The slow steady one-clue to next-clue progress reminds you of the exciting cat-n-mouse thrill of No Country for Old Men. The episode 3 even shows a 2-second glimpse of the monster at the end. This has me keep my fingers crossed as both the detectives slowly follow each clue to the next one. I'd expect the episode 4 to garner much higher viewership due to increased awareness.

Season 1 will be having only 8 episodes. Wish there were more :) Oh boy, am really excited to see all the next new episodes.

Rating : 4.95 / 5.00

Vi editor - Keyboard shortcuts

If you're new to working on a Unix shell, you must know that you can edit files through the command line itself, using the vi mode. Let me quickly show you some basic most frequently used commands (keyboard shortcuts) about vi editing.

1. To create a new file or open an existing file to edit, type vi filename

2. To close an opened file :

type :wq (to save your changes and quit) and press Enter

type :wq! (to quit without saving, use !) and press Enter

type just :w (to save a file without quitting)

3. To add some text to an opened file press i (This will allow you to insert text before the cursor)

Press a to append text after the cursor.

4. To Replace some text exactly under the cursor, type r

5. To delete a character under the cursor, type

6. To delete the entire current line type dd

7. To copy the current line, type yy

8. To paste the copied line, type p

9. To search for a string, word or a pattern in a file, type :/string-to-be-searched (Note the forward slash /) This will search the string in a forward fashion (from top-to-bottom). However, if you want to start searching backwards (from bottom-to-top) type :?string-to-be-searched

10. When you search for a string using forward slash / or question mark ? you could move to the string's next occurrence by typing n

11. To display the total number of lines in the file, type :=

12. To display the current line number, type :.= (Note the dot .)

Monday, 27 January 2014

Random Key Generator using Perl

When you buy new software, there usually is a license key associated with your copy.  Ever wondered, how these keys get generated? Typically, advanced products use heavily encrypted key codes, while simple old-school vintage softwares used simple keys.

In this post, I'll show you a simple script to randomly create some keys using Perl.

Keys can be generated using different combinations. You could have plain vanilla keys containing only digits or alphanumeric keys or alphanumeric keys mixed with special characters or small letters mixed with capital letters. The choice is yours.

Ok, so in this program, we ask the user to enter the desired length of keys to be generated to make it interactive.

Alternatively, you could skip the user input altogether and hardcode a specific figure for key-length. Eg: $len_str = 16

use strict;
use warnings;

print "Enter the desired length of keys to be generated : \n";

my $len_str = <STDIN>;

my $random_string =&generate_random_string($len_str);

print "$random_string \n";

sub generate_random_string() {

$len_str = shift;

my @chars = ('a'..'z', 'A'..'Z','0'..'9','-','*','%','$'); 

my $random_string;

foreach (1..$len_str) {

$random_string .= $chars[rand @chars];

}

  return $random_string;

}  #end of subroutine

The subroutine generate_random_string contains the main logic of the program. Note that we have used the rand function which is responsible for generating random characters.

Note the line above in script, in which we have mentioned the condition - to include small letters a-z, capital letters A-Z, digits 0-9 and special characters -, *, %, $. Here you could add any other character you want to.

Thursday, 23 January 2014

Read command line arguments in Perl

In Perl, the command line arguments are stored in the array @ARGV.

So the first argument passed can be displayed by $ARGV[0]

The second argument passed can be displayed by $ARGV[1] and like wise.

Eg:

If you run a script and pass 3 command line arguments to it :

./eg_script.pl Ironclad Writer 123

The first argument $ARGV[0] = "Ironclad"

The second argument $ARGV[1] = "Writer"

The third argument $ARGV[2] = "123"
Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker