Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The Swift Programming Language: Swift by Tutorials (solutions) | |
// Author: Bill Bonney <billbonney (at) communistech.com> | |
// | |
// This is all the code in The Swift Tour section in the Apple Book on Swift. | |
// This should allow you to play with the code and see behaviours, and help | |
// solve any paticulars you get stuck on when doing the Experiments. | |
// | |
// Please feel free to comment on any part, I am by no means an expert in Swift ;-) | |
// | |
// NOTE: Not all code is identical (since I was tinkering, but should help) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solve_tsp_dynamic(points): | |
#calc all lengths | |
all_distances = [[length(x,y) for y in points] for x in points] | |
#initial value - just distance from 0 to every other point + keep the track of edges | |
A = {(frozenset([0, idx+1]), idx+1): (dist, [0,idx+1]) for idx,dist in enumerate(all_distances[0][1:])} | |
cnt = len(points) | |
for m in range(2, cnt): | |
B = {} | |
for S in [frozenset(C) | {0} for C in itertools.combinations(range(1, cnt), m)]: | |
for j in S - {0}: |