Showing posts with label #programming. Show all posts
Showing posts with label #programming. Show all posts

Tuesday, August 29, 2017

[Solved] Python Quiz : 001

I came across a very simple Python Quiz question in a newsgroup, so thought about expanding on that.

Assuming, you are attending an interview and this question is asked. What would be your answer ?

[ Remember : you need to give this answer from memory and not by trying out this code :-) ]

>>> def foo(num) : print num; return num
...
>>>
>>> foo(5) * foo(1) * foo(2)
5
1
2
10
>>> foo(5) ** foo(1) ** foo(2)

And, as in any good interview, you are asked to explain your answer as well :-)

Post your answers in the comments below
    • There are two parts to this problem.
      1> Order of the execution of the functions
      2> Order of evaluation of the expression that involves **

      From the example mentioned in the earlier part of the question, its very evident that Python invokes these functions from left to right.

      So, that the expression to evaluate, after the functions return their values, would be
      >>> 5 ** 1 ** 2

      ** or exponentiation is the only operator in Python that is right-associative.

      The reason [ as far as I can understand ] for this behaviour, is that, it is the way it is handled in mathematics as well. It adds confusion only when the programming language, evaluates every other operator using left-association

      This link gives a very good explanation on why right-associativity is preferred : https://core.tcl.tk/tips/doc/trunk/tip/274.md

      Thus,
      >>> 5 ** 1 ** 2
      evaluates as
      >>> 5 ** (1 ** 2)
      >>> 5 ** ( 1 )
      >>> 5

      CONGRATULATIONS !!! to all those who got the result and the explanation right :-)

Monday, July 24, 2017

6 different ways to reverse a string in Python

A lot of interviewers seem to have a fascination for this often asked question :

"How to reverse a string in Python ?"

There are of course, many possible answers for this. Here are some that might come in handy for your understanding and learning.

1. Old School while loop method

>>> x = "hello"
>>> idx = len(x)
>>> rev = list()
>>> while idx > 0:
...         rev.append( x[idx-1] )
...         idx -= 1
...
>>> print ''.join(rev)
olleh

2. Using Python's reversed function with a for loop


Monday, July 17, 2017

The Curious Case of Python String Slicing

We were studying about Python strings and we tried to understand Python String Slicing through an example. We found a very strange [at least at first look] result when we tried to use String Slicing for creating a copy of the string.

To better understand the scenario and the result, lets start with the basics of integers and lists w.r.t to slicing.

>>> m = 10         # Integer m initialised to 10
>>> n = m           # Another integer n storing the same value as in m
>>> print m, n
10 10

>>> id(m), id(n)
( 140204825415168 , 140204825415168 ) # <-- Both are of the same id
A variable name in Python, is a "tag" name associated with a memory location.
In the above scenario, when we initialised variable m, Python associated a tag name m with memory location [ 140204825415168 ] ( For this discussion, assume that id represents memory location )

Monday, September 1, 2014

Linux Weekly NewsBytes : 1437

Here is a list of the latest, most happening events and news items from the Linux World. Keep reading to find out what's making the most noise around Linux this week.

Useful CLI Tools For Linux Sys Admins

A Complete Beginner's Guide to Linux
Choosing a Linux Desktop
Elementary OS's Pantheon Desktop Review
Krita Photo Editor
Network Admin's master tool : netstat
systemd 261 released

Monday, August 11, 2014

[Solved] Linux Kernel Challenge : 001

Fancy your Linux Kernel Programming skills ? Take this challenge and answer these simple 5 questions related to Linux Kernel Programming.

Update : Added Solutions

  1. What is the latest mainline version of Linux Kernel [ as of 11-Aug-2014 ] ?
      • 3.16 : Ref : https://www.kernel.org/finger_banner

  2. What is the file extension of a Linux Kernel module ?
      • .ko

  3. Which Linux Kernel function corresponds to the insmod user function ?
      • module_init()

  4. Which kernel functions would use in your kernel module to allocate and free memory ?
      • kmalloc() : to allocate memory in kernel address space
      • kfree() : to free memory in kernel address space

  5. What does COW stand for w.r.t Linux Kernel ?
      • Copy On Write :
      • Its an efficient method of copying, where a resource of a page is shared without making a copy of it, until an attempt to write is made. Only then is a duplicate copy made, and all subsequent writes happen on the copy.

Linux Weekly NewsBytes : 1434

Here is a list of the latest, most happening events and news items from the Linux World. Keep reading to find out what's making the most noise around Linux this week.

