MCPcopy Create free account
hub / github.com/geekcomputers/Python / Swapper

Class Swapper

swap.py:1–68  ·  view source on GitHub ↗

A class to perform swapping of two values. Methods: ------- swap_tuple_unpacking(self): Swaps the values of x and y using a tuple unpacking method. swap_temp_variable(self): Swaps the values of x and y using a temporary variable. swap_arithmetic_operations

Source from the content-addressed store, hash-verified

1class Swapper:
2 """
3 A class to perform swapping of two values.
4
5 Methods:
6 -------
7 swap_tuple_unpacking(self):
8 Swaps the values of x and y using a tuple unpacking method.
9
10 swap_temp_variable(self):
11 Swaps the values of x and y using a temporary variable.
12
13 swap_arithmetic_operations(self):
14 Swaps the values of x and y using arithmetic operations.
15
16 """
17
18 def __init__(self, x, y):
19 """
20 Initialize the Swapper class with two values.
21
22 Parameters:
23 ----------
24 x : int
25 The first value to be swapped.
26 y : int
27 The second value to be swapped.
28
29 """
30 if not isinstance(x, (int, float)) or not isinstance(y, (float, int)):
31 raise ValueError("Both x and y should be integers.")
32
33 self.x = x
34 self.y = y
35
36 def display_values(self, message):
37 print(f"{message} x: {self.x}, y: {self.y}")
38
39 def swap_tuple_unpacking(self):
40 """
41 Swaps the values of x and y using a tuple unpacking method.
42
43 """
44 self.display_values("Before swapping")
45 self.x, self.y = self.y, self.x
46 self.display_values("After swapping")
47
48 def swap_temp_variable(self):
49 """
50 Swaps the values of x and y using a temporary variable.
51
52 """
53 self.display_values("Before swapping")
54 temp = self.x
55 self.x = self.y
56 self.y = temp
57 self.display_values("After swapping")
58
59 def swap_arithmetic_operations(self):
60 """

Callers 1

swap.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected