reorganize dir

Signed-off-by: Mia Steinkirch <mia.steinkirch@gmail.com>
This commit is contained in:
Mia Steinkirch 2019-10-11 04:29:17 -07:00
parent 1b6f705e7c
commit a8e71c50db
276 changed files with 23954 additions and 0 deletions

View file

@ -0,0 +1,27 @@
#!/bin/python
"""
Grab Apple's stock prices and put them in a list called stock_prices, where:
The indices are the time (in minutes) past trade opening time, which was 9:30am local time.
The values are the price (in US dollars) of one share of Apple stock at that time.
So if the stock cost $500 at 10:30am, that means stock_prices[60] = 500.
Write an efficient function that takes stock_prices and returns the best profit I could have made from one purchase and one sale of one share.
"""
def apple_stock_profit(stock_prices):
min_s, max_s = max(stock_prices), 0
while stock_prices:
stock = stock_prices.pop()
min_s = min(min_s, stock)
max_s = max(max_s, stock)
return max_s - min_s
stock_prices = [10, 7, 5, 8, 11, 9]
print apple_stock_profit(stock_prices)
print("Should return 6 (buying for $5 and selling for $11)")

View file

@ -0,0 +1,36 @@
#!/bin/python
"""
Write a function fib() that takes an integer nn and returns the nnth Fibonacci number.
"""
# this is O(2^n)
def fib(n):
if n in [1, 0]:
return n
return fib(n - 1) + fib(n - 2)
print fib(10)
# this is O(n)
def fib(n):
if n < 0:
raise ValueError('Index was negative. No such thing as a '
'negative index in a series.')
elif n in [0, 1]:
return n
prev_prev = 0 # 0th fibonacci
prev = 1 # 1st fibonacci
for _ in range(n - 1):
current = prev + prev_prev
prev_prev = prev
prev = current
return current
print fib(10)

View file

@ -0,0 +1,38 @@
#!/bin/python
"""
Find a duplicate
We have a list of integers, where:
The integers are in the range 1..n1..n
The list has a length of n+1n+1
It follows that our list has at least one integer which appears at least twice. But it may have several duplicates, and each duplicate may appear more than twice.
Write a function which finds an integer that appears more than once in our list. (If there are multiple duplicates, you only need to find one of them.)
"""
def find_dups(num_list):
num_dict = {}
for n in num_list:
if n in num_dict.keys():
num_dict[n] += 1
else:
num_dict[n] = 1
for k,v in num_dict.items():
if v > 1:
print "dup is {}".format(k)
def find_dups_set(num_list):
num_set = set()
for n in num_list:
if n in num_set:
print n
else:
num_set.add(n)
num_list = [6,1,3,7,6,4,5,2,8,5,6,6,7]
find_dups(num_list)
find_dups_set(num_list)

View file

@ -0,0 +1,26 @@
#!/usr/bin/python
''' Given a real number between 0 and 1 (eg: 0.72), this method print the binary
representation. If the Number cannot be represented accurately in binary, with at
most 32 chars, print error:
'''
def get_float_rep(num):
if num >= 1 or num <= 0: return 'Error 1'
result = '.'
while num:
if len(result) >= 32: return 'Error 2', result
r = num*2
if r >= 1:
result += '1'
num = r - 1
else:
result += '0'
num = r
return result
if __name__ == '__main__':
print get_float_rep(0.72) #('Error 2', '.1011100001010001111010111000010')
print get_float_rep(0.1) # ('Error 2', '.0001100110011001100110011001100')
print get_float_rep(0.5) #'.1'

View file

@ -0,0 +1,54 @@
#!/bin/python
"""
Given a list of integers, find the highest product you can get from three of the integers.
The input list_of_ints will always have at least three integers.
"""
def highest_num(list_of_ints):
if len(list_of_ints) == 3:
return list_of_ints[0]*list_of_ints[1]*list_of_ints[2]
sorted_list = sorted(list_of_ints)
return sorted_list[-3]*sorted_list[-2]*sorted_list[-1]
def highest_product_of_3_On(list_of_ints):
highest = max(list_of_ints[0], list_of_ints[1])
lowest = min(list_of_ints[0], list_of_ints[1])
highest_product_of_2 = list_of_ints[0] * list_of_ints[1]
lowest_product_of_2 = list_of_ints[0] * list_of_ints[1]
highest_product_of_3 = list_of_ints[0] * list_of_ints[1] * list_of_ints[2]
for i in range(2, len(list_of_ints)):
current = list_of_ints[i]
highest_product_of_3 = max(highest_product_of_3,
current * highest_product_of_2,
current * lowest_product_of_2)
highest_product_of_2 = max(highest_product_of_2,
current * highest,
current * lowest)
lowest_product_of_2 = min(lowest_product_of_2,
current * highest,
current * lowest)
highest = max(highest, current)
lowest = min(lowest, current)
return highest_product_of_3
list_of_ints = [4, 2, 5, 6]
print highest_num(list_of_ints)
print "Should be 120"
print highest_product_of_3_On(list_of_ints)
print "Should be 120"

View file

@ -0,0 +1,33 @@
#!/bin/python
"""
Write a function for doing an in-place shuffle of a list.
The shuffle must be "uniform," meaning each item in the original list must have the same probability of ending up in each spot in the final list.
Assume that you have a function get_random(floor, ceiling) for getting a random integer that is >= floor and <= ceiling.
"""
import random
def get_random(floor, ceiling):
return random.randrange(floor, ceiling + 1)
def shuffle(the_list):
if len(the_list) <= 1:
return the_list
last_index_in_the_list = len(the_list) - 1
for i in range(len(the_list) - 1):
random_choice_index = get_random(i,
last_index_in_the_list)
if random_choice_index != i:
the_list[i], the_list[random_choice_index] = \
the_list[random_choice_index], the_list[i]
seed_list = [5, 2, 6, 2, 6]
shuffle(seed_list)
print seed_list

View file

@ -0,0 +1,38 @@
#!/bin/python
"""
You have a list of integers, and for each index you want to find the product of every integer except the integer at that index.
Write a function get_products_of_all_ints_except_at_index() that takes a list of integers and returns a list of the products.
For example, given:
[1, 7, 3, 4]
your function would return:
[84, 12, 28, 21]
by calculating:
[7 * 3 * 4, 1 * 3 * 4, 1 * 7 * 4, 1 * 7 * 3]
Here's the catch: You can't use division in your solution!
"""
def get_products_of_all_ints_except_at_index(array):
prod_array = []
for i, num in enumerate(array):
prod = 1
for other_num in array[:i] + array[i+1:]:
prod *= other_num
prod_array.append(prod)
return prod_array
array = [1, 7, 3, 4]
print get_products_of_all_ints_except_at_index(array)
print "Should be [84, 12, 28, 21]"

View file

@ -0,0 +1,32 @@
#!/bin/python
"""
Write a recursive function for generating all permutations of an input string. Return them as a set.
"""
def get_permutations(string):
if len(string) < 2:
return set([string])
all_chars_except_last = string[:-1]
last_char = string[-1]
permutations_of_all_chars_except_last = get_permutations(all_chars_except_last)
permutations = set()
for permutation_of_all_chars_except_last in permutations_of_all_chars_except_last:
for position in range(len(all_chars_except_last) + 1):
permutation = (
permutation_of_all_chars_except_last[:position]
+ last_char
+ permutation_of_all_chars_except_last[position:]
)
permutations.add(permutation)
return permutations
str = "abcd"
print get_permutations(str)