Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String Comparing
#1
Hey! Everyone I Have A Problem:
The Problem is you are given two user input string now you have to compare them and find out unique and common letters!
Please Help Me!
Reply
#2
What have you tried so far, is this homework and you need to use certain ways of doing this ?
Reply
#3
How would you do it yourself? Just imagine step by step how you would do that and then code all those steps in Python.
Just for a start, here is how you can inspect a string letter by letter.
input_string = "abracadabra"
for letter in input_string:
    print(letter)
    # but here you would have to add your own logic.
Reply
#4
No I was Just Trying For Personal Learning Or you can say project for learning more python
But due to some recent windows error!
Windows Was Not Starting Due to Boot Error And The Guy Who repaired Laptop Formatted Whole Windows And All Things Lost!
Not Having Source Code

I don't Rememeber source code:
but here is the implementation:
string_1 = "Hello Bruh!!"
string_2 = "Hello Bruh!"
string_1_len = len(string_1)
string_2_len= len(string_2)
for i in range(string_1_len):
   for p in range(string_2_len):
       if i == p:
           print("The Unique Ones" + are + i)
Reply
#5
       if i == p:
You should not compare the numbers i and p, but the letters at that position.
       if string_1[i] == string_2[p]:
Reply
#6
Do you have an algorithm for doing this? What does it mean to be a "common letter"? What does it mean to be a "unique letter"? If I you write two words on a page how would you determine if the first letter in the first word is common or unique? Once you have the algorithm figured out it should be simple to translate that to Python.
Reply
#7
"deanhystad Wrote:Do you have an algorithm for doing this? What does it mean to be a "common letter"? What does it mean to be a "unique letter"? If I you write two words on a page how would you determine if the first letter in the first word is common or unique? Once you have the algorithm figured out it should be simple to translate that to Python.

@deanhystad, I think this is a valid question. Here is what I think the original poster is meaning to do when comparing the common and unique letters in each string.

string_1 = "Some String"
string_2 = "Something Stringy"
string_3 = "String Cheese"
In my opinion, they are either comparing vowels to consonants or they are comparing and contrasting each letter in each string to see how many times they are referenced. That way they find the most common and least commonly referenced letters in the string.

In this example we have the following letters: E, I, O, Y (Vowels) S, M, T, H, R, N, G (Consonants)

The program would then count the index values and compare it against other string values. I would write the code example if I could but I am not yet that good, still learning :)
Reply
#8
So Basically i want to say that i am trying to compare the strings and try to take letters and print it.
Reply
#9
look at set an set operations
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
Hey! Friends After Some mind blowing tricks on google i approach with these solution!
# Function to print common characters of two Strings 
# in alphabetical order 
from collections import Counter 

def common(str1,str2): 
	
	# convert both strings into counter dictionary 
	dict1 = Counter(str1) 
	dict2 = Counter(str2) 

	# take intersection of these dictionaries 
	commonDict = dict1 & dict2 

	if len(commonDict) == 0: 
		print -1
		return

	# get a list of common elements 
	commonChars = list(commonDict.elements()) 

	# sort list in ascending order to print resultant 
	# string on alphabetical order 
	commonChars = sorted(commonChars) 

	# join characters without space to produce 
	# resultant string 
	print(''.join(commonChars)) 

# Driver program 
if __name__ == "__main__": 
	str1 = input('Please Type The First String:\n')
	str2 = input('Please Type The Second String:\n')
	common(str1, str2) 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Comparing Dataframe to String? RockBlok 2 417 Nov-24-2023, 04:55 PM
Last Post: RockBlok
  Create a new list by comparing values in a list and string DBS 2 3,538 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