Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions list-1/common_end.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def common_end(a, b):
"""
Given 2 arrays of ints, a and b,
return True if they have the same first element or they have the same last element.
Both arrays will be length 1 or more.
"""
return a[0] == b[0] or a[-1] == b[-1]

print(common_end([1, 2, 3], [7, 3]))
print(common_end([1, 2, 3], [7, 3, 2]))
print(common_end([1, 2, 3], [1, 3]))
8 changes: 7 additions & 1 deletion list-1/first_last6.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
def first_last6(nums):
"""Given an array of ints, return True if 6 appears as either the first or last element in the array. The array will be length 1 or more."""
"""
Given an array of ints, return True if 6 appears as either the first or last element in the array.
The array will be length 1 or more.
"""
return nums[0] == 6 or nums[-1] == 6


print(first_last6([1, 2, 6]))
print(first_last6([6, 1, 2, 3]))
print(first_last6([13, 6, 1, 2, 3]))

# Alternative soltion
# return (nums[0] == 6 or nums[-1] == 6)
10 changes: 10 additions & 0 deletions list-1/has23.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def has23(nums):
"""
Given an int array length 2, return True if it contains a 2 or a 3.
"""
return 2 in nums or 3 in nums


print(has23([2, 5]))
print(has23([4, 3]))
print(has23([4, 5]))
10 changes: 10 additions & 0 deletions list-1/make_ends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def make_ends(nums):
"""
Given an array of ints, return a new array length 2 containing the first and last elements
from the original array. The original array will be length 1 or more.
"""
return [nums[0], nums[-1]]

print(make_ends([1, 2, 3]))
print(make_ends([1, 2, 3, 4]))
print(make_ends([7, 4, 6, 2]))
8 changes: 8 additions & 0 deletions list-1/make_pi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def make_pi():
"""
Return an int array length 3 containing the first 3 digits of pi, {3, 1, 4}.
"""
a = 3, 1, 4
return list(a)

print(make_pi())
20 changes: 20 additions & 0 deletions list-1/max_end3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def max_end3(nums):
"""
Given an array of ints length 3, figure out which is larger, the first or last element in the array,
and set all the other elements to be that value. Return the changed array.
"""
if nums[0] > nums[2]:
return [nums[0]] * 3
return [nums[2]] * 3


print(max_end3([1, 2, 3]))
print(max_end3([11, 5, 9]))
print(max_end3([2, 11, 3]))

# Alternative solution
# big = max(nums[0], nums[2])
# nums[0] = big
# nums[1] = big
# nums[2] = big
# return nums
9 changes: 9 additions & 0 deletions list-1/middle_way.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def middle_way(a, b):
"""
Given 2 int arrays, a and b, each length 3, return a new array length 2 containing their middle elements.
"""
return [a[1], b[1]]

print(middle_way([1, 2, 3], [4, 5, 6]))
print(middle_way([7, 7, 7], [3, 8, 0]))
print(middle_way([5, 2, 9], [1, 4, 5]))
10 changes: 10 additions & 0 deletions list-1/reverse3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def reverse3(nums):
"""
Given an array of ints length 3, return a new array with the elements in reverse order, so {1, 2, 3} becomes {3, 2, 1}.
"""
return [nums[2]] + [nums[1]] + [nums[0]]


print(reverse3([1, 2, 3]))
print(reverse3([5, 11, 9]))
print(reverse3([7, 0, 0]))
10 changes: 10 additions & 0 deletions list-1/rotate_left3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def rotate_left3(nums):
"""
Given an array of ints length 3,
return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}.
"""
return nums[1:] + [nums[0]]

print(rotate_left3([1, 2, 3]))
print(rotate_left3([5, 11, 9]))
print(rotate_left3([7, 0, 0]))
13 changes: 13 additions & 0 deletions list-1/same_first_last.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def same_first_last(nums):
"""
Given an array of ints, return True if the array is length 1 or more, and the first element and the last element are equal.
"""
return len(nums) >= 1 and nums[0] == nums[-1]


print(same_first_last([1, 2, 3]))
print(same_first_last([1, 2, 3, 1]))
print(same_first_last([1, 2, 1]))

