Ich bin schwer zu finden, wie diese Frage zu tun.Finden Sie gemeinsame Zeichen in Strings
Ich habe 2 Dateien im selben Verzeichnis.
Dies ist die erste Datei - "lab_2_util.py", die mir gegeben wurde.
def get_common_characters(str1, str2):
"""
This function takes in 2 string parameters, namely str1 and str2, and returns
a string containing all the characters of str1 that appear in str2.
Note that if a character appears more than once in both str1
and str2, it will only appear once in the returned string.
"""
result = ""
for ch in str1:
if (ch in str2) and (not (ch in result)):
result = result + ch
return result
Die zweite Datei - "lab_2_q7.py". Ich muss die Funktion get_common_characters() von lab_2_util.py verwenden, um diese Funktion zu implementieren.
Diese Funktion sollte mir "AB" zurückgeben. Ich muss nur den Rückgabecode ändern.
Dies ist mein Versuch:
from lab_2_util import get_common_characters
def q7_1(str1, str2, str3):
"""
This function takes in 3 strings str1, str2 and str3, and returns
a string that contains the common characters shared by str1, str2
and str3. If a character appears more than once in str1, str2, and
str3, it will only appear once in the returned string.
"""
# Modify the code below to return a correct string.
**return (get_common_characters)**
# The code below is for testing the functions. Do not modify the code below.
s1 = "ABBCDE"
s2 = "ABBC"
s3 = "ABBD"
print(q7_1(s1, s2, s3))
ich völlig verloren bin. Ich weiß, wie man die Funktion aus einer anderen Datei in demselben Verzeichnis importiert, aber ich bin nicht sicher, wie man den Rückkehrcode ändert.
Jede Hilfe würde wirklich geschätzt werden. Vielen Dank.