Tree Walker
This summer the 4th annual PyBay conference dedicated to Python development was held. One of the speakers was Raymond Hettinger , a member of the Python Software Foundation (PFS) since 2003, a member of the PFS board of directors from 2008 to 2012. Raymond's merits in the PFS are very significant: he worked on creating sets (data type), generator expressions, "collections" and "itertools" modules, as well as an ordered dict. He's also well known for his contributions to the Python Cookbook.
Raymond's presentation was called "The Mental Game of Python". You can watch the entire recording of the presentation here . Raymond demonstrated using various strategies to simplify the development process and make the code simpler and therefore better. This mission is a variation of a data tree traversal task used by Raymond to demonstrate an incremental development approach.
In computer science, a tree is an abstract data type that simulates a hierarchical tree structure. All tree elements are called nodes . The lines connecting elements (or nodes) are called branches and nodes without children are called leaves or end-nodes . The topmost node in a tree is called the root node . Depth is the distance between a node and the root. Additionally, every node in a tree can be seen as the root node of the subtree rooted at that node.
In this mission you are given an arbitrary finite tree and a target. In case the target's type is
int
or
str
your task is to calculate how many leaves of the tree are equal to the target.
Otherwise, if the target's type is
list
or
dict
you need to calculate the number of subtrees which are equal to the target.
For a better understanding, have a look at the illustrations below (visual order matters for lists).
If encounter difficulties, try to watch this part of the
video
exposing the use of the incremental development approach.
Input: Two arguments:
- the first is an arbitrary finite tree data structure
- the second is a target , any type of [int, str, list, dict]
Output: The number (integer) of the leaves or subtrees that are equal to the given target.
Example:
tree_walker(tree=[1, "2", 3, [[3], 1, {1: "one"}]], target=1) == 2 #example #1 tree_walker(tree={"one": 1, "two": [{1: "one", 2: "two"}, 1, "1", "one"]}, target=1) == 2 #example #2 tree_walker(tree={"one": [1, 2], "two": [{1: "one", 2: "two"}, [1, 2], "1", "one"]}, target=[1, 2]) == 2 #example #3 tree_walker(tree=5, target=5) == 1 #example #4
Precondition:
- n ∈ [1; 100] - nodes number
- any node of a given tree may be any type of [int, str, list, dict]
CheckiO Extensions allow you to use local files to solve missions. More info in a blog post.
In order to install CheckiO client you'll need installed Python (version at least 3.8)
Install CheckiO Client first:
pip3 install checkio_client
Configure your tool
checkio --domain=py config --key=
Sync solutions into your local folder
checkio sync
(in beta testing) Launch local server so your browser can use it and sync solution between local file end extension on the fly. (doesn't work for safari)
checkio serv -d
Alternatevly, you can install Chrome extension or FF addon
checkio install-plugin
checkio install-plugin --ff
checkio install-plugin --chromium
Read more here about other functionality that the checkio client provides. Feel free to submit an issue in case of any difficulties.
Welcome to Pair Programming! Engage in real-time collaboration on coding projects by starting a session and sharing the provided unique URL with friends or colleagues. This feature is perfect for joint project development, debugging, or learning new skills together. Simply click 'Start Session' to begin your collaborative coding journey!
You are trying to join a pair programming session that has not started yet.
Please wait for the session creator to join.
It looks like the creator of the pair programming session closed the editor window.
It might happen accidentally, so that you can wait for reconnection.