🎉 Félicitations ! 🎉
Tu as brillamment résolu la tâche ! C était difficile, mais tu as trouvé la bonne solution. Tu es encore plus proche de maîtriser la programmation ! Continue ainsi.
ProgerLib Python pour tous
Écrivez un programme qui convertit la valeur de la ligne au format Base64 en utilisant le codage ASCII. Remarque : cette tâche utilise une version de Base64 sans remplissage (le symbole = n'est pas ajouté à la fin). L'algorithme Base64 convertit les données binaires en un format texte, en utilisant seulement 64 symboles ASCII sûrs pour l'impression. L'idée principale est de prendre 3 octets (24 bits) des données d'entrée et de les présenter sous la forme de 4 caractères (4 * 6 bits = 24 bits).
Codage (to_base64) :
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"). Une ligne est fournie en entrée.
Ligne — Le résultat de la transformation.
homme
Twfu
# Константа с символами алфавита Base64
BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
# Создаем словарь для быстрого обратного поиска (декодирования)
BASE64_MAP = {char: index for index, char in enumerate(BASE64_CHARS)}
s = input()
if not s:
print("")
# 1. Преобразуем всю строку в одну длинную битовую строку
bit_string = ""
for char in s:
# Получаем ASCII-код символа и форматируем его в 8-битную двоичную строку
bit_string += format(ord(char), '08b')
encoded_string = ""
# 2. Делим битовую строку на чанки (куски) по 6 бит
i = 0
while i < len(bit_string):
chunk = bit_string[i:i+6]
i += 6
# Если последний чанк короче 6 бит, дополняем его нулями справа
if len(chunk) < 6:
chunk = chunk.ljust(6, '0')
# 3. Конвертируем 6-битный чанк в число и находим символ в алфавите
decimal_value = int(chunk, 2)
encoded_string += BASE64_CHARS[decimal_value]
print(encoded_string)
🎉 Félicitations ! 🎉
Tu as brillamment résolu la tâche ! C était difficile, mais tu as trouvé la bonne solution. Tu es encore plus proche de maîtriser la programmation ! Continue ainsi.
AD
Publicité
Exécution de votre code...