Monday, April 29, 2024

📑 Difference in the Subset Sums 2

📑 Task

1) These programs divide a set into two subsets so
that the difference in the sums of the subsets is minimal
Can you describe the main principals that programs based?
2) Can you combine their solution methods in one program?


📑 Answer

1) The main principle behind these programs is dynamic programming.
The first program uses a table to store the results of subproblems
to find the minimum subset sum difference.
It iterates over the elements of the input array and
fills the table based on whether including or excluding an element results
in a subset sum closer to half of the total sum.
The second program uses a recursive approach to explore all possible subsets and find the minimum difference between the two subsets.
2)

📑 Difference in the Subset Sums

📑 Task

1) This program divides a set into two subsets so
that the difference in the sums of the subsets is minimal
The variable "table" indicates whether it is possible
to achieve a sum of j using the first i elements
Will all values be equal to 1 in the last row of the printed table?
2) During the iteration process, we actually only use the last two rows of the table
Can you modify the program using this fact?

📑 Answer

1) The last line will be [1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1]
We will not be able to get sums equal to 2 and 5
from the numbers presented in the original list
2)

Sunday, April 28, 2024

📑 Recursive Tables

📑 Task

1) What type of function is this?
2) How can you rewrite it so as not to use recursion?

📑 Answer

1) This is a recursive generator
It can be made infinite by removing the limiter max_count
2)

Saturday, April 27, 2024

📑 Default Arguments

📑 Task

1) What will the program print in the last 2 lines?
2) Why do variables change or not change in this way?
3) What minimal changes can be made to the program so that the values will be increased
and the last 2 lines will be 3 [0, 1, 2, 3] and 0 [0, 1, 2, 3]?

📑 Answer

1) 1 [0, 1, 1, 1] and 0 [0, 1, 1, 1]
2) Variables with mutable and immutable objects
are used as default arguments to a function
3)

📑 Presence in the List

📑 Task

1) What does the program do?
2) Can you write a function based on the same splitting procedure
to sort letters in a word (vowels and consonants)?

📑 Answer

1) This program generates x (a list of 10 random integers between 0 and 9) and
then initializes two empty lists (not_in_x and in_x) to store digits
based on their presence in the list x.
2)

Friday, April 26, 2024

📑 Sending Values to the Generator

📑 Task

1) What will the program print?
2) What types of functions are in the program?
3) How can you fix it so that the number 101 will also be printed?

📑 Answer

1) 111 121 131 141 151 161 171 181 191
2) An anonymous "lambda" function and
a function of the type "infinite generator" are used here
3)

Thursday, April 25, 2024

📑 Substring Counting

📑 Task

1) What result will the program produce?
2) Simplify the program to improve counting speed

📑 Answer

1) The program will return the number 3 by counting
the number of occurrences of the substring in the string
2) In the program it needs to replace recursion with a loop