+ 4

Help me in python list problem

I have run this code only one testcase is passed and another testcase is failed. this is the actual problem. https://www.hackerrank.com/challenges/JUMP_LINK__&&__python__&&__JUMP_LINK-lists/problem and this is my solution to this problem https://code.sololearn.com/ca18a0a16A22 https://code.sololearn.com/ca18a0a16A22/?ref=app

29th Apr 2021, 4:29 PM
Utsav Singh
Utsav Singh - avatar
4 Answers
+ 3
Your problem is that you're completely ignoring the user input. You read the length of input into a variable N and then just ignore the value of that variable. User input needs to guide what operations you do on the list but you hardcoded your operations to be only the specific sequence, command names, and values explained in the problem. Very minor but avoid using "list" as a variable name because list is also a class name in Python. Something like this would be a starting point: n = int(raw_input()) L = [] for i in range(n): parts = raw_input().split() for i in range(1, len(parts)): parts[i] = int(parts[i]) command = parts[0] if command == 'insert': L.insert(parts[1], parts[2]) elif command == 'print': print(L) elif command == 'append': ... and process the other commands. Hackerrank has a Discussions tab that may be useful too.
29th Apr 2021, 8:43 PM
Josh Greig
Josh Greig - avatar
+ 3
Boris Karandassov thank you boris :) I think I found my mistake now
30th Apr 2021, 4:17 AM
Utsav Singh
Utsav Singh - avatar
+ 2
Josh Greig Thank you Josh Your basics are amazing. I have found my fault now
30th Apr 2021, 4:20 AM
Utsav Singh
Utsav Singh - avatar
+ 1
lutsavsigh, That was a tough challenge to understand. Please read it carefully. The code should be more generic. In you example it is too specific, just for the one example case. Look at "Sample input 0" and think how would you read that information from the user's input. You can create a function for reading one row at a time, and perform the python method on a list, based on the command received. Within that function you can use if.. elif... -structure, for choosing between commands. 1. read the input row. 2. change input row to the list. 3. define what the received command is. 4. based on the command received, perform required operation with list (search in internet for the use of python3 methods: insert, print, remove, append, sort, pop and reverse and for their syntaxis). Here, change also possible numbers (inserted after command) to integer. 5. function should also return the list with which you are working 6. create while loop 7. connect the while loop to N received as the first input 8. in a while loop use your function for reading one row at a time.
29th Apr 2021, 9:34 PM
ボăƒȘă‚č