-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
lists.py
113 lines (97 loc) · 3.25 KB
/
lists.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Based on materials copyright 2010 Google Inc.
# Licensed under the Apache License, Version 2.0
def match_ends(words):
"""
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.
>>> match_ends(['aba', 'xyz', 'aa', 'x', 'bbb'])
3
>>> match_ends(['', 'x', 'xy', 'xyx', 'xx'])
2
>>> match_ends(['aaa', 'be', 'abc', 'hello'])
1
"""
total = 0
for i in words:
if len(i) >= 2 and i[0] == i[-1]:
total += 1
print total
def front_x(words):
"""
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'].
>>> front_x(['bbb', 'ccc', 'axx', 'xzz', 'xaa'])
['xaa', 'xzz', 'axx', 'bbb', 'ccc']
>>> front_x(['ccc', 'bbb', 'aaa', 'xcc', 'xaa'])
['xaa', 'xcc', 'aaa', 'bbb', 'ccc']
>>> front_x(['mix', 'xyz', 'apple', 'xanadu', 'aardvark'])
['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
"""
x_words = []
not_x_words = []
for i in words:
if i[0] == "x":
x_words.append(i)
else:
not_x_words.append(i)
new_list = []
for i in sorted(x_words):
new_list.append(i)
for i in sorted(not_x_words):
new_list.append(i)
print new_list
def sort_last(tuples):
"""
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)].
>>> sort_last([(1, 3), (3, 2), (2, 1)])
[(2, 1), (3, 2), (1, 3)]
>>> sort_last([(2, 3), (1, 2), (3, 1)])
[(3, 1), (1, 2), (2, 3)]
>>> sort_last([(1, 7), (1, 3), (3, 4, 5), (2, 2)])
[(2, 2), (1, 3), (3, 4, 5), (1, 7)]
"""
print sorted(tuples, key=lambda tup: tup[-1])
def remove_adjacent(nums):
"""
Given a list of numbers, return a list where all adjacent equal
elements have been reduced to a single element, so [1, 2, 2, 3]
returns [1, 2, 3]. You may create a new list or modify the passed
in list.
>>> remove_adjacent([1, 2, 2, 3])
[1, 2, 3]
>>> remove_adjacent([2, 2, 3, 3, 3])
[2, 3]
>>> remove_adjacent([3, 2, 3, 3, 3])
[3, 2, 3]
>>> remove_adjacent([])
[]
"""
new_list = []
for i in nums:
if new_list:
if new_list[-1] != i:
new_list.append(i)
else:
new_list.append(i)
print new_list
def linear_merge(list1, list2):
"""
Given two lists sorted in increasing order, create and return a
merged list of all the elements in sorted order. You may modify
the passed in lists. Ideally, the solution should work in "linear"
time, making a single pass of both lists.
>>> linear_merge(['aa', 'xx', 'zz'], ['bb', 'cc'])
['aa', 'bb', 'cc', 'xx', 'zz']
>>> linear_merge(['aa', 'xx'], ['bb', 'cc', 'zz'])
['aa', 'bb', 'cc', 'xx', 'zz']
>>> linear_merge(['aa', 'aa'], ['aa', 'bb', 'bb'])
['aa', 'aa', 'aa', 'bb', 'bb']
"""
list1.extend(list2)
print sorted(list1)