Wpis z mikrobloga

Cześć!

Robię sobie kolejne zadanie na codefights i nadal jeden test oblewam. Treść brzmi:

Two arrays are called similar if one can be obtained from another by swapping at most one pair of elements in one of the arrays.
Given two arrays a and b, check whether they are similar.

Example
For a = [1, 2, 3] and b = [1, 2, 3], the output should be
areSimilar(a, b) = true.
The arrays are equal, no need to swap any elements.
For a = [1, 2, 3] and b = [2, 1, 3], the output should be
areSimilar(a, b) = true.
We can obtain b from a by swapping 2 and 1 in b.
For a = [1, 2, 2] and b = [2, 1, 1], the output should be
areSimilar(a, b) = false.
Any swap of any two elements either in a or in b won't make a and b equal.

Zrobiłem taką funkcję: http://wklej.org/id/3342021/ i opisałem co według mnie tam się dzieje.

Problem jest z testem:
a: [1, 2, 3]
b: [1, 10, 2]
zwraca True zamiast False.
Wiem co jest problemem - po sprawdzeniu 0 indexu listy warunek na False nie jest spełniony, więc zwraca else - True. Tylko nie mogę dojść do tego, jak sprawić żeby po przejściu całej listy dopiero zwracał boolean, a nie już po 1 elemencie.

#naukaprogramowania #programowanie #python #python3
  • 8