Tuesday, June 20, 2017

C program to encode and decode string

Recently I get introduced to too many technology WhatsApp groups which talks about software development, basic electronics, robotics, AI and many more. It really helps me to know what is going in other areas instead of browsing for news. Also it is happy to be in places where people help each other.

Below is one question someone threw to a groups and I could do something after a week.

Write C program for self defined codec

1.Get a string of chars from user.
2. Convert string to array of chars (each char has value 0~255)
3.Define a lookup table (LUT) and an algorithm to encode the char values.
4.Use the LUT and algoithm from step 3 to encode the array of chars.
5.Output the encoded array of chars.
6.Decode the encoded array of chars.
7.Output the decoded array of integers to prove that your algorithm can decode.

I seriously programmed in C when I was in college and during my job seeking. After I landed in .Net world, there were less than 5 scenarios where I had to look or code in C/C++. Yes the big trouble was with pointers. Also comparing C#, I felt driving 80's model Ambassador car when programmed in C. Enough talks. Below is the code

#include < stdio.h >
char LUT[26] = "QRSTUVWXYZABCDEFGHIJKLMNOP";

int getLocationFromLUT(char c) {
  int i = 0;
  while (i < 26) {
    if (LUT[i] == c) return i;
    else i++;
  }
  return -1;
}

char * encode(char * str) {
  int i = 0;
  while (str[i] != '\0') {
    int locationInAlphabet = str[i] - 65;
    str[i++] = LUT[locationInAlphabet];
  }
  return (str);
}
char * decode(char * str) {
  int i = 0;
  while (str[i] != '\0') {
    int location = getLocationFromLUT(str[i]);
    str[i++] = location + 65;
  }
  return (str);
}
int main() {
  printf("Enter string to encode (caps & max len 64)\n");
  char word[64];
  scanf("%s", word);

  char * encoded = encode( & word[0]);
  printf("Encoded string %s\n", encoded);

  char * decoded = decode(encoded);
  printf("Decoded string %s\n", decoded);
  return 0;
}

So simple isn't it. Took me around 2 hrs to get the code completed.

It can be run in browser using tutorialspoint online environment. Technically it not in browser, browser just acts as console. I used this environment to write this code.
http://www.tutorialspoint.com/compile_c_online.php?PID=0Bw_CjBb95KQMU0tIWi1qcEJUUUk

Tuesday, June 13, 2017

Interception in Microsoft Unity framework for .Net

Introduction

This article is to demonstrate how we can combine dependency injection framework with logging og low level execution flow of application.

Prerequisites


  • Knowledge about dependency injection.
  • Knowledge about unity from Microsoft for dependency injection

Background

If we develop any application, logging is an essential thing to troubleshoot issues which comes in  later stages. Ideally there should be no bugs in production, if we test well. But it is not practical 99.99% of times. Sometimes even if there are no defects we may need to understand how the system is getting used. We call it as instrumentation, telemetry etc...Whatever we call, we have to make sure our application is capable of telling us what it did. Writing log lines inside our business logic has more efforts/costly, makes it longer and very difficult to read. 

Instrumentation technology considerations

Below are the factors to be considered when we select any logging and instrumentation technology for software.
  • Performance - Due to the logging operations our application should not be performing slow. Logging is for application not the other way.
  • Controlling behavior - We should be able to turn the logging on and off without restarting the application. Also change the level of logging without restarting.
  • Compatibility - If our system spans across different technologies better choose one which works everywhere.

Options

Traditional options include inserting log lines at the beginning and end of every methods and additional lines if the logic inside complicated. Another option is to inject the logging code at runtime. Both has their own pros and cons.

Few words on ideal composed application

If we look at SOLID principles we could see that all it tells us is to develop apps in a modular way where modules can be composed easily. In other words single responsible, closed components connected together to become an application. When we say modules, components in general they all translate to classes or functions in code.

How we compose the application by connecting the components is via dependency injection framework.

Combining DI framework and logging

Now most of the people might have got the click of idea. If the application follows SOLID principles religiously, why can't our DI framework log the method calls as it knows how application is composed. When we say how the application is composed it is same as the application flow which we need to log.

This is supported in Microsoft unity framework which provides DI capabilities for .Net. There are other DI frameworks available, one other called MEF which is native to .Net itself. This post is not to discuss what is the best DI framework but on the possibilities of combining DI with function level logging.

