I want to make a reversible number lengthener, what it’s supposed to do is, you input a number, and a length if the number doesn’t have the number of characters that the length specifies, it will lengthen the number as needed, but I also need this process to be reversible. I tried just adding zeros. while this does lengthen the number, it doesn’t do what I need in this situation. I honestly couldn’t think of anything.
Hi @shedontluvjulio, welcome to the community!
This can’t work due to the fact that the identity of this operation is not reversible, assuming that you pad the user-inputted number with more digits.
For example, take the following “padded number”:
12345000
This could be a product of the original number 12345 padded with three 0s, or the number 12345000 left alone. you wouldn’t know what numbers to remove, and whether or not the 0s are part of the original number or added as padding.
What you need to do is to use a non-digit character (e.g. base64 uses = for padding, which can only be used as padding, to avoid conflicts as detailed above), such as = to lengthen the number, and whenever you want to reverse the product, just remove all =s from the number. This way, you wouldn’t be removing characters that were originally there, and not removing characters that were added as padding.
If you found my solution helpful, feel free to mark it as the solution so other community members with the same problem can easily find the solution.
EDIT: here is some code
orig_num = input("Your number: ")
length = int(input("Your length: "))
print("Original number:", orig_num)
new_num = orig_num + "=" * (length - len(orig_num))
# CANNOT USE DIGITS AS PADDING
print("New number:", new_number)
reversed_num = "".join([digit if digit != "=" for digit in new_num])
print("Reversed number:", reversed_num)
def lengthen_number(number, length):
number_str = str(number)
if len(number_str) >= length:
return number_str
else:
zeros_needed = length - len(number_str)
zeros = '0' * zeros_needed
return zeros + number_str
def shorten_number(number):
number_str = str(number)
zeros = number_str.count('0')
return number_str[zeros:]
# User input
input_num = input("Enter a number: ")
desired_length = int(input("Enter the desired length: "))
# Lengthen the number
lengthened_num = lengthen_number(input_num, desired_length)
print('Lengthened number:', lengthened_num)
# Shorten the lengthened number
shortened_num = shorten_number(lengthened_num)
print('Shortened back to original length:', shortened_num)
@Idkwhttph’s solution is close, but won’t work. It will remove all zeros, including those that were present in the original number.
E.g.
original number | New number | reverted number | success |
---|---|---|---|
12345 | 12345000 | 12345 | ![]() |
123450 | 12345000 | 12345 | ![]() |
10203 | 10203000 | 123 | ![]() |
Like I said before, you can’t use digits as padding. You have to use another character.
if
goes after the for
, and you don’t need to create a list
, so replace that with:
ch for ch in new_num if ch != "="
Also there’s a built-in function for padding:
new_num = orig_num.ljust(length, "=")
def pad(string: str, length: int=3, character: str="0"):
"""Adds `character` to the start of `string` until `len(string) == length`"""
if length > len(string):
string = character * (length - len(string)) + string
return string
def unpad(string: str, character: str="0"):
"""Removes `character` from the start of `string` until there are no more `character`s in front of the string"""
return string.lstrip(character)
again, though, there’s already a function for left-padding (or right-justifying) as well: str.rjust(width, fillchar)
I started writing this yesterday before you posted and saved it as a draft then went to sleep and finished it today