Hackathon Java

Question 1:


count=0
while IFS= read -r line
do
if [[ $line =~  [[:digit:]] ]] ;
then
if [[ $line =~ [a-zA-Z]  ]];
then
count=$((count+1))
fi
fi
done <<< $( cat $1 )
echo "Count: $count"

Question 2:


while read -r line;
do
    (( ${#line} > max )) && max=${#line} && longest="$line"
done
echo "$longest"

Question 3:


SET NULL "NULL";
SET FEEDBACK OFF;
SET ECHO OFF;
SET HEADING OFF;
SET WRAP OFF;
SET LINESIZE 10000;
SET TAB OFF;
SET PAGES 0;
SET DEFINE OFF;
/*
Enter your query below.
Please append a semicolon ";" at the end of the query
*/
SELECT DEPTNAME,MAXEMPCOUNT FROM DEPARTMENT;
exit;

Question 4:


SET NULL "NULL";
SET FEEDBACK OFF;
SET ECHO OFF;
SET HEADING OFF;
SET WRAP OFF;
SET LINESIZE 10000;
SET TAB OFF;
SET PAGES 0;
SET DEFINE OFF;
/*
Enter your query below.
Please append a semicolon ";" at the end of the query
*/
SELECT ALLOCID,DID,EID
FROM ALLOCATION
WHERE 
DID=100 OR DID=200;
exit;

Question 5:


SET NULL "NULL";
SET FEEDBACK OFF;
SET ECHO OFF;
SET HEADING OFF;
SET WRAP OFF;
SET LINESIZE 10000;
SET TAB OFF;
SET PAGES 0;
SET DEFINE OFF;
/*
Enter your query below.
Please append a semicolon ";" at the end of the query
*/
SELECT DID, COUNT(*)
FROM ALLOCATION
GROUP BY DID;
exit;

Question 6:


SET NULL "NULL";
SET FEEDBACK OFF;
SET ECHO OFF;
SET HEADING OFF;
SET WRAP OFF;
SET LINESIZE 10000;
SET TAB OFF;
SET PAGES 0;
SET DEFINE OFF;
/*
Enter your query below.
Please append a semicolon ";" at the end of the query
*/
-- SELECT DEPTNAME FROM
-- DEPATMENT
-- WHERE DEPTID  DID IN
-- (SELECT * FROM
-- (SELECT DID, COUNT(*) AS ECOUNT FROM ALLOCATION GROUP BY DID) WHERE ECOUNT>5);
SELECT DNAME FROM
(SELECT DNAME,COUNT(*) AS ECOUNT FROM
(SELECT DEPARTMENT.DEPTNAME AS DNAME
FROM ALLOCATION,DEPARTMENT
WHERE
ALLOCATION.DID = DEPARTMENT.DEPTID)
GROUP BY DNAME)WHERE ECOUNT>5;
exit;

Question 7:


SET NULL "NULL";
SET FEEDBACK OFF;
SET ECHO OFF;
SET HEADING OFF;
SET WRAP OFF;
SET LINESIZE 10000;
SET TAB OFF;
SET PAGES 0;
SET DEFINE OFF;
/*
Enter your query below.
Please append a semicolon ";" at the end of the query
*/
SELECT ALLOCATION.ALLOCID,ALLOCATION.EID,EMPLOYEE.EMPNAME 
FROM ALLOCATION,EMPLOYEE
WHERE
ALLOCATION.EID = EMPLOYEE.EMPID AND 
ALLOCATION.DID=100;
exit;

Question 8:


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)
    {
        //code to read values 
        Scanner sc=new Scanner(System.in);
        String s = sc.nextLine();
        //code to display the result
        s=s.toLowerCase();
        for(int i=s.length()-1;i>=0;i--)
            System.out.print(s.charAt(i));
        sc.close();
    }
}

Question 9:


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)
    {
         Scanner sc=new Scanner(System.in);
         //code to read values 
         String s = sc.nextLine();
         //code to display the result
         String out=""+s.charAt(0);
         for(int i=0;i<s.length();i++){
            char ch = s.charAt(i);
            int count=0;
            for(int j=0;j<out.length();j++){
                if(ch==s.charAt(j))
                count++;
            }
            if(count == 0)
            out=out+ch;
         }
         System.out.print(out);
         sc.close();
    }
}

Question 10:


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) {
        Scanner sc = new Scanner(System.in);
        int phoneId;
        String os;
        String brand;
        int price;
        Phone[] p = new Phone[4];
        for (int i = 0; i < p.length; i++) {
            phoneId = sc.nextInt();
            sc.nextLine();
            os = sc.nextLine();
            brand = sc.nextLine();
            price = sc.nextInt();
            sc.nextLine();
            p[i] = new Phone(phoneId, os, brand, price);
        }
        String checkbrand = sc.nextLine();
        String checkos = sc.nextLine();
        int res = findPriceForGivenBrand(p, checkbrand);
        Phone pres = getPhoneIdBasedOnOs(p, checkos);
        if(res!=0)
            System.out.println(res);
        else
            System.out.println("The given Brand is not available");
        if(pres!=null)
            System.out.println(pres.getPhoneId());
        else
            System.out.println("No phones are available with specified os and price range");
        sc.close();
    }

    public static int findPriceForGivenBrand(Phone[] phone, String brand) {
        int sum = 0;
        for (int i = 0; i < phone.length; i++) {
            if (phone[i].getBrand().toLowerCase().contains(brand.toLowerCase()))
                sum = sum + phone[i].getPrice();
        }
        return sum;
    }

    public static Phone getPhoneIdBasedOnOs(Phone[] phone, String os) {
        Phone temp=null;
        for (int i = 0; i < phone.length; i++) {
            if ((phone[i].getOs().toLowerCase().contains(os.toLowerCase()))&&(phone[i].getPrice()>=50000)) {
                temp=phone[i];
                break;
            }
        }
        return temp;
    }
}

class Phone
{
    private int phoneId;
    private String os;
    private String brand;
    private int price;
    public Phone(int phoneId, String os, String brand, int price) {
        super();
        this.phoneId = phoneId;
        this.os = os;
        this.brand = brand;
        this.price = price;
    }
    public int getPhoneId() {
        return phoneId;
    }
    public void setPhoneId(int phoneId) {
        this.phoneId = phoneId;
    }
    public String getOs() {
        return os;
    }
    public void setOs(String os) {
        this.os = os;
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
}

Question 11:


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 input = sc.nextLine();
        input = input.trim();
        input = input.substring(0,input.length()-1);
        String[] arr=input.split(" ");
        int[] a=new int[arr.length];
        for(int i=0;i<arr.length;i++) {
            a[i]=Integer.parseInt(arr[i]);
        }
        int count =0;
        for(int i=0;i<a.length;i++) {
            if(checkPrime(a[i]))
                count++;
        }
        int[] prime =new int[count];
        int k=0;
        for(int i=0;i<a.length;i++) {
            if(checkPrime(a[i]))
                prime[k++]=a[i];
        }
        for(int i=0;i<prime.length;i++) {
            for(int j=0;j<prime.length-i-1;j++) {
                if(prime[j]<prime[j+1]) {
                    int temp= prime[j];
                    prime[j]=prime[j+1];
                    prime[j+1]=temp;
                }
            }
        }
        System.out.print((prime[1]+count));
        sc.close();
    }
    public static boolean checkPrime(int n) {
        int c=0;
        for(int i=1;i<=n;i++) {
            if(n%i==0)
                c++;
        }
        if(c==2)
            return true;
        else
            return false;
    }
}

 

©2018 by The real one. Proudly created with Wix.com