Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

from typing import Any def nicely_sorted(s: list[list]) -> list[list]: def size_and_content(t: list) -> tuple[int, list]: return (len(t), t) # first by lenght and then it

from typing import Any def nicely_sorted(s: list[list]) -> list[list]: def size_and_content(t: list) -> tuple[int, list]: return (len(t), t) # first by lenght and then it return sorted(s, key=size_and_content) # What you wanna order and a function that tells you how to convert them def power_set(s: list) -> list[list]: if not s: return [[]] recursive: list[list] = power_set(s[:-1]) #-1 very last number of the list return recursive + [elem + [s[-1]] for elem in recursive] def combinations(s: list, k: int) -> list[list]: return[t for t in power_set(s) if len(t) == k] def insert(x: Any, s: list, i: int) -> list: return s[:i] +[x] + s[i:] def insert_everywhere(x: Any, s: list) -> list[list]: return [insert(x, s, i) for i in range(len(s) + 1)] def permute(s: list) -> list[list]: if not s: return [[]] return sum([insert_everywhere(s[0], elem) for elem in permute(s[1:])], []) def permutations(s: list, k: int) -> list[list]: return sum([permute(elem) for elem in combinations(s, k)], []) def permutations_with_repetition(s: list, k: int) -> list[list]: if k == 0: return [[]] return [[elem] + perm for elem in s for perm in permutations_with_repetition(s, k-1)] def combinations_with_repetition( s: list, k: int) -> list[list]: if k == 0: return [[]] return ([[s[i]] + comb for i in range(len(s)) for comb in combinations_with_repetition(s[i:], k - 1)]) I want to know more about the implementation of pemutations_with_repetitons and combinations_with_repetitons cause I'm a little confuse with the recursive implementation. Is there another way reusing the previous functions? Cause I have the implementation that you provided, but I wanted to know if there was another way, like modifying the function power_set so that it accepts subsets with the repeated number like {1, 1}, or some other approach. Without using the functions defined in the itertools module to solve these problems. If anyone can help me, I'd be really grateful :D

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions