-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswappy.py
78 lines (61 loc) · 1.44 KB
/
swappy.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def breaky():
print('------------------')
def type1(x,y):
print('Type 1')
print('resetting values')
print('x: {0} y: {1}\n'.format(x,y))
# logic
x = x + y
y = x - y
x = x - y
print('After swapping')
print('x: {0} y: {1}\n'.format(x,y))
breaky()
def type2(x,y):
print('Type 2')
print('resetting values')
print('x: {0} y: {1}\n'.format(x,y))
# logic
x = x * y
y = x / y
x = x / y
print('After swapping')
print('x: {0} y: {1}\n'.format(x,y))
breaky()
def type3(x,y):
print('Type 3')
print('resetting values')
print('x: {0} y: {1}\n'.format(x,y))
# Using Bitwise addition and subtraction for swapping.
x = (x & y) + (x | y)
y = x + (~y) + 1
x = x + (~y) + 1
print('After swapping')
print('x: {0} y: {1}\n'.format(x,y))
breaky()
def type4(x,y):
print('Type 4')
print('resetting values')
print('x: {0} y: {1}\n'.format(x,y))
# logical Swapping using xor
x = x ^ y
y = x ^ y
x = x ^ y
print('After swapping')
print('x: {0} y: {1}\n'.format(x,y))
breaky()
def type5(x,y):
print('Type 5')
print('resetting values')
print('x: {0} y: {1}\n'.format(x,y))
# This kind of logic is supported by python.
x,y = y,x
print('After swapping')
print('x: {0} y: {1}\n'.format(x,y))
breaky()
breaky()
type1(10,20)
type2(10,20)
type3(10,20)
type4(10,20)
type5(10,20)