-
Notifications
You must be signed in to change notification settings - Fork 1
/
Basic-Data-Type-lists.py
27 lines (24 loc) · 1.59 KB
/
Basic-Data-Type-lists.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# solution notes
# task: perform certain operations on list based on inputs like insert, pop, print etc.
if __name__ == '__main__':
my_list = [] # list on which the operations to be performed
N = int(input()) # number of operations to be performed
for _ in range(N): # running loop n times to get n inputs
string = input().split() # hetting input and splitting it into parts(eg. input = insert 0 5 so we split it as ['insert','0','5'])
command = string[0] # our command is the 0 index element of the list so we save it in a varibale
argument = string[1:] # all the elements from index 1 to last index are our argument so we save it in a varibale with help of slicing
if command != "print": # print command is used in a different way than the regular commands in a list so when the command is not equal to print we do certain operation
command += "("+ ",".join(argument) +")" # concatinate command + "(" + ",".join(argument) + ")" => command(argument1, argument2)
eval("my_list."+command) # now use eval function to evaluate the string => "my_list."+command => "my_list."+"command(argument1, argument2)" => evaluated as my_list.command(argument1, argument2)
else: # and if the command is equal to print then print the list simply
print(my_list)
# solution 2 - 21 feb 2022
if __name__ == '__main__':
N = int(input())
arr = []
for _ in range(N):
command = input().split()
if command[0] != 'print':
eval('arr.'+command[0]+'('+','.join(command[1:])+')')
else:
print(arr)