Homework07

A MathWikiből
(Változatok közti eltérés)
(Problem 4: Tree Cut)
(Note:)
62. sor: 62. sor:
  
 
==Note:==
 
==Note:==
Problem 1: 1 point
+
Problem 1: 1 point  
 +
 
 
Problem 2: 1 point
 
Problem 2: 1 point
 +
 
Problem 3: 1 point
 
Problem 3: 1 point
 +
 
Problem 4: 2 point
 
Problem 4: 2 point

A lap 2021. május 2., 08:11-kori változata

Tartalomjegyzék

Problem 1: Parenthesis Depth

Write a function called parenthesis_depth : its input is a string which is a well formed expression with parenthesis and its output is a string with same length but the symbols are changed to numbers which are the depth of the surrounding parenthesis. Don't modify the parenthesis symbols but every other symbol.

There won't be more then nine parenthesis in depth!

Example

a*(b+c) → 00(111)
-(1/(2-3)) → 0(11(222))

Problem 2: Descartes Product

Write a function called descartes_product with input variadic number of sets, each set is an iterable object without repetition and its output is the Cartesian product of the input sets. If the input is n sets then the output should be a list of n-tuples!

The order of the output is not relevant but the order of the individual tuples is relevant. The tuples should follow the same order as the input sets: First element from the first set, second element from the second set ...

If there is even a single empty set in the input, then the output should be an empty list.

Examples

[1,2,3] → [(1,), (2,), (3,)]
[1,2], ['a','b'] → [(1,'a'), (1,'b'), (2,'a'), (2,'b')]
But this is not correct: [('a', 1), ...]
[1,2], ['a','b'], [3,4,5] → [(1,'a',3), (1,'a', 4), ...]
[1,2], [], ['a','b'] → []

Hint: First make it work for zero or one input set. Then apply recursion: take the product of n-1 sets and make something that produces the n-product.

Bonus: If the input sets have sizes: n1, n2 ... nk then the output should have n1*n2 ... *nk tuples.

Problem 3: Matrix Log

Write a function with two input variables:

n: number of dimensions
M: a n-dimensional array as a list

An n-dimensional array means that it a list where each element is a (n-1)-dimensional array. A 1-dimensional array is just a list of numbers.

We would like to take the base 2 logarithms of every number in the array, but only with 2 digit precision. Return the array with the same shape as the original, where every element is the logarithm of the original element.

Hint:

You can use math.log() for logarithm.
You can use the built-in round() function.
Try with a recursive solution.

There are two ways of returning the result: overwrite the original elements in-place, using the fact that a list is mutable or return a copy of the input with the modified elements. Both can work, but try not to mix the two solutions.

Problem 4: Tree Cut

You will have a tree graph as input and your task is to remove its leaves. A tree is a directed graph without directed circles. A leaf is a node without outgoing edges.

Our trees are represented with embedded lists. A node consists a list of nodes, its children. A children can be an empty list (leaf) or an other list (a subtree).

The tree_cut function should remove the leaves.

For example this:

[[[[], []], [[], []]], [[], [], []]]
becomes this:
[[[], []], []]

(See these trees in Python Tutor)

Write the function tree_cut with one parameter: tree, a list of lists, possibly embedded into each other several times. Return a new tree where the leaves are removed.

Hint: Use recursion!

Note:

Problem 1: 1 point

Problem 2: 1 point

Problem 3: 1 point

Problem 4: 2 point

Személyes eszközök