0% found this document useful (0 votes)
104 views7 pages

CS211-Algorithms & Data Structures: DR - Sameer M. Alrehaili

Lab 1 of the CS211 course covered hidden cells in the notebook. Lab 2 covered 14 hidden cells, and Lab 3 covered 10 hidden cells. Lab 4 introduced hash tables and covered addition, insertion, deletion of elements as well as generating random data to test hash tables. The lab then implemented hash tables using different hashing functions and techniques like linear probing to resolve collisions.

Uploaded by

abdh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views7 pages

CS211-Algorithms & Data Structures: DR - Sameer M. Alrehaili

Lab 1 of the CS211 course covered hidden cells in the notebook. Lab 2 covered 14 hidden cells, and Lab 3 covered 10 hidden cells. Lab 4 introduced hash tables and covered addition, insertion, deletion of elements as well as generating random data to test hash tables. The lab then implemented hash tables using different hashing functions and techniques like linear probing to resolve collisions.

Uploaded by

abdh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CS211-Algorithms & Data Structures

Dr.Sameer M. Alrehaili

Lab#1
↳ 21 cells hidden

Lab#2
↳ 14 cells hidden

Lab#3
↳ 10 cells hidden

Lab#04

Hash tables

Addition, insertion, deletion

Generate random data

1 import names
2 import csv
3 import random
4  
5 city = ['Abhā','Abqaiq','Al-Baḥah','Al-Dammām','Al-Hufūf','Al-Jawf','Al-Kharj (o
6 year = [2017, 2018, 2019, 2020]
7  
8 sname = []
9 sid = []
10 scity = []
11 syear = []
12 for i in range(100000):
13     sname.append(names.get_first_name())
14     sid.append(random.randint(1000000, 9999999))
15     scity.append(random.choice(city))
16     syear.append(random.choice(year))
Write the generated data into a csv le

1 # Write the generated data into a csv file
2 with open('students.csv', 'w') as file:
3   writer = csv.writer(file)
4   for i, val in enumerate(sname):
5     writer.writerow([sid[i], sname[i],scity[i],syear[i]])
6  

Read csv le insto a list

1 with open('students.csv', newline='') as f:
2   spamreader = csv.reader(f)
3   sdata = list(spamreader)
4  

Find an item without Hashing

1 import time
2 s = time.time()
3 for i in sdata:
4   if sid==1427142:
5     print('founded')
6     break
7 e = time.time()
8 print("Time is {0:.10f}".format(e-s))

Double-click (or enter) to edit

1 def hash01(k, size):
2   return k % size
3  

1 def hash01(k, size):
2   return k % size
3  
4 def hash02(k, size):
5   s=0
6   for i in k:
7     s+=ord(i)
8   return s % size
9  
10  
11 def hashtable(arr,size):
12   b=[0,0,0,0,0,0,0,0,0,0,0] 
13   for val in arr:
14     slot = hash02(val,size)
15     #b.append(val)
5      b appe d( a )
16     b[slot]=val
17   return b
18  
19  
20  
21  
22  
23 # w="Mia"
24 # s=0
25 # for i in w:
26 #   s+=ord(i)
27 # idx =s % 11
28 # print("indext = ",idx)
29  
30 # arr=[21,30,20,16,29,26,14,12,11,28,24]
31 # b = hashtable(arr, 11)
32 # print("Items", arr)
33 # print("Hash table", b)
34  
35  
36  
37 arr=['Jan','Tim', 'Mia', 'Sam','Leo','Ted','Bea','Lou','Ada','Max','Zoe']
38 b = hashtable(arr, 11)
39 print('items ', arr)
40 print('Hashtable', b)
41  

1 #!/usr/bin/env python
2 import names
3 import time
4 import csv
5  
6 # # generate random data
7 # sname=[]
8 # for i in range(10000):
9 #   sname.append(names.get_first_name())
10  
11  
12 # #Write the generated data ino a csv file
13 # with open('sss.csv', 'w') as f:
14 #   writer = csv.writer(f)
15 #   for i in sname:
16 #     writer.writerow([i])
17  
18  
19  
20  
21 # Find an item without hashing
22 with open('sss.csv', newline='')as f:
23   reader = csv.reader(f)
24   sname = list(reader)
25  
26  
27 start = time.time()
28 for i in sname:
29   if i=='Sameer':
30     print('founded')
31     break
32  
33  
34 end= time.time()
35  
36 print("Time is {0:.10f}".format(end-start) )
37  
38  
39  
40  
41  
42 def hash02(k, size):
43   s=0
44   for i in k:
45     s+=ord(i)
46   return s % size
47  
48  
49 def hashtable(arr,size):
50   b=[0] * (size+1000)
51   for val in arr:
52     slot = hash02(val,size)
53     #b.append(val)
54     b[slot]=val
55   return b
56  
57 b = hashtable(sname, len(sname))
58  
59 #Write the generated data ino a csv file
60 with open('sss2.csv', 'w') as f:
61   writer = csv.writer(f)
62   for i in sname:
63     writer.writerow([i])
64  
65  
66 # # Find an item with hashing
67 # with open('sss.csv', newline='')as f:
68 #   reader = csv.reader(f)
69 #   sname = list(reader)
70  
71  
72 # start = time.time()
73 # for i in sname:
74 #   if i=='Sameer':
75 #     print('founded')
76 #     break
77  
78  
79 # end= time.time()
80  
81 # print("Time is {0:.10f}".format(end-start) )
82  
83  

1 #!/usr/bin/env python
2  
3 def hash01(k, size):
4   return k % size
5  
6 def hash02(k, size):
7   s=0
8   for i in k:
9     s+=ord(i)
10   return s % size
11  
12  
13 def hashtable(arr,size):
14   b=[0,0,0,0,0,0,0] 
15   for val in arr:
16     slot = hash01(val,size)
17     #b.append(val)
18     b[slot]=val
19   return b
20  
21  
22 def linear_probing(arr,size):
23   b=[0,0,0,0,0,0,0] 
24   for val in arr:
25     slot = hash01(val,size)
26     #b.append(val)
27     b[slot]=val
28   return b
29  
30  
31  
32  
33 arr=[15, 14, 12, 20, 11]
34 b = linear_probing(arr, 6)
35 print("Items", arr)
36 print("Hash table", b)
37  
38  
39 for i in arr:
40   print(i, i%5)

1 #!/usr/bin/env python
2  
3 def hash01(k, size):
4   return k % size
5  
6 def hash02(k, size):
7   s=0
8   for i in k:
9     s+=ord(i)
10   return s % size
11  
12  
13 def hashtable(arr,size):
14   b=[0,0,0,0,0,0,0] 
15   for val in arr:
16     slot = hash01(val,size)
17     #b.append(val)
18     b[slot]=val
19   return b
20  
21  
22 def linear_probing(arr,size):
23   b=[None,None,None,None,None,None] 
24   for val in arr:
25     slot = hash01(val,size)
26     while True:
27       if b[slot] is None:
28         b[slot]=val
29         break
30       else:
31         if slot==size-1:
32           slot=0
33         else:
34           slot+=1
35  
36   return b
37  
38  
39  
40  
41 arr=[15, 14, 12, 20, 11]
42 b = linear_probing(arr, 5)
43 print("Items", arr)
44 print("Hash table", b)
45  
46  
47  

1 import csv
2 from google.colab import files
3 uploaded = files.upload()
4 import io
5 import pandas as pd
6  
7 df = pd.read_csv(io.BytesIO(uploaded['sss.csv']), parse_dates=True)
8 arr = df.values.tolist()
9 print(arr[1])
10  
11  
12  
13  
14  

You might also like