Skip to content
Foxipa
Code Completion 7 Challenges

Complete Python custom subscription, Sequence, Mapping, and Set code

Practice subscription, assignment, deletion, reverse iteration, and implementing the collections.abc Sequence, Mapping, and Set contracts.

Advanced ~12 min read

Challenge 1

Implement subscription so the object returns the stored item at the requested index.

python Your answer
class Shelf:
    def __init__(self, items):
        self.items = list(items)

    def __getitem__(self, index):
        

shelf = Shelf(["A", "B", "C"])
print(shelf[1])

Press Ctrl⌘Enter to check your answer

Challenge 2

Allow bracket assignment to replace the stored item at an index.

python Your answer
class Scores:
    def __init__(self, values):
        self.values = list(values)

    def __setitem__(self, index, value):
        

scores = Scores([10, 20, 30])
scores[1] = 25
print(scores.values)

Press Ctrl⌘Enter to check your answer

Challenge 3

Make del queue[0] remove the first stored item.

python Your answer
class Queue:
    def __init__(self, items):
        self.items = list(items)

    def __delitem__(self, index):
        

queue = Queue(["first", "second"])
del queue[0]
print(queue.items)

Press Ctrl⌘Enter to check your answer

Challenge 4

Provide reverse iteration using the stored list.

python Your answer
class Timeline:
    def __init__(self, events):
        self.events = list(events)

    def __reversed__(self):
        

timeline = Timeline(["start", "middle", "end"])
print(list(reversed(timeline)))

Press Ctrl⌘Enter to check your answer

Challenge 5

Complete the three required Mapping methods. Iteration must yield the mapping keys, and length must count the keys.

python Your answer
from collections.abc import Mapping

class SectionIndex(Mapping):
    def __init__(self, entries):
        self._entries = dict(entries)

index = SectionIndex({"intro": 1, "api": 3})
print(index["api"])
print(list(index.keys()))
print(index.get("missing", 0))

Press Ctrl⌘Enter to check your answer

Challenge 6

Implement the two required Sequence methods. The inherited count(), index(), and reversed() operations should work with the stored tuple.

python Your answer
from collections.abc import Sequence

class TopicSequence(Sequence):
    def __init__(self, topics):
        self._topics = tuple(topics)

topics = TopicSequence(["syntax", "mapping", "syntax"])
print(topics.count("syntax"))
print(topics.index("mapping"))
print(list(reversed(topics)))

Press Ctrl⌘Enter to check your answer

Challenge 7

Implement the three required Set methods so the inherited intersection and disjointness operations work. The constructor accepts any iterable.

python Your answer
from collections.abc import Set

class FeatureSet(Set):
    def __init__(self, features):
        self._features = frozenset(features)

features = FeatureSet(["search", "export", "search"])
other = FeatureSet(["export", "share"])
print(sorted(features & other))
print(features.isdisjoint({"share"}))
print(len(features))

Press Ctrl⌘Enter to check your answer