Skip to main content

Posts

Showing posts from 2015

Python: calculating the area of circle

You can calculate the area of circle with and without using any variable. Both of these approaches are discussed below. Without using variable:- print (22.0 / 7) * (float(raw_input("Enter radius of the circle: ")) ** 2) With variable:- radius = float(raw_input("Enter radius of the circle: ")) print (22.0 / 7) * (radius ** 2)

Java implementing stack with custom class

If you want to create a custom class to implement stack in java, you can do something like this:- import java.util.ArrayList; import java.util.List; interface StackList<T> {     public boolean push(T data);     public T pop();     public int size(); } public class Stack<T> implements StackList<T> {     private final int max;     private int top = -1;     private final List<T> stack;     public Stack(int size) {         max = size - 1;         stack = new ArrayList<>(size);     }     public boolean push(T data) {         if (top <= max) {             top += 1;             stack.add(top, data);   ...

Should javascript be enabled or disabled?

Javascript Javascript is the most popular client-end scripting language used by every website.  Javascript provides a way to create custom tools for the websites which are used to improve the overall site quality and UI (User Interface). It's also popular as it allow the audio and video streaming too. Google used the javascript to load the web page contents dynamically i.e., instead of redirecting the user to a new page or refreshing the same page, the contents are updated on the required parts on the web page. This dynamic content loading approach is known as AJAX which has now become much more popular as almost every website is using this, at least, to validate the form. Javascript vs No-Javascript Some people often disable the javascript from their browser settings. If you are one of them and think that javascript is a security threat, then, you are completely wrong. Javascript is a way to improve the website quality according to the client's machine and navigato...

PHP generating random salt using your own function

Generating salts is useful, when performing some kind of hashing on critical data such as user passwords, as it makes the hash decoding much more complicated. So, even if someone gets into the system and steals the essential data, it is almost impossible for him to obtain the original data because hash decoding requires a large amount of computer resources, time and some serious brutal attacks. You can copy the following PHP script to generate salts with your own function which means you can control the salt generation, The comments are just for documentation purpose, so, you can remove the comments for development purposes. /**      * This function generates random string containing the specified characters and length      * @param string $charset The characters to be used for generating the string      * @param boolean $randomLength TRUE for using random length between <strong>$min</strong> & <strong...

ffplay playing complete directory music files

To play all the music files within the directory using ffmpeg library, you can do the following in the terminal:- Change you current working directory to the directory which you want to play. Now type the following script in the terminal: $ for f in *.m*; do ffplay -autoexit "${f}"; done; Hit the return key and you are done. Enjoy the music now! The -autoexit option automatically terminates the music file once it has been played. Screenshot:-

C program bitwise operations

#include<stdio.h> #include<stdlib.h> int main(){     int a, b;     printf("Enter two numbers:\n");     scanf("%d%d", &a, &b);     printf("AND: %d\n", (a & b));     printf("OR: %d\n", (a | b));     printf("EXOR: %d\n", (a ^ b));     printf("%d's 1's complement: %d\n", a, (~a));     printf("%d's 1's complement: %d\n", b, (~b));     printf("%d's bits shifted towards left: %d\n", a, (a<<1));     printf("%d's bits shifted towards right: %d\n", a, (a>>1));     printf("%d's bits shifted towards left: %d\n", b, (b<<1));     printf("%d's bits shifted towards right: %d", b, (b>>1));     return (0); }

C program call by value and call by reference mechanism

#include<stdio.h> #include<stdlib.h> void value(int, int); void reference(int *, int *); int main(){     int a, b;     printf("Enter the value of a & b:-\n");     scanf("%d%d", &a, &b);     printf("Values before swapping:-\na: %d\nb: %d\n", a, b);     value(a, b);     printf("Values after call by value:-\na: %d\nb: %d\n", a, b);     reference(&a, &b);     printf("Values after call by reference:-\na: %d\nb: %d", a, b);     return (0); } void value(int a, int b){     int temp=a;     a=b;     b=temp;     printf("Values inside call by value:-\na: %d\nb: %d\n", a, b); } void reference(int *a, int *b){     int temp=*a;     *a=*b;     *b=temp;     printf("Values inside call by reference:-\na: %d\nb: %d\n", ...

JQuery disable copy, cut & paste opertions on password fields

Disabling the copy, cut & paste operations on password fields ensures that the user doesn't enter invalid password as the user need to type the password again in the configure field rather than copying it from the original password field. It also improves the security as nobody's password can be copied to the clipboard. But this only works when the client has javascript enabled or else this will fail. JQuery script:- $("input[type=password]").bind("cut copy paste", function (e) {     e.preventDefault(); });

Creating custom carousel using HTML, CSS & Vanilla Javascript

Create a custom carousel with fading effect using HTML, CSS & Vanilla Javascript. I have used the Font Awesome classes for control icons. There is some delay switching between the images when using controls. HTML:- <div class="carousel" data-interval-time="6500">                 <ul>                     <li><img src="img/image1.jpg" alt="CarouselImage_1"/></li>                     <li><img src="img/image2.jpg" alt="CarouselImage_2"/></li>                     <li><img src="img/image3.jpg" alt="CarouselImage_3"/></li>       ...

Vanilla Javascript each()

JQuery's each() is very useful when iterating through elements. But you don't want to use JQuery in your project you can simply add the following javascript code which works somewhat similar to the JQuery's each function. Here the fnc parameter is the function string which is converted to a valid function call replacing all the $(this) with this /**  * This function binds a particular function to every element with the specified selector. It is somewhat same as JQuery's each() with less functionality  * @param {String|DOMElement} selector  * @param {Function} fnc  */ function each(selector, fnc) {     var elem;     if (typeof selector === "string") {         elem = $_(selector);     } else {         elem = selector;     }     fnc = (fnc.toString().replace("$(this)", "elem") + "();").replace("function () {", "").replac...

Creating custom modal using HTML, CSS and Vanilla Javascript

Create custom modal using pure javascript and css. You can copy the javascript and css mentioned below. Comments are added for documentation purpose only. The modal can also be closed using the Esc key. HTML:- <a data-custom-modal="#modal">Modal</a> <div class="custom-modal" id="modal">         <div class="custom-modal-body">                 <div class="header">                     <h2 class="heading">Modal Heading<a class="close">&times;</a></h2>                 </div>                 <div class="body">          ...

Pure javascript to get the style of an element

function getStyle(dom) {         var style;         var returns = {};         /**          * This statement determines whether computed style is available for the DOM element          */         if (window.getComputedStyle) {             style = window.getComputedStyle(dom, null);             for (var i = 0; i < style.length; i++) {                 var prop = style[i];                 var val = style.getPropertyValue(prop);               ...

Java program to get the IP address of local host

import java.net.*; public class Address {     public static void main(String[] args) {         try {             InetAddress address = InetAddress.getLocalHost();             String dottedQuad = address.getHostAddress();             System.out.println(address);             System.out.println("My address is " + dottedQuad);         } catch (UnknownHostException e) {             System.out.println("Could not find the IP");         }     } }

C program to create linked list and perform insertion, searching and deletion

#include <stdio.h> #include <stdlib.h> struct node {     int data;     struct node* next; }; struct node* createNode(int data) {     struct node* n = (struct node*) malloc(sizeof (struct node));     n->data = data;     n->next = NULL;     return n; }; void insert(struct node** root, struct node* n) {     struct node* temp = *root;     if (temp == NULL) {         *root = n;     } else {         while (temp->next != NULL) {             temp = temp->next;         }         temp->next = n;     } } void display(struct node* root) {     struct node* temp = root;     printf("START->");  ...