4

Okey so basically I have to check if one string has the same letters of another string. Both strings are obtained via input().

I don't want to double check if a letter is in the other string, so if I already checked that letter I want to skip to the next letter.

The code I have for now is this:

str1, str2 = list(input()), list(input()) 

if len(str1) > len(str2):
    str = str1
else: 
    str = str2

for x in str:
    c = 0
    if x in str2:
        c += 1
if c != 0:
    print("Both have the same letters!") 
else: 
    print("Nope there are some letters missing..")

I don't know if I should work with the lists instead of using a counter.. please a detailled explanation of the solution or some good quality guidance would be very appreciated! <3

blhsing
  • 91,368
  • 6
  • 71
  • 106
  • Currently your logic does not check for presence of all letters. For example - `str1 = 'foo', str2='fbar'`. The output results in **Both have the same letters!**. One approach is to keep the letters of both the strings in 2 different sets and check if sets are equal. – Mahesh Dec 17 '18 at 22:54

4 Answers4

7

Converting strings to sets of individual symbols remove duplicate symbols, so we can simply compare them:

if set(str1) == set(str2):
    print("Both have the same letters!") 
else:
    print("Nope there are some letters missing..")

Note:

As the order of elements in sets is not important, we may even compare them, e. g.

if set(str1) <= set(str2):        # <= means "is subset" in this context
    print("All symbols in str1 are in str2, too.")

or

if set(str1) < set(str2):        # < means "is a proper subset" in this context
    print("All symbols in str1 are in str2, too, "
          "but str2 has at least 1 symbol not contained in str1.")
MarianD
  • 13,096
  • 12
  • 42
  • 54
  • That was perfect! Thanks a lot! If I understood the method set() well what it does is make a dictionary? Sorry if the question is ducmb but Im trying to learn step by step. I really appreciate everyones help! – Jesus A. Garrido Cordero Dec 18 '18 at 10:32
  • No, `set()` *doesn't make a dictionary*, it makes a *set*, which is itself a *special type of collection*, corresponding to the *mathematical* definition of the *set*. E. g.: `set("abcacab")` will be `{"a", "b", "c"}` (or `{"c", "a", "b"}`, which is the same - the order of elements doesn't matter) . (An example of a dictionary may be `{1: "a", 2: "b", 3: "c"}` - it is enclosed in the *same pair of curly brackets (`{})`*, but consists of pairs `key`: `value`, delimited with a colon (`:`).) – MarianD Dec 18 '18 at 14:54
  • Okey now I fully understand the difference thanks a lot! You are a great person for teching me this without asking anything! – Jesus A. Garrido Cordero Dec 18 '18 at 17:40
0

Some issues with the code are that, str2 is always used for comparison even if str2 is longer than str1

for x in str:
    c = 0
    if x in str2:

Next, c is set to 0 for every character in str, Instead you can have a counter to count the number of chars not in the other string

This should do the job

str1, str2 = list(input()), list(input()) 

if len(str1) > len(str2):
    str = str1
    compared_string = str2
else: 
    str = str2
    compared_string = str1

not_in_other_string = 0
for x in str:
    if x  not in compared_string:
        not_in_other_string += 1
if not_in_other_string == 0:
    print("Both have the same letters!") 
else: 
    print("Nope there are some letters missing..")
tooTired
  • 179
  • 7
0

It sounds like you want to find if all of the characters in one string are in some larger string.

You could use unique = ''.join(set(substring)) to get a list of chars in the substring and then make a list comprehension to get all the chars in the larger string.

Here is my example:

unique = ''.join(set(substring))
not_in = [char for char in unique if char not in superstring]

Now you can check not_in to see if it is null, otherwise there was a char in the substring that was not in the superstring.

For example:

superstring = "hello"
substring = "xllo"

unique = ''.join(set(substring))
not_in = [char for char in unique if char not in superstring]

print not_in
>>>['x']

print not_in == []
>>>False

Typically, the preferred way to do things in python is using list comprehensions or use some built in function that already checks strings for you, rather than the C style where you go through a loop and check letter for letter explicitly. This is the idiomatic way of pythonic programming.

Breaking apart the list comprehension we have a loop like this:

   for char in unique: 
        if char not in superstring:
             char #char meets conditions so it is the value used in [char ... 

Here would be the modification I would make

str1, str2 = input(), input()

unique = ''.join(set(str1))
not_in = [char for char in unique if char not in str2]

if not_in == []:
    print("Both have the same letters!") 
else: 
    print("Nope there are some letters missing..")
0
str1, str2 = input().split()

if len(str1) > len(str2):
    string_to_check= str1
    string_from_which_to_check = str2
else: 
    string_to_check = str2

string_from_which_to_check = str1    
not_in_other_string  = set(string_to_check) - set(string_from_which_to_check)
if len(not_in_other_string )==0:
     print("character are present in string")
else:
    print("{} not present in string".format(str(not_in_other_string )))
sahasrara62
  • 10,069
  • 3
  • 29
  • 44