def calc(nPoint): result = 0.0 print("sqrt(", end="") for key in Dictionary: print("(", Points[nPoint[0]][key], " - ", Points[nPoint[1]][key], ")^2 + ", sep="", end="") result = result + math.pow((Points[nPoint[0]][key] - Points[nPoint[1]][key]),2) print(")") result = math.sqrt(result) return result
def GenerateMap(): print("~~~~") for i in range(1, PSize): for j in range(i + 1, PSize + 1): print(i, " and ", j, " = ", calc( (i, j) ), sep="")
1) ? (0- , 10- )
2) (0- , 10 )
3) ?(0- , 10- )
4) ?(0- , 10- )
5) ? (0- , 10 )
6) ? (0- , 10 )
7) ? (0- , 10 )
8) ? (0- , 10 )
9) ? (0, 10)
10) ? ( 0, 10)
11) (0, 10)
12) (0, 10)
13) (0, 10)
- = 11.83
- = 12.72
- = 12.92
- = 12.88
- = 16.49
- = 9.59
- = 8.77
- = 10
- = 14.28
- = 10.34
- = 13.85
- = 12.4
- = 12.68
- = 14.93
- = 17.66
#!/usr/bin/python import sys import pickle import math Dictionary = [] Points = {} PSize = 0 def DictGen(): print("~~~~") DictList = [] print("For exit enter a \"0\"") while True: s = str(input("> ")) if (s == "0"): break; else: DictList.append(s) print("Enter a name for new list of keys: ") fname = str(input("> ")) with open(fname, 'wb') as f: pickle.dump(DictList, f) print("Saved with name ", fname, sep = "") def DictLoad(): print("~~~~") fname = str(input("Enter a name of list to load\n> ")) with open(fname, 'rb') as f: Dictionary.clear() Dictionary.extend(pickle.load(f)) def NewPoint(): print("~~~~") if (not Dictionary): print("List of keys not loaded (command 2)") else: LocalPoint = {} for key in Dictionary: print(key, ": ", sep="", end="") mark = float(input()) LocalPoint[key] = mark print("Enter a name for new point: ") fname = str(input("> ")) with open(fname, 'wb') as f: pickle.dump(LocalPoint, f) print("New point saved with name ", fname, sep="") def LoadPoint(): print("~~~~") fname = str(input("Enter a name of point to load\n> ")) with open(fname, 'rb') as f: LocalPoint = pickle.load(f) Points[PSize] = LocalPoint def calc(nPoint): result = 0.0 print("sqrt(", end="") for key in Dictionary: print("(", Points[nPoint[0]][key], " - ", Points[nPoint[1]][key], ")^2 + ", sep="", end="") result = result + math.pow((Points[nPoint[0]][key] - Points[nPoint[1]][key]),2) print(")") result = math.sqrt(result) return result def GenerateMap(): print("~~~~") for i in range(1, PSize): for j in range(i + 1, PSize + 1): print(i, " and ", j, " = ", calc( (i, j) ), sep="") while True: print("0 - exit") print("1 - generate a list of keys") print("2 - load a map of marks") print("3 - add a new point in dimension") print("4 - load a new point in dimension") print("5 - calculate distance from two points of dimension") print("6 - print information") print("7 - create a map with distance for every point") i = int(input("#-> ")) if (i == 0): sys.exit() elif (i == 1): DictGen() elif (i == 2): DictLoad() elif (i == 3): NewPoint() elif (i == 4): PSize = PSize + 1 LoadPoint() elif (i == 5): print("Enter a two numbers of which points you want to calculate a distance") nPoint = tuple(int(x.strip()) for x in input().split(' ')) print("Difference: ", calc(nPoint), sep="") input() sys.exit() elif (i == 6): print("Dictionary", Dictionary, sep = ": ") print("Points: ", Points, sep = ": ") print("Total points: ", PSize, sep = "") elif (i == 7): GenerateMap() else: print("Unknown command") sys.exit()
Source: https://habr.com/ru/post/304082/
All Articles