Question

In: Computer Science

Python This part involves creating a function that will manage a List of unique strings. The...

Python

This part involves creating a function that will manage a List of unique strings. The function is passed a string and a list as arguments. It passes a list back.

  1. The function to add a word to a List if word does not exist in the List. If the word does exist in the List, the function does nothing.

  2. Create a test program that takes a string as input and then calls the function over and over until the user enters "Done." If the string "Done" is entered, the program sorts the list and prints it out line by line.

Solutions

Expert Solution

Python code:

#defining unique fnction
def unique(string,lst):
    #checking if strng is not in lst
    if(string not in lst):
        #adding string to lst
        lst.append(string)
    #returning lst
    return lst
#asking for string
string=input("Enter string: ")
#initializing lst
lst=[]
#looping till done is entered
while(string!="Done"):
    #calling unique function
    lst=unique(string,lst)
    #asking for string
    string=input("Enter string: ")
#printing the contents in sorted array
print(*sorted(lst),sep="\n")

Screenshot:


Input and Output:


Related Solutions

Design a function in python that takes a list of strings as an argument and determines...
Design a function in python that takes a list of strings as an argument and determines whether the strings in the list are getting decreasingly shorter from the front to the back of the list
Write a program that uses Python List of strings to hold the five student names, a...
Write a program that uses Python List of strings to hold the five student names, a Python List of five characters to hold the five students’ letter grades, and a Python List of four floats to hold each student’s set of test scores. The program should allow the user to enter each student’s name and his or her four test scores. It should then calculate and display each student’s average test score and a letter grade based on the average....
Creating a list/tuple in python 2. Using a list of lists containing X’s and O’s to...
Creating a list/tuple in python 2. Using a list of lists containing X’s and O’s to represent a tic tac toe board, write code to check if the board has a winner.
(PYTHON) Q2 Strings, a more complex program from requirements In part A you are asked to...
(PYTHON) Q2 Strings, a more complex program from requirements In part A you are asked to write the pseudocode for the program. In part B you are asked to write the syntax of the code for the program you outlined in the pseudocode. Q2 Part A - Pseduocode Create the Pseudocode for the program outlined in Q5. Make sure that your program includes all the requirements outlined. This cell is markdown, you do NOT need to use markdown to format...
This function will be given a list of strings and a character. You must remove all...
This function will be given a list of strings and a character. You must remove all occurrences of the character from each string in the list. The function should return the list of strings with the character removed. Signature: public static ArrayList<String> removeChar(String pattern, ArrayList<String> list)
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings....
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings. The first string is #the filename of a file to which to write (output_file), and #the second string is the filename of a file from which to read #(input_file). # #In the input file, there will be an integer on every line. #To the output file, you should write the integer from the #original file multiplied by the line number on which it #appeared....
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
In Python Create a function called ℎ?????. The function has as arguments a list called ??????...
In Python Create a function called ℎ?????. The function has as arguments a list called ?????? and a list call center. • List ?????? contains lists that represent points. o For example, if ?????? = [[4,2], [3,2], [6,1]], the list [4,2] represents the point with coordinate ? at 4 and y coordinate at 2, and so on for the other lists. Assume that all lists within points contain two numbers (that is, they have x, y coordinates). • List ??????...
r = range(10, 40, 3) def print_lv(strings): strings = strings if isinstance(strings, list) else [strings] for...
r = range(10, 40, 3) def print_lv(strings): strings = strings if isinstance(strings, list) else [strings] for string in strings: st = "f'" + string + ": {" + string + "}'" print_stmt = 'print(' + st + ', end="; ")' # Uncomment the following print statement to see the statement to be executed. # Each will appear on a separate line. # print(f'\n{print_stmt}') exec(print_stmt) print() print_lv(['list(r)', 'r[-2:3:-1]', 'list(r[-2:3:-1])']) OUTPUT: list(r): [10, 13, 16, 19, 22, 25, 28, 31, 34, 37];...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT