String Processing in Python: Integer to String



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:

Nguồn: https://benjaminjcohen.com/

Xem thêm bài viết khác: https://benjaminjcohen.com/cong-nghe/

3 Comments

  1. 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.

  2. So much useful knowledge here. Again, chapeux bas!

  3. 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

Leave a Reply

Your email address will not be published. Required fields are marked *