Skip to content

Commit ad43992

Browse files
Jem ChangJem Chang
authored andcommitted
Adding Logic 2 and List 2
1 parent 2de91f9 commit ad43992

14 files changed

Lines changed: 184 additions & 2 deletions

List-2/big_diff.py

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

List-2/centered_average.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""
2+
Return the "centered" average of an array of ints, 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+
def centered_average(nums):
9+
return (sum(nums)- max(nums) - min(nums))/(len(nums)-2)

List-2/count_evens.py

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

List-2/has22.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
Given an array of ints, return True if the array contains a 2 next to a 2 somewhere.
3+
"""
4+
def has22(nums):
5+
for i in range(len(nums)-1):
6+
if nums[i] == 2 and nums[i+1] == 2:
7+
return True
8+
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+
"""
2+
Return the sum of the numbers in the array, returning 0 for an empty array.
3+
Except the number 13 is very unlucky, so it does not count and numbers that come immediately
4+
after a 13 also do not count.
5+
"""
6+
def sum13(nums):
7+
a = [i for i,x in enumerate(nums) if x == 13]
8+
b = [x+1 for x in a]
9+
adjb = list()
10+
allpos = list()
11+
for i in b:
12+
if i < (len(nums)-1) or i == (len(nums)-1):
13+
adjb = adjb + [i]
14+
allpos = a + adjb
15+
all = 0
16+
for i in range(len(nums)):
17+
if i in allpos: continue
18+
all = all + nums[i]
19+
return all

List-2/sum67.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6
3+
and extending to the next 7 (every 6 will be followed by at least one 7). Return 0 for no numbers.
4+
"""
5+
# based on online answer
6+
# create a flag
7+
def sum67(nums):
8+
state=0
9+
s=0
10+
for n in nums:
11+
if state == 0:
12+
if n == 6:
13+
state=1
14+
else:
15+
s+=n
16+
else:
17+
if n == 7:
18+
state=0 # else: s = s
19+
return s

Logic-2/close_far.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Given three ints, a b c, return True if one of b or c is "close" (differing from a by at most 1),
3+
while the other is "far", differing from both other values by 2 or more.
4+
Note: abs(num) computes the absolute value of a number.
5+
"""
6+
def close_far(a, b, c):
7+
if abs(a-b) <= 1 and abs(a-c) >=2 and abs(b-c) >=2:
8+
return True
9+
elif abs(a-c) <= 1 and abs(a-b) >=2 and abs(b-c) >=2:
10+
return True
11+
else:
12+
return False

Logic-2/lone_sum.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Given 3 int values, a b c, return their sum. However, if one of the values is the same as another
3+
of the values, it does not count towards the sum.
4+
"""
5+
def lone_sum(a, b, c):
6+
if a == b and b !=c:
7+
return c
8+
elif b == c and b !=a:
9+
return a
10+
elif c == a and c !=b:
11+
return b
12+
elif a == b and b == c:
13+
return 0
14+
else:
15+
return a+b+c

Logic-2/lucky_sum.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Given 3 int values, a b c, return their sum. However, if one of the values is 13 then it does not count
3+
towards the sum and values to its right do not count.
4+
So for example, if b is 13, then both b and c do not count.
5+
"""
6+
def lucky_sum(a, b, c):
7+
if a == 13:
8+
return 0
9+
elif b == 13:
10+
return a
11+
elif c == 13:
12+
return a+b
13+
else:
14+
return a+b+c

Logic-2/make_bricks.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
We want to make a row of bricks that is goal inches long. We have a number of small bricks (1 inch each)
3+
and big bricks (5 inches each). Return True if it is possible to make the goal by choosing from the
4+
given bricks. This is a little harder than it looks and can be done without any loops.
5+
"""
6+
def make_bricks(small, big, goal):
7+
# 1. not enough bricks
8+
if goal > (small + big*5):
9+
return False
10+
# 2. not enough small bricks
11+
if (goal % 5) > small:
12+
return False
13+
else:
14+
return True

0 commit comments

Comments
 (0)