Технологии
Есть задание прошу помочь, Ajeje the librarian, recently found a hidden room in the Keras Library (a great place located in Umkansa, the largest - вопрос №5329801
village in the White Mountains). There, she discovered several books, containing music scores of ancient Tarahumara songs. So, she invited over a musician friend to have a look at them, and he informed her that the scores are written in Tarahumara notation and need to be translated into a notation familiar to Umkansanian musicians, so they can play them back. Tarahumaras used numbers instead of letters for writing notes: 0 in place of A, 1 in place of B, and so on, until 7 in place of G. Flat (b) and sharp (#) notes (see note 3 below, if you do not know what flat and sharp notes are) were followed by a — and a +, respectively (for example, 0- meant flat A). Moreover, they just repeated the same number multiple times to represent the note's duration. For example, 0000 would mean that the A note had a length of 4, while 0-0-0-0- would mean that the A flat note had a length of 4.
ноябрь 18, 2023 г.
-
Всего ответов: 1
-
import os
def read_index_file(source_root):
with open(os.path.join(source_root, 'index.txt'), 'r') as index_file:
songs = [line.strip().split(' ') for line in index_file.readlines()]
return songs
def tarahumara_to_umkansanian(note):
mapping = {'0': 'A', '1': 'B', '2': 'C', '3': 'D', '4': 'E', '5': 'F', '6': 'G', '7': 'G'}
return mapping[note]
def translate_song(source_root, song_path):
with open(os.path.join(source_root, song_path), 'r') as song_file:
content = song_file.read()
translated_notes = []
duration = 0
i = 0
while i < len(content):
if content[i] in '01234567':
note = tarahumara_to_umkansanian(content[i])
count = 1
i += 1
while i < len(content) and content[i] == content[i — 1]:
count += 1
i += 1
translated_notes.append(f'{note}{count}')
duration += count
elif content[i] == ' ':
count = 1
i += 1
while i < len(content) and content[i] == ' ':
count += 1
i += 1
translated_notes.append(f'P{count}')
duration += count
else:
i += 1
return ''.join(translated_notes), duration
def save_translated_song(target_root, song_path, translated_content):
target_path = os.path.join(target_root, song_path)
os.makedirs(os.path.dirname(target_path), exist_ok=True)
with open(target_path, 'w') as song_file:
song_file.write(translated_content)
def Umkansanize(source_root:str, target_root:str) -> dict[str,int]:
songs = read_index_file(source_root)
song_lengths = {}
for title, song_path in songs:
translated_song, duration = translate_song(source_root, song_path)
save_translated_song(target_root, song_path, translated_song)
song_lengths[title] = duration
with open(os.path.join(target_root, 'index.txt'), 'w') as index_file:
for title, duration in sorted(song_lengths.items(), key=lambda x: (-x[1], x[0])):
index_file.write(f'"{title}" {duration}\n')
return song_lengths
Похожие вопросы
Где можно расшифровать/декодировать этот текст? backUsageMsg=\u00a76\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u0441\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u043e...
декабрь 30, 2014 г.
vip
Здравствуйте, недавно появился непонятный файл папка: Путь к ней: C/Windows/System32/Tasks Далее в Tasks появилась папка Agent Activation Runtime А там файл S-1-5-21 и куча цифр потом. Что это за
май 28, 2021 г.
Решено
Здравствуйте! Есть ли bluetooth адаптер для мр3 плеера у которого нет встроенного Bluetooth, чтобы подключить их к беспроводным наушникам? Если есть, то можете ли вы посоветовать какой-нибудь со
апрель 21, 2020 г.
Скажите мне, к мыши aceline WM - 904RU нету драйверов вообще или они идут уже изначально? Просто я купил недавно эту модель мыши и при установке
Вопрос задан анонимно декабрь 20, 2019 г.