Question

Python question Write (and test) the following 10 functions (names are taken from old vi editor)...

Python question Write (and test) the following 10 functions (names are taken from old vi editor) for each implementation

(1) cmd_h: move cursor one character to the left

(2) cmd_I: move cursor one character to the right

(3) cmd_j: move cursor vertically up one line

(4) cmd_k: move cursor vertically down one line

(5) cmd_X: delete the character to the left of the cursor

(6) cmd_D: remove on current line from cursor to the end

(7) cmd_dd: delete current line and move cursor to the beginning of next line

(8) cmd_ddp: transpose two adjacent lines

(9) cmd_n: search for next occurrence of a string (assume that string to be searched is fully in one line.

(10) cmd_wq: write your representation as text file and save it

Think of and implement any other 5 functions (your choice)

For testing, you will read the following “nerdy” poem (from the “Zen of Python”) into your “file representation”.

Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Homework Answers

Answer #1

Code:

class func:
    """Store text file as list of strings where each string
    represents a line"""
    def __init__(self):
        self.cursor = 0

    f_in = open("test.txt", "r")
    file_text = f_in.readlines() # read file into list of strings

    def cmd_h(self):
        """move cursor one character to the left"""
        if self.cursor > 0:
            self.cursor = self.cursor - 1
        else:
            return self.cursor
        self.get_text(func.file_text)

    def cmd_I(self):
        """move cursor one character to the right"""
        ls = func.file_text
        length = 0
        prev = 0
        for line in ls:
            length = len(line) + length
            if prev <= self.cursor < length:
                self.cursor = self.cursor + 1
                prev = length
                continue
        self.get_text(func.file_text)

    def cmd_k(self):
        """move cursor down vertically one line"""
        ls = func.file_text
        numLines = len(ls)
        length = 0
        prev = 0
        for counter, line in enumerate(ls):
            length = len(line) + length
            if prev <= self.cursor <= length:
                if counter != numLines:
                    self.cursor = self.cursor + len(line)
                    self.get_text(func.file_text)
                    break
                prev = length

    def cmd_j(self):
        """move cursor up vertically one line"""
        ls = func.file_text
        length = 0
        older = 0
        prev = 0
        for counter, line in enumerate(ls):
            length = len(line) + length
            if prev <= self.cursor <= length:
                if counter != 0:
                    self.cursor = self.cursor - older
                    self.get_text(func.file_text)
                    break
                prev = length
            older = len(line)

    def cmd_X(self):
        """delete the character to the left of the cursor"""
        ls = func.file_text
        length = 0
        prev = 0
        result = []
        if self.cursor > 0:
            for line in ls:
                length = len(line) + length
                if prev <= self.cursor < length:
                    result.append(line[0:self.cursor-1] + line[self.cursor:])
                    self.cursor = self.cursor - 1
                    prev = length
                    continue
                result.append(line)
            self.get_text(result)
        else:
            self.get_text(func.file_text)

    def cmd_D(self):
        """remove on current line from cursor to the end"""
        ls = func.file_text
        length = 0
        prev = 0
        result = []
        for line in ls:
            length = len(line) + length
            if prev <= self.cursor <= length:
                prev = length
                continue
            result.append(line)
        self.get_text(result)

    def cmd_dd(self):
        """delete current line and move cursor to the beginning of next line"""
        ls = func.file_text
        length = 0
        prev = 0
        result = []
        for line in ls:
            length = len(line) + length
            if prev <= self.cursor <= length:
                self.cursor = prev
                prev = length
                continue
            result.append(line)
        self.get_text(result)

    def cmd_ddp(self, l1, l2):
        """transpose two adjacent lines"""
        ls = func.file_text
        tmp1 = ls[l1]
        tmp2 = ls[l2]
        result = []
        for counter, line in enumerate(ls):
            if counter == l1:
                result.append(tmp2)
            elif counter == l2:
                result.append(tmp1)
            else:
                result.append(line)
        self.get_text(result)

    def cmd_n(self, s):
        """search for next occurrence of a string (assume that string to be searched is fully in one line."""
        ls = func.file_text
        length = 0
        prev = 0
        for line in ls:
            length = len(line) + length
            if line.find(s):
                self.cursor = prev + line.find(s)
                break
            prev = length
        self.get_text(func.file_text)

    def cmd_wq(self):
        """write your representation as text file and save it"""
        out_file = open("output.txt", "w")
        out_file.write(str(self.get_text(func.file_text)))
        out_file.close()
        print("\nFile saved to output.txt")

    def setCursor(self, cursor):
        self.cursor = cursor
        self.get_text(func.file_text)

    def get_text(self, ls):
        result = ""
        length = 0
        prev = 0
        for line in ls:
            length = len(line) + length
            if prev <= self.cursor <= length:
                end = self.cursor - length
                result = result + line[0:end] + "^" + line[end:]
                prev = length
                continue
            result =  result + line
        print(result)
        return(result)

def main():
    print("--- Printing initial")
    fo = func()
    fo.get_text(func().file_text)
    print("\n--- Setting cursor randomly at 25")
    fo.setCursor(25)
    print("\n--- Move cursor 1 to the right")
    fo.cmd_I()
    print("\n--- Move cursor 1 to the left")
    fo.cmd_h()
    print("\n--- Move cursor down vertically one line")
    fo.cmd_k()
    print("\n--- Move cursor up vertically one line")
    fo.cmd_j()
    print("\n--- Delete the character to the left of cursor")
    fo.cmd_X()
    print("\n--- Remove current line from cursor")
    fo.cmd_D()
    print("\n--- Delete current line and move cursor to beginning of next line")
    fo.cmd_dd()
    print("\n--- Transpose lines 2 and 3")
    fo.cmd_ddp(2,3)
    print("\n--- Search next \"better\" string")
    fo.cmd_n("better")
    print("\n--- Write text and save as file")
    fo.cmd_wq()
main()

Output:

--- Printing initial
^Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.

--- Setting cursor randomly at 25
Beautiful is better than ^ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.

--- Move cursor 1 to the right
Beautiful is better than u^gly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.

--- Move cursor 1 to the left
Beautiful is better than ^ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.

--- Move cursor down vertically one line
Beautiful is better than ugly.
Explicit is better than i^mplicit.
Simple is better than complex.
Complex is better than complicated.

--- Move cursor up vertically one line
Beautiful is better than ^ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.

--- Delete the character to the left of cursor
Beautiful is better than^ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.

--- Remove current line from cursor
Explicit is better than ^implicit.
Simple is better than complex.
Complex is better than complicated.

--- Delete current line and move cursor to beginning of next line
^Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.

--- Transpose lines 2 and 3
^Beautiful is better than ugly.
Explicit is better than implicit.
Complex is better than complicated.Simple is better than complex.


--- Search next "better" string
Beautiful is ^better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.

--- Write text and save as file
Beautiful is ^better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.

File saved to output.txt

Saved file "output.txt" content:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
You must write a function that works like a small editor. It should open an existing...
You must write a function that works like a small editor. It should open an existing file (let us say this is File1.txt. This file must exist before the function is run), read it line by line. Then write it to another file (let us say this is File2.txt. This file need not exist since Python will create it). However, if a certain line exists in the first file, it should be replaced by another line supplied by you. You...
Please create a python module named homework.py and implement the functions outlined below. Below you will...
Please create a python module named homework.py and implement the functions outlined below. Below you will find an explanation for each function you need to implement. When you are done please upload the file homework.py to Grader Than. Please get started as soon as possible on this assignment. This assignment has many problems, it may take you longer than normal to complete this assignment. This assignment is supposed to test you on your understanding of reading and writing to a...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter...
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter feeds. At a high level, your program is going to perform the following: Read in two files containing twitter feeds. Merge the twitter feeds in reverse chronological order (most recent first). Write the merged feeds to an output file. Provide some basic summary information about the files. The names of the files will be passed in to your program via command line arguments. Use...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class Zillion implements a decimal counter that allows numbers with an effectively infinite number of digits. Of course the number of digits isn’t really infinite, since it is bounded by the amount of memory in your computer, but it can be very large. 1. Examples. Here are some examples of how your class Zillion must work. I’ll first create an instance of Zillion. The string...
Write a Python 3 program called “parse.py” using the template for a Python program that we...
Write a Python 3 program called “parse.py” using the template for a Python program that we covered in this module. Note: Use this mod7.txt input file. Name your output file “output.txt”. Build your program using a main function and at least one other function. Give your input and output file names as command line arguments. Your program will read the input file, and will output the following information to the output file as well as printing it to the screen:...