Hackerank Day 3: Intro to Conditional Statements solution in java
Algorithm
input_number=N
Step:-1 First check the input number is even or odd.
Step:-2 if(even(N) AND(N==2 OR 4)) then print("Not Weird");
Step:-3 else if(even(N) AND(N>=6 OR N<=20)) then print("Weird");
Step:-3 else if(even(N) AND (N>20)) then print("Not Weird");
Step:-3 else print("Weird");
Step:-2 if(even(N) AND(N==2 OR 4)) then print("Not Weird");
Step:-3 else if(even(N) AND(N>=6 OR N<=20)) then print("Weird");
Step:-3 else if(even(N) AND (N>20)) then print("Not Weird");
Step:-3 else print("Weird");
Explanation
At first, Check the number is even or odd by using modulo operator(%) and other techniques.If the input_number(N) is odd then directly print out "Weird" and if the input_number is even then there are three condition which are check one by one with the conditional statement.
First condition:- if it is 2 or 4 then print out "Not Weird".
Second Condition:- if it is within the range inclusion 6 to 20 (6<=N<=20) then print out "Weird".
Third Condition:- if it greater than 20 (N>20) then print out "Not Weird".
Examples
Program
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
if(N%2==0 && (N==2 || N==4))
{
System.out.println("Not Weird");
}
else if(N%2==0 && (N>=6 && N<=20))
{
System.out.println("Weird");
}
else if(N%2==0 && N>20)
{
System.out.println("Not Weird");
}
else
{
System.out.println("Weird");
}
scanner.close();
}
}
Hackerank Day 2: Operators solution in java
Day 2: Operators
Algorithm
Step:-1 Type cast Integer data type to Double. int tip_percent -> double tip_percent, and int tax_percent ->double tax_percent.Step:-2 Calculate tip=(meal_cost*tip_percent)/100 and tax=(meal_cost*tax_percent)/100.
Step:-3 Calculate total_cost=meal_cost+tip+tax.
Step:-4 Round the total_cost as the result,Round(total_cost).
Explanation
Here, three line of input are differents data types- double, integre, and integer and output is rounded value.so, our first aim to convert all into one data types-whether integer or double.But for more efficient, convert into double types.
In this problem we don't convert, simply type casting from integer to double(preserve all bits).
At first, we calculate result and hold in into total variable where int tip_percent and int tax_percent are type cast into double as (double)tip_percent and (double)tax_percent).
At last, simply output the rounded total value using Math.round() Function.
In this problem we don't convert, simply type casting from integer to double(preserve all bits).
int tip_percent, int tax_percent
are type cast into double.At first, we calculate result and hold in into total variable where int tip_percent and int tax_percent are type cast into double as (double)tip_percent and (double)tax_percent).
double total=meal_cost+(meal_cost*(double)tip_percent)/100+(meal_cost*(double)tax_percent)/100;
At last, simply output the rounded total value using Math.round() Function.
System.out.println(Math.round(total));
Calculation
Step 1:- tip=mealCost * (tip_Percent/100).
Step 2:- tax=mealCost * (tax_Percent/100).
Step 3:- totalCost=mealCost + tip + tax.
Step 4:- output=round(totalCost).
Examples
Three line input and its output.
Q.no. | Input | output |
---|---|---|
1 | 12.00 20 8 | 15 |
Program
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the solve function below.
static void solve(double meal_cost, int tip_percent, int tax_percent) {
double total=meal_cost+(meal_cost*(double)tip_percent)/100+(meal_cost*(double)tax_percent)/100;
System.out.println(Math.round(total));
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
double meal_cost = scanner.nextDouble();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int tip_percent = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int tax_percent = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
solve(meal_cost, tip_percent, tax_percent);
scanner.close();
}
}
Hackerank day 1 solution in java| Data Types, day 1 Hacker Rank Java solution | Hackerranks 30 day's coding context java solution
Objective
Today, we're discussing data types. Check out the Tutorial tab for learning materials and an instructional video!
Today, we're discussing data types. Check out the Tutorial tab for learning materials and an instructional video!
Task
Complete the code in the editor below. The variables , , and are already declared and initialized for you. You must:
Complete the code in the editor below. The variables , , and are already declared and initialized for you. You must:
- Declare variables: one of type int, one of type double, and one of type String.
- Read lines of input from stdin (according to the sequence given in the Input Format section below) and initialize your variables.
- Use the operator to perform the following operations:
- Print the sum of plus your int variable on a new line.
- Print the sum of plus your double variable to a scale of one decimal place on a new line.
- Concatenate with the string you read as input and print the result on a new line.
Note: If you are using a language that doesn't support using for string concatenation (e.g.: C), you can just print one variable immediately following the other on the same line. The string provided in your editor must be printed first, immediately followed by the string you read as input.
Input Format
The first line contains an integer that you must sum with .
The second line contains a double that you must sum with .
The third line contains a string that you must concatenate with .
The second line contains a double that you must sum with .
The third line contains a string that you must concatenate with .
Output Format
Print the sum of both integers on the first line, the sum of both doubles (scaled to decimal place) on the second line, and then the two concatenated strings on the third line.
Hackerank day 0 solution in java| Hello, World, day 0 Hacker Rank Java solution | Hackerranks 30 day's coding context java solution
Day 0: Hello, World.
Objective
In this challenge, we review some basic concepts that will get you started with this series. You will need to use the same (or similar) syntax to read input and write output in challenges throughout HackerRank. Check out the Tutorial tab for learning materials and an instructional video!Task
Hello, World.
on a single line, and finally print the value of your variable on a second line.
You've got this!
Note: The instructions are Java-based, but we support submissions in many popular languages. You can switch languages using the drop-down menu above your editor, and the variable may be written differently depending on the best-practice conventions of your submission language.
Input Format
A single line of text denoting (the variable whose contents must be printed).
Output Format
Print
Hello, World.
on the first line, and the contents of on the second line.Left Recursion Remove CFG java programming
Q.What is Left Recursion ?
Ans. In the grammar any production in the form of A -> Αα /β called Left Recursion production.
LHS of the production ,A is appears in the left symbol of the RHS production.That's why it is left recursion.
when parser try to parse it, recursively it come back to the origin and will not be terminate.
Q.How to remove ?
Ans. write the production in the form Α -> βΑ' and A' -> αΑ'/ε .
Step 1 :-write the production A -> βΑ', // where A' is another Non -terminal.
Step 2 :- Again A' -> αΑ'/ε
Example :-
Q 1. E -> E+T / T T -> T*F / F
F -> (E) / id
Ans. Here first and second production contain left recursion.
for first production E -> E+T / T
α =+Τ and β =T,so
E -> TE'
E'-> +TE'/ε
for second production T -> T*F / F
α =*F and β =F,so
T -> FT'
T'-> *T'/ε
Hence, The complete solution will be E -> TE'
E'-> +TE'/ε
Example :-
Q 1. E -> E+T / T
F -> (E) / id
Ans. Here first and second production contain left recursion.
for first production E -> E+T / T
α =+Τ and β =T,so
E -> TE'
E'-> +TE'/ε
for second production T -> T*F / F
α =*F and β =F,so
T -> FT'
T'-> *T'/ε
Hence, The complete solution will be E -> TE'
E'-> +TE'/ε
T -> FT'
T'-> *T'/ε
F -> (E) / id
Q 2. A -> Aa /b/Ac/d
Ans. Here Aa and Ac contain left recursion.
for first production A -> Aa / b
α =a and β =b,so
A -> bA'
A' -> aA'/ε
for second production A -> Ac / d
α =c and β =d,so
A -> dA"
A"-> cA"/ε
Hence, The complete solution will be A -> bA'/dA"
A' -> aA'/ε
Ans. Here Aa and Ac contain left recursion.
for first production A -> Aa / b
α =a and β =b,so
A -> bA'
A' -> aA'/ε
for second production A -> Ac / d
α =c and β =d,so
A -> dA"
A"-> cA"/ε
Hence, The complete solution will be A -> bA'/dA"
A' -> aA'/ε
A"-> cA"/ε
Note:- The order of the A -> Aa /b/Ac/d may be any but we have to combine two - two part separately and try to solve.
Left Factor Remove CFG java programming
Left Factor Remove CFG java programming
/* lhs : left hand side
* rhs : right hand side
* LF : left factoring
* f_count: factoring count that is used to count factor out , i.e X0, X1 , X2
* pro: production
* cfg: context-free-grammar that refers to the entered grammar by user
*/
public class Lab_6_Left_factoring_ {
public static String cfg_Left_factored="";
public static String cfg="A--> aAB | aBc | aAc\n" +""; // you can add more productions by only adding \n at the end of the productions.
public static String Not_LF="";
public static boolean left_factoring=false;
public static int f_count =0;
public static void main(String[] args) {
System.out.println("Context free grammar is \n" + cfg + "\n ----------------------------------------------------------");
check_factor(cfg);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("The result of left factoring is \n" ); // productions that don't need to be left factored out.
System.out.println(Not_LF);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("\t\t\t\t\t\t\t\t Coded by RNR");
System.out.println("\t\t\t\t\t\t contact info: rebaz.najeeb@koyauniversity.org");
System.out.println("\t\t\t\t\t\t\t\t\t 2016");
}
public static void check_factor(String cfg) {
String lines [] = cfg.split("\\\n");
for (String line: lines) {
line= line.replaceAll("\\s+", ""); // just to remove all white spaces from a line
String productions [] = line.split("-->",0); // to split the a line at a time into right hand side and left hand side productions according to -->
String lhs = productions [0];
String rhs = productions[1];
System.out.println("\n*** Left hand side: " + lhs ) ;
System.out.println("------------------------------------------");
System.out.println("*** Right hand side: " + rhs);
System.out.println("------------------------------------------");
System.out.println("");
String [] rhs_productions = rhs.split("\\|") ; // split right hand side for productions based on |
left_factoring=false;
for (int i=0;i<rhs_productions.length-1;i++) {
for(int j=i+1;j<rhs_productions.length;j++) {
if (rhs_productions[i].charAt(0)==rhs_productions[j].charAt(0) && (!rhs_productions[i].equals("epsilon")) ) {
left_factoring=true;
}
}
}
/* lhs : left hand side
* rhs : right hand side
* LF : left factoring
* f_count: factoring count that is used to count factor out , i.e X0, X1 , X2
* pro: production
* cfg: context-free-grammar that refers to the entered grammar by user
*/
public class Lab_6_Left_factoring_ {
public static String cfg_Left_factored="";
public static String cfg="A--> aAB | aBc | aAc\n" +""; // you can add more productions by only adding \n at the end of the productions.
public static String Not_LF="";
public static boolean left_factoring=false;
public static int f_count =0;
public static void main(String[] args) {
System.out.println("Context free grammar is \n" + cfg + "\n ----------------------------------------------------------");
check_factor(cfg);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("The result of left factoring is \n" ); // productions that don't need to be left factored out.
System.out.println(Not_LF);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("\t\t\t\t\t\t\t\t Coded by RNR");
System.out.println("\t\t\t\t\t\t contact info: rebaz.najeeb@koyauniversity.org");
System.out.println("\t\t\t\t\t\t\t\t\t 2016");
}
public static void check_factor(String cfg) {
String lines [] = cfg.split("\\\n");
for (String line: lines) {
line= line.replaceAll("\\s+", ""); // just to remove all white spaces from a line
String productions [] = line.split("-->",0); // to split the a line at a time into right hand side and left hand side productions according to -->
String lhs = productions [0];
String rhs = productions[1];
System.out.println("\n*** Left hand side: " + lhs ) ;
System.out.println("------------------------------------------");
System.out.println("*** Right hand side: " + rhs);
System.out.println("------------------------------------------");
System.out.println("");
String [] rhs_productions = rhs.split("\\|") ; // split right hand side for productions based on |
left_factoring=false;
for (int i=0;i<rhs_productions.length-1;i++) {
for(int j=i+1;j<rhs_productions.length;j++) {
if (rhs_productions[i].charAt(0)==rhs_productions[j].charAt(0) && (!rhs_productions[i].equals("epsilon")) ) {
left_factoring=true;
}
}
}
CFG java programming S->aSb | bSa | epsilon | equal number of a's and b's
PROGRAM:- 1
Note:- this program is based on the following cfg production rules.
---------------------------------------------------------------------------------------------------------
Q 1. write a java program for the cfg having equal number of a's and b's
production rule //file 1 readable (to be read)
S -> aSb
S -> bSa
S -> ~ ( epsilon) // the symbol "~" denote the epsilon
---------------------------------------------------------------------------------------------------------
Note:- this program is based on the following cfg production rules.
---------------------------------------------------------------------------------------------------------
Q 1. write a java program for the cfg having equal number of a's and b's
production rule //file 1 readable (to be read)
S -> aSb
S -> ~ ( epsilon) // the symbol "~" denote the epsilon
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
file two= (where code of the cfg written)
code
import java.io.*;
import java.util.Scanner;
class Cfg
{
public static void main(String args []) throws IOException
{
Scanner r=new Scanner(System.in);
File file=new File("/home/rajeevkumarmahato/CG/C.java"); //first file location
BufferedReader br=new BufferedReader(new FileReader(file));
String t="";
while((t=br.readLine())!=null)
System.out.println(t);
System.out.println("enter the string");
String input=r.next();
if(input.length()%2==0)
{
int lastpointer=input.length()-1;
for(int i=0;i<input.length()/2;i++)
{
if(input.charAt(i)=='a' && input.charAt(lastpointer)=='b')
{
System.out.println("aSb");
lastpointer--;
}
else if(input.charAt(i)=='b' && input.charAt(lastpointer)=='a')
{
System.out.println("bSa");
lastpointer--;
}
else
{
System.out.println("not in the cfg");
break;
break;
}
}
}
else
{
System.out.println("Not belongs to the cfg");
}
}
}
Subscribe to:
Posts (Atom)