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                                                                                                                                          

*/

Thursday 16 March 2017

Solution to Fractional Knapsack

Fractional Knapsack
for Understanding this program please refer my previous post: Fractional Knapsack Problem (Meaning &Explaination given)

Source code in C:
Topic:Fractional Knapsack
#include<stdio.h>
#include<stdlib.h>
struct knapsack //creating structure for Knapsack
{
int item;//to store element numbers
int weight;//to store weight of the item
int profit;//to store profit associated with the item
    double ratio;// to store profit/weight ratio
};
void main()
{
        int i, n;
        printf("enter no of elements");
        scanf("%d",&n);
        struct knapsack k[n];double x[n];
for( i=0;i<n;i++)
{
        printf("enter weight and value of an item");
        scanf("%d%d",&k[i].weight,&k[i].profit);
       k[i].ratio=k[i].profit/k[i].weight;
       k[i].item=i;
}
struct knapsack temp;
int j;
for(i=0;i<n;i++)//sorting items in descending order of profit/weight ratio
{
for( j=0;j<n-1-i;j++)
{
if(k[j].ratio<k[j+1].ratio)
{
temp=k[j];
k[j]=k[j+1];
k[j+1]=temp;
}


}
}
int capacity;double total_profit=0.0;
int map;
printf("enter the capacity");
scanf("%d",&capacity);
for( i=0;i<n;i++)
{
if(capacity>=k[i].weight)//if true
{

capacity-=k[i].weight;// decreasing the capacity of the bag
total_profit+=k[i].profit;//incresing the profit
int q=k[i].item;//adding item completly
x[q]=1;
}
else if(capacity<k[i].weight && capacity!=0)

{

      double fractprofit=capacity*k[i].ratio;//partial profit of the item added

total_profit=total_profit+fractprofit;

int y=k[i].item;

double fractweight=(double)capacity/(double)k[i].weight;

x[y]=fractweight;

break;


}
else

   break;

}
printf("total profit is: %f\n",total_profit);
for( i=0;i<n;i++)
{
printf("%f\t",x[i]); //indicates that the i th item is added completly(ie 1) or                                                                          //partially(ie.ratio)


}
}

/* output:
enter no of elements4                                                                                                                                        
enter weight and value of an item50 100                                                                                                                      
enter weight and value of an item20 100                                                                                                                      
enter weight and value of an item30 90                                                                                                                        
enter weight and value of an item10 80                                                                                                                        
enter the capacity70                                                                                                                                          
total profit is: 290.000000                                                                                                                                  
0.200000        1.000000        1.000000        1.000000                                                                                              
*/                                                 

Wednesday 8 March 2017

Fractional Knapsack Problem

You might have heard about Knapsack Problem but find it difficult to Implement it?
Then you are on right post. Today you are gonna learn what is Fractional Knapsack Problem in an easy way.So let's get started.


What does Knapsack Problem say?
Statement: You have being given a bag of capacity C.Where in if you try to add extra weight then the Bag will burst i.e total weight < = C. You have given n  items of individual weights and have some different profit.Your job is to insert items in the bag such that the profit is maximum.

Special case:  if the weight of the item is greater than the capacity of the bag then the items can be placed in the bag partially.

let's see an example,
The capacity C of bag is 70. and the items are given in the following table along with the profit and weight associated with it.

                ITEM NO
              WEIGHT
         PROFIT
                   1
                  50
                100
                   2
                  20
                100
                   3
                  30
                 90
                   4
                  10
                 80

 step 1:Evaluate the vale of PROFIT/WEIGHT ratio.

                ITEM NO
              WEIGHT
         PROFIT
PROFIT/WEIGHT
                   1
                  50
                  100
             2
                   2
                  20
                  100
             5
                   3
                  30
                  90
             3
                   4
                  10
                  80
             8

 step 2:Arrange in descending order of PROFIT/WEIGHT ratio.

                ITEM NO
              WEIGHT
         PROFIT
PROFIT/WEIGHT
                   4
                    10
                   80
                8
                   2
                   20
                  100
               5
                   3
                   30
                   90
               3
                   1
                  50
                  100
               2


 let profit=0, C =70(given).
keep on adding items in the mentioned order in table 3. such that
weight of item<capacity of bag at that point(C)
As an item is added the capacity decreases and the profit increases.

As 10<70
profit=80; capcity=capacity-weight of item added.
C=70-10=60;
as 20<60
C=60-20=40
profit=180;
as 30<40
profit=270;
C=40-30=10;

now 50>10
if the weight of the item is greater than the capacity of the bag then the partial part of item is added in the bag.
profit= profit+(capacity of bag/weight of object) *profit of that item.
C=0;

profit=profit+(10/50)*100;
         =290
hence the total profit(max ) earned is 290
This was the Fractional Knapsack problem .
hope you understood the concept well.

FOR SOLUTION PROGRAM  of  Fractional Knapsack please refer my post
Solution to Fractional Knapsack.