Put out a shortened URL string in Python

I forgot somewhere in the original story, but I ported some code such as Ruby that had fallen somewhere.

# -*- coding: utf-8 -*-

"""
convert_table is a list of characters.
Use this list for radix conversion.
If the length of the list is 62, it feels like converting to 62 base.
Enter int in decimal.
"""

import math


def string_to_int(target, convert_table):

    def char_to_int(char):
        return convert_table.index(char)

    int_result = 0
    target = [char for char in target]
    target.reverse()
    for index, char in enumerate(target):
        int_result += char_to_int(char) * int(math.pow(len(convert_table), index))

    return int_result


def int_to_string(target, convert_table):

    def int_to_char(int_value):
        return convert_table[int_value]

    string_result = ""
    while target > 0:
        string_result = int_to_char(target % len(convert_table)) + string_result
        target /= len(convert_table)

    return string_result


def generate_convert_table():
    convert_table_base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
    convert_table_base_list = list(convert_table_base)
    import random
    random.shuffle(convert_table_base_list)
    return "".join(convert_table_base_list)

Recommended Posts

Put out a shortened URL string in Python
Create a random string in Python
Find out the apparent width of a string in python
Generate a class from a string in Python
How to embed a variable in a python string
String manipulation in python
To add a module to python put in Julialang
Create a datetime object from a string in Python (Python 3.3)
Insert an object inside a string in Python
Check if the string is a number in python
Parse a JSON string written to a file in Python
How to convert / restore a string with [] in python
I want to embed a variable in a Python string
[Python] How to expand variables in a character string
Take a screenshot in Python
Create a function in Python
Create a dictionary in Python
String object methods in Python
[Python] Use a string sequence
Make a bookmarklet in Python
String date manipulation in Python
Relative url handling in python
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
[python] Manage functions in a list
Hit a command in Python (Windows)
Create a DI Container in Python
Draw a scatterplot matrix in python
Put python, numpy, opencv3 in ubuntu14
Until you put Python in Docker
Write A * (A-star) algorithm in Python
Create a binary file in Python
Solve ABC036 A ~ C in Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
Implementing a simple algorithm in Python 2
Create a Kubernetes Operator in Python
Solve ABC037 A ~ C in Python
Run a simple algorithm in Python
Draw a CNN diagram in Python
Schedule a Zoom meeting in Python
When writing a program in Python
Create an instance of a predefined class from a string in Python
How to make a string into an array or an array into a string in Python
How to get a string from a command line argument in python
Timezone specification when converting a string to datetime type in python
Get the formula in an excel file as a string in Python
Generate a first class collection in Python
I put Python 2.7 in Sakura VPS 1GB.
Conversion of string <-> date (date, datetime) in Python
Use print in a Python2 lambda expression
A simple HTTP client implemented in Python
Do a non-recursive Euler Tour in Python
I made a payroll program in Python!
Precautions when pickling a function in python
Display a list of alphabets in Python 3
Try sending a SYN packet in Python
Try drawing a simple animation in Python
Note: [Python3] Convert datetime to a string in any format you like
[Itertools.permutations] How to put permutations in Python