Preview

26 - Coding Challenge #4 & Past Paper Simulation

 1. Please work through the following presentation which takes you through solving a programming challenge (using for loops and 2d arrays (lists) step by step. Select 'True' when done. (1 mark)

  FALSE

  TRUE

 2. What is the name of the array in this program?(1 marks)
global numbers
#An array/list with 10 spaces each corresponding to a number
numbers=["","","","","","","","","",""]
numbers[3]="A"
print(numbers[3])

 3. Referring to the question above, what is the output of this program? (1 mark)

 4. What is the output from the following program (1 mark)
global numbers
#An array/list with 10 spaces each corresponding to a number
numbers=["","","","","","","","","",""]
if numbers[0]=="":
  print("Empty")
else:
  print("Taken")

 5. In the following code, the list called numbers has been declared as 'global'. What does this refer to? (1 mark)
global numbers
#An array/list with 10 spaces each corresponding to a number
numbers=["","","","","","","","","",""]

def makemove():
  player=input("Enter Player Name:")
  move=int(input("Enter your move:"))
  if numbers[move-1]=="":
    print("Succesfully stored your move")
    freecount()
    #In the next line we assign the player name (e.g. A B or C to the place in the array)
    numbers[move-1]=player
    print(numbers)
    freecount()
    makemove()
    

 6. Assuming this is the whole of the code, Line 10 and 14 of this program will fail to run/execute. Can you explain why? (1 mark)
global numbers
#An array/list with 10 spaces each corresponding to a number
numbers=["","","","","","","","","",""]

def makemove():
  player=input("Enter Player Name:")
  move=int(input("Enter your move:"))
  if numbers[move-1]=="":
    print("Succesfully stored your move")
    freecount()
    #In the next line we assign the player name (e.g. A B or C to the place in the array)
    numbers[move-1]=player
    print(numbers)
    freecount()
    makemove()
    
  else:
    print("Sorry, that number has been taken")
    makemove()

 7. In the above program, a user enters their player name (e.g. A) and then enters the number 15. What is the output? Be specific (1 mark)

 8. In the following code, refer to line 8. What is the value of move-1, if the input for move on line 7 is 3? (1 mark)
global numbers
#An array/list with 10 spaces each corresponding to a number
numbers=["","","","","","","","","",""]

def makemove():
  player=input("Enter Player Name:")
  move=int(input("Enter your move:"))
  print("Your move was:",move-1)


makemove()

 9. The array (python language) is 0 index based. If the user entered move=3, and we wanted to store the player's name in the third available place in the list, this would be equivalent to index postion:

 10. This function counts up the number of free spaces in the list. What needs to go in line 5? (1 mark)
def freecount():
  free=0
  for i in range(len(numbers)):
      if numbers[i]=="":
        ?
  print("Remaining count of free numbers:",free)
makemove()