Question 5
OCR Computer Science June 2019 Paper 1A level23 marks
On the specification
- (a)
- 1.4 Data types, data structures and algorithms
- (b)
- 1.4 Data types, data structures and algorithms
- (c)
- 1.4 Data types, data structures and algorithms2.2 Problem solving and programming
- (d)
- 1.4 Data types, data structures and algorithms
Question text
5 A programmer is writing software for a firewall. She is writing code so that it keeps a track of websites that users are permitted to visit. The software stores the websites’ addresses along with details about who can view them and when. The following data is also stored about each website: • Access level needed (1-4) • If it is available all the time (true) or just lunch times and out of work hours (false). So a website which is available to users of access level 2 and above, all the time, would have the details [2, true] stored. A website accessible to users of access level 3 and above, only outside of work hours, would have the details [3, false] stored. (a) State the name of a data structure that could be used to store a single site’s details. [1] The address of each website, along with the relevant details, are stored in a hash table. The hash table’s hash function is carried out on the website’s address (which acts as the key). The hash function works in the following way: 1. Discard the characters up to and including the first dot. 2. Discard the characters including and to the right of the remaining leftmost dot. 3. Convert the characters to uppercase. 4. Add the ASCII values of the characters together. A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 For example www.ocr.org.uk gets hashed in the following manner: Step 1: ocr.org.uk Step 2: ocr Step 3: OCR Step 4: 79+67+82 = 228 giving a hashed value of 228. (b) State what hashed value would be given by the website www.foo.co.uk [1] (c) Complete the function hash which takes in a string and returns the hashed value. You can assume you have access to the following three functions. • asc() – this takes in a character and returns its ASCII value. For example asc("A") returns 65. • locate() – this takes in a string and character and returns the location of the first instance of the character (with the string starting at character 0). For example locate("electricity","c") returns 3. • upper() – this takes in a string and returns the UPPERCASE version. For example upper("hello") returns "HELLO". You should also assume that all given website names use letters but no numbers or symbols. You will be given credit for the readability of your code. function hash(siteName) endfunction [5] A flaw with the current hash function is it tends to generate lots of collisions (addresses that compute to the same hash). Below is a diagram of part of the hash table. The address www.rnd.com with details [2, true] is being added to the hash table. (d) Explain how a hash table can be used to handle collisions, referring to the example below. 227 228 www.ocr.org.uk : [1, true] 229 230 www.ppf.nz : [2, false] 231 232 www.ntf.biz : [4, true] 234 235 [4] The hash function is changed so there are no longer high numbers of collisions. During busy periods the firewall is expected to check several addresses a second. It is anticipated that roughly 10 new addresses will be added to a whitelist (list of acceptable addresses) each day. There is a debate as to whether a hash table (with the new hash function) is the best approach, or if the whitelist would be better stored in a linked list. (e) *Discuss whether a hash table or linked list is better to store acceptable websites. You should compare how each structure can be searched and has data added and come to a recommendation as to which is better for the whitelist. [12]