Skip to content

Commit 82258f4

Browse files
authored
Add files via upload
1 parent 22eae9a commit 82258f4

12 files changed

+232
-0
lines changed

List-1/common_end.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#CodingBat - Python
2+
3+
#common_end
4+
5+
6+
#Given 2 arrays of ints, a and b, return True if they have the same first
7+
#element or they have the same last element. Both arrays will be length 1 or more.
8+
9+
# common_end([1, 2, 3], [7, 3]) → True
10+
# common_end([1, 2, 3], [7, 3, 2]) → False
11+
# common_end([1, 2, 3], [1, 3]) → True
12+
13+
def common_end(a, b):
14+
return True if a[0] == b[0] or a[-1] == b[-1] else False
15+
16+
17+
print(common_end([1, 2, 3], [7, 3]))
18+
print(common_end([1, 2, 3], [7, 3, 2]))
19+
print(common_end([1, 2, 3], [1, 3]))

List-1/first_last6.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#CodingBat - Python
2+
3+
#first_last6
4+
5+
#Given an array of ints, return True if 6 appears as either the first or last
6+
#element in the array. The array will be length 1 or more.
7+
8+
# first_last6([1, 2, 6]) → True
9+
# first_last6([6, 1, 2, 3]) → True
10+
# first_last6([13, 6, 1, 2, 3]) → False
11+
12+
def first_last6(nums):
13+
return True if nums[0] == 6 or nums[-1] == 6 else False
14+
15+
16+
print(first_last6([1, 2, 6]))
17+
print(first_last6([6, 1, 2, 3]))
18+
print(first_last6([13, 6, 1, 2, 3]))

List-1/has23.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#CodingBat - Python
2+
3+
#has23
4+
5+
#Given an int array length 2, return True if it contains a 2 or a 3.
6+
7+
# has23([2, 5]) → True
8+
# has23([4, 3]) → True
9+
# has23([4, 5]) → False
10+
11+
def has23(nums):
12+
if (nums[0] == 2 or nums[-1] == 2) or (nums[0] == 3 or nums[-1] == 3):
13+
return True
14+
else:
15+
return False
16+
17+
18+
print(has23([2, 5]))
19+
print(has23([4, 3]))
20+
print(has23([4, 5]))

List-1/make_ends.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#CodingBat - Python
2+
3+
#make_ends
4+
5+
#Given an array of ints, return a new array length 2 containing the first and
6+
#last elements from the original array. The original array will be length 1 or
7+
#more.
8+
9+
# make_ends([1, 2, 3]) → [1, 3]
10+
# make_ends([1, 2, 3, 4]) → [1, 4]
11+
# make_ends([7, 4, 6, 2]) → [7, 2]
12+
13+
def make_ends(nums):
14+
return [nums[0], nums[-1]]
15+
16+
print(make_ends([1, 2, 3]))
17+
print(make_ends([1, 2, 3, 4]))
18+
print(make_ends([7, 4, 6, 2]))

List-1/make_pi.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#CodingBat - Python
2+
3+
#make_pi
4+
5+
#Return an int array length 3 containing the first 3 digits of pi, {3, 1, 4}.
6+
7+
#make_pi() → [3, 1, 4]
8+
9+
def make_pi():
10+
return [3, 1, 4]
11+
12+
print(make_pi())

List-1/max_end3.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#CodingBat - Python
2+
3+
#max_end3
4+
5+
#Given an array of ints length 3, figure out which is larger, the first or last
6+
#element in the array, and set all the other elements to be that value. Return
7+
#the changed array.
8+
9+
# max_end3([1, 2, 3]) → [3, 3, 3]
10+
# max_end3([11, 5, 9]) → [11, 11, 11]
11+
# max_end3([2, 11, 3]) → [3, 3, 3]
12+
13+
def max_end3(nums):
14+
if nums[0] > nums[-1]:
15+
return [nums[0], nums[0], nums[0]]
16+
else:
17+
return [nums[-1], nums[-1], nums[-1]]
18+
19+
20+
print(max_end3([1, 2, 3]))
21+
print(max_end3([11, 5, 9]))
22+
print(max_end3([2, 11, 3]))

