Skip to content

Commit b395976

Browse files
committed
List-2 Solutions
1 parent 3bd573a commit b395976

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

List-2/centered_average.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Return the "centered" average of an array of ints, which we'll say is the mean average of the values,
2+
# except ignoring the largest and smallest values in the array. If there are multiple copies of the smallest value,
3+
# ignore just one copy, and likewise for the largest value. Use int division to produce the final average. You may
4+
# assume that the array is length 3 or more.
5+
#
6+
#
7+
# centered_average([1, 2, 3, 4, 100]) → 3
8+
# centered_average([1, 1, 5, 5, 10, 8, 7]) → 5
9+
# centered_average([-10, -4, -2, -4, -2, 0]) → -3
10+
11+
def centered_average(nums):
12+
return (sum(nums) - min(nums) - max(nums)) / (len(nums) - 2)
13+
14+
15+
print(centered_average([1, 1, 5, 5, 10, 8, 7]))

List-2/has22.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, return True if the array contains a 2 next to a 2 somewhere.
2+
#
3+
#
4+
# has22([1, 2, 2]) → True
5+
# has22([1, 2, 1, 2]) → False
6+
# has22([2, 1, 2]) → False
7+
8+
9+
def has22(nums):
10+
i = 0
11+
while i < len(nums)-1:
12+
if nums[i] == 2 and nums[i + 1] == 2:
13+
return True
14+
i += 1
15+
return False
16+

List-2/sum13.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Return the sum of the numbers in the array, returning 0 for an empty array. Except the number 13 is very unlucky,
2+
# so it does not count and numbers that come immediately after a 13 also do not count.
3+
#
4+
#
5+
# sum13([1, 2, 2, 1]) → 6
6+
# sum13([1, 1]) → 2
7+
# sum13([1, 2, 2, 1, 13]) → 6
8+
9+
10+
def sum13(nums):
11+
total = 0
12+
i = 0
13+
while i < len(nums):
14+
if nums[i] == 13:
15+
i += 2
16+
continue
17+
total += nums[i]
18+
i += 1
19+
return total
20+

List-2/sum67.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to
2+
# the next 7 (every 6 will be followed by at least one 7). Return 0 for no numbers.
3+
#
4+
#
5+
# sum67([1, 2, 2]) → 5
6+
# sum67([1, 2, 2, 6, 99, 99, 7]) → 5
7+
# sum67([1, 1, 6, 7, 2]) → 4
8+
9+
def sum67(nums):
10+
indicator = True
11+
total = 0
12+
for i in nums:
13+
if i == 6:
14+
indicator = False
15+
if indicator:
16+
total += i
17+
continue
18+
if i == 7:
19+
indicator = True
20+
return total

0 commit comments

Comments
 (0)