Author's Archive

I was recently doing Codility tests for practice. I, for once decided that it would be good to practice these things instead of just read them and absorb nothing. The one documented below is the BinaryGap. Essentially the task is find longest sequence of zeros in binary representation of an integer, ignoring those that are not bound on either side with a one.

To explain the code, and why I did what I did.
First thing I did was to convert the integer to a binary string, this was then trimmer as the requirement says you must ignore all trailing spaces “The number 32 has binary representation 100000 and has no binary gaps.” My test case here failed the first time because I didn’t read correctly.

string bits = Convert.ToString(N, 2).TrimEnd('0');

Next thing to do is to decide what we are looking at, and count the occurrence or ignore the occurrence. I initially tried to do this with a switch case, but the interesting thing that I found about the Codility compiler is that it wouldn’t accept

case "0" when count > 0

. So writing this in a switch statement was out of the question. It was that or perhaps just me.

Read Full Article

In C# Getting the Constant Value from Class using Reflection need not be a difficult and there are quite few articles and blogs written on the subject. I wanted to mimic the GetNameQuery function that is in Hibernate, maintaining backward compatibility while converting services to C# using Dapper. For Inspiration I looked at this blog. This returned a list of the constant Names and what I actually wanted to do was look through all the constant names and then match mine and return the Sql value inside.

Where to start: reflection

Firstly we need to look at the FieldInfo Class looking at the example it shows us how we can get the field values.

Read Full Article

Creating Dapper entities using MSSQL and Mapping classes is rather easy, but very time consuming from scratch. This came around because I really got tired of the typing a multitude of class and mapping files So let’s jump into it.

Firstly, lets go get the list of tables, this will return a list in the correct format for the T-SQL in clause. The reason why I did it this way was because it’s easier to remove those that I don’t want to generate entities for.

1
2
3
4
5
6
7
8
9
10
DECLARE @db VARCHAR(50) = 'Your_db_name'
DECLARE @tempTable TABLE (TableName VARCHAR(250))
 
SELECT DISTINCT '''' +  ltrim(rtrim(t.table_name))+ ''',' AS TABLE_NAME
FROM     information_schema.columns c
         INNER JOIN information_schema.tables t
           ON c.table_name = t.table_name
              AND c.table_schema = t.table_schema
              AND t.table_type = 'BASE TABLE'
WHERE t.TABLE_CATALOG = @db

Read Full Article

This is just a short post in using Dapper ORM to Map column names in Mssql generating c# csharp code.

I had a requirement using Dapper ORM to Easily Map Column Names, problem was that the columns names had Underscores in them and the need to Have uppercase letter at the beginning of each word to conform to naming conventions. The service we are re-writing has numerous mapping tables so it all started out fine doing the tables with only a few columns, but as things cot more intense at 32 plus columns then I needed a better way to do it. MSSQL, to the rescue, I found a method on the web to convert the initial capitalisation for the beginning of each word for the I used this method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
CREATE FUNCTION [dbo].[InitCap] ( @InputString VARCHAR(4000) ) 
RETURNS VARCHAR(4000)
AS
BEGIN
 
DECLARE @INDEX          INT
DECLARE @CHAR           CHAR(1)
DECLARE @PrevChar       CHAR(1)
DECLARE @OutputString   VARCHAR(255)
 
SET @OutputString = LOWER(@InputString)
SET @INDEX = 1
 
