Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entry Challenge C1 #1

Open
hum-dev opened this issue Aug 25, 2024 · 3 comments · May be fixed by #112
Open

Entry Challenge C1 #1

hum-dev opened this issue Aug 25, 2024 · 3 comments · May be fixed by #112
Labels
Challenge For Challenges

Comments

@hum-dev
Copy link
Collaborator

hum-dev commented Aug 25, 2024

Entry Challenge C1

Reverse Integer

Write a program that takes an integer as input and return an integer with reversed digit
ordering.

For example

For input 500, the program should return 5. 
For input -56, the program should return -65. 
For input -90, the program should return -9. 
For input 91, the program should return 19. 

@hum-dev hum-dev added the Challenge For Challenges label Aug 25, 2024
@Ocheezy-glitch
Copy link

def reverse_integer(n):
# Check if the number is negative
negative = n < 0
# Convert the number to a string, remove the sign if negative, and reverse the digits
reversed_str = str(abs(n))[::-1]
# Convert back to integer and restore the sign if it was negative
reversed_int = int(reversed_str) * (-1 if negative else 1)
return reversed_int

Input from the user

number = int(input("Enter an integer: "))
result = reverse_integer(number)
print("Reversed integer:", result)

@daviewisdm
Copy link

def reverse_integer(n):
# Convert to string to easily manipulate digits
str_n = str(abs(n))

# Reverse the digits
reversed_str = str_n[::-1]

# Convert back to integer and maintain original sign
reversed_n = int(reversed_str)

# If the original number was negative, make the result negative
if n < 0:
    reversed_n = -reversed_n

return reversed_n

Test cases

print(reverse_integer(500)) # Should print 5
print(reverse_integer(-56)) # Should print -65
print(reverse_integer(-90)) # Should print -9
print(reverse_integer(91)) # Should print 19

@gateremark
Copy link
Contributor

STOP WRITING YOUR SOLUTIONS IN THE ISSUES KINDLY.

Create a Fork; Work on the challenge; Then create a PR (Pull Request) with your solution(s).

  • Check the How to Submit section in the email you have received.

Thank you.

cc: @Angote433 , @daviewisdm , @Ocheezy-glitch

MuthoniMN added a commit to MuthoniMN/DSA-Clinics24 that referenced this issue Oct 10, 2024
@NewtonMutugi NewtonMutugi linked a pull request Oct 10, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Challenge For Challenges
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants