|
| 1 | +# Medium python list problems -- 1 loop.. Use a[0], a[1], ... to access elements in a list, len(a) is the length. |
| 2 | + |
| 3 | + |
| 4 | +# ---------------------------------------------------------------------------------------------------------------------- |
| 5 | +# |
| 6 | +# Return the number of even ints in the given array. Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1. |
| 7 | +# |
| 8 | +# |
| 9 | +# count_evens([2, 1, 2, 3, 4]) → 3 |
| 10 | +# count_evens([2, 2, 0]) → 3 |
| 11 | +# count_evens([1, 3, 5]) → 0 |
| 12 | + |
| 13 | +def count_evens(nums): |
| 14 | + count = 0 |
| 15 | + for i in nums: |
| 16 | + if i % 2 == 0: |
| 17 | + count = count + 1 |
| 18 | + |
| 19 | + return count |
| 20 | + |
| 21 | + |
| 22 | +# ---------------------------------------------------------------------------------------------------------------------- |
| 23 | +# |
| 24 | +# Given an array length 1 or more of ints, return the difference between the largest and smallest values in the array. |
| 25 | +# Note: the built-in min(v1, v2) and max(v1, v2) functions return the smaller or larger of two values. |
| 26 | +# |
| 27 | +# |
| 28 | +# big_diff([10, 3, 5, 6]) → 7 |
| 29 | +# big_diff([7, 2, 10, 9]) → 8 |
| 30 | +# big_diff([2, 10, 7, 2]) → 8 |
| 31 | + |
| 32 | +def big_diff(nums): |
| 33 | + return max(nums) - min(nums) |
| 34 | + |
| 35 | + |
| 36 | +# ---------------------------------------------------------------------------------------------------------------------- |
| 37 | +# |
| 38 | +# Return the "centered" average of an array of ints, which we'll say is the mean average of the values, |
| 39 | +# except ignoring the largest and smallest values in the array. If there are multiple copies of the smallest value, |
| 40 | +# ignore just one copy, and likewise for the largest value. Use int division to produce the final average. |
| 41 | +# You may assume that the array is length 3 or more. |
| 42 | + |
| 43 | +# |
| 44 | +# centered_average([1, 2, 3, 4, 100]) → 3 |
| 45 | +# centered_average([1, 1, 5, 5, 10, 8, 7]) → 5 |
| 46 | +# centered_average([-10, -4, -2, -4, -2, 0]) → -3 |
| 47 | + |
| 48 | +def centered_average(nums): |
| 49 | + nums.sort() |
| 50 | + return sum(nums[1:-1]) / (len(nums) - 2) |
| 51 | + |
| 52 | + |
| 53 | +# ---------------------------------------------------------------------------------------------------------------------- |
| 54 | +# |
| 55 | +# Return the sum of the numbers in the array, returning 0 for an empty array. |
| 56 | +# Except the number 13 is very unlucky, |
| 57 | +# so it does not count and numbers that come immediately after a 13 also do not count. |
| 58 | +# |
| 59 | +# |
| 60 | +# sum13([1, 2, 2, 1]) → 6 |
| 61 | +# sum13([1, 1]) → 2 |
| 62 | +# sum13([1, 2, 2, 1, 13]) → 6 |
| 63 | + |
| 64 | +def sum13(nums): |
| 65 | + while 13 in nums: |
| 66 | + if nums.index(13) < len(nums) - 1: |
| 67 | + nums.pop(nums.index(13) + 1) |
| 68 | + nums.pop(nums.index(13)) |
| 69 | + |
| 70 | + return sum(nums) |
| 71 | + |
| 72 | + |
| 73 | +# ---------------------------------------------------------------------------------------------------------------------- |
| 74 | +# |
| 75 | +# Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to |
| 76 | +# the next 7 (every 6 will be followed by at least one 7). Return 0 for no numbers. |
| 77 | +# |
| 78 | +# |
| 79 | +# sum67([1, 2, 2]) → 5 |
| 80 | +# sum67([1, 2, 2, 6, 99, 99, 7]) → 5 |
| 81 | +# sum67([1, 1, 6, 7, 2]) → 4 |
| 82 | + |
| 83 | +def sum67(nums): |
| 84 | + count = 0 |
| 85 | + blocked = False |
| 86 | + |
| 87 | + for n in nums: |
| 88 | + if n == 6: |
| 89 | + blocked = True |
| 90 | + continue |
| 91 | + if n == 7 and blocked: |
| 92 | + blocked = False |
| 93 | + continue |
| 94 | + if not blocked: |
| 95 | + count += n |
| 96 | + |
| 97 | + return count |
| 98 | + |
| 99 | + |
| 100 | +# ---------------------------------------------------------------------------------------------------------------------- |
| 101 | +# |
| 102 | +# Given an array of ints, return True if the array contains a 2 next to a 2 somewhere. |
| 103 | +# |
| 104 | +# |
| 105 | +# has22([1, 2, 2]) → True |
| 106 | +# has22([1, 2, 1, 2]) → False |
| 107 | +# has22([2, 1, 2]) → False |
| 108 | + |
| 109 | +def has22(nums): |
| 110 | + for i, v in enumerate(nums[:-1]): |
| 111 | + if v == 2 and nums[i + 1] == 2: |
| 112 | + return True |
| 113 | + return False |
| 114 | + |
| 115 | +# ---------------------------------------------------------------------------------------------------------------------- |
0 commit comments