-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding to Swap variables for all CPP,Python, and Java
- Loading branch information
Showing
4 changed files
with
196 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Title: Swap Variables | ||
* | ||
* Given two integers, write a function that swaps them without using any | ||
* temporary variables. | ||
* | ||
* Execution: g++ SwapVariables.cpp -o SwapVariables | ||
* | ||
* For more details, check out http://www.byte-by-byte.com/swapvariables | ||
*/ | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
vector<int> swap_1(int x, int y) { | ||
vector<int> vars; | ||
|
||
x = x + y; | ||
y = x - y; | ||
x = x - y; | ||
|
||
vars.push_back(x); | ||
vars.push_back(y); | ||
|
||
return vars; | ||
} | ||
|
||
vector<int> swap_2(int x, int y) { | ||
vector<int> vars; | ||
|
||
x = x ^ y; | ||
y = x ^ y; | ||
x = x ^ y; | ||
|
||
vars.push_back(x); | ||
vars.push_back(y); | ||
return vars; | ||
} | ||
|
||
int main() { | ||
vector<int> test_1 = swap_1(1, 2); | ||
assert(test_1[0] == 2); | ||
assert(test_1[1] == 1); | ||
cout << "Swapped (1,2) with (2,1)" << endl; | ||
|
||
vector<int> test_2 = swap_1(5, 5); | ||
assert(test_2[0] == 5); | ||
assert(test_2[1] == 5); | ||
cout << "Swapped (5,5) with (5,5)" << endl; | ||
|
||
vector<int> test_3 = swap_1(-1, 40); | ||
assert(test_3[0] == 40); | ||
assert(test_3[1] == -1); | ||
cout << "Swapped (40,-1) with (-1,40)" << endl; | ||
|
||
vector<int> test_4 = swap_2(1, 2); | ||
assert(test_4[0] == 2); | ||
assert(test_4[1] == 1); | ||
cout << "Swapped (1,2) with (2,1)" << endl; | ||
|
||
vector<int> test_5 = swap_2(5, 5); | ||
assert(test_5[0] == 5); | ||
assert(test_5[1] == 5); | ||
cout << "Swapped (5,5) with (5,5)" << endl; | ||
|
||
vector<int> test_6 = swap_2(-1, 40); | ||
assert(test_6[0] == 40); | ||
assert(test_6[1] == -1); | ||
cout << "Swapped (40,-1) with (-1,40)" << endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Title: Swap Variables | ||
* | ||
* Given two integers, write a function that swaps them without using any temporary variables. | ||
* | ||
* Execution: javac SwapVariables.java && java SwapVariables | ||
* For more details, check out http://www.byte-by-byte.com/swapvariables | ||
*/ | ||
|
||
public class SwapVariables { | ||
public static int[] swap_1(int x, int y) { | ||
x = x + y; | ||
y = x - y; | ||
x = x - y; | ||
|
||
int ar[] = new int[2]; | ||
ar[0] = x; | ||
ar[1] = y; | ||
return ar; | ||
} | ||
|
||
public static int[] swap_2(int x, int y) { | ||
x = x ^ y; | ||
y = x ^ y; | ||
x = x ^ y; | ||
|
||
int ar[] = new int[2]; | ||
ar[0] = x; | ||
ar[1] = y; | ||
return ar; | ||
} | ||
|
||
// Sample test cases | ||
public static void main(String[] args) { | ||
int ar_1[] = swap_1(1, 2); | ||
assert ar_1[0] == 2 && ar_1[1] == 1: | ||
"Swapped (1,2) with (2,1)"; | ||
|
||
int ar_2[] = swap_1(5, 5); | ||
assert ar_2[0] == 5 && ar_2[1] == 5: | ||
"Swapped (5,5) with (5,5)"; | ||
|
||
int ar_3[] = swap_1(-1, 40); | ||
assert ar_3[0] == 40 && ar_3[1] == -1: | ||
"Swaped(-1,40) with (40,-1)"; | ||
|
||
int ar_4[] = swap_2(1, 2); | ||
assert ar_4[0] == 2 && ar_4[1] == 1: | ||
"Swapped (1,2) with (2,1)"; | ||
|
||
int ar_5[] = swap_2(5, 5); | ||
assert ar_5[0] == 5 && ar_5[1] == 5: | ||
"Swapped (5,5) with (5,5)"; | ||
|
||
int ar_6[] = swap_2(-1, 40); | ||
assert ar_6[0] == 40 && ar_6[1] == -1: | ||
"Swapped(-1,40) with (40,-1)"; | ||
|
||
System.out.println("Passed all test cases"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
""" | ||
Title: Swap Variables | ||
Given two integers, write a function that swaps them without using any | ||
temporary variables. | ||
Execution: python SwapVariables.py | ||
For more details, check out http://www.byte-by-byte.com/swapvariables | ||
""" | ||
import unittest | ||
|
||
|
||
def swap_1(x: int, y: int): | ||
x = x + y | ||
y = x - y | ||
x = x - y | ||
return x, y | ||
|
||
|
||
def swap_2(x: int, y: int): | ||
x = x ^ y | ||
y = x ^ y | ||
x = x ^ y | ||
return x, y | ||
|
||
|
||
class TestSwap(unittest.TestCase): | ||
def test_swap_1_1_2(self): | ||
self.assertEqual(swap_1(1, 2), (2, 1)) | ||
print("Swapped (1, 2) with (2, 1)") | ||
|
||
def test_swap_1_5_5(self): | ||
self.assertEqual(swap_1(5, 5), (5, 5)) | ||
print("Swapped (5, 5) with (5, 5)") | ||
|
||
def test_swap_1_neg1_40(self): | ||
self.assertEqual(swap_1(-1, 40), (40, -1)) | ||
print("Swapped (-1, 40) with (40, -1)") | ||
|
||
def test_swap_2_1_2(self): | ||
self.assertEqual(swap_2(1, 2), (2, 1)) | ||
print("Swapped (1, 2) with (2, 1)") | ||
|
||
def test_swap_2_5_5(self): | ||
self.assertEqual(swap_2(5, 5), (5, 5)) | ||
print("Swapped (5, 5) with (5, 5)") | ||
|
||
def test_swap_2_neg1_40(self): | ||
self.assertEqual(swap_2(-1, 40), (40, -1)) | ||
print("Swapped (-1, 40) with (40, -1)") | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |