ISF DP Computer Science

Exam Review - Snakes #

This page contains the Snake pseudocode problem seen on the 2025 April Exam.

Completing the problems as coding exercises allows you to receive real-time feedback on whether your algorithm works.

Open up the pseudocode compiler in a new tab and use it to complete the exercises below.



A safari is home to a variety of snakes.

The safari organization maintains a database of 100 snake species which are categorized by their toxicity. The database is read into a collection SNAKES, as follows:

Viper, 7.2, Krait, 8.5, Mamba, 6.1, Ribbon, 0.0, Cobra, 9.0, Adder, 4.8, Python, 2.3, Garter, 0.0

Part A #

๐Ÿ’ป Construct an algorithm in pseudocode to read the data from SNAKES and store the names of the snakes in a one-dimensional string array, NAME, and the toxicity levels of the snakes into another one-dimensional string array, TOX.

Here is the starter code for you to copy/paste into your editor. Write your code where it says YOUR CODE GOES HERE:

// set up
SNAKES = new Collection()
SNAKES.addItem("Viper")
SNAKES.addItem(7.2)
SNAKES.addItem("Krait")
SNAKES.addItem(8.5)
SNAKES.addItem("Mamba")
SNAKES.addItem(6.1)
SNAKES.addItem("Ribbon")
SNAKES.addItem(0.0)
SNAKES.addItem("Cobra")
SNAKES.addItem(9.0)
SNAKES.addItem("Adder")
SNAKES.addItem(4.8)
SNAKES.addItem("Python")
SNAKES.addItem(2.3)
SNAKES.addItem("Garter")
SNAKES.addItem(0.0)
NAME = ["", "", "", "", "", "", "", ""] // create "empty" array
TOX = [-1,-1,-1,-1,-1,-1,-1,-1] // create "empty" array


"YOUR CODE GOES HERE"


// testing if it worked
output "names array"
loop I from 0 to 7
    output NAME[I]
end loop
output "tox array"
loop I from 0 to 7
    output TOX[I]
end loop

Once you’ve given it a solid try on your own, you can reference this solution:



Part B #

๐Ÿ’ป Construct an algorithm in pseudocode to sort the contents of the array NAME in alphabetical order using the selection sort algorithm. The indexes for the corresponding data in the two parallel arrays must remain the same after sorting.
๐Ÿ‘พ ๐Ÿ’ฌ

In pseudocode, you can compare two strings alphabetically using < and >. For example,

"Apple" < "Banana"
true
"Apple" > "Banana"
false

Here is the starter code for you to copy/paste into your editor. Write your code where it says YOUR CODE GOES HERE:

NAME = ["Viper", "Krait", "Mamba", "Ribbon", "Cobra", "Adder", "Python", "Garter"]
TOX = [7.2, 8.5, 6.1, 0.0, 9.0, 4.8, 2.3, 0.0]

output "Before sorting"
printNums(NAME)
printNums(TOX)



"YOUR CODE GOES HERE"



output "After sorting"
printNums(NAME)
printNums(TOX)

method printNums(ARR)
   loop C from 0 to 7
      output ARR[C]
   end loop
   output "========"
end method

Once you’ve given it a solid try on your own, you can reference this solution:



Part C #

๐Ÿ’ป Construct a method in pseudocode that takes the name of a snake as a parameter, searches for the name in the NAME array using a binary search, and returns the toxicity level of the snake. If the inputted name does not occur in the NAME array, it returns -1.
๐Ÿ‘พ ๐Ÿ’ฌ

This question requires integer division, but the pseudocode compiler doesn’t do it properly.

What you would write on an exam:

7 div 3

What the pseudocode compiler expects:

div(7,3)

Here is the starter code for you to copy/paste into your editor. For the method definition and call, choose which version to use (recursive or non-recursive) and delete unneeded code.

Write your code where it says YOUR CODE GOES HERE:

//setup
TOX = [4.8,9,0,8.5,6.1,2.3,0,7.2]
NAME = ["Adder","Cobra","Garter","Krait","Mamba","Python","Ribbon","Viper"]

method toxicityLookup(TARGET) //SL non-recursive approach
method toxicityLookup(TARGET, LOW, HIGH) //HL recursive approach



"YOUR CODE GOES HERE"



end method

//testing method
output "Type the ID number that you wish to find"
input USERTARGET

RESULT = toxicityLookup(USERTARGET) //SL non-recursive approach
RESULT = toxicityLookup(TARGET, LOW, HIGH) //HL recursive approach

if RESULT >= 0 then
    output USERTARGET , ":" , RESULT
else
    output USERTARGET , " was not found"
end if

Once you’ve given it a solid try on your own, you can reference this solution:

Once you’ve given it a solid try on your own, you can reference this solution:



Deliverables #

โšกโœจ

Paste all your code in your Code Log