Saddam Al-Nasser
Saddam Al-Nasser

Reputation: 13

Find out if this sequence is one in which the same numbers are in pairs, but there is one other number between the same numbers

I am total newbie in python who is practising right now with little 'algorithms'. So here is my exercise:

N-number integers are given (user have to input). The task is to find out if this sequence is one in which the same numbers are in pairs, but there is one other number between the same numbers.

For example, for a sequence of 11 numbers 3 3 5 10 10 1 12 12 3 6 6, the answer is "yes" and for the sequence 2 2 3 15 15 4 4 8 4 4 1 the answer is "no".

Here's what I tried. But this gypsy-code is not working well:

n = int(input())
right_sequence = True

for i in range(n):
    current = int(input())
    if i > 0:
        if current == previous:
            if previous != current:
                right_sequence = True
        else:
            right_sequence = False
    previous = current

if right_sequence == True:
    print('Yes')
else:
    print('No')

Upvotes: 0

Views: 218

Answers (3)

Sasadi
Sasadi

Reputation: 104

I tried to use your own code as much as possible. Basically the number of inputs should be at least 5. (The shortest correct answer would be in this format : A A B A A which has 5 input.

If we replace the i's with their remainder to 3. (i.e i % 3) Then the indexes for 8 input values would be:

0 1 2 0 1 2 0 1 ...

For which a correct answer would look like bellow:

A A B A A B A A ...

A correct list (The one that outputs "Yes") is the one that:

  1. all the 0 indexes are different from their previous value (Except the first 0 which deosn't have previous value)
  2. all the 1 indexes are equal to their previous value
  3. all the 2 indexes are different from their previous value
  4. The list ends with a 1 index

These 4 points are summarized into 4 if conditions in the bellow code. (The ones that have the value 'k' which carries the remainder of i'th to 3)

n = int(input())
right_sequence = True
k = 0
if n < 5:
    right_sequence = False

for i in range(n):
    current = int(input())

    if i > 0:
        k = i % 3
        if k == 0:
            if current == previous:
                right_sequence = False # Print (No)
        if k == 1:
            if current != previous:
                right_sequence = False # Print (No)
        if k == 2:
            if current == previous:
                right_sequence = False # Print (No)
    previous = current


if k != 1:
    print('No')
elif right_sequence == True:
    print('Yes')
elif right_sequence == False:
    print('No')

Upvotes: 1

Artiom  Kozyrev
Artiom Kozyrev

Reputation: 3836

I have solved the issue the following way:

x = [2, 2, 3, 15, 15, 4, 4, 8, 4, 4, 1]
y = [3, 3, 5, 10, 10, 1, 12, 12, 6, 6]


def check_order(x):
    for i in range(len(x)):
        only_equal_in_row = True  # all previous checked j index elements were equal to i
        for j in range(i+1, len(x)):
            if x[i] == x[j]:
                if only_equal_in_row is False: # check if there was j not equal to i elements before
                    return False 
            else:
                only_equal_in_row = False # found j element not equal to i
    return True


if __name__ == "__main__":
    print(check_order(x))
    print(check_order(y))

Edit: Without functions due to OP request:

x = [2, 2, 3, 15, 15, 4, 4, 8, 4, 4, 1]

is_right = True
stop = False  # need to stop outer for loop

for i in range(len(x)):
    if stop:
        break
    only_equal_in_row = True  # all previous checked j index elements were equal to i
    for j in range(i+1, len(x)):
        if x[i] == x[j]:
            if only_equal_in_row is False: # check if there was j not equal to i elements before
                is_right = False
                stop = True
                break
        else:
            only_equal_in_row = False  # found j element not equal to i

print(is_right)

Upvotes: 1

user2390182
user2390182

Reputation: 73480

You could slices and zip:

def f(l):
     all(a == b != c for a, b, c in zip(x[::3], x[1::3], x[2::3]))

f([3, 3, 5, 10, 10, 1, 12, 12, 3, 6, 6])
# True
f([2, 2, 3, 15, 15, 4, 4, 8, 4, 4, 1])
# False

This will work only if the sequence starts with a pair and you might have to check special cases at the end, but it should hint in the right direction.

Upvotes: 1

Related Questions