Create a dictionary of 20 random values in the range 1–99. Determine whether there are any duplicate values in the dictionary. Hint: you many want to sort the list first.

What will be an ideal response?


```
# Determining and display duplicate values
# in a randomly generated dictionary.

import random

aDictionary = {}
duplicates = 0 # true if the dictionary contains duplicates

# randomly generate 20 dictionary values
for i in range( 0, 20 ):
aDictionary[ i ] = random.randrange( 1, 100 )

# obtain and display list of sorted dictionary values
valuesList = aDictionary.values()
valuesList.sort()
print "The dictionary values are:", valuesList

print

# loop through the value list
for i in range( len( valuesList ) - 1 ):

if valuesList[ i ] == valuesList[ i + 1 ]:
duplicates = 1
break

if duplicates:
print "There are duplicate values in the dictionary."
else:
print "There are no duplicate values in the dictionary."
```
The dictionary values are: [4, 12, 16, 17, 26, 30, 38, 43, 53, 53, 69,
69, 71, 76, 80, 82, 82, 84, 87, 88]
There are duplicate values in the dictionary.
The dictionary values are: [4, 8, 9, 14, 15, 20, 32, 33, 34, 35, 38,
41, 42, 49, 50, 59, 66, 77, 88, 89]
There are no duplicate values in the dictionary.

Computer Science & Information Technology

You might also like to view...

Answer the following questions true (T) or false (F)

1. A queue is a last-in/first-out structure. 2. A collection is a data structure for holding elements.

Computer Science & Information Technology

A footnote is a citation that appears at the bottom of a page

Indicate whether the statement is true or false

Computer Science & Information Technology

(Packing Characters into Unsigned Integers) The left-shift operator can be used to pack two character values into a two-byte unsigned integer variable. Write a program that inputs two charac- ters from the keyboard and passes them to function packCharacters. To pack two characters into an unsigned integer variable, assign the first character to the unsigned variable, shift the unsigned variable

left by 8 bit positions and combine the unsigned variable with the second character using the bitwise inclusive-OR operator. The program should output the characters in their bit format before and after they’re packed into the unsigned integer to prove that they’re in fact packed cor- rectly in the unsigned variable. What will be an ideal response?

Computer Science & Information Technology

The ________ is applied to all text in a presentation except the slide titles

Fill in the blank(s) with correct word

Computer Science & Information Technology