String Processing in Python: Integer to String

preview_player
Показать описание
In this video, we will be solving the following problem:

You are given some integer as input, (i.e. ... -3, -2, -1, 0, 1, 2, 3 ...)
Convert the integer you are given to a string. Do not make use
of the built-in "str" function.
Examples:
Input: 123
Output: "123"
Input: -123
Output: "-123"

This video is part of a series on string processing and, specifically, on how these problems tend to show up in the context of a technical interview:

This video is also part of an "Algorithm" series. For more algorithm tutorials:

The software written in this video is available at:

Do you like the development environment I'm using in this video? It's a customized version of vim that's enhanced for Python development. If you want to see how I set up my vim, I have a series on this here:

If you've found this video helpful and want to stay up-to-date with the latest videos posted on this channel, please subscribe:
Рекомендации по теме
Комментарии
Автор

Okay. I have almost completed the Data structure and algorithms course on Educative and co-incidentally, everything is same with your videos and educative lecture.I could not have completed without your videos.I could not understand anything from article and every video helped me.You are true gem.I wonder why there is only a 90 likes on video.Highly underrated programming teaching channel.

arungiri
Автор

So much useful knowledge here. Again, chapeux bas!

mateuszsmendowski
Автор

Exceptional video! I would never have thought the chr() and ord() functions and how the could be used.

But in respect to the "appending to a list and then reverse" issue wouldn't be simpler to do something like the following?


def int_to_str(input_int: int) -> str:
"""
You are given some integer as input, (i.e ... -3, -2, -1, 0, 1, 2, 3, ...)

Convert the integer you are given to a string. Do not make use of the built-in
"str" function.

Examples:
Input: 123
Output: "123"

Input: -123
Output: "-123"


:param input_int: int
:return: str
"""

is_negative = False
if input_int < 0:
is_negative = True
input_int *= -1

output_str = ''

while input_int > 0:

last_digit = input_int % 10

last_digit_str = chr((ord('0')) + last_digit)

# since the order of the extraction is the opposite of the number that we want to show
# we concatenate the string by putting the extracted digit_str to the beginning

output_str = last_digit_str + output_str

input_int //= 10

if is_negative:
output_str = '-' + output_str

return output_str

GiorgosPerakis
Автор

Thank you for this, I'm looking for some direction here; Write a function that receives an integer and returns it in words:- Given 123 should return 'one hundred and twenty three'. How would you approach this?

sostomc
Автор

brother help us the video how to change this number 76896 to replac 2 to remove 8 to be come like this 76296 using python.

johnmwansa