Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String Comparing
#11
For doing set operations (&) you should use sets. Logic for common would be very similar to your dictionary solution.
a = set('aaabbbcccdddeeefffggg')
b = set('fffggghhhiiijjjkkkhhh')
common = a & b
Finding 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.
Reply
#12
Hey! First: This is not my code i found it in google
Second: i post here to get some logic about or some simple answer than this
third: Thanks! For your answers!
Reply
#13
(Aug-05-2020, 05:09 PM)Harshil Wrote: Hey! First: This is not my code i found it in google
Second: i post here to get some logic about or some simple answer than this
third: Thanks! For your answers!
It is quite obviously not your code, and finding a complete answer to a problem you are trying to solve is not a good way to improve your skills. Neither is asking questions until you've actually done enough work to understand the answer.
Reply
#14
Sorry But I don't what types of project i should do to make python skills stronger
Reply
#15
Do you have any programming experience? If know another programming language you could focus on translating some of you old programs to Python. If you are new to programming I suggest making simple games. They teach skills and you have something to play when you are done.
Reply
#16
Yes! In C but not very much cause i am a reverse engineer and i thought to crack crackmes in x64dbg and writing keygen in python!
But Why are you asking?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Comparing Dataframe to String? RockBlok 2 410 Nov-24-2023, 04:55 PM
Last Post: RockBlok
  Create a new list by comparing values in a list and string DBS 2 3,532 Jan-14-2017, 07:59 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020