Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Python: finding matching numbers algorithm between two sorted arrays

Hi! I am trying to write a python code that given two sorted arrays, looks for matching numbers. I am trying to run it on my Terminal by "Python matching.py", but it does not give me anything back.. How do I run my code on my Terminal and is the code below correct?? Thank you!!

def matching():
    a = [13, 27, 35, 40, 49, 55, 59]
    b = [17, 35, 39, 40, 55, 58, 60]
    c = []
    count = 0
    for i in range(len(a)):
        for j in range(len(b)):
            if b[j] == a[i]:
                c.append(b[j])
                count += 1
                continue
            elif b[j] > a[i] or b[j] < a[i]:
                continue

        print(count)
        print(c)

        return 1

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

You define your function matching() but do not call it. Add a call at the bottom of your file:

matching()
Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Alternatively, you do have to define a function and call it. You can just list the code and call it. In either case using python matching.py Is correct.

Chris Freeman,

I wasn't sure what the return value had to be, and I just chose to return 1. Since I don't want to return a particular value, are there any "rules" in terms of what to return in case like this? I've seen return 1 return 0 return -1, but I'm not sure what they really mean.. Thank you!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

You could also do...

a = [13, 27, 35, 40, 49, 55, 59]
b = [17, 35, 39, 40, 55, 58, 60]
c = []

for num in a:
    if a in b:
        c.append(a)
print(c)
Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

A function defaults to return None if not specified. In Python, functions typically return some value or None (the default).

Using a return value depends on you needs and can provide a "return status" if no other object is returned. Some systems have standard values. "0" typically is used for "success", all other values for an error status.

Relying on None in Python you can do:

If not some_funct():
   # code to handle None here