INSERT with exam rules
Intermediate · Write the code · from Exam Pseudocode in Python: Every Construct Side by Side
From the lesson: Exam Pseudocode in Python: Every Construct Side by Side
The Computer Science Principles exam writes its code questions in its own pseudocode, so the fastest way to read them is to know exactly how each line would look in Python, and where the two languages quietly disagree.
Read the lessonWrite exam_insert(a_list, i, value) that behaves like the exam's INSERT(aList, i, value): it puts value at position i, counting from 1, and shifts the later elements right. Return the list.
If i is not a valid position in the list (less than 1 or more than its length), return the string "error" instead and leave the list unchanged.
def exam_insert(a_list, i, value):
# Your code here
pass
What Check runs
Your code runs first, then each of these lines, and 2 more that stay hidden. The output must match exactly.
| Runs | Expected output |
|---|---|
| exam_insert(['a', 'b', 'c'], 1, 'x') | ['x', 'a', 'b', 'c'] |
| exam_insert(['a', 'b', 'c'], 3, 'x') | ['a', 'b', 'x', 'c'] |
| exam_insert(['a', 'b', 'c'], 0, 'x') | 'error' |
More Pseudocode exercises
- Translate a procedureBeginner