codage.py

Created by alex-juge84

Created on November 02, 2020

718 Bytes

Codage et décodage en base 36


from math import *

car = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]

def code(n):
    global car
    code = []

    while n != 0 : #Méthode des divisions succesives
        c = ((n %  36))
        c = car[c]
        code.append(str(c))
        n = n//36
           
    code.reverse()
    code = "".join(code) #Transformation en chaine
    return code


def decode(positions):
    global car
    result = []
    valeur = 0

    pos = list(positions)
    pos.reverse()

    for i,elt in enumerate(pos):
        valeur = car.index(elt)
        result.append(valeur*36**i)
    
    return(sum(result))

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.