• 1
    Input and Output Data
    • Tasks
  • 2
    Conditions
    • Tasks
  • 3
    For Loop
    • Tasks
  • 4
    Strings
    • Tasks
  • 5
    While Loop
    • Tasks
  • 6
    Lists
    • Tasks
  • 7
    Two-Dimensional Arrays
    • Tasks
  • 8
    Dictionaries
    • Tasks
  • 9
    Sets
    • Tasks
  • 10
    Functions and Recursion
    • Tasks
  • к

Занятие 4. Lines

Difficulty level:

Task«Sports games»

write a program that converts the line value to the Base64 format using the ASCII encoding. 14pt; "> note: & nbsp; this task uses a version of Base64 without filling (the symbol & nbsp; = is not added to the end). Style = "Font-Size: 14pt;"> The Base64 algorithm converts binary data into a text format, using only 64 ASCII symbols safe for printing. The main idea is to take 3 bytes (24 bits) of the input data and present them in the form of 4 characters (4 * 6 bits = 24 bits).

rn

coding ( to_base64 ):

rn
    rn
  1. is converted into a sequence ASCII-codes.
  2. rn
  3. each ascii code is presented as an 8-bit binary number.
  4. rn
  5. all these 8-bit sequences are combined into one long bite line.
  6. rn
  7. This long line is cut into groups of 6 bits.
  8. rn
  9. each 6-bit group is converted to a decimal number (from 0 to decimal 63).
  10. rn
  11. this number is used as an index for choosing a symbol from the alphabet of the alphabet Base64 ( "ABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/" ).
  12. rn

    Input format

    A line is supplied to the input.

    Output format

    Line & mdash; The result of the transformation.

    Example

    Input

    man

    Output

    Twfu

    Hint

    There will be no clue here, decide for yourself!

main.py
Test 1
Test 2
Test 3
Test 4
Test 5
Test 6
Test 7
Test 8
Test 9
Test 10
Developer’s solution
# Константа с символами алфавита 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) 

🎉 Congratulations! 🎉

You did an excellent job with the task! It was a challenging problem, but you found the correct solution. You are one step closer to mastering programming! Keep up the good work, because every stage you pass makes you even stronger.

AD

Advertisement

red-snake blue-snake green-snake

Running your code...

Помощник ИИ

Привет! Я твой помощник по программированию. Задавай любые вопросы по Python, я могу рассказать о функциях, методах, обьяснить то, что тебе не понятно, а так же о текущей задаче!