0

So I am answering this question in a challenge that I am currently participating in

Question: Keeping up with the best streamers is hard, so you decide to ask your friends for subscription recommendations. Write a program to help track the streamers to watch. Your program should ask for a friend's name and a streamer they recommend, until 5 different streams are suggested. Each stream cannot be recommended more than once. Once 5 different streams have been suggested, it should print the list of streams and the friends who recommended them.

Note: Friends are allowed to recommend more than one stream.

if someone suggests a stream that has already been recommended. The program prints: Someone else already recommended that.

Here's what I have:

playlist = {}

while len(playlist) < 5:
  name = input('Friend: ')
  stream = input('Which stream did they recommend? ')
  print (f'{name} recommended {stream}!')
  playlist[name] = stream
  if stream in playlist:
    playlist = playlist + ""

  else:
playlist = playlist + stream
print('Playlist complete! Subscribe to:')
for name, stream in playlist.items():
  print(f'{stream}: recommended by {name}')

I wanna check if the same stream has been recommended twice and print someone else already recommended that

here's how the output should look:

output

tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • 1
    Did something go wrong? Put that in the question. Also, consider 4 space indentation, especially on a site like this. Its estimated that we won't run out of the space character for at least 30 years. – tdelaney Aug 19 '21 at 04:18
  • Please [don’t post images of code, error messages, or other textual data.](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors) – tripleee Aug 19 '21 at 05:20

2 Answers2

2

You seem to already know how to check what's in the dictionary, but you will want to invert the mapping so that you can prevent duplicate keys and ensure that len(playlist) is actually 5 unique streams, not 5 friends (since friends can recommend more than one stream)

name = input('Friend: ')
stream = input('Which stream did they recommend? ')
if stream not in playlist:
    playlist[stream] = name 
    print (f'{name} recommended {stream}!')
else:
    print('someone else recommended that')

If you wanted to store all friend names for each recommendation, even if they repeat recommendations, use a list in the values

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
1

All dictionaries allow you to access an iterable using dict.values(). With that you can check if a stream is in the values of the dictionary with an if statement using:

if stream in playlist.values():
   ... # Do stuff

So a full solution would look something like:

playlist = {}

while len(playlist) < 5:
    name = input('Friend: ')
    stream = input('Which stream did they recommend? ')
    if stream in playlist.values():
        print("Someone else already recommended that")
    else:
        print (f'{name} recommended {stream}!')
        playlist[name] = stream

print('Playlist complete! Subscribe to:')
for name, stream in playlist.items():
    print(f'{stream}: recommended by {name}')
Kieran Wood
  • 1,297
  • 8
  • 15
  • 1
    Note: this is quadratic runtime-complexity compared to flipping the dictionary, which is linear – OneCricketeer Aug 19 '21 at 04:28
  • @OneCricketeer the swapping itself actually isn't any faster. Both are in the same while loop (otherwise the solution is not guaranteed to have 5) and require the same lookup using the `in` keyword which traverses the entire `dict.values()` list in my case and the entire `dict.keys()` list in yours. I still think swapping it is better for usability though since it feels more intuitive. Unless you are also including the bottom `for` loop, which I think it redundant but I left in since it was part of OP's code, in which case you are right. – Kieran Wood Aug 19 '21 at 04:40
  • I was under the impression that checking the keyset is constant time? Or you're saying that calling `in` or calling `.keys()` implicitly will still collect a set object in linear time itself? – OneCricketeer Aug 19 '21 at 04:44
  • Seems like it's O(1) - https://stackoverflow.com/questions/17539367/python-dictionary-keys-in-complexity – OneCricketeer Aug 19 '21 at 04:46
  • @OneCricketeer as far as I know the lookup complexity for the `in` keyword is identical in `dict.values()` as it is in `dict.keys()`, but I may be wrong. This is the only spot our code differs (bar the `for` loop, which I mentioned would make it higher complexity). – Kieran Wood Aug 19 '21 at 04:50
  • 1
    @OneCricketeer nvm you are right. It's an entirely different system. Doing native iteration uses a completely different method. I was under the impression python was just aliasing the lookup, but it's not https://stackoverflow.com/questions/44244281/how-come-key-in-dictionary-keys-slower-than-key-in-dictionary – Kieran Wood Aug 19 '21 at 04:51