Steps in general

  • Have a interceptor mechanism which can tell us what is happening in the DI framework
  • Let the interceptor log the details into appropriate medium using best technology.
  • Register the interceptor into the DI framework.

Understanding sample

Coming directly to the sample which is available in the below location.
https://github.com/joymon/dotnet-demos/tree/master/patterns-practices/DI/unity/Interception

class LogCallHandler : ICallHandler

This is the interceptor we have written. It has Invoke() implemented which came via ICallHandler. Invoke() will be called by the unity DI framework when things happen there.

This is one mechanism unity supports. There are other ways to hook interceptors.

UnityConfiguration.xml

This is the configuration file which tells the unity framework about the components and how they composed. In simple words when someone request object of x interface give object of y class which implemented x. This file also has options to register interceptor.

This configuration can be specified via code too.

class Logger

This is a simple logger class which logs via .Net trace. .Net tracing is configured in the sample to write to file.Refer app.config file. We have to consider intercepting the method calls and writing the logs separate. If our log writing takes more time the performance of application may get affected. Choosing log writing mechanism is not in the scope of this article. In short, recommended mechanisms are ETW for on premise and Application insights for Azure environments.

References

https://msdn.microsoft.com/en-us/library/dn178466(v=pandp.30).aspx
http://mohtasebi.com/patterns/2013/10/24/using-unity-for-aspect-oriented-programming-and-interception-part-1.html

Tuesday, June 6, 2017

Puzzle - Sales tax

Below is famous problem which could be asked in coding interviews. I got this problem when I attended ThoughtWorks long back and could pass to the next round of pair programming.  The solution what I sent to them is uploaded to Github and URL is given at the end of this post. The mail came up when I was cleaning my mail box.

PROBLEM SALES TAXES 

Basic sales tax is applicable at a rate of 10% on all goods, except books, food, and medical products that are exempt. Import duty is an additional sales tax applicable on all imported goods at a rate of 5%, with no exemptions.

When I purchase items I receive a receipt which lists the name of all the items and their price (including tax), finishing with the total cost of the items, and the total amounts of sales taxes paid. The rounding rules for sales tax are that for a tax rate of n%, a shelf price of p contains (np/100 rounded up to the nearest 0.05) amount of sales tax.

Write an application that prints out the receipt details for these shopping baskets...
INPUT:

Input 1:
1 book at 12.49
1 music CD at 14.99
1 chocolate bar at 0.85

Input 2:
1 imported box of chocolates at 10.00
1 imported bottle of perfume at 47.50

Input 3:
1 imported bottle of perfume at 27.99
1 bottle of perfume at 18.99

1 packet of headache pills at 9.75
1 box of imported chocolates at 11.25

OUTPUT

Output 1:
1 book : 12.49
1 music CD: 16.49
1 chocolate bar: 0.85
Sales Taxes: 1.50
Total: 29.83

Output 2:
1 imported box of chocolates: 10.50
1 imported bottle of perfume: 54.65
Sales Taxes: 7.65
Total: 65.15

Output 3:
1 imported bottle of perfume: 32.19
1 bottle of perfume: 20.89
1 packet of headache pills: 9.75
1 imported box of chocolates: 11.85
Sales Taxes: 6.70
Total: 74.68

Solution

The major pattern here applicable is decorator to structure the calculation rules. Full code is present in GitHub repo.

Tuesday, May 30, 2017

Toolbox for open source developers

It is the era of SaaS and its byproduct freemium licensing model. Simply saying, so many tools and services are there for free to help software engineering. There are a lot of lists published which aggregates these free tools and services for software development. This is my version of the same which index all the software tools and services which I know, use and recommend.

There is a thought process going on about moving this to GitHub where it is easy to manage. No plans to merge with other public lists as one of the aim is to index what I used, not all those are out in the wild internet. Let google do it.

Design & Development

Project management

CheatSheets

https://dzone.com/refcardz
http://www.cheat-sheets.org/
https://www.cheatography.com/

Wireframe & diagramming

Coding

Visual Studio Code
Eclipse
Notepad++

Explore public APIs

https://www.programmableweb.com/
https://any-api.com/
https://apis.guru/browse-apis/
http://www.apiforthat.com/

Source control

Github
VisualStudio.com

Continuous integration

