L
L
Lisa2015-10-01 12:42:59
Python
Lisa, 2015-10-01 12:42:59

Where is the error in the Python code?

s = (input("Число: "))
n,k,nom = [],[], 0
for i in range(1,5):
    n.append(s[i-1:i+4])
    k.append(min(n[i-1][nom:i+4]))
    nom = n[i-1].find(min(n[i-1]))
print(''.join(k))

The essence of the algorithm: the program calculates the minimum four-digit number from an eight-digit number, which in turn consists of the numbers 1,2,3. The four-digit numbers may not be adjacent, but must be in order.
1 four-digit number can be in the range from 1 to 5 characters in eight-digit, the rest with an offset of 1 from the previous one. The program calculates the minimum in the first slice and writes it to the list, then from the number of this minimum it starts searching in the next slice.
Example: 12312321
Reply: 1121

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey Dugin, 2015-10-01
@Matreni

c7fdea37884c4cd081adfa0c7c607527.png

def solve(sequence):
    def select(shift=0):
        for start in range(4):
            selection = sequence[start:][shift:5]
            value = min(selection)
            shift += selection.index(value)
            yield value
    return ''.join(select())

print(solve('13321311')) # 1111
print(solve('12312321')) # 1121
print(solve('33211213')) # 1113
print(solve('12345678')) # 1234
print(solve('21221132')) # 1112

V
Vladimir Martyanov, 2015-10-01
@vilgeforce

Use a debugger.

K
Kirill Romanov, 2015-10-01
@Djaler

min(s[i:i+4] for i in range(4))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question