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...
USING PYTHON ONLY Write a gradebook program that lets a teacher keep track of test averages...
USING PYTHON ONLY Write a gradebook program that lets a teacher keep track of test averages for his or her students. Your program should begin by asking the teacher for a number of students in their class as well as the total # of tests that will be given to the class. Validate this information to ensure that the numbers entered are positive. Next, prompt the teacher to enter in scores for each student. Ensure that the values entered are...
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)
1. Write a python program to create a list of integers using random function. Use map...
1. Write a python program to create a list of integers using random function. Use map function to process the list on the expression: 3x2+4x+5 and store the mapped elements in another list. Now use filter to do sum of all the elements in another list. 2. Write a function that takes n as input and creates a list of n lists such that ith list contains first 10 multiples of i. 3. Write a function that takes a number...
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)...
Python Question Using lists, write the function non_unique(list) that takes a list list as argument. It...
Python Question Using lists, write the function non_unique(list) that takes a list list as argument. It returns a list which duplicated elements remains and each duplicated element is followed by a number which shows how many times it appears. All elements in return list should be in the same order as their appearance in the original list. For example, given the input [‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘d’, ‘a’,‘e’], the function would return [‘a’, 3, ‘b’, 2]. Another example, ['abc',...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT