Skip to content

dnoyes/number-swapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Number Swapper

This script swaps two integers without introducing new variables, using tuple unpacking. This is very similar to destructuring in JavaScript.

Run script

Using >= python 3.3:

python swap.py

You'll be prompted to enter two numbers:

Swap two numbers! (prepare to be amazed 😎)
Type 'exit' to stop swapping

Enter first number: 7
Enter second number: 3

It will then print the original input and then the swapped input

original - x: 7, y: 3
swapped - x: 3, y: 7
💃 🕺

To stop swapping, type exit as one of the numbers.

Run Tests

First, set up a virtual env and install dependencies (pytest)

python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Then run the tests

python -m pytest tests/*.py

=================== test session starts ====================
collected 2 items

tests/test_swapnums.py ..                             [100%]
==================== 2 passed in 0.00s =====================

Deactivate virtual env when done

deactivate

Conclusion

And that's it. I hope you enjoyed this short exercise. If you want to see any other exercises, let me know :)

About

Simple number swapper that doesn't introduce new variables (tests included)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages