📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Собеседование на программиста 1С. Задача 1. Распределение в таблице значений.

1С Франчайзи Домрачев консалтинг16:34

Transcription

Hello friends!

Today we have an unusual session. I want to dedicate it to analyzing a problem that often comes up in interviews for programming positions in 1C.

Today, our first task is about distributing values in a table. The condition is to proportionally distribute a total of 40 rubles with an accuracy of up to one ruble.

Let’s note that it’s a proportional distribution and with an accuracy of up to one ruble. Below is a simple table with three rows and three columns: quantity and sum.

Let’s consider how we can solve this problem. I have prepared a template in Excel. Here we have the quantity, and I immediately calculate the total, which is 6. We then determine the coefficient for distributing our 40 rubles. For this, we need to divide the quantity in this row, which is 1, by the total, which is 6. This gives us the following fraction. We do the same for the second and third rows.

In the end, the coefficient should equal one. Next, we write 40 rubles for each row and multiply the coefficient by our distribution sum. This gives us such a fraction. According to the problem's condition, we need to round everything to the nearest ruble.

So, rounding to the nearest ruble, we get 7 in the first row, 13 in the second row, and 20 in the third row. In total, we have 40. Everything matches perfectly here, but there can be difficult cases when there are many data points and many fractions, and the rounding sometimes doesn’t match.

For example, let’s consider a case where we get approximately such fractions: 7, 13, 12, 4, 19. In the end, we might not distribute the entire sum, say only 38 rubles instead of 40. Therefore, we have to think about what to do with the remainder. Usually, we would add or subtract from the row with the largest sum or from the last row.

To avoid this, let’s consider a small algorithm. First, I will take everything back to where it was.

Now, on the first iteration step, we got 7, and we record the sum, adding our sum to the distribution. For clarity, I will copy our table below but will only look at the two remaining rows.

So, I will remove the nomenclature from the first row as if we have already processed it. We have already distributed 7 rubles, leaving us with 33 rubles to distribute and a quantity of 5.

Next, we determine the coefficients by dividing the quantity by our total. Here it will be 2 divided by 5, which is 0.4, and for the second row, it will be 0.6.

Now we have recalculated the distribution sums, which are fractional, and we round them mathematically: 13 and 20. We take 13, add it to our sum, resulting in 143.

In the third step of our cycle, we also don’t consider the last row. Here, the total quantity is 3, and the coefficient will be 1. We have already distributed 7 and 13, which totals 20, leaving us with 20 rubles to distribute. We assign this 20 rubles to our third row.

Now, I will present this in a more visually appealing way. The distribution results are as follows: the first row is 7, the second is 14, and the third is 20.

In total, we have a sum of 40, and the initial value was 38. Thus, we have distributed 40 rubles proportionally according to our quantities.

This example allows us to think about the remainder and correctly distribute the sums.

Let’s move to the configurator and create an external processing module. We will go to forms, add a new form, go to the command section, and add a new command action. We write "Command 1," return to the command field of the form, and create procedures for the client and server. We drag the command onto the form, and a button appears on the form.

Now, let’s go to the module and insert the code for the "Command 1" procedure on the server. We create a variable Tab equal to a new value table. We will add columns. The name of the first column will be "Product," the type will be "New," and the description type will be a string.

We write the string qualifiers, for example, the string length qualifier with a maximum length of 100. I will copy this line for the next two columns. The second column will be "Quantity," which will be of type number, and the qualifier will be a number with a length of 10. After the comma, we write the number of decimal places, which will be 3, and I will set it to be non-negative.

I will add the third column, which will be "Sum." I will set the number of decimal places to 2.

Now, let’s add our values to the value table. We will set the variable "Row" equal to Tab.add, where Product equals "Nomenclature 1," Quantity equals "1," and Sum equals "100."

I will copy this for the other two rows, just changing the values: "Nomenclature 2," Quantity "2," Sum "130," and "Nomenclature 3," Quantity "3," Sum "150."

So, we have filled our table according to the problem's condition.

Now, let’s check for errors. There are no errors, and let’s display what we have. We will use a loop for each row in Tab and display "Product," "Quantity," and "Sum" separated by spaces.

Let’s test our processing. We will open 1C Enterprise on the desktop and open the external processing module. We agree to the processing we wrote, click the command button, and see that the table we had in the problem condition has appeared.

Now, let’s distribute our sum proportionally to the quantity. We introduce a variable "Sum," which is 40 rubles, and the base for distribution will be the total of the quantity column. We write Tab.total column quantity.

Let’s see what we have. It should be 6. We saved the processing and went back to 1C Enterprise. The command shows that the quantity has been calculated for the entire table, which is 6.

Let’s return to the configurator and write our loop for each row in Tab. For each row in our value table, we execute the loop. I will write the end of the loop.

We determine the coefficient. We will set the variable "Coefficient" equal to the quantity of the row divided by the base for distribution. This means we are dividing the quantity of the first row, which is 1, by 6.

Now we introduce a variable "Sum for Distribution," which equals the coefficient multiplied by the distributed sum. This gives us the fraction 1/6 multiplied by 40.

According to the conditions, we need to round this to the nearest ruble, so we wrap everything in the rounding function with the second parameter as 0.

Now we write this value into the field "Sum." The row.Sum equals row.Sum plus the sum for distribution. This means we are adding our fraction to 100.

We have recorded our value in the first row. Now, let’s prepare the data for the second and subsequent iterations of the loop. The base for distribution decreases by the quantity, so we subtract 1 from it.

Next, we will divide by 5 for distribution, and the sum also decreases by our calculated expression. The distributed sum equals the total sum minus the sum for distribution.

Now, let’s display our value table to see what we got after our manipulations.

We check for errors, save our processing, and reopen our processing in the enterprise.

We see that we calculated 107, 143, and 170, which is correct.

Friends, this was a lesson dedicated to interview tasks. If you have any questions or tasks, please write in the comments or email. We will consider them. Thank you!