# Alternative solution
# return (len(nums) >= 1 and nums[0] == nums[-1])
14 changes: 14 additions & 0 deletions list-1/sum2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def sum2(nums):
"""
Given an array of ints, return the sum of the first 2 elements in the array.
If the array length is less than 2, just sum up the elements that exist,
returning 0 if the array is length 0.
"""
if len(nums) < 2:
return sum(nums)
return sum(nums[:2])


print(sum2([1, 2, 3]))
print(sum2([1, 1]))
print(sum2([1, 1, 1, 1]))
9 changes: 9 additions & 0 deletions list-1/sum3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def sum3(nums):
"""
Given an array of ints length 3, return the sum of all the elements.
"""
return sum(nums)

print(sum3([1, 2, 3]))
print(sum3([5, 11, 2]))
print(sum3([7, 0, 0]))
21 changes: 21 additions & 0 deletions list-2/big_diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def big_diff(nums):
"""
Given an array length 1 or more of ints,
return the difference between the largest and smallest values in the array.
Note: the built-in min(v1, v2) and max(v1, v2) functions return the smaller or larger of two values.
"""
maximum, minimum = max(nums), min(nums)
return maximum - minimum

print(big_diff([10, 3, 5, 6]))
print(big_diff([7, 2, 10, 9]))
print(big_diff([2, 10, 7, 2]))

# With Loop
# maximum, minimum = 0, nums[0]
# for i in range(len(nums)-1):
# if max(nums[i:i+2]) > maximum:
# maximum = max(nums[i:i+2])
# if min(nums[i:i+2]) < minimum:
# minimum = min(nums[i:i+2])
# return maximum - minimum
20 changes: 20 additions & 0 deletions list-2/centered_average.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def centered_average(nums):
"""
Return the "centered" average of an array of ints,
which we'll say is the mean average of the values,
except ignoring the largest and smallest values in the array.
If there are multiple copies of the smallest value, ignore just one copy,
and likewise for the largest value. Use int division to produce the final average.
You may assume that the array is length 3 or more.
"""
nums.sort()
if nums.count(nums[0]) > 0:
nums.remove(nums[0])
if nums.count(nums[-1]) > 0:
nums.remove(nums[-1])
return sum(nums) // len(nums)


print(centered_average([1, 2, 3, 4, 100]))
print(centered_average([1, 1, 5, 5, 10, 8, 7]))
print(centered_average([-10, -4, -2, -4, -2, 0]))
5 changes: 2 additions & 3 deletions list-2/count_evens.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
def count_evens(nums):
"""Return the number of even ints in the given array.

Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1.
"""
Return the number of even ints in the given array.
"""
evens = 0
for number in nums:
Expand Down
12 changes: 12 additions & 0 deletions list-2/has22.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def has22(nums):
"""
Given an array of ints, return True if the array contains a 2 next to a 2 somewhere.
"""
for i in range(len(nums)-1):
if nums[i:i+2] == [2, 2]:
return True
return False

print(has22([1, 2, 2]))
print(has22([1, 2, 1, 2]))
print(has22([2, 1, 2]))
20 changes: 20 additions & 0 deletions list-2/sum13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def sum13(nums):
"""
Return the sum of the numbers in the array, returning 0 for an empty array.
Except the number 13 is very unlucky, so it does not count and numbers that come immediately after a 13 also do not count.
"""
result = 0
for i in range(len(nums)):
if i == len(nums) - 1:
if nums[i] != 13:
result += nums[i]
elif nums[i] == 13:
if nums[i+1] != 13:
result -= nums[i+1]
else:
result += nums[i]
return result

print(sum13([1, 2, 2, 1]))
print(sum13([1, 1]))
print(sum13([1, 2, 2, 1, 13]))
21 changes: 21 additions & 0 deletions list-2/sum67.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def sum67(nums):
"""
Return the sum of the numbers in the array,
except ignore sections of numbers starting with a 6 and extending to the next 7
(every 6 will be followed by at least one 7). Return 0 for no numbers.
"""
result = 0
sum_off = False
for number in nums:
if number == 6:
sum_off = True
elif number == 7 and sum_off:
sum_off = False
elif not sum_off:
result += number
return result


print(sum67([1, 2, 2]))
print(sum67([1, 2, 2, 6, 99, 99, 7]))
print(sum67([1, 1, 6, 7, 2]))
19 changes: 19 additions & 0 deletions logic-1/alarm_clock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def alarm_clock(day, vacation):
"""
Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat,
and a boolean indicating if we are on vacation,
return a string of the form "7:00" indicating when the alarm clock should ring.
Weekdays, the alarm should be "7:00" and on the weekend it should be "10:00".
Unless we are on vacation -- then on weekdays it should be "10:00" and weekends it should be "off".
"""
if vacation:
if day == 0 or day == 6:
return "off"
return "10:00"
elif day == 0 or day == 6:
return "10:00"
return "7:00"

print(alarm_clock(1, False))
print(alarm_clock(5, False))
print(alarm_clock(0, False))
23 changes: 23 additions & 0 deletions logic-1/caught_speeding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def caught_speeding(speed, is_birthday):
"""
You are driving a little too fast, and a police officer stops you.
Write code to compute the result, encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket.
If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1.
If speed is 81 or more, the result is 2.
Unless it is your birthday -- on that day, your speed can be 5 higher in all cases.
"""
if is_birthday:
if speed <= 65:
return 0
elif 66 <= speed <= 85:
return 1
return 2
elif speed <= 60:
return 0
elif 61 <= speed <= 80:
return 1
return 2

print(caught_speeding(60, False))
print(caught_speeding(65, False))
print(caught_speeding(65, True))
16 changes: 13 additions & 3 deletions logic-1/cigar_party.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
def cigar_party(cigars, is_weekend):
"""When squirrels get together for a party, they like to have cigars. A squirrel party is successful when the number of cigars is between 40 and 60, inclusive. Unless it is the weekend, in which case there is no upper bound on the number of cigars. Return True if the party with the given values is successful, or False otherwise."""
return is_weekend or (not is_weekend and 40 <= cigars <= 60)
"""
When squirrels get together for a party, they like to have cigars.
A squirrel party is successful when the number of cigars is between 40 and 60, inclusive.
Unless it is the weekend, in which case there is no upper bound on the number of cigars.
Return True if the party with the given values is successful, or False otherwise.
"""
return is_weekend and cigars >= 40 or (not is_weekend and 40 <= cigars <= 60)


print(cigar_party(30, False))
print(cigar_party(40, False))
print(cigar_party(50, False))
print(cigar_party(70, True))

# Alternative solution
# if is_weekend:
# return (cigars >= 40)
# else:
# return (cigars >= 40 and cigars <= 60)
25 changes: 25 additions & 0 deletions logic-1/date_fashion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def date_fashion(you, date):
"""
You and your date are trying to get a table at a restaurant.
The parameter "you" is the stylishness of your clothes, in the range 0..10, and "date" is the stylishness of your date's clothes.
The result getting the table is encoded as an int value with 0=no, 1=maybe, 2=yes.
If either of you is very stylish, 8 or more, then the result is 2 (yes).
With the exception that if either of you has style of 2 or less, then the result is 0 (no). Otherwise the result is 1 (maybe).
"""
if you <= 2 or date <= 2:
return 0
elif you >= 8 or date >= 8:
return 2
return 1

print(date_fashion(5, 10))
print(date_fashion(5, 2))
print(date_fashion(5, 5))

# Alternative solution
# if (you <= 2) or (date <= 2):
# return 0
# elif (you >= 8) or (date >= 8):
# return 2
# else:
# return 1
13 changes: 13 additions & 0 deletions logic-1/in1to10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def in1to10(n, outside_mode):
"""
Given a number n, return True if n is in the range 1..10, inclusive.
Unless outside_mode is True, in which case return True if
the number is less or equal to 1, or greater or equal to 10.
"""
if outside_mode:
return n <= 1 or n >= 10
return 1 <= n <= 10

print(in1to10(5, False))
print(in1to10(11, False))
print(in1to10(11, True))
12 changes: 12 additions & 0 deletions logic-1/love6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def love6(a, b):
"""
The number 6 is a truly great number.
Given two int values, a and b, return True if either one is 6. Or if their sum or difference is 6.
Note: the function abs(num) computes the absolute value of a number.
"""
return a == 6 or b == 6 or a + b == 6 or abs(a - b) == 6


print(love6(6, 4))
print(love6(4, 5))
print(love6(1, 5))
Loading