Question

In: Computer Science

USE PYTHON-LIST Q1 - You need to keep track of book titles. Write code using function(s)...

USE PYTHON-LIST 

Q1 - You need to keep track of book titles. Write code using function(s)
that will allow the user to do the following.


1. Add book titles to the list.
2. Delete book titles anywhere in the list by value.
3. Delete a book by position.
    Note: if you wish to display
    the booklist as a vertical number booklist , that is ok
    (that is also a hint)
4. Insert book titles anywhere in the list.
5. Clear the list (remove all items from the list)
6. Print the list horizontally using sys.stdout.write()
7. Sort the list
8. The user can add, remove, insert, clear, delete, print books as
   much as they wish.

Note:   The person will add or remove books, one book at at a time
        When a book is add, deleted, inserted or sorted the list should
        automatically be printed 
(HINT: a function will make this easy)

###

PYTHON LIST

Solutions

Expert Solution

Program:

import sys #importing sys library
List=[]
def add(ele):
    List.append(ele) #adding elements
    printlist() #printing list
def del_by_val(ele):
    List.remove(ele) #removing element in a list
    printlist() #printing list
def del_by_pos(pos):
    List.pop(pos-1) #deleting elemnt by position
    printlist() #printing list
def insert_anywhere(ele,pos):
    List.insert(pos-1,ele) #inserting element by position
    printlist() #printing list
def clear():
    List.clear() #removing all elemnts in a list
def printlist():
    for i in range(len(List)):
        sys.stdout.write(List[i]+', ')
    sys.stdout.write('\n')
def sort():
    List.sort() #sorting a list
    printlist()
while(1):
    print('''Press
1. Add book titles to the list.
2. Delete book titles anywhere in the list by value.
3. Delete a book by position.
4. Insert book titles anywhere in the list.
5. Clear the list (remove all items from the list)
6. Print the list horizontally using sys.stdout.write()
7. Sort the list
8.Exit''')
    k=int(input())
    if k==1:
        ele=input("Enter book title:")
        add(ele)
    elif k==2:
        val=input("enter book title to delete:")
        del_by_val(val)
    elif k==3:
        pos=int(input("Enter position:"))
        del_by_pos(pos)
    elif k==4:
        pos=int(input("enter position:"))
        ele=input("Enter book title:")
        insert_anywhere(ele,pos)
    elif k==5:
        clear()
    elif k==6:
        printlist()
    elif k==7:
        sort()
    else:
        exit(0)




Program Screenshot:

Output:

Hope you understand...

If you have any doubts comment below...plss dont dislike...


Related Solutions

(Python) a) Using the the code below write a function that takes the list xs as...
(Python) a) Using the the code below write a function that takes the list xs as input, divides it into nss = ns/nrs chunks (where nrs is an integer input parameter), computes the mean and standard deviation s (square root of the variance) of the numbers in each chunk and saves them in two lists of length nss and return these upon finishing. Hint: list slicing capabilities can be useful in implementing this function. from random import random as rnd...
"PYTHON" Write some code " USING PYTHON" to keep reading numbers from the user until the...
"PYTHON" Write some code " USING PYTHON" to keep reading numbers from the user until the users enters a negative number. The program then prints out: a) the sum of all the numbers b) the average of all the numbers c) the max of the numbers d) the min of the numbers Note we did not cover lists (array) so you will not use them in this problem. Finally, ask the user for their name, then print their name as...
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
Use C++ please You will be building a linked list. Make sure to keep track of...
Use C++ please You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. PlaylistNode.h - Class declaration PlaylistNode.cpp - Class definition main.cpp - main() function Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps. Default constructor (1 pt) Parameterized constructor (1 pt) Public member functions InsertAfter() - Mutator (1 pt)...
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
Using LIST and FUNCTION Write a program in Python that asks for the names of three...
Using LIST and FUNCTION Write a program in Python that asks for the names of three runners and the time it took each of them to finish a race. The program should display who came in first, second, and third place.
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Please write a python code for the following. Use dictionaries and list comprehensions to implement the...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output. Here is the starter outline for the homework: a. def count_character(text, char): """ Count the number of times a character occurs in some text. Do not use the count() method. """ return 0 b. def count_sentences(text): """...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output. Here is the starter outline for the homework: g. def big_words(text, min_length=10): """ Return a list of big words whose length is at least min_length """ return [] h. def common_words(text, min_frequency=10): """ Return words occurring at...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT