pytudes._2021.coderbyte.array_challenge__mediumο
https://coderbyte.com/information/Tree%20Constructor%20Two
- Description:
For this challenge you will determine if an array of integer pairs can form a binary tree properly.
Array Challenge: Have the function ArrayChallenge(strArr) take the array of strings stored in strArr, which will contain pairs of integers in the following format: (i1,i2), where i1 represents a child node in a tree and the second integer i2 signifies that it is the parent of i1.
For example: if strArr is [β(1,2)β, β(2,4)β, β(7,2)β], then this forms the following tree:
which you can see forms a proper binary tree. Your program should, in this case, return the string true because a valid binary tree can be formed. If a proper binary tree cannot be formed with the integer pairs, then return the string false. All of the integers within the tree will be unique, which means there can only be one node in the tree with the given integer value.
Module Contentsο
Functionsο
|
Determines whether the given input represents a proper binary tree |
- pytudes._2021.coderbyte.array_challenge__medium.ArrayChallenge(strArr)[source]ο
Determines whether the given input represents a proper binary tree
- A proper binary tree is a graph where:
All nodes have β€ 2 children
All nodes other than root node have 1 and only 1 parent node
The graph (if not empty) has 1 and only 1 root node
- Args:
- strArr:
A list of (child, parent) tuples in string representation that form a graph.
- Returns:
βtrueβ if the graph formed from the entries in strArr is a proper binary tree, otherwise βfalseβ.
- Examples:
>>> ArrayChallenge(["(1,2)", "(2,4)", "(5,7)", "(7,2)", "(9,5)"]) 'true' >>> ArrayChallenge(["(1,2)", "(2,4)", "(7,2)"]) 'true' >>> ArrayChallenge(["(1,2)", "(1,2)"]) # Duplicate tuples are ignored 'true' >>> ArrayChallenge([]) # Vacuous binary tree 'true' >>> ArrayChallenge(["(1,2)", "(1,3)"]) # 1 has >1 parent 'false' >>> ArrayChallenge(["(1,2)", "(2,4)", "(7,2)", "(8,8)"]) # 8 has a Self-edge 'false' >>> ArrayChallenge(["(1,2)", "(4,2)", "(7,2)"]) # 2 has >2 children 'false' >>> ArrayChallenge(["(1,2)", "(4,2)", "(7,9)"]) # Graph has >1 root node 'false'