AppVeyor
TravisCI

Runtime

Hosting - Static site

GitHub pages

Hosting - Server side execution

Webtask.io
flaw.microsoft.com

Hosted Log management (Yet to finalize best one)

https://www.sumologic.com/pricing/
http://www.muscula.com/prices/
https://papertrailapp.com/plans
https://sentry.io/pricing/
https://www.loggly.com/plans-and-pricing/
https://rollbar.com/pricing/
https://www.bugsnag.com/pricing/
https://github.com/cheeaun/javascript-error-logging

eMail

SMS

In progress...

Sites to search for software and compare

https://www.getapp.com/
https://www.slant.co/

Make money

Algorithmia.com
https://www.chargebee.com/ - For SaaS billing.(Yet to try)

Other indexes like this

https://news.ycombinator.com/item?id=8874227
https://github.com/ripienaar/free-for-dev

The title says tools for open source developers. That's just a catchy title. Most of these tools and services are available to closed source and enterprise.

Enjoy the tools...

Tuesday, May 23, 2017

PowerShell - Find VPN adapter and change DNS addresses

If we have to join VPN, the DNS will mostly be set during the joining time. If we want to change the DNS servers after connecting to VPN, we have to manually do it every time. It involves so many clicks to change. 
Below is PowerShell script which finds a Cisco VPN adapter and change its DNS address

$index = Get-NetAdapter | where InterfaceDescription -like 'Cisco*' | Select ifIndex

Set-DnsClientServerAddress -InterfaceIndex $index.ifIndex -ServerAddresses "1.1.1.1,1.1.1.2"

This script has to run in admin mode to get it applied. Better save the above 2 lines as .ps1 file and execute in admin mode.

The first line can be further optimized to check whether VPN connected or not, exact name of adapter etc...

The PowerShell module seems already installed in Win 10 machines. The API may differ for other versions.

Tuesday, May 16, 2017

Protractor - CI with AppVeyor issue on webdriver-manager update

Protractor is a nice tool to do testing of Angular applications. It really shines when used in continuous delivery scenarios because of its end to end testing capabilities. We can simply host the site and run our tests with Protractor. But when we use it with hosted CI solutions there is a chance for getting the below exception in the build process.

(node:1184) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Failed to make Github request, rate limit reached.

This happens when we issue the below command which we cannot avoid to get it working.

webdriver-manager update

One such failure can be seen here. What is really happening is that during the update, the webdriver-manager is trying to download something from GitHub for Gecko driver. Since most of the hosted CI solutions use same single IP to access outside internet, the GitHub API limits comes into play.

This is already known issue and the only way is to disable Gecko driver for now. Command to disable Gecko driver is as follows.

webdriver-manager update --gecko false

Thanks to the AppVeyor support team.

Tuesday, May 9, 2017

Functional JavaScript - Find hidden string in the input

This is continuation of my functional programming experiments in JavaScript. This problem is from HackerRank.

The problem

Input is a string and we have to find out whether the letters of word 'hackerrank' are present inside it in the right order. When we say order, there might be some other letters in between the letters of 'hackerrank' the the order should be maintained. More details can be found in the actual problem link

Traditional solution

If the guy is not from the computer science background, there is 99% chance that the solution will be based on loops where a lot of states might have stored temporarily. But it looks like a familiar situation of email validation where we are expecting chaaracters in specific order.

The RegExp way

Regular expressions are great. There is no doubt on that. Here goes the regular expression way.

function checkForhackerrank(input) {
  if(testForhackerrank(input)) {
    return 'YES';
  }
  else return 'NO';
}
function testForhackerrank(input){
  return /h(.)*a(.)*c(.)*k(.)*e(.)*r(.)*r(.)*a(.)*n(.)*k/g.test(input)
}

Works great for the word hackerrank. Lets generalize

Generalized version

Below goes the generalized version which can work for any pattern other than 'hackerrank'.

function testForhackerrank(input){
  return getFindPattern('hackerrank').test(input);
}
function getFindPattern(stringToSearch) {
  return getGlobalRegExp(stringToSearch.split('').map(a=> `(.)*${a}`).join(''));
}
function getGlobalRegExp(pattern){
  return new RegExp(pattern,'g');
}

This doesn't need anything special regarding functional paradigm as the magic is done by regular expression. Plnkr is here.

Enjoy coding.