62 scripts de conversion décimale <-> par langage (R, Python, Java)

Aperçu

62 scripts de conversion décimale <-> par langue

Langue

R

Installation R

$ sudo yum -y install epel-release
$ sudo yum -y install R --enablerepo=epel

scénario

convert_decimal62.r


encode_decimal62 <- function(num, chars=c(0:9, letters, LETTERS)) {
    base <- length(chars)
    str <- NULL
    repeat {
        str <- c(chars[num %% base+1], str)
        num <- num %/% base
        if (num == 0) {
            break
        }
    }
    paste(str, collapse="")
}

decode_decimal62 <- function(str, chars=c(0:9, letters, LETTERS)) {
    base <- length(chars)
    str_list <- unlist(strsplit(str, split = ""))
    num <- 0
    for (char in str_list) {
        num <- num * base + which(chars == char) - 1
    }
    num
}

encode_decimal62(0)
encode_decimal62(12345)

decode_decimal62("0")
decode_decimal62("3d7")

Courir

$ Rscript decode_decimal62.r
[1] "0"
[1] "3d7"
[1] 0
[1] 12345

python

convert_decimal62.py


alnum = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJELMNOPQRSTUVWXYZ"

def encode_decimal62(num, chars = alnum):
    base = len(chars)
    string = ""
    while True:
        string = chars[num % base] + string
        num = num // base
        if num == 0:
            break
    return string

def decode_decimal62(string, chars = alnum):
    base = len(chars)
    num = 0
    for char in string:
        num = num * base + chars.index(char)
    return num

print(encode_decimal62(0))
print(encode_decimal62(12345))

print(decode_decimal62('0'))
print(decode_decimal62('3d7'))

Courir

$ python convert_decimal62.py
0
3d7
0
12345

Java

Decimal62Converter.java


import java.util.regex.Pattern;

public class Decimal62Converter {
    private static final String TABLE = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final int BASE = 62;

    public static void main(String[] args) {
        System.out.println(encrypt(0));
        System.out.println(encrypt(12345));
        System.out.println(decrypt("0"));
        System.out.println(decrypt("3d7"));
    }

    /** concatenate integers
     * 
     * @param digits
     * @return string value of concatenated integers
     */
    public static long concatnateDigits(int... digits)
    {
        StringBuffer sb = new StringBuffer(digits.length);
        for(int digit: digits) {
            sb.append(digit);
        }
        return Long.parseLong(sb.toString());
    }
     
    /** convert integer to alphanumeric characters
     * 
     * @param number
     * @return encrypted string
     */
    public static String encrypt(long number)
    {
        StringBuffer sb = new StringBuffer();
        while(true) {
            sb.insert(0, TABLE.charAt((int)(number % BASE)));
            number = number / BASE;

            if (number == 0) {
                break;
            }
        }
        return sb.toString();
    }

    /** convert alphanumeric characters to integer
     * 
     * @param phrase
     * @return Integer converted from phrase.
     *         Return -1 if the phrase contains not convertible character.
     */
    public static long decrypt(String phrase)
    {
        // validation.
        // if the phrase has character other than in TABLE
        // return -1;
        if(Pattern.compile("[^" + TABLE + "]").matcher(phrase).find()) {
            return -1;
        }
        String reverse = new StringBuffer(phrase).reverse().toString();
        long decimal = 0;
        for(int i = 0; i < reverse.length(); i++) {
            int digit = TABLE.indexOf(reverse.charAt(i));
            decimal = (long) (decimal + digit * Math.pow(BASE, i));
        }
        return decimal;
    }
}

compiler

$ javac Decimal62Converter.java

Courir

$ java Decimal62Converter
0
3d7
0
12345

référence

Recommended Posts

62 scripts de conversion décimale <-> par langage (R, Python, Java)
100 Language Processing Knock Chapitre 1 par Python
4 langage de comparaison de fermeture (Python, JavaScript, Java, C ++)
[Python] [R] Rendre le GPU reconnu par tensorflow-2.x
Communication socket par langage C et Python
[Traitement du langage 100 coups 2020] Résumé des exemples de réponses par Python
L'histoire de la conversion automatique du langage de TypeScript / JavaScript / Python