-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList-1.py
75 lines (63 loc) · 2.45 KB
/
List-1.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
#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.
def first_last6(nums):
return nums[0]==6 or nums[len(nums)-1]==6
#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.
def same_first_last(nums):
l=len(nums)-1
return len(nums)>=1 and nums[0]==nums[l]
#Return an int array length 3 containing the first 3 digits of pi, {3, 1, 4}.
def make_pi():
return [3,1,4]
#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.
def common_end(a, b):
l_a=len(a)-1
l_b=len(b)-1
return a[0]==b[0] or a[l_a]==b[l_b]
#Given an array of ints length 3, return the sum of all the elements
def sum3(nums):
return sum(nums)
#Given an array of ints length 3, return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}.
def rotate_left3(nums):
e=nums[0]
nums.remove(nums[0])
nums.append(e)
return 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}
def reverse3(nums):
nums.reverse()
return 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.
def max_end3(nums):
a=nums[0]
b=nums[len(nums)-1]
c=0
if(a>b):
c=a
elif(a<b):
c=b
else:
c=a
for x,y in enumerate(nums):
nums[x]=c
return 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.
def sum2(nums):
if(len(nums)<=2):
return sum(nums)
else:
return sum(nums[:2])
#Given 2 int arrays, a and b, each length 3, return a new array length 2 containing their middle elements.
def middle_way(a, b):
return [a[1],b[1]]
#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.
def make_ends(nums):
l = len(nums) - 1
if (len(nums) == 2):
return nums
elif (len(nums) == 1):
return nums * 2
else:
return [nums[0], nums[l]]
#Given an int array length 2, return True if it contains a 2 or a 3.
def has23(nums):
return (2 in nums) or (3 in nums)