A
A
Artem Zinkin2020-02-22 11:32:43
Python
Artem Zinkin, 2020-02-22 11:32:43

Is it possible to compose a sequence of numbers from the given digits, which is an arithmetic progression?

Given a string consisting of numbers. The string length does not exceed 15 characters.

It is required to write a program that will determine whether it is possible to compose a sequence of numbers from the given numbers, which is an arithmetic progression. The new sequence must use all the digits in the original string, once each.

Input data: a sequence of numbers.
51791

Output data: the desired sequence of numbers (at least three), written with a space, or 0 if the sequence could not be compiled.
5 7 9 11

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Sokolov, 2020-02-22
@sergiks

You can always

В любом случае, при любых цифрах, числом более одной, можно составить «последовательность» всего из двух чисел и объявить её арифметической % )
51791
5,  5 + 1786 = 1791, третьим было бы 3577

Seriously, break it down into subtasks.
Here is a set of numbers. How to quickly understand whether it is an arithm. sequence?
Probably, it should be a set of 3 or more numbers. Sort in ascending order, get the difference between 0 and 1. Move on, compare 2nd and 1st. As soon as the difference differs from the first, it fails. If you have successfully reached the end of the array and everywhere the difference is the same - this is success, this is arfim. be consistent.
Another subtask: to make all possible numbers from a set of digits. There should be 3 or more numbers. In all variants of permutations, but taking into account the repeated digits in the original set.
You can add any optimizations. For example, parity check.
In an arithmetic sequence, the parity is either constant or alternates every other time. An even number ends in an even number. Therefore, if there is only one odd digit in the original set, either its place is not at the end of the number, or there are only three numbers in the sequence and this digit is at the end of the middle one.

L
longclaps, 2020-02-22
@longclaps

'012' 0 1 2
'0123' 0 1 2 3
'01234' 0 1 2 3 4
'012345' 0 1 2 3 4 5
'0123456' 0 1 2 3 4 5 6
'01234567' 0 1 2 3 4 5 6 7
'012345678' 0 1 2 3 4 5 6 7 8
'0123456789' 0 1 2 3 4 5 6 7 8 9
'01234567890' 0 18 36 54 72 90
'012345678901' 0 1 2 3 4 5 6 7 8 9 10
'0123456789012' 56 94 132 170 208
'01234567890123' 57 120 183 246 309
'012345678901234' 100 312 524 736 948
'0123456789012345' 0 181 362 543 724 905
'01234567890123456' 58 179 300 421 542 663
'012345678901234567' 50 1736 3422 5108 6794
'0123456789012345678' 6 139 272 405 538 671 804
'01234567890123456789' 0 9 18 27 36 45 54 63 72 81 90
'012345678901234567890' 162 300 438 576 714 852 990
'0123456789012345678901' 28 37 46 55 64 73 82 91 100 109
'01234567890123456789012' 584 1007 1430 1853 2276 2699
'012345678901234567890123' 102 219 336 453 570 687 804 921
'0123456789012345678901234' 91 674 1257 1840 2423 3006 3589
'01234567890123456789012345' 105 984 1863 2742 3621 4500 5379
'012345678901234567890123456' 162 1300 2438 3576 4714 5852 6990
'0123456789012345678901234567' 1621 3002 4383 5764 7145 8526 9907
'01234567890123456789012345678' 72 163 254 345 436 527 618 709 800 891
'012345678901234567890123456789' 108 197 286 375 464 553 642 731 820 909
You can write, but it will get worse on bad data.

X
xmoonlight, 2020-02-28
@xmoonlight

Given: String of digits :
51791
Restrictions:
Minimum number of elements in the output: 3
Heuristics:
Maximum value of the element: 975
Maximum number of digits in one output element: 3
1: 517, 9, 1 => 517-9=9-1 ? => false
2: 517, 1, 9 => 517-1=1-9 ? => false
3: 171, 5, 9 => ... => false
... etc.,
we first do gluing, starting with the maximum possible values, then shifting / rearranging one digit (and gluing again), not exceeding the maximum number of digits (in this case: 3) in one tested value.
At each step, we check the equal distance.
As soon as we have picked up one, we leave these numbers and check the remaining ones ("immersion").

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question