PPR Code Snippets

A PPR is created to assist in responding to the written response prompts on exam day. The goal of the PPR is to submit required portions of your code by capturing and pasting program code segments you developed during the administration of this task.

PPR Submission Requirements:

  • Your code segments should not include any comments.
  • Screen captures should not be blurry.
  • Text should be at least 10-point font size.

PPR Snippet Requirements:

  • Two Procedure “Program Codes”
    • The first program code segment must be a student-developed procedure that:
      • Defines the procedure’s name and return type (if necessary)
      • Contains and uses one or more parameters that have an effect on the functionality of the procedure
      • Implements an algorithm that includes sequencing, selection, and iteration
    • The second program code segment must show where your student-developed procedure is being called in your program.
  • Two List “Program Codes”
  • The first program code segment must show how data have been stored in the list.
  • The second program code segment must show the data in the same list being used.

Function (with parameters, if-else statement, and Sequencing + Selection + Iteration)

alt text

Explaination:

  • Procedure: Called “restore”
  • Return type: List - List of restored Vote objects.
  • Parameter: Data (list) - List of dictionaries containing vote data.
  • Sequencing: First defines an empty dictionary for restored classes. Then, iterates through votes to determine if it is already existing. Then, if it already exists, it will fix inconsistences. Otherwise, it will add the vote.
  • Selection: If a vote already exists, it updates the record instead of creating duplicates. SELECTS only erased votes using a if statement.
  • Iteration: The function iterates over database queries to maintain vote consistency. ITERATES using an for, ensuring that every vote is properly handled and updated as necessary.

Call to Function

alt text

Explaination:

  • This procedure is called when the user wants to restore data. It will accept the vote data as an argument and procede.

List Creation

alt text

Explaination:

  • The upvotes and downvotes lists are populated by filtering all votes based on their type.
  • Each vote’s data is added to the corresponding list using list comprehension.

List Process

alt text

Explaination:

  • In this code snippet, list comprehension is used within the following lines:
data.upvotes.forEach(vote => voteData.upvotes.push(parseInt(vote.post_id, 10)));
data.downvotes.forEach(vote => voteData.downvotes.push(parseInt(vote.post_id, 10)));

Explanation of List Comprehension: The list comprehension is not exactly written in the traditional syntax (like Python’s list comprehensions), but the .forEach() method is used in a similar way to loop through the arrays data.upvotes and data.downvotes.

  1. Purpose of upvotes and downvotes arrays:
    • upvotes and downvotes are arrays in the data object, each containing vote objects. Each vote object has a post_id that represents a vote on a specific post.
  2. What is happening inside the .forEach() method:
    • For each vote in the upvotes array, the post_id of the vote is parsed as an integer (parseInt(vote.post_id, 10)) and then pushed into the voteData.upvotes array.
    • The same process occurs for the downvotes array, where the post_id is parsed and pushed into the voteData.downvotes array.

Use of List: The lists voteData.upvotes and voteData.downvotes are used to store the post IDs of the upvoted and downvoted posts, respectively. This structure keeps track of which posts received upvotes and which ones received downvotes. By the end of the loop, voteData will contain two arrays:

  • voteData.upvotes: Contains the post IDs of all posts that received upvotes.
  • voteData.downvotes: Contains the post IDs of all posts that received downvotes.

I have included all of this information on the CPT guide that I created for my team before the final check: https://illuminati1618.github.io/prism_frontend/cptguide