Created
May 7, 2017 02:51
-
-
Save jeremy886/70f456cd953a80482a69970f4dfe4bfa to your computer and use it in GitHub Desktop.
This file contains 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 num_buses(n): | |
""" (int) -> int | |
Precondition: n >= 0 | |
Return the minimum number of buses required to transport n people. | |
Each bus can hold 50 people. | |
>>> num_buses(75) | |
2 | |
""" | |
import math | |
return math.ceil(n/50) | |
#return n//50 + 1 | |
def stock_price_summary(price_changes): | |
""" (list of number) -> (number, number) tuple | |
price_changes contains a list of stock price changes. Return a 2-item | |
tuple where the first item is the sum of the gains in price_changes and | |
the second is the sum of the losses in price_changes. | |
>>> stock_price_summary([0.01, 0.03, -0.02, -0.14, 0, 0, 0.10, -0.01]) | |
(0.14, -0.17) | |
""" | |
gains = 0 | |
losses = 0 | |
for change in price_changes: | |
if change > 0: | |
gains += change | |
else: | |
losses += change | |
return (gains, losses) | |
def swap_k(L, k): | |
""" (list, int) -> NoneType | |
Precondtion: 0 <= k <= len(L) // 2 | |
Swap the first k items of L with the last k items of L. | |
>>> nums = [1, 2, 3, 4, 5, 6] | |
>>> swap_k(nums, 2) | |
>>> nums | |
[5, 6, 3, 4, 1, 2] | |
""" | |
if k > len(L)/2 or k <= 0: | |
return | |
else: | |
L[:] = L[len(L)-k:len(L)] + L[k:len(L)-k] + L[0:k] | |
if __name__ == '__main__': | |
import doctest | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment