# List Problem with user input

Consider a list ( `list = []` ). You can perform the following commands:

1. `insert i e` : Insert integer e at position i.
2. `print` : Print the list.
3. `remove e` : Delete the first occurrence of integer e .
4. `append e` : Insert integer e at the end of the list.
5. `sort` : Sort the list.
6. `pop` : Pop the last element from the list.
7. `reverse` : Reverse the list.

Initialize your list and read in the value of n followed by n lines of commands where each command will be of the 7 types listed above. Iterate through each command in order and perform the corresponding operation on your list.

Input Format

The first line contains an integer n denoting the number of commands.
Each line i of the n subsequent lines contains one of the commands described above.

Constraints

• The elements added to the list must be integers.

Output Format

For each command of type `print`, print the list on a new line.

Sample Input 0

12
insert 0 5
insert 1 10
insert 0 6
print remove 6
append 9
append 1
sort print pop reverse print

Sample Output 0

[6, 5, 10]
[1, 5, 9, 10]
[9, 5, 1]

``````# my code <>
list_ = []
# These are functions that are dependent on users choice of input
def insert_(index,value):
list_.insert(index,value)
return list_

def remove_(x):
list_.remove(x)
return list_

def append_(x):
list_.append(x)
return list_

def sort_():
list_.sort()
return list_

def pop_():
list_.pop()
return list_

def reverse_():
list_.reverse()
return list_

# Main

num_command = int(input())
count = 0
while count < num_command:
cmd = input()
i= cmd.split()
count+=1

for x in i:
if x == 'insert':
insert_(x,x)

elif x == 'print':
print(list_)

elif x == 'remove':
remove_(x)

elif x == 'append':
append_(x)

elif x == 'sort':
sort_()

elif x == 'pop':
pop_()

elif x == 'reverse':
reverse_()
``````

Why am I not getting any sort of output? My structure of logic is correct where am I going wrong.

( I did search online for answers but I got other programmers solution whose code is much shorter than me but I fail to totally understand their code )

I suspect that `x` is not what you think it is.

Consider the input `insert 0 1`:

``````==> cmd == 'insert 0 1'
==> i = cmd.split() == ['insert', '0', '1']``````

then in the first pass of the for-loop:

``````==> x == 'insert'
==> x == 'i'``````

…which doesn’t match any of your tests.

To help you debug this, I would add an else branch at the end of your if statement that runs anyaway and tells you that none of the other tests succeeded. And you could also give your variables more descriptive names – I think this makes more of a difference to our brain than we normally think.

1 Like

I’m just curious. Are that homework of yours? To be most helpful to you, I suggest you try as hard as you can and as long as it took, to solve it by yourself. You only will learn to program by doing it by yourself. Good luck.

3 Likes

No, that isn’t homework of mine, I completed LPTHW recently and I think the most efficient way to learn is to tackle problems online. I am practising python on HackerRank.

I do, but I don’t want to sit idle for too long in front of the laptop, before posting anything here I do thorough a search online and also write my code as weird as possible to reach a particular solution.
At first, I used to write my doubts on StackOverflow but at there people think my question is just stupid and straightaway downvote my questions and I don’t get the answers for decades lol.
Then I actually got to know that there’s a forum by Zed Shaw and I started to post here and luckily people are helping me here.
Sometimes I feel bad that I ask too much and Florian is helping me all the time but I am grateful that someone is helping me.
If you know how can I improve my programming skills on Python then please do let me know?

1 Like

Yeah, I agree with @DidierCH: keep banging your head against such problems, that’s pretty much the only way to get better at solving them on your own. But don’t feel bad for asking! I think we all help here because we like it. Now, not to undermine that… but I found this intriguing, sort of a mini-mini-mini language for handling lists. I made my own version, and I kinda like it so I thought I’d post it here. Do criticise!

``````from collections import namedtuple
Action = namedtuple('Action', 'call argc')

actions = {
"print": Action(print, 0),
"append": Action(list.append, 1),
"pop": Action(list.pop, 0),
"insert": Action(list.insert, 2),
"remove": Action(list.remove, 1),
"sort": Action(list.sort, 0),
"reverse": Action(list.reverse, 0),
}

l = []
n = int(input("How many commands? "))
c = 0

while c < n:
line = input().split()
line.reverse()

while line:
try:
action = actions[line.pop()]
args = []
while len(args) < action.argc:
args.append(int(line.pop()))
action.call(l, *args)
c += 1
except:
print("Um... what?")
exit()
``````
2 Likes

Hey, no problem. I just thought it’s homework because of the style you described your problem Keep going!

1 Like

Thanks, I kind of didn’t get the for loop working but now I do, so I did the necessary changes and now my code works.

``````list = []
# These are functions that are dependent on users choice of input
def insert_(index,value):
list.insert(index,value)
return list

def print_():
print(list)

def remove_(x):
list.remove(x)
return list

def append_(x):
list.append(x)
return list

def sort_():
list.sort()
return list

def pop_():
list.pop()
return list

def reverse_():
list.reverse()
return list

# Main

num_command = int(input())
count = 0
while count < num_command:
cmd = input()
i= cmd.split()
count+=1

if i == 'insert':
insert_(int(i),int(i))

elif i == 'print':
print(list)

elif i == 'remove':
remove_(int(i))

elif i == 'append':
append_(int(i))

elif i == 'sort':
sort_()

elif i == 'pop':
pop_()

elif i == 'reverse':
reverse_()
else:
print("Error")
``````

Your answer is also interesting I didn’t think of using dictionary. I am still a bit not used to dictionary, but I will try to solve the same problem now with dictionary. 1 Like

I know it’s a matter of style but using the syntax methodname_() looks odd.

Usually _methodname() is a Python style for private (but not really private) which you are not using of course, but it looks weird when I see yours.

As a suggestion for learning, why not try to understand the other solutions that you couldn’t work out? That might yield more results?

1 Like

Just a quick look but you don’t actually print anything. Try this at the very bottom, in the 0 column:

``````print(list_)
``````

Also, you can probably get rid of the `num_command` variable and just loop until someone types “quit”.

1 Like