Spaces:
Running
Running
can you generate a python code to swap 2 numbres
Browse files- README.md +7 -4
- swap_numbers.py +57 -0
README.md
CHANGED
|
@@ -1,10 +1,13 @@
|
|
| 1 |
---
|
| 2 |
-
title: Python Swappy Numbers
|
| 3 |
-
|
| 4 |
-
colorFrom: blue
|
| 5 |
colorTo: pink
|
|
|
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Python Swappy Numbers 🐍
|
| 3 |
+
colorFrom: red
|
|
|
|
| 4 |
colorTo: pink
|
| 5 |
+
emoji: 🐳
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
| 8 |
+
tags:
|
| 9 |
+
- deepsite-v3
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# Welcome to your new DeepSite project!
|
| 13 |
+
This project was created with [DeepSite](https://deepsite.hf.co).
|
swap_numbers.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
python
|
| 2 |
+
# Function to swap two numbers
|
| 3 |
+
def swap_numbers(a, b):
|
| 4 |
+
"""
|
| 5 |
+
Swaps two numbers using a temporary variable
|
| 6 |
+
|
| 7 |
+
Args:
|
| 8 |
+
a: First number
|
| 9 |
+
b: Second number
|
| 10 |
+
|
| 11 |
+
Returns:
|
| 12 |
+
Tuple of (a, b) after swapping
|
| 13 |
+
"""
|
| 14 |
+
print(f"Before swap: a = {a}, b = {b}")
|
| 15 |
+
|
| 16 |
+
# Swapping logic
|
| 17 |
+
temp = a
|
| 18 |
+
a = b
|
| 19 |
+
b = temp
|
| 20 |
+
|
| 21 |
+
print(f"After swap: a = {a}, b = {b}")
|
| 22 |
+
return a, b
|
| 23 |
+
|
| 24 |
+
# Alternative method without temporary variable (using arithmetic)
|
| 25 |
+
def swap_without_temp(a, b):
|
| 26 |
+
"""
|
| 27 |
+
Swaps two numbers without using temporary variable
|
| 28 |
+
|
| 29 |
+
Args:
|
| 30 |
+
a: First number
|
| 31 |
+
b: Second number
|
| 32 |
+
|
| 33 |
+
Returns:
|
| 34 |
+
Tuple of (a, b) after swapping
|
| 35 |
+
"""
|
| 36 |
+
print(f"Before swap: a = {a}, b = {b}")
|
| 37 |
+
|
| 38 |
+
a = a + b
|
| 39 |
+
b = a - b
|
| 40 |
+
a = a - b
|
| 41 |
+
|
| 42 |
+
print(f"After swap: a = {a}, b = {b}")
|
| 43 |
+
return a, b
|
| 44 |
+
|
| 45 |
+
# Example usage
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
num1 = 5
|
| 48 |
+
num2 = 10
|
| 49 |
+
|
| 50 |
+
# Using temporary variable
|
| 51 |
+
swapped1, swapped2 = swap_numbers(num1, num2)
|
| 52 |
+
|
| 53 |
+
print("\nAlternative method:")
|
| 54 |
+
# Using arithmetic operations
|
| 55 |
+
swapped1, swapped2 = swap_without_temp(num1, num2)
|
| 56 |
+
|
| 57 |
+
</html>
|