This is another of many exercises that wants us to use knowledge we have not been given.
We are being told that using in we can check wether its True or False that one string is contained in another.
So, this method returns True or False. We were NOT being told how to use in method to gain acces to contents of string. I can tell you if banana and cream have the same letters but how can I append the common part to a list?
The explanation given in the narrative is sufficient enough to describe membership which is what the in operator is designed to seek.
There are two ways to use in:
as an extraction tool: for x in range(10); and,
as a membership check: if x in y.
In the case of #1, we could print all the values from 0 to 9, and in the case of #2, we could print the outcome which will be True if x is in y, else False.
print ('a' in 'banana') # True
We will have by this point done some work with loops, in particular the example above,
for x in range(10):
or,
for letter in word:
Inside the loop we need to make comparisons and when there is a match, to append the matching character to a list.
matches = []
for x in str_a:
if x in str_b: matches.append(x)
return matches
Now if we don’t wish to allow duplicates, then we need a conditional inside the if statement (or some added logic)…
if x in str_b:
if x not in matches: matches.append(x)
You’d try each letter from one of them, so iterate through one.
For each such letter, you would compare it to each of the letters in the other string, so you would iterate through that as well.
And if it is found in the other string, then you’ve found that it exists in both. You can now append it to your result.
What’s new in that? Take a moment to consider how you would do it manually, you would end up with what I just described wouldn’t you? Don’t look for some magic solution to solve the whole thing for you, break it down into smaller parts and solve each individual subproblem until you’ve implemented the overall thing.
Is there some clever method programmers use to break problem into smaller parts? Somthing I could “whiteboard”, understand and then piece all of it together and form a code out of it? I heard about pseudocode but I don’t know if I’m using it correctly since stufff like above gets missed by me.
give yourself the input and produce the output, observe yourself. what. did. you. do.
your program isn’t going to do it any differently
If you know you’re going to iterate through one of the words, then write the loop for that. You’ve now solved one problem.
Inside that loop more things will happen. Pick something and implement it.
You’re not supposed to find a specific piece of code.
You’re supposed to come up with some series of steps whatever that may be, and then write that down in code.
So before you touch any code at all, get yourself a complete idea of what needs to happen. Can you tell another person to carry that out and get the correct result? If you don’t have that idea then what are you writing in code anyway, deciding what to do comes first.
That said, you might decide on a very small part, write it, and then reassess what else needs to be done, maybe you’d write out what you have so far to see whether the thing you wrote had the desired effect, and then you’d think of what needs to be done next.
Of course, it helps if your plan involves things that you know how to use. So for example a list, what can you do with a list, and what things of those would be useful, and then make a plan in terms of those operations.
If you know what the different things you’re using are, then you can make some good guesses to what should be possible to do with them as well, even if you haven’t been introduced to them. Or if such a thing does not exist, then go implement it and then it does exist.
Test are reporting incorrectly for this when this is valid and correct output.
def contains(big_string, little_string):
return True if big_string in little_string else False
print(contains("big", "bigger"))
print(contains("big", "fast"))
def common_letters(string_one, string_two):
return [x for x in string_one if x in string_two]
print(common_letters("abc", "abde"))