INLAB-7

INLAB-7

Q.In a small village library, books are lended to the members for a period of one week. Details such as name of the book, author, date of purchase, accession number and availability status are stored for each book. Given the details of the 'n' books sorted by the accession number of book and an accession number to search, develop an algorithm and write the Python code to perform binary search in the given data. Print 'Found' or 'Not Found' appropriately.
Input Format
First line contains the number of books in the library, n
Next n*5 lines contains the details of the book in the following order:
name of the book
author of the book
date of purchase of the book
accession number of the book
availability status of the book
Next line contains the accession number of book to be searched 'n'
Output Format
Print Found or Not Found







INPUT:

First line contains the number of books in the library, n
Next n*5 lines contains the details of the book in the following order:
name of the book
author of the book
date of purchase of the book
accession number of the book
availability status of the book
Next line contains the accession number of book to be searched 'n'

OUTPUT:

Print Found or Not Found

PROGRAM:

n=int(input())
l=[]
l1=[]
l2=[]
l3=[]
l4=[]
for i in range(n):
    name=input().rstrip()
    author=input().rstrip()
    date=input().rstrip()
    accnum=int(input())
    avail=input().rstrip()
    l.append(accnum)
    l1.append(name)
    l2.append(author)
    l3.append(date)
    l4.append(avail)

l.sort()

entry=int(input())
i = 0
j = len(l)
while i < j:
    middle = int((i + j) // 2)
    if entry > l[middle]:
        i = middle + 1
    else:
        j = middle

if i==len(l):
    print("Not Found")
else:
    print("Found")

PROCESSING INVOLVED:

i=0
j=len(j)
while i < j:
    middle = int((i + j) / 2)
    if entry > l[middle]:
        i = middle + 1
    else:
        j = middle

if i==len(l):
    print("Not Found")
else:
    print("Found")


PSEUDO CODE:


READ n
START 5 empty lists, l, l1, l2, l3, l4
FOR i in n
    READ name
    READ author
    READ date
    READ accnum
    READ avail
    ADD name in l1, author in l2, date in l3, avail in l4 and accnum in l
END FOR
SORT list l
READ entry
LET i=0
j= LENGTH of l
WHILE i<j
    middle=(i+j)/2
    IF entry>l[middle]
        i=middle+1
    ELSE
        j=middle
    END IF
IF i=LENGTH(l)
    PRINT "Not Found"
ELSE
    PRINT "Found"
END IF

Comments

Popular posts from this blog

MATLAB WEEK-2

PRACTICE PROBLEM 8

Practice Problem 6-Isomorphic Numbers