0

waiting for your suggestions about symmetric difference algorithm

A code I wrote about the symmetric difference algorithm. How could this code be written better? It works fine, but is there a bug you noticed? def sym(a, *args): c = 0 T = True while T: lst = [] for i in a: if i not in args[c]: if i not in lst: lst.append(i) else: continue for i in args[c]: if i not in a: if i not in lst: lst.append(i) else: continue c += 1 a = lst if c == len(args): T = False lst.sort() return lst

2nd Jan 2022, 8:10 PM
Bekir
Bekir - avatar
3 ответов
+ 2
from : https://www.freecodecamp.org/learn/coding-interview-prep/algorithms/find-the-symmetric-difference The mathematical term symmetric difference (△ or ⊕) of two sets is the set of elements which are in either of the two sets but not in both. For example, for sets A = {1, 2, 3} and B = {2, 3, 4}, A △ B = {1, 4}. Symmetric difference is a binary operation, which means it operates on only two elements. So to evaluate an expression involving symmetric differences among three elements (A △ B △ C), you must complete one operation at a time. Thus, for sets A and B above, and C = {2, 3}, A △ B △ C = (A △ B) △ C = {1, 4} △ {2, 3} = {1, 2, 3, 4}.
2nd Jan 2022, 8:40 PM
Bekir
Bekir - avatar
0
I'm not sure about symmetric deference algorithm. But your code makes me think of this code class Point{ constractor(x,y) { this.x = x; this.y = y; } static slopeAB (A, B) { const dx = A.x-B.x; const dy = A.y-B.y; return dy/dx; } } const P1= new Point(1,2); const P2 = new Point(3,4); console.log(Point.slopeAB(P1,P2));
2nd Jan 2022, 8:30 PM
Steve Nova
Steve Nova - avatar
0
Bekir Not sure if this is what you require as I'm pretty weak in math: def sym(*sets): result = [ ] for each in sets: for elm in each: if elm in result: result.remove(elm) else: result.append(elm) return result
3rd Jan 2022, 7:04 AM
Œ ㅤ
Œ ㅤ - avatar