Saturday, 1 April 2017

Marks Higher than 75

Query the Name of any student in STUDENTS who scored higher than  Marks. Order your output by the last three characters of each name. If two or more students both have names ending in the same last three characters (i.e.: Bobby, Robby, etc.), secondary sort them by ascending ID.
Input Format
The STUDENTS table is described as follows:The Name column only contains uppercase (A-Z) and lowercase (a-z) letters.
Sample Input
Sample Output
Ashley
Julia
Belvet
Explanation
Only Ashley, Julia, and Belvet have Marks > . If you look at the last three characters of each of their names, there are no duplicates and 'ley' < 'lia' < 'vet'.
Query:
MS SQL SERVER:
select Name
from students
where marks>75
order by right(Name,3),id ;

Weather Observation Station 7

Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. Your result cannot contain duplicates.
Input Format
The STATION table is described as follows:
where LAT_N is the northern latitude and LONG_W is the western longitude.
Query:
MYSQL:
select distinct city
from station
where city like '%a' or city like '%e' or city like '%i'or city like '%o' or city like '%u';
MS SQL SERVER:

select  distinct city
from station
where right(city,1) in('a','e','i','o','u');

Friday, 31 March 2017

Weather Observation Station 6

Weather Observation Station 6

Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION. Your result cannot contain duplicates.

Input Format

The STATION table is described as follows:


where LAT_N is the northern latitude and LONG_W is the western longitude.
Query  for:
Mysql:
select distinct city
from station 
where city like 'a%' or city like 'e%' or city like 'i%'or city like 'o%' or city like 'u%';

or
 select distinct city 
from station
where city like '[aeiou]%';

MS SQL Server:select distinct city
from station 
where left(city,1) in('a','e','i','o','u') ;


Weather Observation Station 5

Weather Observation Station 5


Query the two cities in STATION with the shortest and longest CITY names, as well as their respective lengths (i.e.: number of characters in the name). If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically.
Input Format
The STATION table is described as follows:
where LAT_N is the northern latitude and LONG_W is the western longitude.
Sample Input
Let's say that CITY only has four entries: DEFABCPQRS and WXY
Sample Output
ABC 3
PQRS 4
Explanation
When ordered alphabetically, the CITY names are listed as ABC, DEF, PQRS, and WXY, with the respective lengths  and . The longest-named city is obviously PQRS, but there are  options for shortest-named city; we choose ABC, because it comes first alphabetically.
Note 
You can write two separate queries to get the desired output. It need not be a single query.

Query:(Oracle)
select city , length(city)
from (select city,length(city) from station where length(city)=(select min(length(city)) from station ) order by city)
where rownum=1
union
select city , length(city)
from (select city,length(city) from station where length(city)=(select max(length(city)) from station ) order by city) 
where rownum=1;
or
select city , length(city)
from (select city,length(city) from station where length(city)=(select min(length(city)) from station ) order by city)
where rownum=1;
select city , length(city)
from (select city,length(city) from station where length(city)=(select max(length(city)) from station ) order by city) 
where rownum=1;

Friday, 24 March 2017

Counting Salesperson Acccording to their Commission

import java.util.*;
class Saleperson
{
String name;
int id;
int sal;
int com;
Saleperson()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the name,id,sal");
name=sc.nextLine();
id=sc.nextInt();
sal=sc.nextInt();

}
void display()
{
System.out.println(name+"\t"+id+"\t"+sal+"\t"+com);
}
void calcom()
{
com=2000+(int)((0.09)*sal);
}
}
class Demo
{
public static void main(String args[])
{ Vector v=new Vector();
int count1=0,count2=0,count3=0,count5=0,count4=0;

for(int i=0;i<3;i++)
{
Saleperson p=new Saleperson();
p.calcom();
v.addElement(p);
int a=p.com;
if(a>=2000&&a<=2999)
count1++;
else if(a>=3000&&a<4000)
count2++;
else if(a>=4000&&a<5000)
count3++;
else if(a>=5000&&a<6000)
count4++;
else
count5++;
}
for(int i=0;i<3;i++)
{
Saleperson temp=(Saleperson)v.elementAt(i);
temp.display();
}

System.out.println("Salary Range\n"+"From\t"+"To\t"+"No of SalePersons\t");
System.out.println("2000\t"+"2999\t"+count1);
System.out.println("3000\t"+"3999\t"+count2);
System.out.println("4000\t"+"4999\t"+count3);
System.out.println("5000\t"+"5999\t"+count4);
System.out.println("6000\t"+"above\t"+count5);
}
}

/*************************************************************************output*************************************************************************************
C:\pradnya>java Demo
enter the name,id,sal
p
23
10000
enter the name,id,sal
q
45
20000
enter the name,id,sal
t
65
50000
p       23      10000   2900
q       45      20000   3800
t       65      50000   6500
Salary Range
From    To      No of SalePersons
2000    2999    1
3000    3999    1
4000    4999    0
5000    5999    0
6000    above   1
**********************************************************************************************************************************************************************/

