-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist-1.py
More file actions
executable file
·95 lines (82 loc) · 2.5 KB
/
list-1.py
File metadata and controls
executable file
·95 lines (82 loc) · 2.5 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
# first_last6
def first_last6(nums):
'''
Given an array of ints, return True if 6 appears as either the first or last element
in the array. The array will be length 1 or more.
'''
return 6 == nums[0] or 6 == nums[-1]:
# same_first_last
def same_first_last(nums):
'''
Given an array of ints, return True if the array is length 1 or more, and the first element
and the last element are equal.
'''
return len(nums) >= 1 and nums[0] == nums [-1]
# make_pi
def make_pi():
'''
Return an int array length 3 containing the first 3 digits of pi, {3, 1, 4}.
'''
return [3,1,4]
# common_end
def common_end(a, b):
'''
Given 2 arrays of ints, a and b, return True if they have the same first element or they have
the same last element. Both arrays will be length 1 or more.
'''
return a[0] == b[0] or a[-1] == b[-1]
# sum3
def sum3(nums):
'''
Given an array of ints length 3, return the sum of all the elements.
'''
return sum(nums)
# rotate_left3
def rotate_left3(nums):
'''
Given an array of ints length 3, return an array with the elements "rotated left" so {1, 2, 3}
yields {2, 3, 1}.
'''
return [nums[1], nums[-1], nums[0]]
# reverse3
def reverse3(nums):
'''
Given an array of ints length 3, return a new array with the elements in reverse order,
so {1, 2, 3} becomes {3, 2, 1}.
'''
return nums[::-1]
# max_end3
def max_end3(nums):
'''
Given an array of ints length 3, figure out which is larger, the first or last element in the
array, and set all the other elements to be that value. Return the changed array.
'''
i = max(nums[0],nums[-1])
return [i,i,i]
# sum2
def sum2(nums):
'''
Given an array of ints, return the sum of the first 2 elements in the array. If the array
length is less than 2, just sum up the elements that exist, returning 0 if the array is length 0.
'''
return sum(nums[0:2])
# middle_way
def middle_way(a, b):
'''
Given 2 int arrays, a and b, each length 3, return a new array length 2 containing their
middle elements.
'''
return [a[1],b[1]]
# make_ends
def make_ends(nums):
'''
Given an array of ints, return a new array length 2 containing the first and last elements from
the original array. The original array will be length 1 or more.
'''
return [nums[0],nums[-1]]
# has23
def has23(nums):
'''
Given an int array length 2, return True if it contains a 2 or a 3.
'''
return 2 in nums or 3 in nums