Skip to content

Commit 17f8373

Browse files
committed
List-2 directory added
1 parent bb184ca commit 17f8373

File tree

6 files changed

+94
-0
lines changed

6 files changed

+94
-0
lines changed

List-2/big_diff.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Given an array length 1 or more of ints,
2+
# return the difference between the largest and smallest values in the array.
3+
# Note: the built-in min(v1, v2) and max(v1, v2) functions return the smaller or larger of two values.
4+
5+
6+
# big_diff([10, 3, 5, 6]) → 7
7+
# big_diff([7, 2, 10, 9]) → 8
8+
# big_diff([2, 10, 7, 2]) → 8
9+
10+
def big_diff(nums):
11+
return max(nums) - min(nums)

List-2/centered_average.py

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

List-2/count_evens.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Return the number of even ints in the given array.
2+
# Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1.
3+
4+
5+
# count_evens([2, 1, 2, 3, 4]) → 3
6+
# count_evens([2, 2, 0]) → 3
7+
# count_evens([1, 3, 5]) → 0
8+
9+
def count_evens(nums):
10+
sum = 0
11+
for i in nums:
12+
if i % 2 == 0:
13+
sum += 1
14+
return sum

List-2/has22.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Given an array of ints,
2+
# return True if the array contains a 2 next to a 2 somewhere.
3+
4+
5+
# has22([1, 2, 2]) → True
6+
# has22([1, 2, 1, 2]) → False
7+
# has22([2, 1, 2]) → False
8+
9+
def has22(nums):
10+
for i in range(len(nums)-1):
11+
if nums[i] == 2 and nums[i+1] == 2:
12+
return True
13+
return False

List-2/sum13.py

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

List-2/sum67.py

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

0 commit comments

Comments
 (0)