Question

unique_list.py] A unique list has no repeated items. For example, the list ['orange', 'juice', 'futures'] is...

unique_list.py] A unique list has no repeated items. For example, the list ['orange', 'juice', 'futures'] is unique because each item only appears one time. However, the list ['sell','mortimer','sell'] is not unique because the string 'sell' appears more than one time.

Write a program named `unique_list` where a user inputs a series of positive integers and, when finished, it reports whether or not the sequence of numbers input is unique or not. For example:

This program tests if the sequence of positive numbers you input are unique
Enter -1 to quit
Enter the first number: 9
Next: 5
Next: 3
Next: 6
Next: 23
Next: -1
The sequence [9, 5, 3, 6, 23] is unique!
This program tests if the sequence of positive numbers you input are unique
Enter -1 to quit
Enter the first number: 83
Next: 20
Next: 593
Next: 28
Next: 20
Next: 51
Next: -1
The sequence [83, 20, 593, 28, 20, 51] is NOT unique!
0 0
Add a comment Improve this question Transcribed image text
Answer #1

This code works in python 3. Two implementations of check() are given. You can call any based on time complexities.

Check() runs inO(n2) and check_efficient() runs in O(n) time

def check(test_list):
flag = 0
  
for i in range(len(test_list)):
for i1 in range(len(test_list)):
if i != i1:
if test_list[i] == test_list[i1]:
flag = 1
if(not flag) :
print ("List ",test_list," is unique!")
else :
print ("List",test_list," is NOT unique!")
return   

def check_efficient(test_list):
flag = 0
flag = len(set(test_list)) == len(test_list)
if(not flag) :
print ("List ",test_list," is unique!")
else :
print ("List",test_list," is NOT unique!")
return


#main code
a=[]
print("Enter -1 to quit")
x=int(input("Enter the first number:"))
if x==-1:
print("Empty list not allowed")
exit
a.append(x)
while(1):
x=int(input("Next"))
if x==-1:
check(a) #call check(a) or check_efficient(a)
else:
a.append(x)

Add a comment
Know the answer?
Add Answer to:
unique_list.py] A unique list has no repeated items. For example, the list ['orange', 'juice', 'futures'] is...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please answere using python language codes. Write a program to print out Collatz sequence for a...

    Please answere using python language codes. Write a program to print out Collatz sequence for a user-supplied number. Prompt the user for a positive integer which will become the first number in the sequence. Next number in the sequence is derived as follows: If previous number is odd, the next number is 3 times the previous, plus 1. If previous number is even, the next number is half of the previous According to Collatz proposition, the sequence ultimately reaches 1...

  • in java the program must contain the main method and example( the number 50 should be...

    in java the program must contain the main method and example( the number 50 should be the input) must be included in the main method to check the correctness of your programs. Create a GUI program to let user enter a positive integer n through the first window, and then your program prints out all the prime integers within the range [1, n) through the second window. Your program should contain two windows in total. Below are two example pictures:...

  • I want this using while loop This using stringin python Use list or some thing in...

    I want this using while loop This using stringin python Use list or some thing in python Using list in python I want answer as soon as posdible E. Last Number time limit per test: 1 second memory limit per test: 256 megabytes input standard input output standard output You are given a sequence of positive integers aj, , 03, ... Print the last element of the sequence. Input The input consists of multiple lines. The i-th line contains a...

  • Complete function long_list_printer.print_list(). When it's finished, it should be able to print this list, a =...

    Complete function long_list_printer.print_list(). When it's finished, it should be able to print this list, a = [ [93, 80, 99, 72, 86, 84, 85, 41, 69, 31], [15, 37, 58, 59, 98, 40, 63, 84, 87, 15], [48, 50, 43, 68, 69, 43, 46, 83, 11, 50], [52, 49, 87, 77, 39, 21, 84, 13, 27, 82], [64, 49, 12, 42, 24, 54, 43, 69, 62, 44], [54, 90, 67, 43, 72, 17, 22, 83, 28, 68], [18, 12, 10,...

  • Part 3: More Numeric Analysis Now add three more functions to the num_stats module you created...

    Part 3: More Numeric Analysis Now add three more functions to the num_stats module you created in part 2. These will determine if a given positive integer is EVEN, PERFECT and ABUNDANT. Each of these functions should accept a single integer as an argument and return whether the integer meets the criteria for that classification. Here's some IPO notation to get you started: # function: is_even # input: a positive integer # processing: determines if the supplied number is even...

  • Python program This assignment requires you to write a single large program. I have broken it...

    Python program This assignment requires you to write a single large program. I have broken it into two parts below as a suggestion for how to approach writing the code. Please turn in one program file. Sentiment Analysis is a Big Data problem which seeks to determine the general attitude of a writer given some text they have written. For instance, we would like to have a program that could look at the text "The film was a breath of...

  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

  • Write a Python program that tests the function main and the functions discussed in parts a...

    Write a Python program that tests the function main and the functions discussed in parts a through g. Create the following lists: inStock - 2D list (row size:10, column size:4) alpha - 1D list with 20 elements. beta - 1D list with 20 elements. gamma = [11, 13, 15, 17] delta = [3, 5, 2, 6, 10, 9, 7, 11, 1, 8] a. Write the definition of the function setZero that initializes any one-dimensional list to 0 (alpha and beta)....

  • I'm making a To-Do list in Python 2 and had the program running before remembering that...

    I'm making a To-Do list in Python 2 and had the program running before remembering that my professor wants me to put everything in procedures. I can't seem to get it to work the way I have it now and I can't figure it out. I commented out the lines that was originally in the working program and added procedures to the top. I've attached a screenshot of the error & pasted the code below. 1 todo_list = [] 2...

  • The task involves writing a C++ program that determines the prime numbers between 1 and 100....

    The task involves writing a C++ program that determines the prime numbers between 1 and 100. The steps you should follow to identify the prime numbers are the following. 1. The number 1 is not a prime number, so it should be scratched. 2. Starting from the first prime number, which is 2, you scratch all the numbers that are the multiple of 2. You should not scratch out 2 itself. 3. The next number in the sequence after the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT