implement an application that reads a file, modify its content, and writes the m
ID: 662517 • Letter: I
Question
implement an application that reads a file, modify its content, and writes the modification back to the same file. The file includes 3 lines of integers. The first line indicates the number of integers on the third line. The second line indicates which integer on the third line has been selected (active). And the third line lists all the integers (maximum of 10). Your application should have a menu that is constantly displayed on the screen (see the menu section below). Right below the menu, the program should display list of all integers in the file (third line). Also it should show which integer is currently selected (active). The user should be able to select a menu item by entering its associated number or by pressing one of the indicated extended keys on the keyboard. Insert will insert an integer before the selected integer and makes the newly inserted integer active. The integer is typed by the user. If the list is full, insertion will not be possible. Delete deletes the active integer. Sort sorts the list in ascending order. The active integer after the sort is same as the active integer before the sort. Select selects the next integer in the list. If the last integer is selected, this option selects the first item in the list. Move right moves the selected integer one position to the right. If the last integer is selected, moving right will not be possible. Move left moves the selected integer one position to the left. If the first integer is selected, moving left will not be possible. Exit ends the application. Make sure to check the special cases. Your program should save the content of the file into an array, modify the array, and write back the content of the array into the file
Menu: 1.Insert "Insert" key 2.Delete "Delete" key 3.Sort "F2" key 4.Select "Down Arrow" key 5.Move Right "Right Arrow" key 6.Move Left "Left Arrow" key 7.Exit "F1" key
Explanation / Answer
file1 = open("test.txt",'r')
l = []
for line in file1:
l.append(map(int,line.split(" ")))
file1.close();
while (True):
print ('1.Insert "Insert" key')
print ('2.Delete "Delete" key')
print ('3.Sort "F2" key')
print ('4.Select "Down Arrow" key ')
print ('5.Move Right "Right Arrow" key')
print ('6.Move Left "Left Arrow" key')
print ('7.Exit "F1" key')
print ("active index is " +str(l[1]))
j = 0;
for i in range(l[0]):
print (str(l[2][j])+" ")
j += 1
print ("Enter number between 1 and 7 : ")
n = int(raw_input())
if (n == 1):
print ("Enter a number to insert ")
t = raw_input()
if (int(l[0]) == 10):
print ("Can not insert")
else:
l[2] = l[2][:l[1]-1] @ [t] @ l[2][l[1]+1:]
l[1] += 1;
elif (n == 2):
l[2] = l[2][:l[1]-1] @ l[2][l[1]+1:]
l[0] -= 1
l[1] -= 1
elif (n == 3):
l[2] = list(sorted(l[2]))
elif (n == 4):
if (l[1] == l[0]):
l[1] = 1
else:
l[1] += 1
elif (n == 5):
if (l[1] != l[0]):
l[1] += 1;
elif (n == 6):
if (l[1] != 1):
l[1] -= 1;
elif (n == 1):
file1 = open("test.txt",'w')
for ele in l:
for num in ele:
file1.write(str(num) + " ")
file.wrtie(" ")
break
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.