Skip to content

Instantly share code, notes, and snippets.

  • Select an option

  • Save LizaPiya/5574df58f0eef1d7bd9ed49c44464855 to your computer and use it in GitHub Desktop.

Select an option

Save LizaPiya/5574df58f0eef1d7bd9ed49c44464855 to your computer and use it in GitHub Desktop.
11.8. Accumulating the Best Key
## 1. Create a dictionary called d that keeps track of all the characters in the string placement and notes how many times each character was seen. Then, find the key with the lowest value in this dictionary and assign that key to min_value.
placement = "Beaches are cool places to visit in spring however the Mackinaw Bridge is near. Most people visit Mackinaw later since the island is a cool place to explore."
d={}
for i in placement:
if i not in d:
d[i]=0
d[i]=d[i]+1
print (d)
ks=list(d.keys())
print (ks)
min_value=ks[0]
for key in ks:
if d[key]< d[min_value]:
min_value=key
## Create a dictionary called lett_d that keeps track of all of the characters in the string product and notes how many times each character was seen. Then, find the key with the highest value in this dictionary and assign that key to max_value.
product = "iphone and android phones"
lett_d={}
for i in product:
if i not in lett_d:
lett_d[i]=0 #Initializing the dictionary
lett_d[i]=lett_d[i]+1
print (lett_d)
ks=list(lett_d.keys())
print (ks)
max_value=ks[0]
for key in ks:
if lett_d[key] > lett_d[max_value]:
max_value=key
@hadrocodium
Copy link

"""
 Create a dictionary called d that keeps track of all the characters in the string placement and notes how many times each character was seen. Then, find the key with the lowest value in this dictionary and assign that key to min_value.
 """

placement = "Beaches are cool places to visit in spring however the Mackinaw Bridge is near. Most people visit Mackinaw later since the island is a cool place to explore."

d = {}
min_value = None

for char in placement:
    d[char] = d.get(char, 0) + 1

for character in d:
    if min_value is None or d[min_value] > d[character]:
        min_value = character

print(min_value)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment