Code coach Solutions





YouTube Link Finder
Difficulty: Medium


Problem Statement:

You and your friends like to share YouTube links all throughout the day. You want to keep track of all the videos you watch in your own personal notepad, but you find that keeping the entire link is unnecessary. 
Keep the video ID (the combination of letters and numbers at the end of the link) in your notepad to slim down the URL.


Task: 

Create a program that parses through a link, extracts, and outputs the YouTube video ID.


Input Format: 

A string containing the URL to a YouTube video. The format of the string can be in "https://www.youtube.com/watch?v=kbxkq_w51PM" or the shortened "https://youtu.be/KMBBjzp5hdc" format.


Output Format: 

A string containing the extracted YouTube video id.   


Sample Input: 

https://www.youtube.com/watch?v=RRW2aUSw5vU


Sample Output: 

RRW2aUSw5vU


Note: The input can be in two different formats.



Solution:


    x=input()
    for i in x:
        if i=='=':
            n=x.index(i)
            r=x[n+1:]
            print(r)
            exit(0)
    f=x.rfind('/')
    s=x[f+1:]
    print(s)


Explanation:

I come with simple logic is that to find the separate indexes of  '='  and the last occurrence of '/' characters. if the '=' case is executed, no need to execute the '/' case and to exit from the program using exit(0) function. In both cases, the string must be printed from index+1. rfind() method is used to hold the index of the last occurrence of the character.


Result:



Hope You understand the solution. The more you practice more you Learn. If there are any queries let us know in the comment section.


Tips to solve a problem in less time:


1. Don't read the whole problem statement initially. First, go through the input and output.


2. Mostly you will understand the problem while reading input, output, and Output explanation.

3. Now, Divide your Logic into 3 steps:

    step1: Input & output

   check whether you have written the correct syntax for input. To check you have to print the input.

    step2: Build your own logic with proper indentation.

    step3: Print the expected output.


Thank you!