Aug-05-2020, 02:14 PM
(This post was last modified: Aug-05-2020, 02:14 PM by deanhystad.)
For doing set operations (&) you should use sets. Logic for common would be very similar to your dictionary solution.
I have to wonder why you didn't start with something obvious like this:
From the obvious solution you may decide to use a list comprehension.
With your solution I am left wondering why you are using a Counter? What does a Counter do? I go read up on Counter and see it creates a dictionary that has the count for each element. Ok, you counted the number of times each letter is used in each word. Wonder why you are doing that? Is the count important? Oh, you are just using Counter because the counter keys are a list of each letter in the word. Aren't there easier ways todo that? And what does "&" do in a Counter? It gives you the elements that are in both dictionaries. That is a set operation. Why not use sets?
This is the kind of solution you only come to through a google search. It is a really odd way to solve the problem, and I don't know if you learned much in solving the problem this way other than some exposure to the Collections library.
With your next programming challenge pretend it is 1980 and the internet didn't exist. Pull out a paper notebook and a pencil and treat the problem as a logic puzzle instead of a programming problem. Once you can solve on paper you can translate to code. The important skills you need to develop are the problem solving and the translation. These skills are not learned in google.
a = set('aaabbbcccdddeeefffggg') b = set('fffggghhhiiijjjkkkhhh') common = a & bFinding unique letters is a tiny bit more complicated, but not much.
I have to wonder why you didn't start with something obvious like this:
a = 'aaabbbcccdddeeefffggg' b = 'fffggghhhiiijjjkkklll' common = '' unique = '' for letter in a: if letter in b: common += letter else: unique += letter for letter in b: if letter not in a: unique += letter print('Common letters are:', common) print('Unique letters are:', unique)
Output:Common letters are: fffggg
Unique letters are: aaabbbcccdddeeehhhiiijjjkkklll
The output may look funny, but it is easy to clean up the duplicate letters:print('Common letters are:', *set(common)) print('Unique letters are:', *set(unique))
Output:Common letters are: f g
Unique letters are: a c d e b h i k l
The nice thing about this solution is it is it uses the steps you would use to solve this problem using pencil and paper. Anyone can look at this code and it is very obvious what it is doing.From the obvious solution you may decide to use a list comprehension.
a = 'aaabbbcccdddeeefffggg' b = 'fffggghhhiiijjjkkklll' common = [letter for letter in a if letter in b] unique = [letter for letter in a if letter not in b] + \ [letter for letter in b if letter not in a] print('Common letters are:', *set(common)) print('Unique letters are:', *set(unique))This is less obvious, but you can still read the code and get an understanding of what is going on. From here you might wonder if there is something in Python that does what the for loop is doing and that might lead you to thinking about the words as a set of letters and using set algebra. You would have a path of development, starting with comprehension of the problem, and initial solution, refinement and maybe a flash of insight.
With your solution I am left wondering why you are using a Counter? What does a Counter do? I go read up on Counter and see it creates a dictionary that has the count for each element. Ok, you counted the number of times each letter is used in each word. Wonder why you are doing that? Is the count important? Oh, you are just using Counter because the counter keys are a list of each letter in the word. Aren't there easier ways todo that? And what does "&" do in a Counter? It gives you the elements that are in both dictionaries. That is a set operation. Why not use sets?
This is the kind of solution you only come to through a google search. It is a really odd way to solve the problem, and I don't know if you learned much in solving the problem this way other than some exposure to the Collections library.
With your next programming challenge pretend it is 1980 and the internet didn't exist. Pull out a paper notebook and a pencil and treat the problem as a logic puzzle instead of a programming problem. Once you can solve on paper you can translate to code. The important skills you need to develop are the problem solving and the translation. These skills are not learned in google.