Webpack-3.3
Webpack installation:
The webpack installation similar like other npm modules.
Above command will install the webpack locally.
If we want we can install webpack globally with above command.
Installing locally is recommended for most projects. This makes it easier to upgrade projects individually when breaking changes are introduced. Typically webpack is run via one or more npm scripts which will look for a webpack installation in your local node_modules directory.
Angular-webpack-seed setup:
Loading CSS
In order to import a CSS file from within a JavaScript module, you need to install and add the style-loader and css-loader to your module configuration:
The webpack installation similar like other npm modules.
npm install --save webpack
Above command will install the webpack locally.
If we want we can install webpack globally with above command.
npm install -g --save webpack
Installing locally is recommended for most projects. This makes it easier to upgrade projects individually when breaking changes are introduced. Typically webpack is run via one or more npm scripts which will look for a webpack installation in your local node_modules directory.
Angular-webpack-seed setup:
mkdir angular-webpack-seed
cd angular-webpack-seed
npm init -y // To generate the package.json
npm install --save webpack // This will add webpack as a dev dependencies.
Loading CSS
In order to import a CSS file from within a JavaScript module, you need to install and add the style-loader and css-loader to your module configuration:
npm install --save style-loader css-loader
@angular/cli installation.
Today, I had to spent lot of time on the "@angular/cli" installation.
My improper steps ate lot of time.
To install the "@angular/cli" in Ubuntu 16.04, I must had to follow the below steps.
Note: Before following below steps we should install nodeJS.
Step-1 Create a directory.
Step-2 Install the angular cli globally
Now, I'm going to create project with ng command but when I type the below command, it said ng not found.
My improper steps ate lot of time.
To install the "@angular/cli" in Ubuntu 16.04, I must had to follow the below steps.
Note: Before following below steps we should install nodeJS.
Step-1 Create a directory.
mkdir angular2-demo
Step-2 Install the angular cli globally
npm install -g @angular/cli
Now, I'm going to create project with ng command but when I type the below command, it said ng not found.
Load css files with webpack 3.3
Here we can see how to load the external css files with webpack.
Webpack is automatically creates the link tag and add to our html file.
So, we can easily load the module specific css.
To play with the css files in webpack we need to install npm module named "style-loader", "css-loader" .
Install css loaders for webpack.
npm install --save style-loader css-loader
Once we are done with the installation we can import the css file by using the below statement in js file.
import '../css/styles.css';
From above code, style tag will create and all css styles are added into DOM as inline styles.
But, this is not what we want because If we take the large scale project thousands of lines css will be there.
We can't add all of them into the DOM.
[Ubuntu] npm permissions
Are you using any Debian based linux machine ?
- If you say Yes, you might face the permission issue with NPM.
Issue is every time I run npm command it's getting EACCESS error.
It means I have to run the command as a super user mode, but I don't like that.
I have been facing this issue from couple of days.
Finally, I decided to fix this issue.
Let's see how did I fix the issue.
There are couple of ways to fix this issue:
- If you say Yes, you might face the permission issue with NPM.
Issue is every time I run npm command it's getting EACCESS error.
It means I have to run the command as a super user mode, but I don't like that.
sudo npm install [module name]
I have been facing this issue from couple of days.
Finally, I decided to fix this issue.
Let's see how did I fix the issue.
There are couple of ways to fix this issue:
- Change the permission to npm's default directory.
- Change npm's default directory to another directory.
- Install node with a package manager that takes care of this for you.
[Problem] Dinner with GF
My girl friend is interested to buy some cities. Inorder to buy two cities, she needs to buy the road connecting those two cities. She is concentrating on only the roads not cities as she has lot of other work.
My bad luck, same time I invited her for dinner at Greenpark.
She said invitation will be accepted when I give number of cities count which she bought.
Now, I was given a list of roads, bought by my GF. I need to tell her how many cities did she buy.
Input:
First line contains an integer T, denoting the number of test cases. The first line of each test case contains an integer E, denoting the number of roads. The next E lines contain two space separated integers X and Y, denoting that there is an road between city X and city Y.
Output:
For each test case, I need to print the number of cities she bought.
Constraint:
1 <= T <= 100
1 <= E <= 1000
1 <= X, Y <= 10000
Let me solve this with Graph representation.
Key points: Each city will be vertex of the graph.
Road will be the edge between the vertex.
My bad luck, same time I invited her for dinner at Greenpark.
She said invitation will be accepted when I give number of cities count which she bought.
Now, I was given a list of roads, bought by my GF. I need to tell her how many cities did she buy.
Input:
First line contains an integer T, denoting the number of test cases. The first line of each test case contains an integer E, denoting the number of roads. The next E lines contain two space separated integers X and Y, denoting that there is an road between city X and city Y.
Output:
For each test case, I need to print the number of cities she bought.
Constraint:
1 <= T <= 100
1 <= E <= 1000
1 <= X, Y <= 10000
Let me solve this with Graph representation.
Key points: Each city will be vertex of the graph.
Road will be the edge between the vertex.
[Problem]Coffee treat with crush
I recently learnt about Graphs and very excited!
I went over to the Graph-making factory to watch some freshly prepared graphs. Incidentally, I have seen beautiful girl who is making nice graphs.
so I decided to step in and invite her for a coffee.
But she gave me one of her problem and said if I solve that she will come with me for a coffee :-(.
My Job is to Identify whether the incoming graph is a tree or not. I was given given N, the number of vertices in the graph and the degree of each vertex.
Find if the graph is a tree or not.
Input:
First line contains an integer N, the number of vertices.
Second line contains N space-separated integers, the degrees of the N vertices.
Output:
Print "Yes" (without the quotes) if the graph is a tree or "No" (without the quotes) otherwise.
Constraints:
1 ≤ N ≤ 100
1 ≤ Degree ≤ 1000
I went over to the Graph-making factory to watch some freshly prepared graphs. Incidentally, I have seen beautiful girl who is making nice graphs.
so I decided to step in and invite her for a coffee.
But she gave me one of her problem and said if I solve that she will come with me for a coffee :-(.
My Job is to Identify whether the incoming graph is a tree or not. I was given given N, the number of vertices in the graph and the degree of each vertex.
Find if the graph is a tree or not.
Input:
First line contains an integer N, the number of vertices.
Second line contains N space-separated integers, the degrees of the N vertices.
Output:
Print "Yes" (without the quotes) if the graph is a tree or "No" (without the quotes) otherwise.
Constraints:
1 ≤ N ≤ 100
1 ≤ Degree ≤ 1000
Graph representation
You can represent a graph in many ways.
Graph terminology:
Vertices: From below graph 1, 2, 3, 4 are Vertices.
Edges: (1,2), (2,3)..etc are edges.
Degree: Number of edges from the vertex.
From below graph vertex 1 degree is 2 because it had two edges which are(1,2) (1,4).
Order: Number of vertices will be graph order.
Size: Number of Edges will be graph size.
Note: The Sum of the degrees is twice the number of edges.
Spanning subgraph: A graph obtained only by edge deletion(G\E).
(i.e Original graph and subgraph number of vertices should be same)
Induced subgraph: A graph obtained only by vertex deletion(G-V)
Edge Existence:
Source: https://www.hackerearth.com/
You have been given an undirected graph consisting of N nodes and M edges. This graph can consist of self-loops as well as multiple edges. In addition , you have also been given Q queries. For each query , you shall be given 2 integers A and B. You just need to find if there exists an edge between node A and node B
. If yes, print "YES" (without quotes) else , print "NO"(without quotes).
Input Format:
The first line consist of 2 integers N
and M denoting the number of nodes and edges respectively. Each of the next M lines consist of 2 integers A and B denoting an undirected edge between node A and B. The next line contains a single integer Q denoting the number of queries. The next Line contains 2 integers A and B denoting the details of the query.
Output Format
Print Q lines, the answer to each query on a new line.
Constraints:
1≤N≤103
1≤M≤103
1≤A,B≤N
1≤Q≤103
The two most common ways of representing a graph is as follows:
Adjacency matrix:
It needs lot of space because if there is no edge between the vertices's it will store the value as zero.
The adjacency matrix of the following graph is:
i/j : 1 2 3 4
1 : 0 1 0 1
2 : 1 0 1 0
3 : 0 1 0 1
4 : 1 0 1 0
In real time scenarios this weight could be distance between two cities.
If you take network as example then file transfer capacity between the two system could be the graph weight.
below code will illustrate the edge representation with adjacent matrix.
Binary search tree
Generally the tree have one root node and child node, each child node can have leaf nodes.
How elements will insert in a tree ?
Assume your going to insert a element 10 in the tree.
As there are no elements it will insert as root node.
Then again if we try to insert element 20, it will insert right side of tree because 20 is greater then 10.
What if element is less then 10 ? - It goes to left side of tree.
Finally the tree will look like this.
How elements will insert in a tree ?
Assume your going to insert a element 10 in the tree.
As there are no elements it will insert as root node.
Then again if we try to insert element 20, it will insert right side of tree because 20 is greater then 10.
What if element is less then 10 ? - It goes to left side of tree.
Finally the tree will look like this.
Java SHA-256
Secure Hash Algorithm-256:
Most of the cryptographic hash functions runs on the digital data.
Using SHA-256 algorithm we can determine the data integrity.
For example, computing the hash of a downloaded file and comparing the result to a previously published hash result can show whether the download has been modified or tampered with some other content.
In addition, cryptographic hash functions are extremely collision-resistant.
Cryptographic hash functions:
It takes input of any length and gives us an output in fixed length.
Collision-resistant ?
In other words, it should be extremely difficult to produce the same hash output from two different input values using a cryptographic hash function.
This is the example to print the SHA-256 value for given string:
Also, It consists of six identical hashing algorithms(i.e., SHA-256, SHA-512, SHA-224, SHA-384, SHA-512/224, SHA-512/256) with a variable digest size.
SHA-256 is a 256-bit ( 32 byte) hashing algorithm which can calculate a hash code for an input of up to 264-1 bits. It undergoes 64 rounds of hashing and calculates a hash code that is a 64-digit hexadecimal number.
Source: https://www.hackerrank.com
Most of the cryptographic hash functions runs on the digital data.
Using SHA-256 algorithm we can determine the data integrity.
For example, computing the hash of a downloaded file and comparing the result to a previously published hash result can show whether the download has been modified or tampered with some other content.
In addition, cryptographic hash functions are extremely collision-resistant.
Cryptographic hash functions:
It takes input of any length and gives us an output in fixed length.
Collision-resistant ?
In other words, it should be extremely difficult to produce the same hash output from two different input values using a cryptographic hash function.
This is the example to print the SHA-256 value for given string:
import java.io.*;
import java.util.*;
import java.security.*;
import java.math.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
try{
MessageDigest md = MessageDigest.getInstance("SHA-256");
System.out.printf("%064x\n", new BigInteger(1, md.digest(s.getBytes())));
}catch(NoSuchAlgorithmException e){
//Handle exception
}
}
}
Also, It consists of six identical hashing algorithms(i.e., SHA-256, SHA-512, SHA-224, SHA-384, SHA-512/224, SHA-512/256) with a variable digest size.
SHA-256 is a 256-bit ( 32 byte) hashing algorithm which can calculate a hash code for an input of up to 264-1 bits. It undergoes 64 rounds of hashing and calculates a hash code that is a 64-digit hexadecimal number.
Source: https://www.hackerrank.com
Java MD5
Message Digest algorithm version 5:
MD5 is a widely-used cryptographic hash function with a -bit hash value. Here are some common uses for MD5:
This is the example to encrypt the alphanumeric string in java.
We have package named security in java(Check here).
Below code take the string as input and encrypt using MD5 algorithm.
MD5 is a widely-used cryptographic hash function with a -bit hash value. Here are some common uses for MD5:
- To store a one-way hash of a password.
- To provide some assurance that a transferred file has arrived intact.
This is the example to encrypt the alphanumeric string in java.
We have package named security in java(Check here).
Below code take the string as input and encrypt using MD5 algorithm.
import java.io.*;
import java.util.*;
import java.security.*;
import java.math.BigInteger;
public class MleClass {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
try{
MessageDigest md= MessageDigest.getInstance("MD5");
System.out.printf("%032x\n", new BigInteger(1, md.digest(s.getBytes())));
}catch(NoSuchAlgorithmException e){
// Handle exceptions
}
}
}
Method reference.
Before reading this I would like to suggest to go through Lamda expressions
We have seen the how to pass the behaviour as parameter.
In java8 we can pass method reference as argument.
It's not different, same as Lambda exprssion but syntax is little change.
With lambda expression we will write like this.
Same we can write with method reference like below.
Output: Show method
I'm from printMsg
We have seen the how to pass the behaviour as parameter.
In java8 we can pass method reference as argument.
It's not different, same as Lambda exprssion but syntax is little change.
With lambda expression we will write like this.
interface MleInterface{
void show();
}
public class HelloWorld{
void showMsg( MleInterface mle){
// Write some logic
mle.show();
}
public static void main(String []args){
HelloWorld obj = new HelloWorld();
/* Simplified Lambda expression. */
obj.showMsg(() -> System.out.println("I'm from Lambda expression"));
}
}
Same we can write with method reference like below.
interface MleInterface{
void show();
}
public class HelloWorld{
void showMsg( MleInterface mle){
// Write some logic
System.out.println("Show method");
mle.show();
}
public static void printMsg(){
System.out.println("I'm from printMsg");
}
public static void main(String []args){
HelloWorld obj = new HelloWorld();
/* Passing method reference.
This way new syntax in java8.
*/
obj.showMsg(HelloWorld :: printMsg);
}
}
Output: Show method
I'm from printMsg
Lambda expressions
All we know Lambda expressions are new concept in Java 8.
Why we need it ?
Why we need it ?
- Enable the functional program.
Really do we need functional programming in java.
We have been writing the millions of line java code with OOPS concepts.
Nothing new to do with functional programing, whatever we have been doing with OOPS same we can achieve with functional programing.
Why we need the new functional programming feature in JAVA ?
- It's readable and maintainable.
When we are working on client project requirement makes us write millions of lines java code
It' hard to maintain the complex code.
So, functional programing came in to the picture to improve the code readability and maintain it in elegant way.
How can we improve coding style with functional programing in JAVA?
Java this keyword.
this: is called as reference variable because it always refer an current object.
Eg:
Is this valid ?
Obj is local variable i.e we can't access that variable outside main method.
In above code snippet value is a instance variable.
So we must need an object or object or object reference to access value;
Then still valid the above snippet ?
Eg:
Is this valid ?
public class MleClass{
int value=10;
void show(){
System.out.println(value);
}
public static void main(){
MleClass obj =new MleClass();
ob.show();
}
}
Obj is local variable i.e we can't access that variable outside main method.
In above code snippet value is a instance variable.
So we must need an object or object or object reference to access value;
Then still valid the above snippet ?
Java execution priority
Whenever JVM loads the class, it will execute in specific order.
In the above order JVM execute the program.
First it will looks for Class variables.|
Then static blocksThis blocks will execute top to bottom.
Finally, main method will execute.
So far so good!
Then can we execute java program with out main method?
- Class variable.
- Static blocks
- public static void main(String args[])
In the above order JVM execute the program.
First it will looks for Class variables.|
static int value =90; static String helloString ="Hello";
Then static blocksThis blocks will execute top to bottom.
static{
System.out.println("Static block-1");
}
static{
System.out.println("Static block-2");
}
Finally, main method will execute.
So far so good!
Then can we execute java program with out main method?
Types of java variables
Java variables are very straightforward, just they are variables.
Yes, just they are variable but we have to understand when the memory allocated and how can we access those.
I have seen some people who can write the code without knowing all this.
Seriously!, is that possible ?
Yes, just they are variable but we have to understand when the memory allocated and how can we access those.
I have seen some people who can write the code without knowing all this.
Seriously!, is that possible ?
Rules for Java source file.
#Rule-1
A source file can have only one public class.
It is ambiguous ?
Then what exactly JVM doing with Public class ?
A source file can have only one public class.
It is ambiguous ?
Then what exactly JVM doing with Public class ?
Do you know what are the Interface hidden rules ?
Interface:
Just simple definition is collection of public static final variable and public abstract methods.
Let me write one example:
Is this example telling you what exactly I give in the definition ?
Just simple definition is collection of public static final variable and public abstract methods.
Let me write one example:
interface A{
int a= 10;
void show();
}
Is this example telling you what exactly I give in the definition ?
Do you know what is method hiding ?
Method hiding is possible with static method.
Let's see example:
Let's see example:
class A{
static void show(){
System.out.println("From A class");
}
}
public class B extends A{
static void show(){
System.out.println("From B class");
}
public static void main(String []args){
A a =new HelloWorld();
B b =new B();
a.show(); // A class show method will call
b.show(); // B class show method will call
}
}
Important rules for Overriding
What is overriding ?
Just simple definition is "Two or more methods with the same name and same parameter list then it said to be Method overriding" .Eg-1:
class A{
void show(){
System.out.println("Overridden method");
}
}
public class B extends A{
void show(){
System.out.println("Overriding method");
}
}
Subscribe to:
Posts (Atom)


