Q. Write a Python program by considering a mapping of list of countries and their capital cities such as:
CountryCapital = {'India': 'New Delhi’, 'UK': 'London', 'France': 'Paris', 'Switzerland': 'Berne', 'Australia': 'Canberra'}

Let us presume that our hash function is the length of the Country Name. Take two lists of appropriate size: one for keys (Country) and one for values (Capital). To put an element in the hash table, compute its hash code by counting the number of characters in Country, then put the key and value in both the lists at the corresponding indices. For example, India has a hash code of 5. So, we store India at the 5th position (index 4) in the keys list, and New Delhi at the 5th position (index 4) in the values list and so on. So that we end up with:


Hash index = length of key - 1

List of Keys

List of Values

0

None

None

1

UK

London

2

None

None

3

Cuba

Havana

4

India

New Delhi

5

France

Paris

6

None

None

7

None

None

8

Australia

Canberra

9

None

None

10

Switzerland

Berne


Now search the capital of India, France and the USA in the hash table and display your result.
Answer :-


def hashFind(key , hashTable , dic):
    if (hashTable[ len (key ) % 10 -1 ] == key):
        return "Capital of ", key , "is" , dic [ key ]
    else:
        return key,"is not present in the hash table"

hashTableCountry = [None, None, None, None, None, None, None, None, None, None, None]
hashTableCapital = [None, None, None, None, None, None, None, None, None, None, None]

print("We have created a hash Table of 11 positions:")
print(hashTableCountry)

CountryCapital = {'India': 'New Delhi', 'UK': 'London', 'France': 'Paris', 'Switzerland': 'Berne', 'Australia': 'Canberra'}

key = list ( CountryCapital.keys() )
print(key)
L = [ ]
for i in key:
    L.append( len( i ) )

print("The given list is", L[::] )

for i in range(0,len(L)):
    for j in key:
        if len( j ) == L[i]:
            hashTableCountry [ L[i] -1 ] = j
            hashTableCapital [ L[i] -1] = CountryCapital [ j ]

print("The hash table contents are: " )

for i in range( 0,len( hashTableCountry )):
    print("hashindex=",  i, " , Key =", hashTableCountry[ i ], " , value = ", hashTableCapital [i] )

print( hashFind( "India", hashTableCountry, CountryCapital ))
print( hashFind( "France", hashTableCountry, CountryCapital ))
print( hashFind( "USA", hashTableCountry, CountryCapital ))

Output:-

We have created a hash Table of 11 positions:
[None, None, None, None, None, None, None, None, None, None, None]
['India', 'UK', 'France', 'Switzerland', 'Australia']
The given list is [5, 2, 6, 11, 9]
The hash table contents are:
hashindex= 0  , Key = None  , value =  None
hashindex= 1  , Key = UK  , value =  London
hashindex= 2  , Key = None  , value =  None
hashindex= 3  , Key = None  , value =  None
hashindex= 4  , Key = India  , value =  New Delhi
hashindex= 5  , Key = France  , value =  Paris
hashindex= 6  , Key = None  , value =  None
hashindex= 7  , Key = None  , value =  None
hashindex= 8  , Key = Australia  , value =  Canberra
hashindex= 9  , Key = None  , value =  None
hashindex= 10  , Key = Switzerland  , value =  Berne
('Capital of ', 'India', 'is', 'New Delhi')
('Capital of ', 'France', 'is', 'Paris')
('USA', 'is not present in the hash table')

>>>

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post