+ 1

I'm not able to pass Test Case #2 (YouTube ID Challenge)

I'm not able to pass Test Case #2, although my code is working perfectly with any youtube video url. Pleass help me :( Also, I'm trying it on C language. https://code.sololearn.com/c4uqeYk73KKD/?ref=app https://code.sololearn.com/c4uqeYk73KKD/?ref=app

16th Jun 2021, 12:38 PM
I Am a Baked Potato
I Am a Baked Potato - avatar
15 Respostas
+ 1
What is your logic of using indexes from 17 to 28 ? I don't understand, but you printing extra charecter there, so use i<28 instead of I<=28
16th Jun 2021, 1:16 PM
Jayakrishna 🇮🇳
+ 1
Tushar Kumar For passing all the SL test cases, this code is enough: #include <stdio.h> int main() { char a[100]; fgets(a, 100, stdin); puts(a + 17 + 15 * (a[8] == 'w')); } // Hope this helps
16th Jun 2021, 6:05 PM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Calvin Thomas please explain this part a + 17 + 15 * (a[8] == 'w')
17th Jun 2021, 5:43 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
+ 1
Tushar Kumar The 'puts()' function takes a char pointer as its argument. From this character pointer onwards, the function writes every character to the stdout file (the operating system prints every single character to the screen) until a null character is encountered. In my code, whenever the ninth character of the input text contains a 'w' (i.e., the www format), the video id starts from the 33rd position onwards, all the way to the last position. In this case, "17 + 15 * (a[8] == 'w')" evaluates to "17 + 15 * (1)" {since C doesn't have a boolean type, '1' means true and '0' means false in boolean expressions} which finally evaluates to "32". This offset is added to the char pointer, thereby, making it point to the 32nd element from the first letter (i.e., the 33rd character, which is actually the starting point of the video id). Note that pointer arithmetic works like this: Pointer + offset ==> ptr + n * sizeof(ptr)
17th Jun 2021, 5:57 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Now, whenever the input is of the second form, the ninth character isn't 'w'. So, "17 + 15 * (a[8] == 'w')" evaluates to "17 + 15 * (0)" (as '0' means 'false' in C), which in turn evaluates to "17", meaning that the id starts from the 18th character onwards. I hope that this explanation helps.
17th Jun 2021, 6:05 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Calvin Thomas Great explanation! This means a lot, thank you.
17th Jun 2021, 6:15 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
+ 1
//Why this has to be too much complex #include <stdio.h> #include <string.h> int main() { int index; char url[40]; scanf("%s", url); index = strlen(url) - 11; puts(url + index); }
17th Jun 2021, 6:42 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 1
🌀 Shail Murtaza شعیل مرتضیٰ Well ya not in challenge but this case may arise when user open a video link using shortened youtu.be link.
17th Jun 2021, 7:32 PM
I Am a Baked Potato
I Am a Baked Potato - avatar
+ 1
Tushar Kumar Here's another way to do this without the need to store all the elements of the array: for (char i=0,j=0,k;(k=getchar())>0;i++) { if (i == 8 && k == 'w') j++; else if (i > 16 + 15 * j) putchar(k); } // Max. memory used = 3 bytes // Hope this helps
18th Jun 2021, 3:07 AM
Calvin Thomas
Calvin Thomas - avatar
0
Jayakrishna🇮🇳 Omg Thank you So much! It worked and I passed the both cases. Actually here when condition is true I'm trying to print next 11 character from current index cos it contains youtube video ID.
16th Jun 2021, 1:27 PM
I Am a Baked Potato
I Am a Baked Potato - avatar
0
Oh. I have no idea of fixed length 11 charecters. I think it may vary.. and problem contains 1st,3rd type of input only.. so 2nd,4th if blocks not useful here. Anyways its done.👍 You're welcome...
16th Jun 2021, 1:43 PM
Jayakrishna 🇮🇳
0
Calvin Thomas Thank you Sir, it's really helpful. Just wondering how arguments inside puts is working. Please explain that part.
17th Jun 2021, 4:48 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
0
Tushar Kumar You're welcome.
17th Jun 2021, 8:14 AM
Calvin Thomas
Calvin Thomas - avatar
0
🌀 Shail Murtaza شعیل مرتضیٰ Well maybe becaus of this case where video ID is somewhere at the middle like https://m.youtube.com/watch?v=bWLgA4jUkuw&feature=youtu.be (this case not included in challenge)
17th Jun 2021, 6:54 PM
I Am a Baked Potato
I Am a Baked Potato - avatar
0
Tushar Kumar Ok But! There is not any case like that
17th Jun 2021, 6:56 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar