-2

I was wondering if you could give me a hand with some questions on the google python class, they are based around lists.

    # 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.
# Note: python does not have a ++ operator, but += works.
def match_ends(words):
    print words


# B. 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']
# Hint: this can be done by making 2 lists and sorting each of them
# before combining them.
def front_x(words):
  return

these are the questions, I've had several attempets and never really got anywhere, I was wondering if someone could do it so I could see the answer and work backwards.

Thanks! George

4

2 回答 2

2
def match_ends(words):
    count = 0
    for item in words:
        if len(item) >= 2 and item[0] == item[-1]:
            count += 1
    return count


def front_x(words):
    x_list = []
    other_list = []
    for item in words:
        if item[0].lower() == "x":
            x_list.append(item)
        else:
            other_list.append(item)
    x_list.sort()
    other_list.sort()
    return x_list + other_list
于 2013-04-19T21:31:25.217 回答
0

你不会从被告知答案中学到很多东西,但也许这可以帮助你自己到达那里:

def match_ends(words):
    count = 0
    for word in words:
        # (do something)
    return count

def front_x(words):
    with_x = []
    without_x = []
    for i in words:
        # (do something)
    # (sort the lists)
    return with_x + without_x
于 2013-04-19T21:24:31.427 回答