+ 5
Youtube Link Finder Problem
Just solved this challenge a while ago, and somehow the scanf is not working when the format string doesn't have https://. Can anybody explain? https://code.sololearn.com/cU1uUpnNROIO/?ref=app
22 Answers
+ 5
ethereal you are right it works without the https:// after doing a little digging I found this:
"The format string has the following parts:
Non whitespace characters except % each of which consumes one identical character from the input stream. It can cause the function to fail if the next character on the stream does not compare equal."
So here is what I think happens. When the URL is in the short form the first scanf consumes from the input stream the https:// and then encounters the y character and not a w. Then the function fails and nothing is loaded in the variable text. Then the second scanf executes and starts reading the input stream. The https:// has been consumed by the first scanf and therefore the second scanf starts at the y.
What do you think?
https://www.programiz.com/cpp-programming/library-function/cstdio/scanf
[EDIT lol I should have read more carefully! You already came to the same conclusion above 🙃 I am glad I came across the question because I learned something]
+ 3
ethereal no worries on the upvotes, just glad to learn something. It's a good conversation that led to some enlightenment; nice!
+ 2
Aw. So that's the way to do it. Funny. I solved it by just taking the last 11 letters from the input since the video id is always 11 letters long and at the end.
+ 2
~ swim ~
At first I didn't get what you meant, but thanks to your answer I finally understood what happens here.
No, I wasn't asking why removing https:// in the input would fail to read the string. I was asking why scanf would read the input when I omitted the https:// in the second format string and when the input is, for example, https://youtu.be/KMBBjzp5hdc.
And I wasn't expecting to have multiple inputs. That's a problem on my part, I should've explained my problem well.
Anyway, the reason why the second scanf doesn't need another https:// in the format string is because it was already read in the first scanf. When the test case is the shortened youtube link, it didn't match the format string, the first scanf stopped reading the input then went to the second scanf. Since the https:// is already read, it is not needed anymore in the second one, therefore must start with youtu.be/, otherwise it would fail to read the rest of the input.
+ 2
ethereal the CC problem only gives one input at each test so you have to capture the input first and then detect which format it is in. Your code seems to assume two inputs to capture at each test with one or the other being empty depending on the URL format. That is not the case and was why your code did not work.
[EDIT: the above paragraph is not correct, read on for further detail]
As ~ swim ~ says you need a different algorithm. For example I first capture the URL and then I used a reverse search of the the string strrchr() and check for = or / to capture the starting location of the video ID based on the format (determined in my code by = or /)
You could also use the 11 character method like Mallu[inactive till March 21]
+ 2
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
import re
takeLink =list(input())
stri="".join(takeLink )
backSlash=0
if re.search("com",stri):
for i in range(len(takeLink)):
if takeLink[i]==r'=':
break
print("".join(takeLink[i+1:]))
+ 1
~ swim ~
No, I don't need to. The code works. What I want to know is why it doesn't work for youtu.be test cases. I don't understand why it wouldn't
+ 1
Paul K Sadler
No it doesn't. First of all, it works and it's not what I'm asking. Try it
+ 1
Paul K Sadler
Yeah. It seems the one marked as best answer is not easily seen by everyone. Also, sorry if I can't upvote yours, my account is not yet activted
+ 1
I guess this was a matter of bad coding. This would've been a lot easier to understand if I separated the scanf for https:// and added another scanf for www.youtube.com/watch?v= and youtu.be/
0
Swim became mod? Congrats swim
0
No, what I mean is the challenge has https:// but somehow it is not read by scanf
0
Partly, yes. But since you said
"but if the input doesn't start with "https://" then nothing will be read"
"but CC will not provide you the input again"
which was not the problem, it didn't answer my questions.
0
Well, yes. But the problem is when the input is like this
https://youtu.be/KMBBjzp5hdc
and if you try to put https:// in the second scanf, it wouldn't read it, which makes my explanation correct.
0
Which language?
0
Tuguldur (Төгөлдөр)
That's written in C
0
Ok guys give me something to code for beginners