Tuesday, July 14, 2015

How to check or verify if atleast one of the form radio buttons were selected?

This page keeps collecting feedback from a user for 5 times and then responds with a thank you message. This example using jQuery + forms is mainly written to expand it to a series of questions to be posed to a user and collect feedback about them from the user in a compact way.


Include the jQuery reference:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script  type='text/javascript'>
        var count=1;

And then write the repeat function that does the trick to slide over a set of questions. Here only one question is repeating but you can extend it to other purposes.


        function submitform()
        {
                //alert(count);
                //alert("Inside submitform");
                if(count==5)
                {
                         $('#formid').html("Thank you!");
                }
                else
                {
                        //alert("Inside the submitform");
                        count=count+1;
                        $('#formid').html("<form action=\"submitform()\" method=\"post\" id=\"reportform\"> <label><input type=\"radio\" name=\"report\" value=\"customer\" /> Customers</label><br /><label><input type=\"radio\" name=\"report\" value=\"item\" /> Items Sold</label><br /><label><input type=\"radio\" name=\"report\" value=\"department\" /> Sales Departments</label><br /><label><input type=\"radio\" name=\"report\" value=\"person\" /> Sales People</label><br /><input type=\"button\" name=\"reportsubmit\" value=\"Submit\" onClick=\"verifyText(this.form)\" /> </form>");
                }
        }
</script>

</head>
<body>

Now the body section starts.. You can show the user whatever you want. Here I am asking the same question. Once you click the submit it verified is atleast one of the radio buttons is selected. If selected, then repeat if not, stay on the same page.

<div id="formid">
<form action="submitform()" method="post" id="reportform">
    <label><input type="radio" name="report" value="customer" /> Customers</label><br />
    <label><input type="radio" name="report" value="item" /> Items Sold</label><br />
    <label><input type="radio" name="report" value="department" /> Sales Departments</label><br />
    <label><input type="radio" name="report" value="person" /> Sales People</label><br />
    <input type="button" name="reportsubmit" value="Submit" onClick="verifyText(this.form)" />
</form>
</div>

This is the verify section:

<script>
function verifyText (form) {
        var checked = form.querySelector('input:checked');
        var value = checked ? checked.value : null;
        alert(value);
        if(value!=null)
                submitform();

}


</script>


Thursday, July 2, 2015

[Latex][MikTex][windows] Fixing the error that mathtools.sty package is not found

These are the steps I took on windows OS to fix the package error in MikTex.

Go to the console and type "cmd"

Now go to the directory where MikTex was installed on your machine.

>> cd C:\Program Files (x86)\MiKTeX 2.9

>> cd miktex\bin

And then with the administrator settings open the package console using this command:

>> mpm_mfc_admin

This prompts a gui where you can see different packages available. Simply search for the "mathtools" package and install it by clicking + symbol on the top-left side of this GUI. Hope this helps!


Tuesday, May 19, 2015

[Latex] Installing packages like mathtools, enumitem, etc on Windows 8 using MikTek 2.9

You can directly download a .sty file and refer it in your .tex file but not always this works as sometimes a single package may require other sub-packages to be installed.

Inorder to install a new package using MikTex on Windows 8, it is very simple. Here are the steps:

1. Search for cmd in the applications search option
2. And right click this option and choose 'Run as administrator'
3. Then go to the folder where MikTex 2.9 is installed. For me it is: 'C:\Program Files (x86)\MikTex 2.9'
4. Further go to the subdirectory: 'C:\Program Files (x86)\MiKTeX 2.9\miktex\bin'
5. Once you are in this folder, type the command mpm_mfc_admin on command prompt which pops out a MikTex Package Manager console showing the different packages available.
6. From here choose whatever package you want to install by selecting that and pressing the '+' option on the top.

Thats all folks! 

How to dump a .sql file into mysql database

There are different ways you can dump a .sql file into a mysql database. But here is a quick way to do this.

Assume that you want to load the file "datafile.sql" into database named "alldata" Simply use these commands:

>> mysql

>> use alldata;

>> source datafile.sql;

>> show tables; //Displays all the tables dumped into this database. 

Wednesday, April 8, 2015

Convert a byte array to Hexadecimal string in Java

Use the DatatypeConverter liibrary that can be imported as:

>> import javax.xml.bind.DatatypeConverter;

And then lets say certificate.getSignature() is returning a byte array, use these commands:

>> byte[] sign = certificate.getSignature();
>> String hexasign = DatatypeConverter.printHexBinary(sign);
>> System.out.println("Signature on the Certificate: "+hexasign+"\n");

Thursday, May 15, 2014

How to install Amazon awscli on your ubuntu machine

It is very easy to interact with Amazon S3 or ec2 (in general, Amazon aws services) from your command line interface. Amazon provided this useful setup which makes our things more easy now!!


All you have to do is to run a set of commands and you are all set to make connections to your S3 account. Note that these instructions are only for Ubuntu machine which are almost similar to the other linux versions.

1. You first have to install python (version >= 2.6.5).

2.  Download this file get-pip.py (If its not downloading you can visit this page: https://bootstrap.pypa.io/get-pip.py)

3. After downloading it, execute this command that installs all the setup tools:

>> sudo python get-pip.py

4.  After installing the setup tools, you have to install pip which is a python package installer. Execute these commands:

>> sudo apt-get install python-pip

5. Now install the awscli using pip by executing this command:

>> pip install awscli

6. Now all the installations are done. Verify if this got installed properly using this command:

>> aws help

7. Now we have to configure this system by providing security credentials. Execute:

>> aws configure

8. This asks you to enter your access key, secret key, region, etc. Provide all the necessary information and you are all set!

Hope you will find this useful. For more information, you can check this website: https://aws.amazon.com/cli/


Monday, December 30, 2013

How to compute the most frequently occuring element in a list in python

In python there is a module named "collections" which has some specialized functions that can be used as an alternative to dict, list, etc.

There is a tool called Counter which performs operations like tallying the number of occurrences of objects in a list, etc. Here is how you can make use of this to compute the mode or most frequently occurring element in a list.

%First import the Counter tool from the collections module
>>> from collections import Counter

%Built a counter for a list. This list can be of numbers or strings. For example, if I consider a list of numbers
>>> info = Counter([1, 2, 1, 1, 1, 2])

%To know the frequencies of each element, you can call the most_common function with no arguments. This will return a list of elements attached with their frequencies in descending order.
>>>  info.most_common()
[(1, 4), (2, 2)]

%If you want to know what is the top element with highest frequency, you can send in the argument '1'
>>> info.most_common(1)
[(1, 4)]             %Here 1 is the element and 4 is the frequency of '1'

You can access these elements like this:
>>> info.most_common(1)[0][0]
1
>>> info.most_common(1)[0][1]
4

%If you want to know the top 2 elements with their frequency, you can send argument '2'
>>> info.most_common(2)
[(1, 4), (2, 2)]

To compute the size of the unique elements, you can say:
>>> len(info)
2

Similar operations on a list of strings:

>>> info = Counter(["a","b","c","a","b","c","a","a"])

>>> info.most_common()
[('a', 4), ('c', 2), ('b', 2)]
>>> info.most_common(1)
[('a', 4)]
>>> info.most_common(2)
[('a', 4), ('c', 2)]
>>> info.most_common(3)
[('a', 4), ('c', 2), ('b', 2)]

%The interesting thing is, even if you pass the argument which is greater than the length of the counter, it won't give any segmentation fault. So, it is suggested to put a check on the length of the counter.

>>> info.most_common(4)
[('a', 4), ('c', 2), ('b', 2)]

>>> len(info)
3

Hope this is useful.