Homework07

A MathWikiből
(Változatok közti eltérés)
(Parenthesis Depth)
(Problem 1: Parenthesis Depth)
7. sor: 7. sor:
 
  a*(b+c) → 00(111)
 
  a*(b+c) → 00(111)
 
  -(1/(2-3)) → 0(11(222))
 
  -(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.

A lap 2021. május 2., 09:02-kori változata

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.

Személyes eszközök