• 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«Video»

you decided to see a very long stream, recording a lecture or the passage of the game. The player displays the time that you have already watched, and the total duration of the video. Your task & mdash; Write a program that will determine what share from the whole video you have already watched and present this share in the form of a simple unratestible fraction.

rn

for example, if you looked at 2 hours 20 minutes, it is 1/3 of all of all. Video.

rn

The program should take two lines to the input, representing the time in the "HF: MM: SS": the first line & mdash; Viewed time, second & mdash; total duration. Then the program must calculate and display a share in the form of a list of two numbers & nbsp; [numerator, denominator] & nbsp; and accompany the output with an understandable text message.

rn Important restriction: & nbsp; to find the greatest common divider and simplification of fraction, you cannot use built -in functions from libraries (for example, from the library & nbsp; math ). You must implement the search algorithm on your own using the & nbsp; for .

rn

& nbsp;

Input format

Viewed time (line in the format "HCh: MM: SS")

Output format

Simplified fraction (a list of two integers)

Example

Input

02:20:00
07:00:00

Output

You watched 1/3 of the total video.

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
# Функция для преобразования времени в секунды
def time_to_seconds(time_str):
    # Разделяем строку времени на часы, минуты, секунды
    hours, minutes, seconds = map(int, time_str.split(':'))
    # Преобразуем все в секунды
    total_seconds = hours * 3600 + minutes * 60 + seconds
    return total_seconds

# Функция для нахождения наибольшего общего делителя (НОД)
def find_gcd(a, b):
    # Используем алгоритм Евклида
    while b != 0:
        a, b = b, a % b
    return a

# Считываем время просмотра и общее время
watched_time = input()
total_time = input()

# Преобразуем время в секунды
watched_seconds = time_to_seconds(watched_time)
total_seconds = time_to_seconds(total_time)

# Вычисляем долю просмотренного времени в виде дроби
numerator = watched_seconds
denominator = total_seconds

# Находим НОД для сокращения дроби
gcd = find_gcd(numerator, denominator)

# Сокращаем дробь
numerator //= gcd
denominator //= gcd

# Выводим результат
print(f"Вы просмотрели {numerator}/{denominator} всего видео.")

🎉 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, я могу рассказать о функциях, методах, обьяснить то, что тебе не понятно, а так же о текущей задаче!