Repository navigation
Expand file tree
/
Copy pathlists.py
More file actions
129 lines (98 loc) · 3.39 KB
/
Copy pathlists.py
File metadata and controls
129 lines (98 loc) · 3.39 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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
"""
cnt = 0
for val in words:
if len(val) >= 2 and val[0] == val[len(val)-1]:
cnt = cnt + 1
return cnt
raise NotImplementedError
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']
"""
retlist = words
retlist.sort()
insindx = 0
for val in retlist:
if val[0] == 'x':
retlist.remove(val)
retlist.insert(insindx,val)
insindx = insindx + 1
return retlist
raise NotImplementedError
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)]
"""
return sorted(tuples, key=lambda s:s[-1])
raise NotImplementedError
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([])
[]
"""
retnums = []
currentnum = 0
for numindx in range(0,len(nums)):
if not nums[numindx] == currentnum:
retnums.append(nums[numindx])
currentnum = nums[numindx]
return retnums
raise NotImplementedError
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']
"""
mergedlist = list1 + list2
mergedlist.sort()
return mergedlist
raise NotImplementedError