Hackathon Java
Hackathon Java
Write the SQL query to display the name of the department along with the count of
employees working in it.
Solution:
select Departments.deptName,COUNT(Employees.eDeptId) From departments LEFT Join
Employees on Departments.deptId=Employees.eDeptId Group By
Departments.deptId,Departments.deptName Order by count(Employees.eDeptId) Desc,
departments.deptName;
Write the SQL query to print the department id and name of the respective
Department for all the departments which are located in Ground Floor
Solution:
select Dept_Id,Dept_Name from Department where Dept_Location='Ground Floor';
Write the SQL query to print the names of the departments which does not have any
Programmers.
Solution:
select distinct Dept_name from Departments,Employees where Dept_ID=Emp_Dept_Id and
Dept_name not in (select distinct Dept_name from Departments,Employees where
Dept_id=Emp_Dept_Id and Emp_Skill like'Programmer');
*****************************************************************
Unit - Hackerrank Activities - Unix
Write the unix command to count the occurrence of the word "Unix" in a given file.
The file will be given as command line argument while the script containing your
command will be run.
For example,
Write the unix command to count the number of words in the first 3 lines of a file.
The file will be given as a command line argument when the script containing your
command will run.
For example,
If the input file contains the following lines
Write a shell script to find the sum of all even numbers from a list of given
numbers. The script should first of all take the count of numbers to be added as
user input followed by the numbers one by one.
Solutions:
read n
awk '
BEGIN {
sum=0;
}
{ if ( $0%2==0 ){
sum+=$0;
}
}
END { print "Total","=",sum}'
Student details are stored in a file in the following order with space as the
delimiter:
RollNo Name Score
Write a unix command to find the name of the Student who has the highest score.
The file will be given as a command line argument when the script containing your
command will run.
For example,
If the input file has the below content
Solution: sort -k3,3 -rn -t" " | head -n1 | awk '{print $2}'
Write a shell script to find the count of employees whose salary is less than the
average salary of all employees.
The file with the employee details will be given as a command line argument when
your script will run.
For example,
If the input file contains the following employee records
EmpID;EmpName;Salary
100;A;30000
102;B;45000
103;C;15000
104;D;40000
Solutions:
lim=10
max=0
salary=0
read
n=0
#count the avg plus store in array
for i in $(seq $lim)
do
read line
num=$(echo "$line"|cut -d ";" -f3)
if [ $num -gt 0 ];
then
salary=$((salary+num))
arr[n]=$num
n=$((n+1))
fi
done;
avg=$((salary/n))
#count now
count=0
for val in ${arr[@]}
do
if [ $val -lt $avg ];
then
count=$((count+1))
fi
done;
echo "$count"
(or)
awk 'BEGIN {FS = ";"} ; {s += $3; ++c;a[$3] += $3;k[$1] += $1} END{ for (i in a)
if(i<s/(c-2)) p+=1;for (j in k);if(j == 5) {print(p+1)} else if(j == 6) {print(p)}
else {print(p-1)}}'
***********************************************************************************
Unit- Hackerrank Activities - JAVA
Java - Numeric Computation:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution
{
public static void main(String args[] ) throws Exception
{
int a;
double b,c;
Scanner sc = new Scanner(System.in);
a = sc.nextInt();
b = sc.nextDouble();
c = sc.nextDouble();
Account account = new Account(a, b, c);
int noOfYear;
noOfYear = sc.nextInt();
double answer = calculateInterest(account, noOfYear);
System.out.format("%.3f",answer);
}
public static double calculateInterest(Account account, int noOfYear)
{
double temp = noOfYear * account.getInterestRate() / 100;
return (account.getBalance() * (account.getInterestRate()+temp) / 100);
}
}
class Account
{
private int id;
private double balance;
private double interestRate;
Account(int id, double balance, double interestRate)
{
this.id = id;
this.balance = balance;
this.interestRate = interestRate;
}
public int getId()
{
return this.id;
}
public void setId(int id)
{
this.id = id;
}
public double getBalance()
{
return this.balance;
}
public void setBalance(double balance)
{
this.balance = balance;
}
public double getInterestRate()
{
return this.interestRate;
}
public void setInterestRate(double interestRate)
{
this.interestRate = interestRate;
}
}
***********************************************************************************
************
Java - Classes and Objects
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String args[] ) throws Exception {
int x1,y1,x2,y2;
Scanner scn=new Scanner(System.in);
x1=scn.nextInt();
y1=scn.nextInt();
x2=scn.nextInt();
y2=scn.nextInt();
Point p1=new Point(x1, y1);
Point p2=new Point(x2, y2);
double distance=findDistance(p1, p2);
System.out.format("%.3f",distance);
}
public static double findDistance(Point p1, Point p2)
{
double distance=Math.sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
return distance;
}
}
class Point
{
int x,y;
Point(int x,int y)
{
this.x=x;
this.y=y;
}
}
***********************************************************************************
**************
Conditional operators
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
double x1,y1,x2,y2,x3,y3;
Scanner scn=new Scanner(System.in);
x1=scn.nextDouble();
y1=scn.nextDouble();
x2=scn.nextDouble();
y2=scn.nextDouble();
x3=scn.nextDouble();
y3=scn.nextDouble();
Point p1=new Point(x1, y1);
Point p2=new Point(x2, y2);
Point p3=new Point(x3, y3);
Point highest=pointWithHighestOriginDistance(p1, p2, p3);
System.out.format("%.1f \n",highest.x);
System.out.format("%.1f",highest.y);
}
public static Point pointWithHighestOriginDistance(Point p1, Point p2, Point p3)
{
double d1=Math.sqrt(p1.x*p1.x+p1.y*p1.y);
double d2=Math.sqrt(p2.x*p2.x+p2.y*p2.y);
double d3=Math.sqrt(p3.x*p3.x+p3.y*p3.y);
return d1>d2?(d1>d3?p1:p3):(d2>d3?p2:p3);
}
}
class Point
{
double x,y;
Point(double x, double y)
{
this.x=x;
this.y=y;
}
}
***********************************************************************************
*******
Java Arrays - 1
Problem 1: Find docs with odd page
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String args[] ) throws Exception {
Document[] document=new Document[4];
Scanner sc=new Scanner(System.in);
for(int i=0;i<document.length;i++)
{
document[i]=new
Document(sc.nextInt(),sc.next(),sc.next(),sc.nextInt());
}
Document[] output=docsWithOddPages(document);
for(Document i:output)
{
System.out.println(i.getId()+" "+i.getTitle()+" "+i.getFolderName()+"
"+i.getPages());
}
}
public static Document[] docsWithOddPages(Document[] d)
{ Document[] d2=new Document[0];
for(Document i:d)
{
if(i.getPages()%2!=0)
{
d2=Arrays.copyOf(d2, d2.length+1);
d2[d2.length-1]=i;
}
}
for(int i=0;i<d2.length-1;i++)
{
for(int k=0;k<d2.length-i-1;k++)
{
if(d2[k].getId()>d2[k+1].getId())
{
Document temp=d2[k];
d2[k]=d2[k+1];
d2[k+1]=temp;
}
}
}
return d2;
}
}
class Document
{
private int id;
private String title;
private String folderName;
private int pages;
Document(int id,String title,String folderName,int pages)
{
this.id=id;
this.title=title;
this.folderName=folderName;
this.pages=pages;
}
public int getId()
{
return id;
}
public String getTitle()
{
return title;
}
public String getFolderName()
{
return folderName;
}
public int getPages()
{
return pages;
}
}
Problem 2: Sort books by price
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution
{
public static void main(String args[] ) throws Exception
{
Scanner sc=new Scanner(System.in);
Book[] book=new Book[4];
for(int i=0;i<book.length;i++)
{
book[i]=new Book(sc.nextInt(),sc.next(),sc.next(),sc.nextInt());
}
Book[] output=sortBooksByPrice(book);
for(Book i: output)
{
System.out.println(i.getId()+" "+i.getTitle()+" "+i.getAuthor()+"
"+i.getPrice());
}
}
public static Book[] sortBooksByPrice(Book[] book)
{
for(int i=0;i<book.length-1;i++)
{
for(int k=0;k<book.length-i-1;k++)
{
if(book[k].getPrice()>book[k+1].getPrice())
{
Book temp=book[k];
book[k]=book[k+1];
book[k+1]=temp;
}
}
}
return book;
}
}
class Book
{
private int id;
private String title;
private String author;
private double price;
Book(int id,String title,String author,double price)
{
this.id=id;
this.title=title;
this.author=author;
this.price=price;
}
public int getId()
{
return id;
}
public String getTitle()
{
return title;
}
public String getAuthor()
{
return author;
}
public double getPrice()
{
return price;
}
}
***********************************************************************************
*****
Java Arrays - 2
Problem 1:
import java.util.Scanner;
public class Solution
{
public static void main(String args[] ) throws Exception
{
/* Do not alter code in main method */
Shirt[] shirts = new Shirt[5];
Scanner sc = new Scanner(System.in);
for(int i = 0;i<5;i++)
{
int tag = sc.nextInt();sc.nextLine();
String brand = sc.nextLine();
double price = sc.nextDouble();sc.nextLine();
char g = sc.nextLine().charAt(0);
shirts[i] = new Shirt(tag,brand,price,g);
}
double price = sc.nextDouble();
for(Shirt s: shirts)
{
System.out.println(getDiscountPrice(s));
}
Shirt[] result = getShirtWithMoreThanSpecificPrice(shirts,price);
for(Shirt s: result)
{
if(s.getTag()!=0)
System.out.println(s.getTag()+" "+s.getPrice()+ " " + s.getBrand());
}
}
/* implement your methods here*/
public static double getDiscountPrice(Shirt s)
{
double discount;
if(s.gender=='m')
discount=10;
else if(s.gender=='f')
discount=20;
else if(s.gender=='u')
discount=30;
else
discount=0;
return s.price-(discount*s.price)/100;
}
public static Shirt[] getShirtWithMoreThanSpecificPrice(Shirt [] shirts, double
price)
{
Shirt[] res = new Shirt[5];
for(int i=0;i<res.length;i++)
res[i]=new Shirt(0,"",0,'c');// taking sample to avoid null pointer exception
int j=0;
for(int i=0;i<shirts.length;i++)
{
if(shirts[i].price>price)
{
res[j++]= new
Shirt(shirts[i].tag,shirts[i].brand,shirts[i].price,shirts[i].gender);
}
}
return res;
}
}
class Shirt
{
//define the class as per details shared in the question
int tag;
String brand;
double price;
char gender;
Shirt(int tag, String brand, double price, char gender)
{
this.tag=tag;
this.brand=brand;
this.price=price;
this.gender=gender;
}
public int getTag()
{
return this.tag;
}
public void setTag(int tag)
{
this.tag=tag;
}
public String getBrand()
{
return this.brand;
}
public void setBrand(String brand)
{
this.brand=brand;
}
public double getPrice()
{
return this.price;
}
public void setPrice(double price)
{
this.price=price;
}
public char getGender()
{
return this.gender;
}
public void setGender(char gender)
{
this.gender=gender;
}
}
Problem 2:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.*;
public class Solution {
public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner scn=new Scanner(System.in);
Book[] booksArray=new Book[4];
Book[] res=new Book[4];
for(int i=0;i<booksArray.length;i++)
{
booksArray[i]=new Book();
res[i]=new Book();
}
for(int i = 0;i<4;i++)
{
booksArray[i].id = scn.nextInt();scn.nextLine();
booksArray[i].title = scn.nextLine();
booksArray[i].author = scn.nextLine();
booksArray[i].price = scn.nextDouble();
}
String value=scn.next();
res=searchTitle(value, booksArray);
int [] matchedId=new int[4];
int j=0;
for(int i=0;i<res.length;i++)
{
if(res[i].id!=0)
{
matchedId[j++]=res[i].id;
}
}
Arrays.sort(matchedId);
for(int i=0;i<matchedId.length;i++)
{
if(matchedId[i]!=0)
System.out.println(matchedId[i]);
}
}
public static Book[] searchTitle(String value, Book[] books)
{
int k=0;
Book[] matching=new Book[4];
for(int i=0;i<matching.length;i++)
matching[i]=new Book();
for(int i=0;i<books.length;i++)
{
String val=value.toLowerCase();
String bookTitle=books[i].title.toLowerCase();
if(bookTitle.contains(val))
{
matching[k++]=books[i];
}
}
return matching;
}
}
class Book
{
int id;
String title;
String author;
double price;
public int getId()
{
return this.id;
}
public void setId(int id)
{
this.id=id;
}
public String getTitle()
{
return this.title;
}
public void setTitle(String title)
{
this.title=title;
}
public String getAuthor()
{
return this.author;
}
public void setAuthor(String author)
{
this.author=author;
}
public double getPrice()
{
return this.price;
}
public void setPrice(double price)
{
this.price=price;
}
******************************************************************************
Java - Iteration 1
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
char min=s.charAt(0);
for(int i=0;i<=s.length()-1;i++)
{
char c=s.charAt(i);
if(min>c)
{
min=c;
}
}
System.out.println(min);
}
}
***********************************************************************************
*********************
Java - Iteration 2
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
for(int i=0;i<=arr.length-1;i++){
fact=1;
for(int j=1;j<=arr[i];j++)
{
fact=fact*j;
}
System.out.println(fact);
}
}
}