Skip to content

Instantly share code, notes, and snippets.

@marrcandre
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marrcandre/67b4f1f20142c12f82f9 to your computer and use it in GitHub Desktop.
Save marrcandre/67b4f1f20142c12f82f9 to your computer and use it in GitHub Desktop.
import unittest
import itertools
class CombinaTrajetoriasTests(unittest.TestCase):
def test_combina_3_trajetorias(self):
trajetorias = (801, 802, 803)
trajetorias_combinadas = {801:[(801,802),(801,803)],
802:[(802,801),(802,803)],
803:[(803,801),(803,802)]
}
self.assertEqual(combina_trajetorias(trajetorias), trajetorias_combinadas)
def test_combina_4_trajetorias(self):
trajetorias = (801, 802, 803, 804)
trajetorias_combinadas = {801:[(801,802),(801,803), (801,804)],
802:[(802,801),(802,803), (802,804)],
803:[(803,801),(803,802), (803,804)],
804:[(804,801),(804,802), (804,803)]
}
self.assertEqual(combina_trajetorias(trajetorias), trajetorias_combinadas)
def combina_trajetorias(trajetorias):
trajetorias_combinadas = {}
for trajetoria in trajetorias:
trajetorias_combinadas[trajetoria] = list(itertools.product([t for t in trajetorias if t == trajetoria], [t for t in trajetorias if t != trajetoria]))
return trajetorias_combinadas
if __name__ == '__main__':
unittest.main()
#Outra tentativa foi usar permutação, mas não ele traz todas as combinações juntas:
#trajetorias = (801, 802, 803)
#list(itertools.permutations(trajetorias, 2))
#[(801, 802), (801, 803), (802, 801), (802, 803), (803, 801), (803, 802)]
#trajetorias = (801, 802, 803, 804)
#list(itertools.permutations(trajetorias, 2))
#[(801, 802), (801, 803), (801, 804),
#(802, 801), (802, 803), (802, 804),
#(803, 801), (803, 802), (803, 804),
#(804, 801), (804, 802), (804, 803)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment