Exemplo n.º 1
0
    def frequency(self):
        '''
        This function prints the number of times each word occurs in the list.
        :param: None
        :precondition: None
        :postcondition: The output is the word alongside the number of times it has occurred in the list
        :complexity: Best Case = Worst Case = O(n), where n is the length of the list
        '''

        self.tmp = Arrayfreq()
        self.final_list = Arrayfreq()
        for i in range(1, len(self.array) + 1):
            current_words = self.array[i].split(
                ' ')  # for every line, splitting to get the words
            for j in range(len(current_words)):
                self.tmp.append(
                    current_words[j])  # adding the words to a temporary list
        bubble_sort(self.tmp)  # sorting the list
        count = 1
        for b in range(len(self.tmp)):
            for a in range(b + 1, len(
                    self.tmp)):  # frequency of words is case sensitive
                if self.tmp[b] == self.tmp[a]:
                    count += 1
            if self.tmp[b] not in self.final_list:
                self.final_list.append(
                    self.tmp[b]
                )  # searching through the list and appending the word and its count
                self.final_list.append(count)
            count = 1

        for k in range(0, len(self.final_list) - 1, 2):
            print(self.final_list[k], self.final_list[k + 1])

        return True
Exemplo n.º 2
0
 def test_len(self):
     obj = ArrayOperations()
     self.assertEqual(0,
                      len(obj))  # Case_1: Checks whether the self.count = 0
     obj.append(1)
     obj.append(2)
     self.assertEqual(
         2, len(obj)
     )  # Case_2: Appends two items into list to check whether self.count = 2
Exemplo n.º 3
0
 def test_str(self):
     obj = ArrayOperations()
     self.assertEqual(str(obj),
                      "")  # Case_1: checks whether the empty list gives ""
     obj.append(1)
     obj.append(2)
     self.assertEqual(
         str(obj),
         "1\n2\n")  # Case_2: For the list [1,2], we will get "1\n2\n"
Exemplo n.º 4
0
 def test_append(self):
     obj = ArrayOperations()
     obj.append(20)
     obj.append(40)
     self.assertEqual(
         40, obj[1]
     )  # Case_1: Checks whether the item appended is at the last index, if last appended item equals index count - 1, its true
     self.assertLess(
         0, len(obj)
     )  # Case_2: Checks whether the initial count = 0 is less than the new count after appending items into the list
     self.assertEqual(2, len(
         obj))  # Case_3: Checks whether after appending 2 items, count = 2
Exemplo n.º 5
0
 def test_insert(self):
     obj = ArrayOperations()
     obj.append(1)
     obj.append(2)
     obj.append(3)
     obj.insert(
         2, 4
     )  # Case_1: Inserts item 4 at index 2 and shifts 3 to the right, checks whether this occurs, if so test passes
     self.assertEqual([1, 2, 4, 3], obj)
     with self.assertRaises(
             IndexError
     ):  # Case_2: Checks whether it raises indexerror for an index > len(obj)
         obj.insert(5, 20)
Exemplo n.º 6
0
 def test_eq(self):
     obj = ArrayOperations()
     other = [1, 2, 3, 4]
     obj.append(2)
     obj.append(3)
     self.assertFalse(
         obj.__eq__(other)
     )  # Case_1: If the length of the original list is not equal to the length of the other list, then assertFalse
     other1 = [2, 3, 5, 6]
     obj.append(5)
     obj.append(6)
     self.assertTrue(
         obj.__eq__(other1)
     )  # Case_2: Checks whether every index of both the lists is the same, if so, assertTrue
Exemplo n.º 7
0
 def test_setitem(self):
     obj = ArrayOperations()
     with self.assertRaises(IndexError):
         obj.__setitem__(
             5, 20
         )  # Case_1: Index 5 is out of range hence indexerror will be raised
     obj.append(1)
     obj.append(2)
     self.assertLess(
         1, len(obj)
     )  # Case_2: Index 1 is in range, as two items apppended to give self.count = 2 (index < count i.e. index in range)
     with self.assertRaises(IndexError):
         obj.__setitem__(
             -4, 20
         )  # Case_3: Accounting for negative indexes which are also out of range
Exemplo n.º 8
0
 def test_getitem(self):
     obj = ArrayOperations()
     obj.append(20)
     obj.append(40)
     with self.assertRaises(IndexError):
         obj.__getitem__(
             2
         )  # Case_1: Index 2 is out of range since count is 2, only index 0 and 1 exist, hence indexerror will be raised
     self.assertLess(
         1, len(obj)
     )  # Case_2: Index 1 is in range, as two items apppended to give self.count = 2 (index < count i.e. index in range)
     with self.assertRaises(IndexError):
         obj.__getitem__(
             -1
         )  # Case_3: Accounting for negative indexes which are also out of range
Exemplo n.º 9
0
 def test_contains(self):
     obj = ArrayOperations()
     obj.append(1)
     obj.append(2)
     self.assertTrue(obj.__contains__(
         1))  # Case_1: list contains 1, hence will give us true
     self.assertFalse(obj.__contains__(
         4))  # Case_2: list does not contain 4 hence will give false
Exemplo n.º 10
0
 def test_resize_list(self):
     obj = ArrayOperations()
     obj.append(150)
     obj.append(175)
     self.assertEqual(
         100, len(obj.array)
     )  # Case_1: checks for the if condition failing, when the list is not full, the size remains the same i.e. 100
     for i in range(
             len(obj.array)
     ):  # Case_2: appends items into the list equal to the len of the array, to check that when the array is full, if it resizes the array by 10 times to a len of 1000
         obj.append(i)
     self.assertEqual(1000, len(obj.array))
     self.assertIn(
         150, obj.array
     )  # Case_3: To check whether the original elements in the list are copied back into it after re-sizing
     self.assertIn(175, obj.array)
Exemplo n.º 11
0
class TextEditor:
    def __init__(self):
        self.array = ArrayOperations()

    def insert_num(self, num, line):
        '''
        This function inserts a line of text in the list at the position inputted by the user, and raises an exception if no position is entered by the user
        :param: number position inputted by the user and the line of text inputted by the user
        :precondition: A list must already exist
        :postcondition: The list is updated as a line is inserted into it or an error is raised depending on the user's input
        :complexity: Best Case = Worst Case = O(1), constant time complexity.
        '''

        try:
            num = int(
                num
            )  # if the string cannot be converted to integer, returns False resulting in '?' to be printed
            self.array.insert(num, line)
        except ValueError:
            return False
        except IndexError:
            return False
        return True

    def read_filename(self, filename):
        '''
        This function takes a filename as an input by the user and reads the contents of the file into a list, storing each line as a single item in the list
        :param: filename of the file to be read
        :precondition: A file must exist from which the contents of the file can be read
        :postcondition: A list is created with each line in the file stored as a single item in the list
        :complexity: Best Case = Worst Case = O(n), where n is the length of the contents in the file, even if the contents are empty or filled the loop will run n times
        '''

        try:
            self.array = file_list(filename)
        except FileNotFoundError:
            return False
        return True

    def write_filename(self, filename):
        '''
        This function creates a new file or opens an existing file and writes every item in the list into the file and then closes the file.
        :param: filename of the file to be written to
        :precondition: There must be an existing list from which it copies the items into the file
        :postcondition: An existing file is overwritten with the contents of the list copied into it or a new file is created with the contents of the list copied into it
        :complexity: Best Case = Worst Case = O(n), where n is the length of the list, the loop will run n times
        '''

        infile = open(filename, 'w')
        for i in range(1, len(self.array) + 1):
            infile.writelines(self.array[i] + str('\n'))
        infile.close()

    def print_num(self, num):
        '''
        This function prints the line from the file at the position inputted by the user, however, if no position is inputted by the user it prints all the lines of the file
        :param: number position inputted by the user
        :precondition: The file must exist already to be accessed
        :postcondition: The particular line or all the lines of the file are printed for the user
        :complexity: Best Case = Worst Case = O(1), constant time complexity.
        '''

        if num == '':  # if no input for position is given, all the lines are printed
            print(self.array)
        else:
            try:
                num = int(
                    num
                )  # if the string cannot be converted to integer, returns False resulting in '?' to be printed
                print(self.array[num])
            except ValueError:
                return False
            except IndexError:
                return False
        return True

    def delete_num(self, num):
        '''
        This function deletes the line from the file at the position inputted by the user, however, if no position is inputted by the user it deletes all the lines of the file
        :param: number position inputted by the user
        :precondition: The file must have some existing lines to be deleted i.e. the file cannot be empty.
        :postcondition: The file is updated after the deletion has taken place depending on the user input
        :complexity: Best Case = Worst Case = O(1), constant time complexity.
        '''

        try:
            if num == '':
                for i in range(len(self.array), 0, -1):
                    self.array.delete(
                        i
                    )  # if no input for position is given, all the lines are deleted from the list
            else:
                num = int(
                    num
                )  # if the string cannot be converted to integer, returns False resulting in '?' to be printed
                self.array.delete(num)
        except IndexError:
            return False
        except ValueError:
            return False
        return True

    def frequency(self):
        '''
        This function prints the number of times each word occurs in the list.
        :param: None
        :precondition: None
        :postcondition: The output is the word alongside the number of times it has occurred in the list
        :complexity: Best Case = Worst Case = O(n), where n is the length of the list
        '''

        self.tmp = Arrayfreq()
        self.final_list = Arrayfreq()
        for i in range(1, len(self.array) + 1):
            current_words = self.array[i].split(
                ' ')  # for every line, splitting to get the words
            for j in range(len(current_words)):
                self.tmp.append(
                    current_words[j])  # adding the words to a temporary list
        bubble_sort(self.tmp)  # sorting the list
        count = 1
        for b in range(len(self.tmp)):
            for a in range(b + 1, len(
                    self.tmp)):  # frequency of words is case sensitive
                if self.tmp[b] == self.tmp[a]:
                    count += 1
            if self.tmp[b] not in self.final_list:
                self.final_list.append(
                    self.tmp[b]
                )  # searching through the list and appending the word and its count
                self.final_list.append(count)
            count = 1

        for k in range(0, len(self.final_list) - 1, 2):
            print(self.final_list[k], self.final_list[k + 1])

        return True
Exemplo n.º 12
0
 def test_remove(self):
     obj = ArrayOperations()
     obj.append(30)
     obj.append(40)
     obj.append(50)
     obj.remove(40)
     self.assertEqual(
         2, len(obj)
     )  # Case_1: checks whether the delete function works by comparing whether the count is equal to length after deleting an item at index 1
     self.assertEqual(
         50, obj.__getitem__(1)
     )  # Case_2: Checks whether the list has shifted after deletion of an item, this test proves that 50 has moved to index 1 from index 2
     with self.assertRaises(
             ValueError
     ):  # Case_3: Since 6 does not exist as an item in the list, the ValueError is raised.
         obj.remove(6)
Exemplo n.º 13
0
 def test_delete(self):
     obj = ArrayOperations()
     obj.append(30)
     obj.append(40)
     obj.append(50)  # obj count initially 3
     obj.delete(1)  # after delete obj count 2
     self.assertEqual(
         2, len(obj)
     )  # Case_1: checks whether the delete function works by comparing whether the count is equal to length after deleting an item at index 1
     self.assertEqual(
         50, obj.__getitem__(1)
     )  # Case_2: Checks whether the list has shifted after deletion of an item, this test proves that 50 has moved to index 1 from index 2
     with self.assertRaises(IndexError):
         obj.delete(
             3
         )  # Case_3: Checks whether the index is less than the count to check if index is valid