Created
December 2, 2018 03:44
-
-
Save korkridake/3c36295963e392aeaef40bbe27219143 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def function_template(nums): | |
| ''' | |
| Signature: list ---> int | |
| Author: @Korkrid Akepanidtaworn | |
| Description: INSERT HERE | |
| ''' | |
| pass | |
| def has22(nums): | |
| ''' | |
| Signature: list ---> bool | |
| Author: @Korkrid Akepanidtaworn | |
| Description: Given an array of ints, return True if the array contains a 2 next to a 2 somewhere. | |
| CodingBat: https://codingbat.com/prob/p119308 | |
| has22([1, 2, 2]) → True | |
| has22([1, 2, 1, 2]) → False | |
| has22([2, 1, 2]) → False | |
| ''' | |
| import numpy as np | |
| # This is useful when you want to access the list prior to the final one | |
| for w in np.arange(len(nums) - 1): | |
| if (nums[w] == 2) & (nums[w+1] == 2): | |
| return True | |
| return False | |
| print(has22([1, 2, 2])) # → True | |
| print(has22([1, 2, 1, 2])) # → False | |
| print(has22([2, 1, 2])) # → False | |
| def sum13(nums): | |
| ''' | |
| Signature: list ---> int | |
| Author: @Korkrid Akepanidtaworn | |
| Description: 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. | |
| CodingBat: https://codingbat.com/prob/p167025 | |
| sum13([1, 2, 2, 1]) → 6 | |
| sum13([1, 1]) → 2 | |
| sum13([1, 2, 2, 1, 13]) → 6 | |
| ''' | |
| import numpy as np | |
| total_sum = 0 | |
| # Handle an empty array case | |
| if len(nums) == 0: | |
| return total_sum | |
| for v in np.arange(len(nums)): | |
| if nums[v] != 13: | |
| total_sum += nums[v] | |
| else: | |
| break # Very Unlucky Here, Just Break! | |
| return total_sum | |
| print(sum13([1, 2, 2, 1])) # → 6 | |
| print(sum13([1, 1])) # → 2 | |
| print(sum13([1, 2, 2, 1, 13])) # → 6 | |
| print(sum13([])) # 0 | |
| print(sum13([1,6,7,8,13,14,1,1,1])) # 22 | |
| def big_diff(nums): | |
| ''' | |
| Signature: list ---> int | |
| Author: @Korkrid Akepanidtaworn | |
| Description: 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. | |
| Coding Bat: https://codingbat.com/prob/p184853 | |
| big_diff([10, 3, 5, 6]) → 7 | |
| big_diff([7, 2, 10, 9]) → 8 | |
| big_diff([2, 10, 7, 2]) → 8 | |
| ''' | |
| import numpy as np | |
| return np.max(nums) - np.min(nums) | |
| print(big_diff([10, 3, 5, 6])) # → 7 | |
| print(big_diff([7, 2, 10, 9])) # → 8 | |
| print(big_diff([2, 10, 7, 2])) # → 8 | |
| def centered_average(nums): | |
| ''' | |
| Signature: list ---> int | |
| Author: @Korkrid Akepanidtaworn | |
| Description: 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. | |
| CodingBat: https://codingbat.com/prob/p126968 | |
| centered_average([1, 2, 3, 4, 100]) → 3 | |
| centered_average([1, 1, 5, 5, 10, 8, 7]) → 5 | |
| centered_average([-10, -4, -2, -4, -2, 0]) → -3 | |
| ''' | |
| import numpy as np | |
| nums_max = np.max(nums) | |
| nums_min = np.min(nums) | |
| # Ignore the Largest and Smallest Values in the Array | |
| nums.remove(nums_max) # Remove only one although there're two maximum values... | |
| nums.remove(nums_min) # Remove only one although there're two minimum values... | |
| return int(np.mean(nums)) | |
| print(centered_average([1, 2, 3, 4, 100])) # → 3 | |
| print(centered_average([1, 1, 5, 5, 10, 8, 7])) # → 5 | |
| print(centered_average([-10, -4, -2, -4, -2, 0])) # → -3 | |
| def count_evens(nums): | |
| ''' | |
| Signature: list ---> int | |
| Author: @Korkrid Akepanidtaworn | |
| Description: Return the number of even ints in the given array. | |
| Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1. | |
| CodingBat: https://codingbat.com/prob/p189616 | |
| count_evens([2, 1, 2, 3, 4]) → 3 | |
| count_evens([2, 2, 0]) → 3 | |
| count_evens([1, 3, 5]) → 0 | |
| ''' | |
| nums_even = 0 | |
| for i in range(len(nums)): | |
| if (nums[i] % 2) == 0: | |
| nums_even += 1 | |
| else: | |
| continue | |
| return nums_even | |
| print(count_evens([2, 1, 2, 3, 4])) # → 3 | |
| print(count_evens([2, 2, 0])) # → 3 | |
| print(count_evens([1, 3, 5])) # → 0 | |
| def sum67(nums): | |
| ''' | |
| Signature: list ---> int | |
| Author: @Korkrid Akepanidtaworn | |
| Description: 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. | |
| CodingBat: https://github.com/snowpolar/codingbat-solutions/blob/master/Python/List-2/sum67.py | |
| sum67([1, 2, 2]) → 5 | |
| sum67([1, 2, 2, 6, 99, 99, 7]) → 5 | |
| sum67([1, 1, 6, 7, 2]) → 4 | |
| ''' | |
| record = True # To capture the positioning | |
| total_sum = 0 # To capture the total sum | |
| for n in nums: | |
| # If the function encounters 6 | |
| if n == 6: | |
| record = False | |
| # If the record is True | |
| if record: | |
| total_sum += n | |
| continue | |
| # If the function encounters 7 | |
| if n == 7: | |
| record = True | |
| return total_sum | |
| print(sum67([1, 2, 2])) # → 5 | |
| print(sum67([1, 2, 2, 6, 99, 99, 7])) # → 5 | |
| print(sum67([1, 1, 6, 7, 2])) # → 4 | |
| print(sum67([])) # → 0 | |
| print(sum67([1,2,6,5,7,6,7])) # → 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment