Skip to content

Commit 4382c01

Browse files
committed
List-1 directory added
1 parent ec79024 commit 4382c01

12 files changed

+145
-0
lines changed

List-1/common_end.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Given 2 arrays of ints, a and b,
2+
# return True if they have the same first element or they have the same last element.
3+
# Both arrays will be length 1 or more.
4+
5+
6+
# common_end([1, 2, 3], [7, 3]) → True
7+
# common_end([1, 2, 3], [7, 3, 2]) → False
8+
# common_end([1, 2, 3], [1, 3]) → True
9+
10+
def common_end(a, b):
11+
return (len(a) and len(b) >= 1 and a == b or a[0] == b[0] or a[-1] == b[-1])

List-1/first_last6.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Given an array of ints,
2+
# return True if 6 appears as either the first or last element in the array.
3+
# The array will be length 1 or more.
4+
5+
6+
# first_last6([1, 2, 6]) → True
7+
# first_last6([6, 1, 2, 3]) → True
8+
# first_last6([13, 6, 1, 2, 3]) → False
9+
10+
def first_last6(nums):
11+
return(nums[0] == 6 or nums[-1] == 6)

List-1/has23.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Given an int array length 2,
2+
# return True if it contains a 2 or a 3.
3+
4+
5+
# has23([2, 5]) → True
6+
# has23([4, 3]) → True
7+
# has23([4, 5]) → False
8+
9+
def has23(nums):
10+
return 2 == nums[0] or 2 == nums[1] or 3 == nums[0] or 3 == nums[1]

List-1/make_ends.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Given an array of ints,
2+
# return a new array length 2 containing the first and last elements from the original array.
3+
# The original array will be length 1 or more.
4+
5+
6+
# make_ends([1, 2, 3]) → [1, 3]
7+
# make_ends([1, 2, 3, 4]) → [1, 4]
8+
# make_ends([7, 4, 6, 2]) → [7, 2]
9+
10+
def make_ends(nums):
11+
return [nums[0], nums[-1]]

List-1/make_pi.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Return an int array length 3 containing the first 3 digits of pi, {3, 1, 4}.
2+
3+
4+
# make_pi() → [3, 1, 4]
5+
6+
def make_pi():
7+
return [3, 1, 4]

List-1/max_end3.py

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

List-1/middle_way.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Given 2 int arrays, a and b, each length 3,
2+
# return a new array length 2 containing their middle elements.
3+
4+
5+
# middle_way([1, 2, 3], [4, 5, 6]) → [2, 5]
6+
# middle_way([7, 7, 7], [3, 8, 0]) → [7, 8]
7+
# middle_way([5, 2, 9], [1, 4, 5]) → [2, 4]
8+
9+
def middle_way(a, b):
10+
return [a[1], b[1]]

List-1/reverse3.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Given an array of ints length 3,
2+
# return a new array with the elements in reverse order,
3+
# so {1, 2, 3} becomes {3, 2, 1}.
4+
5+
6+
# reverse3([1, 2, 3]) → [3, 2, 1]
7+
# reverse3([5, 11, 9]) → [9, 11, 5]
8+
# reverse3([7, 0, 0]) → [0, 0, 7]
9+
10+
def reverse3(nums):
11+
return [nums[2], nums[1], nums[0]]

List-1/rotate_left3.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Given an array of ints length 3,
2+
# return an array with the elements "rotated left"
3+
# so {1, 2, 3} yields {2, 3, 1}.
4+
5+
6+
# rotate_left3([1, 2, 3]) → [2, 3, 1]
7+
# rotate_left3([5, 11, 9]) → [11, 9, 5]
8+
# rotate_left3([7, 0, 0]) → [0, 0, 7]
9+
10+
def rotate_left3(nums):
11+
return [nums[1], nums[2], nums[0]]

List-1/same_first_last.py

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

List-1/sum2.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Given an array of ints,
2+
# return the sum of the first 2 elements in the array.
3+
# If the array length is less than 2,
4+
# just sum up the elements that exist,
5+
# returning 0 if the array is length 0.
6+
7+
8+
# sum2([1, 2, 3]) → 3
9+
# sum2([1, 1]) → 2
10+
# sum2([1, 1, 1, 1]) → 2
11+
12+
def sum2(nums):
13+
if len(nums) < 2:
14+
return sum(nums)
15+
else:
16+
return nums[0]+nums[1]

List-1/sum3.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Given an array of ints length 3,
2+
# return the sum of all the elements.
3+
4+
5+
# sum3([1, 2, 3]) → 6
6+
# sum3([5, 11, 2]) → 18
7+
# sum3([7, 0, 0]) → 7
8+
9+
def sum3(nums):
10+
return (nums[0] + nums[1] + nums[2])
11+
#return sum(nums)

0 commit comments

Comments
 (0)