-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymmetric_difference.py
More file actions
32 lines (26 loc) · 1.28 KB
/
symmetric_difference.py
File metadata and controls
32 lines (26 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/python3
# Symmetric Difference Challenge Solution
# problem link: https://www.hackerrank.com/challenges/symmetric-difference/problem
# Author: Audity Ghosh
# Date: 21 January 2020
# Description:
# This script takes two sets of integers, calculates their symmetric difference,
# and prints the result in ascending order.
# Read the size of the first set
n = int(input()) # Read the size of set A
datap = list(map(int, input().split(' '))) # Read the space-separated integers for set A
# Read the size of the second set
m = int(input()) # Read the size of set B
datal = list(map(int, input().split(' '))) # Read the space-separated integers for set B
# Convert the lists into sets
data1 = set(datap) # Create set A
data2 = set(datal) # Create set B
# Calculate the symmetric difference by performing union and intersection operations
o = set(data1.union(data2)) # Union of both sets
u = set(data1.intersection(data2)) # Intersection of both sets
l = set(o.difference(u)) # Symmetric difference: union minus intersection
# Convert the symmetric difference to a sorted list and print the result
k = list(l) # Convert the set to a list
k.sort() # Sort the list in ascending order
for i in k:
print(i) # Print each element of the sorted symmetric difference