Есть задание прошу помочь, 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.Pauses were written down as spaces; for example, twelve spaces represent a pause of 12. Both notes and pauses could span different lines of the score (e.g., starting on line x and continuing on line x + 1, x + 2, and so on). Finally, music scores were written from right to left and from top to bottom, and going to a new line did not mean anything in terms of the music score. Umkansanians, instead, are used to write down notes using letters, and each note is followed by its duration (so, the example above would be written as A4). Flat and sharp notes are followed by a b or a #, respectively (for example, A flat is written as Ab, so the example above would be written ad Ab4). Pauses are written using the letter P, followed by their duration, and no spaces are used at all. Finally, they are used to read music from left to right on a single row. As Ajeje knows that you are a skilled programmer, she provides you with a folder containing the transcription of all the Tarahumara songs she found, organized in multiple subfolders and files (one song per file). Also, she prepared an index file in which each row contains the title of a Tarahumara song (in quotes), followed by a space and the path of the file containing that song (in quotes, relative to the root folder). She would like to translate all the songs listed in the index and store them into new files, each one named with the title of the song it contains (.txt), in a folder structure matching the original one. Also, she would like to store in the root folder of the created structure, a file containing on each row the title of a song (in quotes) and the corresponding song length, separated by a space. Songs in the index need to be ordered in descending length and, if the length of some songs is the same, in ascending alphabetical order. The length of a song is the sum of the durations of all notes and pauses it is made of. Would you be able to help Ajeje out in translating the Tarahumara songs into Umkansanian ones? Note 0: below, you are provided with a function to Umkansanize the Tarahumara songs; after being executed, it must return a dictionary in which each key is a song title and the associated value is the song's duration Note 1: the songs index file index.txt is stored in the source_root folder Note 2: the index of the translated songs index.txt is in the target_root folder Note 3: flat and sharp notes are just «altered» versions of regular notes; for example an F# («F sharp») is the altered version of an F, that is, an F note which is a half of a tone higher than a regular F; the same holds for flat notes, which are a half of a tone lower than regular notes; from the point of view of the homework, flat and sharp notes must be treated the same as regular notes (except for their notation). Note 4: to create the directory structure you can use the 'os' library functions (e.g. os.makedirs) ''' import os def Umkansanize(source_root:str, target_root:str) -> dict[str,int]: pass q2a.di.uniroma1.it/assets/programming-23-24/HW4req.zip?v=0
18.11.23
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
18.11.23

Глеб Черняк

Читать ответы

Олег Николаевич

Читать ответы

Alexander

Читать ответы
Посмотреть всех экспертов из раздела Технологии
Пользуйтесь нашим приложением Доступно на Google Play Загрузите в App Store