Good day everyone,
I need help with sorting and writing a sorting function in python. I am trying to write a function insert_in_order which takes a list of strings items and a string item. I'm trying to do this assuming that items is already sorted in alphabetical order and i must insert item into the correct position in items
Also
in regards to the same problem i am facing, i also want to right a function remove which takes a list items and a string item. This function should remove the first occurrence of item in items. Also, if item does not occur at all in items, the function should leave items unchanged.
Edit:
my original set of functions is as follows
def read_list(fname):
items = []
with open(fname, 'r') as fin:
for line in fin:
items = insert_in_order(items, line[:-1])
return items
def write_list(items, fname):
fout = open(fname, 'w')
for item in items:
fout.write(item + '\n')
fout.close()
and i also have a test file which is supposed to test those functions:
class TestLabThre(unittest.TestCase):
def test_read_list(self):
self.assertEqual(
read_list('lab06ReadTest.txt'),
['a', 'b', 'c', 'd', 'e'])
def test_write_list(self):
write_list(['a', 'b', 'c', 'd', 'e'], 'lab06WriteTest.txt')
in_file = open('lab06WriteTest.txt', 'r')
self.assertEqual(in_file.read(), 'a\nb\nc\nd\ne\n')
my insert_in_order and remove functions are supposed to be added to the functions so that when i run my tests, they pass. But i get a "failed test" every time.
I'm really confused and any help pointing me in the right direction will be appreciated.