Artificial Intelligence Programs For Programming in Python

Traveling Salesman Problem ( TSP )] Artificial Intelligence Programs For Master Leval Programming in Python from iter tools import permutations def distance(point1, point2): >>> distance([3,4],[0,0]) 5.0 >>> distance([3,6],[10,6]) 7.0 return ((point1[0] - point2[0])**2 + (point1[1] - point2[1])**2) ** 0.5 def total_distance(points): all the points are in the given order. >>> total_distance([[1,2],[4,6]]) 5.0 >>> total_distance([[3,6],[7,6],[12,6]]) 9.0 return sum([distance(point, points[index + 1]) for index, point in enumerate(points[:-1])]) def travelling_salesman(points, start=None): Time complexity is O(N!), so never use on long lists. >>> travelling_salesman([[0,0],[10,0],[6,0]]) ([0, 0], [6, 0], [10, 0]) >>> travelling_salesman([[0,0],[6,0],[2,3],[3,7],[0.5,9],[3,5],[9,1]]) ([0, 0], [6, 0], [9, 1], [2, 3], [3, 5], [3, 7], [0.5, 9]) """ if the start is None: start = points[0] return min([perm for perm

MongoDB Practice Questions Aggregation,Array,Dealing with NULL values


MongoDB Practice Questions Aggregation, Array, Dealing with NULL values



Working With Aggregation in MongoDB

Insert Documents in emp collection
> db.emp.insert([
{eno:1,ename:"Sachin",sal:6000,desig:"manager",dept:"Purchase"},
{eno:2,ename:"Dhoni",sal:5000,desig:"manager",dept:"Sales"},
{eno:3,ename:"Kohli",sal:3000,desig:"clerk",dept:"Admin"},
{eno:4,ename:"Dravid",sal:4000,desig:"manager",dept:"Purchase"},
{eno:5,ename:"Bumrah",sal:3500,desig:"manager",dept:"Sales"},
{eno:6,ename:"Jadeja",sal:2500,desig:"clerk",dept:"Admin"},
{eno:7,ename:"Rohit",sal:4000,desig:"manager",dept:"Sales"}])

List all emplyees name with their salary who are in admin department.

> db.emp.aggregate([{$match:{dept:'Admin'}},{$project:{ename:1,sal:1,_id:0}}])

List all emplyees name with their salary and designation who are in Purchase department.
> db.emp.aggregate([{$match:{dept:'Purchase'}},{$project :{ename:1,desig:1,sal:1,_id:0}}])

List all employees no, name, salary who are in the Sales department and Designation is Manager.
> db.emp.aggregate([{$match:{dept:'Sales',desig:'manager'}},{$project : {eno:1,ename:1,sal:1,_id:0}}])

calculate Annual salary of each emplyees
>db.emp.aggregate([{$project:{eno:1,ename:1,_id:0,salary:{$multiply:['$sal',12]}}}])

Convert the names of the emplyees in Upper case and display only no and name of each emplyees.
>db.emp.aggregate([{$project:{eno:1,ename:1,_id:0,Employee:{$toUpper:['$ename']}}}])

Convert the names of the emplyees in Lower case and display only no and name of each emplyees.
>db.emp.aggregate([{$project:{eno:1,ename:1,_id:0,Employee:{$toLower:['$ename']}}}])

Display each document of the collection emplyee the total salary of each emplyee should be displayed department wise.
>db.emp.aggregate([{$project:{eno:1,ename:1,_id:0,Dept_desig:{$concat:['$dept','-','$desig']}}}])

Findout Total Salary manager designation wise.
> db.emp.aggregate([{$match:{desig:'manager'}},{$group:{ _id:"$dept",Tot_Sal:{$sum:"$sal"}}}])

Find the Document from emp Collection where designation is manager data should be grouped department wise and find the total salary of each employee departmentwise. and then display the departments have total salary graterthan 6200.
> db.emp.aggregate([{$match:{desig:"manager"}},{$group:{_id:"$dept",total:{$sum:"$sal"}}},{$match:{total:{$gt:6200}}}])

Find The Average Salary of each department in emp collection.
> db.emp.aggregate([{$group:{_id:"$dept",avg:{$avg:"$sal"}}}])


Find The MAX Salary of each department in emp collection.

> db.emp.aggregate([{$group:{_id:"$dept",Max:{$avg:"$sal"}}}])


Find The MIN Salary of each department in emp collection.
> db.emp.aggregate([{$group:{_id:"$dept",Min:{$avg:"$sal"}}}])

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
Dealing with NULL Values
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
update The Document with null value in desig field where name is kohli.
> db.emp.update({ename:"Kohli"},{$set:{desig:null}},{multi:true}) 

To search for Null value to desig field
> db.emp.find({desig:null})
 
To remove design Field having Null Values in emp Collection.
> db.emp.update({enm:"Kohli"},{$unset:{desig:null},{multi:1}})

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
Arrays in MongoDB
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

=>insert single document>db.foods.insert({_id:1,fruits:'banana','apple','cherry']})


=>2. insert 5 records 
>db.foods.insert([
{_id:1,fruits:['banana','apple','cherry']},{_id:2,fruits:['orange','watermelon','mango']},{_id:3,fruits:['pinaple','strawberry','grapes']},{_id:4,fruits:['banana','strawberry','grapes']},{_id:5,fruits:['orange','grapes']}])

3. display all document in food collection>db.foods.find()

4. display those documents from in food collection which has fruits array consituted of banana,apple,cherry > db.foods.find({fruits:['banana','apple','cherry']})

NOTE: No [] braces are required for single value

5. display those documents From food Collection which has fruits array having a banana as an element.> db.foods.find({fruits:'banana'})


NOTE: if Anywhere in Array Index Value write into the ""(Double Quote) eg. "arrnm.1"

6.display those documents From food Collection which have the Fruits array having grapes in First index position.
> db.foods.find({"fruits.1":'grapes'})


7.Find those documents From food Collection where grapes is present in second index position of fruits array.
db.foods.find({"fruits.2":'grapes'})


8.display those documents From food Collection where size of array is 2.
> db.foods.find({fruits:{$size:2}})

9.Find those documents From food Collection with _id:1 and display first 2 elements From the array Fruits.
9.1> db.foods.find({_id:1},{fruits:{$slice:2}}) 


For Multiple value starting to ending position.
9.2> db.foods.find({_id:1},{fruits:{$slice:[1,2]}}) # starting to ending start from 1 and then after take 2 elemetns

Comments

Post a Comment

Popular posts from this blog

MongoDB Exercises Practice Solution Exercise First

How to create Pagination with PHP and MySql - Codingpoint

Simple Basic Commands For MongoDB