Reverse Number Coding Challenge

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this challenge, you will be tasked with creating a function that reverses a given number. The function should take a number as an argument and return the reverse of that number.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13708{{"`Reverse Number Coding Challenge`"}} python/variables_data_types -.-> lab-13708{{"`Reverse Number Coding Challenge`"}} python/numeric_types -.-> lab-13708{{"`Reverse Number Coding Challenge`"}} python/strings -.-> lab-13708{{"`Reverse Number Coding Challenge`"}} python/type_conversion -.-> lab-13708{{"`Reverse Number Coding Challenge`"}} python/lists -.-> lab-13708{{"`Reverse Number Coding Challenge`"}} python/tuples -.-> lab-13708{{"`Reverse Number Coding Challenge`"}} python/function_definition -.-> lab-13708{{"`Reverse Number Coding Challenge`"}} python/importing_modules -.-> lab-13708{{"`Reverse Number Coding Challenge`"}} python/using_packages -.-> lab-13708{{"`Reverse Number Coding Challenge`"}} python/standard_libraries -.-> lab-13708{{"`Reverse Number Coding Challenge`"}} python/math_random -.-> lab-13708{{"`Reverse Number Coding Challenge`"}} python/build_in_functions -.-> lab-13708{{"`Reverse Number Coding Challenge`"}} end

Reverse Number

Write a function reverse_number(n) that takes a number as an argument and returns the reverse of that number. The function should meet the following requirements:

  • The function should reverse the number, regardless of whether it is positive or negative.
  • The function should return a float if the input is a float, and an integer if the input is an integer.
  • The function should not use any built-in functions that directly reverse a number (e.g. reversed()).
  • The function should not use any built-in functions that directly convert a number to a string (e.g. str()).
  • The function should not use any built-in functions that directly convert a string to a number (e.g. int() or float()).
from math import copysign

def reverse_number(n):
  return copysign(float(str(n)[::-1].replace('-', '')), n)
reverse_number(981) ## 189
reverse_number(-500) ## -5
reverse_number(73.6) ## 6.37
reverse_number(-5.23) ## -32.5

Summary

In this challenge, you have learned how to reverse a number using Python. You have also learned how to handle both positive and negative numbers, as well as floats and integers. Keep practicing and exploring Python to become a better programmer!

Other Python Tutorials you may like