Reputation: 39
emp = []
def run(first, second):
front_index = 0
back_index = 0
if len(first) > len(second):
front = second
back = first
num = first_bigger_than_second(front, back)
return num
elif len(first) < len(second):
front = first
back = second
break
else:
front = second
back = first
break
def first_bigger_than_second(first, back):
first_index = 0
back_index = 0
print(len(first))
print(len(back))
while first_index < len(first) and back_index < len(back):
if first[first_index] == back[back_index] and back_index == len(back):
print("first", first)
print("first_index", first_index)
print("first[first_index]", first[first_index])
print("back", back)
print("back_index", back_index)
print("back[back_index]", back[back_index])
emp.append(back[back_index])
first_index += 1
back_index = 0
print(1)
elif first[first_index] == back[back_index]:
print("first", first)
print("first_index", first_index)
print("first[first_index]", first[first_index])
print("back", back)
print("back_index", back_index)
print("back[back_index]", back[back_index])
emp.append(back[back_index])
back_index += 1
print(2)
elif back_index == len(back) - 1:
print("first", first)
print("first_index", first_index)
print("first[first_index]", first[first_index])
print("back", back)
print("back_index", back_index)
print("back[back_index]", back[back_index])
first_index += 1
back_index = 0
print(3)
else:
print("first", first)
print("first_index", first_index)
print("first[first_index]", first[first_index])
print("back", back)
print("back_index", back_index)
print("back[back_index]", back[back_index])
back_index += 1
print(4)
num1 = 0plm1qaz
num2 = 1qaz0pl
run(num1, num2)
print(emp)
"""
All the print
functions are used to trace with if loop is been executed
The problem is when I print the emp
, the output is ["1", "q", "a", "z"]
expected output is: ["1", "q", "a", "z", "0", "p", "l"]
.
The code compares two word user inputs and returns the same word. Example: When you input "2aba"
and "2ac"
, it will give a result as ["2", "a", "a"]
since the code is work like the "2"
from the "2aba"
compare with "2ac"
and the "a"
from the "2aba"
is compared again with "2ac"
and "b"
from the "2aba"
is compared with "2ac"
and "a"
from the "2aba"
compare with "2ac"
Upvotes: 2
Views: 103
Reputation: 1173
break
in your run()
raises SyntaxError
as there is no loop. After removing it, your code can run but it is not giving you expected result.
When first_index = 3 (z in num2)
, back_index = 7 (z in num1)
, the second elif statement runs, back_index + 1 = 8 == len(num1)
, while loop break and you get the result ["1", "q", "a", "z"]
. Although you put the first and third if statement to reset the back_index, the first one never run as the while loop already break and the third one will not run as the second one runs.
We can use in
to check if a character of a word exist in another word.
def same_chars(first,second):
# use the shorter one as first
if len(first) > len(second):
first, second = second, first
emp = [chars for chars in first if chars in second]
return emp
num1 = "0plm1qaz"
num2 = "1qaz0pl"
print(same_chars(num1, num2))
Output:
['1', 'q', 'a', 'z', '0', 'p', 'l']
Upvotes: 1