List-1/middle_way.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#CodingBat - Python
2+
3+
#middle_way
4+
5+
#Given 2 int arrays, a and b, each length 3, return a new array length 2
6+
#containing their middle elements.
7+
8+
# middle_way([1, 2, 3], [4, 5, 6]) → [2, 5]
9+
# middle_way([7, 7, 7], [3, 8, 0]) → [7, 8]
10+
# middle_way([5, 2, 9], [1, 4, 5]) → [2, 4]
11+
12+
def middle_way(a, b):
13+
return [a[1], b[1]]
14+
15+
16+
print(middle_way([1, 2, 3], [4, 5, 6]))
17+
print(middle_way([7, 7, 7], [3, 8, 0]))
18+
print(middle_way([5, 2, 9], [1, 4, 5]))

List-1/reverse3.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#CodingBat - Python
2+
3+
#reverse3
4+
5+
#Given an array of ints length 3, return a new array with the elements in
6+
#reverse order, so {1, 2, 3} becomes {3, 2, 1}.
7+
8+
# reverse3([1, 2, 3]) → [3, 2, 1]
9+
# reverse3([5, 11, 9]) → [9, 11, 5]
10+
# reverse3([7, 0, 0]) → [0, 0, 7]
11+
12+
13+
def reverse3(nums):
14+
return [nums[2], nums[1], nums[0]]
15+
16+
print(reverse3([1, 2, 3]))
17+
print(reverse3([5, 11, 9]))
18+
print(reverse3([7, 0, 0]))

List-1/rotate_left3.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#CodingBat - Python
2+
3+
#rotate_left3
4+
5+
#Given an array of ints length 3, return an array with the elements
6+
#"rotated left" so {1, 2, 3} yields {2, 3, 1}.
7+
8+
# rotate_left3([1, 2, 3]) → [2, 3, 1]
9+
# rotate_left3([5, 11, 9]) → [11, 9, 5]
10+
# rotate_left3([7, 0, 0]) → [0, 0, 7]
11+
12+
def rotate_left3(nums):
13+
return [nums[1], nums[2], nums[0]]
14+
15+
16+
print(rotate_left3([1, 2, 3]))
17+
print(rotate_left3([5, 11, 9]))
18+
print(rotate_left3([7, 0, 0]))

List-1/same_first_last.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#CodingBat - Python
2+
3+
#same_first_last
4+
5+
#Given an array of ints, return True if the array is length 1 or more, and the
6+
#first element and the last element are equal.
7+
8+
# same_first_last([1, 2, 3]) → False
9+
# same_first_last([1, 2, 3, 1]) → True
10+
# same_first_last([1, 2, 1]) → True
11+
12+
def same_first_last(nums):
13+
return True if len(nums) >= 1 and nums[0] == nums[-1] else False
14+
15+
print(same_first_last([1, 2, 3]))
16+
print(same_first_last([1, 2, 3, 1]))
17+
print(same_first_last([1, 2, 1]))

List-1/sum2.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#CodingBat - Python
2+
3+
#sum2
4+
5+
#Given an array of ints, return the sum of the first 2 elements in the array.
6+
#If the array length is less than 2, just sum up the elements that exist,
7+
#returning 0 if the array is length 0.
8+
9+
# sum2([1, 2, 3]) → 3
10+
# sum2([1, 1]) → 2
11+
# sum2([1, 1, 1, 1]) → 2
12+
13+
def sum2(nums):
14+
result = 0
15+
16+
if len(nums) == 0:
17+
return result
18+
19+
elif len(nums) < 2:
20+
for e in nums:
21+
result += e #result = result + e
22+
23+
else:
24+
result = nums[0] + nums[1]
25+
26+
return result
27+
28+
29+
print(sum2([1, 2, 3]))
30+
print(sum2([1, 1]))
31+
print(sum2([1, 1, 1, 1]))

List-1/sum3.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#CodingBat - Python
2+
3+
#sum3
4+
5+
#Given an array of ints length 3, return the sum of all the elements.
6+
7+
# sum3([1, 2, 3]) → 6
8+
# sum3([5, 11, 2]) → 18
9+
# sum3([7, 0, 0]) → 7
10+
11+
def sum3(nums):
12+
result = 0
13+
14+
for e in nums:
15+
result += e #result = result + e
16+
17+
return result
18+
19+
print(sum3([1, 2, 3]))
20+
print(sum3([5, 11 , 2]))
21+
print(sum3([7, 0, 0]))

0 commit comments

Comments
 (0)