Programming 102 Week 4 Tasks (Python)

Python Search Functions

Continuing on with Programming 102 via FutureLearn, and onto Week 4’s Tasks and Challenges.

4.5 Implementing Linear Search

Task: “Have a go at implementing your own linear search function, in a way that would work on an unsorted sequence of integers.”

# geektechstuff
# Linear Search
sequence = [‘A’,’B’,’C’,’D’]
def sequence_while(list):
position = 0
while position <len(sequence):
    print(sequence[position],”is at position”,str(position))
position += 1
def sequence_loop(list):
for position inrange(len(sequence)):
    print(sequence[position],’ is at position’,str(position))
def sequence_enumerate(list):
for position,item inenumerate(sequence):
    print(item,’is at position’,position)
def while_find(list,item_to_find):
position = 0
while position <len(sequence):
    if sequence[position] == item_to_find:
        print(sequence[position],”is at position”,str(position))
    break
else:
   position += 1

102_4_5_linear_search_geektechstuff
Python Search Functions

One thought on “Programming 102 Week 4 Tasks (Python)

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.