Monday, 20 March 2017

Inheritance -Manager is a Employee and Employee is a Person

import java.util.*;
class Person
{
String name;
String address;
int age;
Person()
{
name=" ";
address=" ";
age=0;
}
Person(String name,String address,int age)
{
this.name=name;
this.address=address;
this.age=age;

}
void display()
{
System.out.println(name+"\t"+age+"\t"+address+"\t");
}

}
class Employee extends Person
{
int id;
static int count=0;
float sal;
Employee()
{
super();
id=0;
sal=0.0f;
}
Employee(String name,String address,int age,float sal)
{
super(name,address,age);
this.sal=sal;id=++count;
}
void display()
{
super.display();
System.out.println(id+"  "+sal);
}
}
class Manager extends Employee
{
float totalsal;
float incentive;
Manager()
{
super();
incentive=0.0f;
}
Manager(String name,String address,int age,float sal,float incentive)
{
super(name,address,age,sal);
this.incentive=incentive;
}
void display()
{
super.display();
System.out.println(incentive+"\t"+totalsal+"\t");
}
void totalsalary()
{
totalsal=sal+incentive;
}
}
class Demo
{
public static void main(String args[])
{
Person p=new Person();
Employee e=new Employee();

Scanner sc=new Scanner(System.in);
System.out.println("enter name ,address,age,sal,incentive");
Manager m=new Manager(sc.nextLine(),sc.nextLine(),sc.nextInt(),sc.nextFloat(),sc.nextFloat());
m.totalsalary();
m.display();
}
}

Saturday, 18 March 2017

Dynamic 0/1 Knapsack Problem

0/1 Knapsack Problem:

Problem Statement:

 Given n items from i1,i2,.....,in.
with weight w1,w2,......,wn.
and profit p1,p2,.....,pn.
 The Capacity of the bag is W.
Given a knapsack bag, we can either put an item completely in a bag or reject it you cannot put the item partially in the bag as done in Fractional Knapsack Problem ( Refer Fractional knapsack Problem: http://coders-domain.blogspot.in/2017/03/fractional-knapsack-problem.html )

Logic :we have to create a matrix where row=items+1 and columns=intial capacity +1;
         Put the entries in the first Row and first column as 0.
         The remaining entries to be filled by the following logic:
                a) if weight of the item i > current weight of bag
                      v[i,w]=v[i-1,w].
                b) if weight of item i <current weight of bag
                        v[i,w]=max(v[i-1,w],profit[i]+v[i-1,w-wt[i]])
       

code: #include<stdio.h>
#include<string.h>
int max(int i,int j)
{
if(i>j)
return i;
else
return j;
}

int main()
{
int n,j,i;
int c;
printf("enter number of items");
scanf("%d",&n);
int profit[n+1];
int weight[n+1];
for( i=1;i<n+1;i++)
{
printf("enter profit and weight of the item %d:",i);
scanf("%d%d",&profit[i],&weight[i]);
}
printf("enter capacity of knapsack");
scanf("%d",&c);
int knapsack[n+1][c+1];
for(i=0;i<n+1;i++)
{
for(j=0;j<c+1;j++)
{
if(i==0||j==0)
knapsack[i][j]=0;
else
{
if(weight[i]>j)
{
knapsack[i][j]=knapsack[i-1][j];
}
else
{
knapsack[i][j]=max(knapsack[i-1][j],profit[i]+knapsack[i-1][j-weight[i]]);
}
}
}
}
for(i=0;i<n+1;i++)
{
for(j=0;j<c+1;j++)
{
printf("%d\t",knapsack[i][j]);
}
printf("\n");
}
printf("the maximum profit earned is %d\n",knapsack[n][c]);
i=n;
j=c;
printf(" The items added are: \n");
//logic to print the items added
while(i>0 && j>0)
{
if(knapsack[i][j]!=knapsack[i-1][j])
{

printf("%d\t",i);
j-=weight[i];
i-=1;



}
else
i-=1;
}

}
/*output:
enter number of items4                                                                                                                                      
enter profit and weight of the item 1:100 3                                                                                                                  
enter profit and weight of the item 2:20 2                                                                                                                  
enter profit and weight of the item 3:60 4                                                                                                                  
enter profit and weight of the item 4:40 1                                                                                                                  
enter capacity of knapsack5                                                                                                                        
   items ->    0       1       2        3        4          5                (because capacity of bag is 5)
 |  columns
\ /       0        0       0       0        0         0          0                                                                                                                    
          1        0       0       0       100     100     100                                                                                                                  
           2       0       0       20      100     100     120                                                                                                                  
          3        0       0       20      100     100     120                                                                                                                  
          4        0       40      40      100     140     140                                                                                                                  
the maximum profit earned is 140                                                                                                                            
 The items added are:                                                                                                                                        
4       1                                                                                                                                          

*/