WHILE @INDEX < = LEN(@InputString)
BEGIN
    SET @CHAR     = SUBSTRING(@InputString, @INDEX, 1)
    SET @PrevChar = CASE WHEN @INDEX = 1 THEN ' '
                         ELSE SUBSTRING(@InputString, @INDEX - 1, 1)
                    END
 
    IF @PrevChar IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(')
    BEGIN
        IF @PrevChar != '''' OR UPPER(@CHAR) != 'S'
            SET @OutputString = STUFF(@OutputString, @INDEX, 1, UPPER(@CHAR))
    END
 
    SET @INDEX = @INDEX + 1
END
 
RETURN @OutputString
 
END
GO

Read Full Article

Hackerrank – Weather Observation Station 4
I have always loved just killing some time on Hackerrank, doing some challenges. And today was no different, but what was funny about it was I got stumped by one of the stupidest and simplest things under the sun. I really believe in software we should always look for the simplest answer, but in my case I couldn’t for the love of me come up with the simplest answer which is:

1
2
SELECT COUNT(CITY) - COUNT (DISTINCT City)
FROM STATION ;

However what came out was a rather convoluted attempt: why do we try do something complex when the answer is actually so simple at the end of the day:

1
2
3
4
5
6
7
8
9
SELECT	
	a.cities - a.uniqueCities
FROM (
    SELECT
	COUNT(city) AS cities, 
	COUNT(DISTINCT city) AS uniqueCities 
    FROM
	station
) a;

looking at some of the answers the where submitted afterwards there where a few interesting ones:

SELECT abs(COUNT(CITY)-COUNT(DISTINCT CITY)) FROM STATION;
 
SELECT ((SELECT COUNT(CITY) FROM STATION) - (SELECT COUNT(DISTINCT CITY) FROM STATION)) FROM STATION WHERE ROWNUM=1;

At the end of the Day I suppose we all have days where we take something so simple, but then over complicate it.

I guess we have learnt something just by trying

ApkTool

Yesterday, we had a little issue whereby we needed to modify a production APK to work with Integration. So I want describe how to Decompile, Alter and Compile an apk using the Apktool. I have done some decompiling of Android APK’s in the past to learn how people and companies have achieved something. Never did I have to decompile, alter and re-compile an app using the Apktool. I volunteered naturally as this option seemed loads simpler than looking for the original branch source code at that point.

You should have a good compression tool on you machine I prefer Winrar, but you can use anything you wish.

A text Editor, I use Notepad++

At this point I will assume you already have the current JAVA JRE and SDK installed. You will need this for signing the APK. You should also have the Android SDK, for editing & Compiling etc… You will more then likely already have these if you are at this point.

You will need the ApkTool,the latest version can be downloaded here. The tool allows you to reverse engineer 3rd party, closed and binary Android applications. The tool does this by decompiling / compiling the code using Smali / baksmali. This tool required three files:

  1. apktool.bat
  2. apktool.jar
  3. aapt.exe

Once you have all these tools (not many I know this isn’t that difficult at all), the you are ready to get cracking… I mean reverse engineering.

Read Full Article

In this article I hope to show how to use a Jquery UI dialog prevent / cancel form submission. Based on a work requirement I wanted to stop the submission of a form, so I could validate the some checkboxes using JQuery, if they were all checked the okay button would be enabled and the form would be submitted. This Solution, always for an infinite amount of check boxes to be iterated through and only if all of them are checked will you be allowed to proceed, then it will submit the form as per original. You can check the entire Fiddle here

jquery ui dialog

<form id="objectdata" name="objectdata" action="newOrder.jsp" method="post">
    <div id="validateServicesContainer" title="Service Cancellation"  style="display: none">
        <tr>
            <td>
                The following Services will be suspended during upgrade.<br /> 
                Check service to confirm?
            </td>
        </tr><br /><br />
    <input class="sServices" id="chkBox1" type="checkbox"/>Service 1<br /> 
    <input class="sServices" id="chkBox2" type="checkbox"/>Service 2<br /> 
    <input class="sServices" id="chkBox3" type="checkbox"/>Service 3<br /> 
    <input class="sServices" id="chkBox4" type="checkbox"/>Service 4<br /> 
    <input class="sServices" id="chkBox5" type="checkbox"/>Service 5       
     </div>
     <input id="chkBox6" type="checkbox"/>Service 6<br /><br />
     <input type="submit" id="btnOpenDialog" value="Validate then submit" onclick="checkUpgradeSuspensions.call(this,event)"/>
</form>

Read Full Article

This last year has been an interesting year, Let’s hope the new year is a better year. There has been a huge amount of hack attempts against this site, it really makes you wonder what the intentions are.

I lost almost an Entire year’s worth of articles, partially due to me not making making my own back-up’s but al on over reliance on the host. They migrated servers and my sites bar this one came up. I should have disabled my plugins first but I didn’t, they back-up’s with host wasn’t complete, and they overwrote all my changes, and tha lost all the new articles I had made very sad.

Oh well, all this stuff happens we learn from it and we build more knowledge. So Happy New Year welcome 2016

HAPPY NEW YEAR

Happy new year