2014 Linux Training Scholarship Program

KNOPPIX 7.4.0 released
  • [ http://knopper.net/knoppix/knoppix740-en.html ]
  • The good old Linux on CD program that I first heard of, viz., KNOPPIX has released its latest version 7.4.0 that uses Linux Kernel 3.15.6 and comes built-in with tons of softwares that can run on LXDE, KDE or GNOME desktop environments.
Exciting features merged so far in Linux Kernel 3.17
  • http://www.phoronix.com/scan.php?page=news_item&px=MTc1OTM ]
  • Linux Kernel 3.17 release is still a few months away, but there are already a bunch of very exciting features already included in it. Some noteworthy features included are new features for Samsung Flash-Friendly File System, support for plenty of new ARM hardware, Intel Braswell audio support, and others
Oracle delivers Solaris 11.2
Docker comes to OpenSuSE

Better Security with Proper Management of Open Source

Monday, August 4, 2014

[Solved] Linux Programming Challenge : 001

Fancy your Linux Programming skills ? Take this challenge and answer these simple 5 questions related to Linux System and Linux Programming.

Update : Solutions added



  1. If getppid() returns the parent process id, what does getepid() return ?
      • Effective user id

  2. Which daemon is used to update log files on a Linux system ?
      • syslogd

  3. Which is the only directory [ filesystem ] on a Linux machine that does not exist on the hard disk ?
      • The proc filesystem that resides at /proc

  4. Will a hardlink have an inode number different from the inode of the file it links to ? Or is this the case with a soft link ?
      • Hardlink and the file it links to use the same inode number
      • Softlink and the file it links to use different inode numbers

  5. What is the default file permission of all newly created files when the umask is 0755 ?
      • 022 [ only group users and others can write to this file !!! ]

Monday, June 30, 2014

[Solved] Linux Commands Challenge : 007

Fancy your Linux Administration skills ? Take this challenge and answer these simple 5 questions related to everyday tasks of Linux Administration.

Winner : Congratulations to Sankar P for getting the highest number [ 2 ] of correct solutions.


  1. How do you restore GRUB bootloader to MBR of your primary hard disk ?
      • grub-install /dev/sda

  2. What does initrd contain ?
      • It contains the initial RAM disk, i.e., the initial root file system that is mounted prior to locating the real root file system

  3. How do you unpack the contents of an RPM without installing it ?
      • rpm2cpio | copi -id

  4. What is the maximum number of primary partitions you can have on a hard disk ?
      • 4

  5. How to boot into single user mode using the GRUB boot loader ?
      • After selecting the kernel you are interested to boot into, edit the kernel command line and add “S” to the end of the kernel command

Monday, June 16, 2014

[Solved] Linux Commands Challenge : 005

Fancy your Linux skills ? Take this challenge and answer these simple 5 questions on Linux commands.

Update : Added Solutions



  1. What does the command units do ?
      • It is a program to convert quantities expressed in one scale to another. Ex : meters to miles

  2. What happens if I apply the sticky bit to a directory ?
      • You make the files in that directory “sticky”. In the sense, users cannot delete the files in that directory. Only the root user can delete the files. Ex : /tmp directory

  3. How do you print the reverse of your username on Linux ?
      • whoami | rev

  4. How do you find out the most commonly used command by a given user ?
      • hash | sort -r | head

  5. What is the difference in the output of these two commands : echo * and ls -R ?
      • echo * : Lists all the files in the current directory and all its sub directories ls -R : Lists all the files and folders in the current directory and all its sub directories

Monday, June 9, 2014

[Solved] PERL Programming Challenge

Fancy your PERL programming skills ? Take this challenge and answer these simple 5 questions on PERL.
Update : Added solutions


  1. How do I eliminate the newline character from the input variable $age in PERL ?
      • chomp($age);

  2. Write a PERL one-liner to convert tab spaces in a file called tabs.txt to one space each
      • perl -pe 's/\t/ /g' tabs.txt;

  3. How do you empty the contents of an array called : myarray in PERL?
      • $#myarray = -1;

  4. How do I append a new line containing the text “Get water” before the 3rd line of a file : todo.txt using PERL ?
      • perl -pe 'print "Get water\n" if $. == 3’ todo.txt;

  5. How do you download the contents of a URL with PERL ?
      • By using LWP.pm [ Perl Module ]
      • use LWP::Simple;
      • $webdata = get ‘http://your-website.com';