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...
Write a python program to create a list of integers using random function. Use map function...
Write a python program to create a list of integers using random function. Use map function to process the list on the series: x + x2/2! + x3/3! + ….n and store the mapped elements in another list. Assume the value of n as 10
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...
"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)...
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...
Write a python code that calculates π using numpy rand function.
Write a python code that calculates π using numpy rand function.
PYTHON CODE - Write the body of a function second_instance(s, c) which consumes a string s...
PYTHON CODE - Write the body of a function second_instance(s, c) which consumes a string s and a length 1 string c that is contained at least twice in s and returns the index of the second location of c. second_instance: Str Str -> Nat Requires: len(c) == 1 c occurs at least twice in s    Examples: second_instance("banana", "a") => 3 second_instance("bb", "b") => 1 - Write the body of a function make_list(n) which consumes a natural number n...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT