Python: List basic operations

The program below is a solution to google’s python exercise which contains basic list operations.


# A. match_ends
# Given a list of strings, return the count of the number of
# strings where the string length is 2 or more and the first
# and last chars of the string are the same.

def match_ends(words):
# +++your code here+++
string_counter = 0
for word in words:
if (len(word) >= 2):
if (word[0] == word[len(word)-1]):
string_counter += 1

return string_counter

# front_x
# Given a list of strings, return a list with the strings
# in sorted order, except group all the strings that begin with ‘x’ first.
# e.g. [‘mix’, ‘xyz’, ‘apple’, ‘xanadu’, ‘aardvark’] yields
# [‘xanadu’, ‘xyz’, ‘aardvark’, ‘apple’, ‘mix’]

def front_x(words):

x_list = []
non_x_list = []
for word in words:
if (word[0] == “x”):
x_list.append(word)
else:
non_x_list.append(word)

x_list.sort()
non_x_list.sort()

for i in range(len(non_x_list)):
x_list.append(non_x_list[i])

return x_list

# sort_last
# Given a list of non-empty tuples, return a list sorted in increasing
# order by the last element in each tuple.
# e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields
# [(2, 2), (1, 3), (3, 4, 5), (1, 7)]

def sort_last(tuples):
# +++your code here+++
my_dict = {}
last_items = []
for my_tuple in tuples:
last_items.append(my_tuple[len(my_tuple)-1])
my_dict[my_tuple[len(my_tuple)-1]] = my_tuple
last_items.sort()
#print (my_dict)
#print (“%s” %last_items)
sorted_items = []
for key in last_items:
sorted_items.append(my_dict[key])
#print (“%s ” %sorted_items)

return sorted_items

# Simple provided test() function used in main() to print
# what each function returns vs. what it’s supposed to return.
def test(got, expected):
if got == expected:
prefix = ‘ OK ‘
else:
prefix = ‘  X ‘
print (‘%s got: %s expected: %s’ % (prefix, repr(got), repr(expected)))

# Calls the above functions with interesting inputs.
def main():
print (‘match_ends’)
test(match_ends([‘aba’, ‘xyz’, ‘aa’, ‘x’, ‘bbb’]), 3)
test(match_ends([”, ‘x’, ‘xy’, ‘xyx’, ‘xx’]), 2)
test(match_ends([‘aaa’, ‘be’, ‘abc’, ‘hello’]), 1)

print ()
print (‘front_x’)
test(front_x([‘bbb’, ‘ccc’, ‘axx’, ‘xzz’, ‘xaa’]),
[‘xaa’, ‘xzz’, ‘axx’, ‘bbb’, ‘ccc’])
test(front_x([‘ccc’, ‘bbb’, ‘aaa’, ‘xcc’, ‘xaa’]),
[‘xaa’, ‘xcc’, ‘aaa’, ‘bbb’, ‘ccc’])
test(front_x([‘mix’, ‘xyz’, ‘apple’, ‘xanadu’, ‘aardvark’]),
[‘xanadu’, ‘xyz’, ‘aardvark’, ‘apple’, ‘mix’])

print ()
print (‘sort_last’)
test(sort_last([(1, 3), (3, 2), (2, 1)]),
[(2, 1), (3, 2), (1, 3)])
test(sort_last([(2, 3), (1, 2), (3, 1)]),
[(3, 1), (1, 2), (2, 3)])
test(sort_last([(1, 7), (1, 3), (3, 4, 5), (2, 2)]),
[(2, 2), (1, 3), (3, 4, 5), (1, 7)])

if __name__ == ‘__main__’:
main()

Advertisements

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

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


%d bloggers like this: