Skip to content

Instantly share code, notes, and snippets.

@timmyshen
timmyshen / PartyBill
Last active December 23, 2015 20:39
A group of friends went to a party and purchased some food and drinks for that. Now they are trying to evenly split the total bill in an efficient way.
import random
numOfPeople = 8
amountScale = 100
init_amounts = [random.randrange(amountScale) for i in range(numOfPeople)]
# Real world example
init_amounts = [56.16, 0, 26.7, 14, 80.1, 225.98, 238.86/2, 238.86/2]
# print init_amounts
sorted_amounts = sorted(init_amounts, reverse=True)
@timmyshen
timmyshen / getmonitor.R
Created October 7, 2013 17:53
Write a function named 'getmonitor' that takes three arguments: 'id', 'directory', and 'summarize'. Given a monitor ID number, 'getmonitor' reads that monitor's particulate matter data from the directory specified in the 'directory' argument and returns a data frame containing that monitor's data. If 'summarize = TRUE', then 'getmonitor' produce…
# getmonitor.R
getmonitor <- function(id, directory, summarize = FALSE) {
## 'id' is a vector of length 1 indicating the monitor ID
## number. The user can specify 'id' as either an integer, a
## character, or a numeric.
## 'directory' is a character vector of length 1 indicating
## the location of the CSV files
@timmyshen
timmyshen / complete.R
Created October 7, 2013 18:25
Write a function that reads a directory full of files and reports the number of completely observed cases in each data file. The function should return a data frame where the first column is the name of the file and the second column is the number of complete cases.
complete <- function(directory, id = 1:332) {
## 'directory' is a character vector of length 1 indicating
## the location of the CSV files
## 'id' is an integer vector indicating the monitor ID numbers
## to be used
## Return a data frame of the form:
## id nobs
## 1 117
@timmyshen
timmyshen / corr.R
Created October 7, 2013 19:08
Write a function that takes a directory of data files and a threshold for complete cases and calculates the correlation between sulfate and nitrate for monitor locations where the number of completely observed cases (on all variables) is greater than the threshold. The function should return a vector of correlations for the monitors that meet th…
corr <- function(directory, threshold = 0) {
## 'directory' is a character vector of length 1 indicating
## the location of the CSV files
## 'threshold' is a numeric vector of length 1 indicating the
## number of completely observed observations (on all
## variables) required to compute the correlation between
## nitrate and sulfate; the default is 0
## Return a numeric vector of correlations
@timmyshen
timmyshen / best.R
Created October 8, 2013 21:51
Write a function called best that take two arguments: the 2-character abbreviated name of a state and an outcome name. The function reads the outcome-of-care-measures.csv le and returns a character vector with the name of the hospital that has the best (i.e. lowest) 30-day mortality for the specied outcome in that state. The hospital name is the…
best <- function(state, outcome) {
data <- read.csv(file='outcome-of-care-measures.csv', colClasses = 'character')
if(!any(state == data$State)) {
stop('invalid state')
}
if(outcome == 'heart attack') {
i <- 11
@timmyshen
timmyshen / rankhospital.R
Last active July 4, 2017 21:59
Write a function called rankhospital that takes three arguments: the 2-character abbreviated name of a state (state), an outcome (outcome), and the ranking of a hospital in that state for that outcome (num). The function reads the outcome-of-care-measures.csv le and returns a character vector with the name of the hospital that has the ranking sp…
rankhospital <- function(state, outcome, num = "best") {
# read outcome
data <- read.csv(file="outcome-of-care-measures.csv", colClasses = 'character')
if(!any(state == data$State)) {
stop('invalid state')
}
if(outcome == 'heart attack') {
i <- 11
@timmyshen
timmyshen / rankall.R
Created October 9, 2013 18:04
Write a function called rankall that takes two arguments: an outcome name (outcome) and a hospital rank- ing (num). The function reads the outcome-of-care-measures.csv le and returns a 2-column data frame containing the hospital in each state that has the ranking specied in num. For example the function call rankall("heart attack", "best") would…
rankall <- function(outcome, num = "best") {
data <- read.csv(file="outcome-of-care-measures.csv", colClasses = 'character')
if(outcome == 'heart attack') {
i <- 11
}
else if(outcome == 'heart failure') {
i <- 17
}
else if(outcome == 'pneumonia') {
@timmyshen
timmyshen / number_before_reaching_sum.sml
Last active August 16, 2018 14:31
Write a function number_before_reaching_sum that takes an int called sum, which you can assume is positive, and an int list, which you can assume contains all positive numbers, and returns an int. You should return an int n such that the rst n elements of the list add to less than sum, but the rst n + 1 elements of the list add to sum or more. A…
fun number_before_reaching_sum (sum : int, xs : int list) =
if null xs then 0
(*else if null (tl xs) then 1*)
else if hd xs > sum then 0
else
let fun helper (i : int, partial_sum : int, tl_xs : int list) =
if partial_sum < 0 then i
else
helper(i + 1, partial_sum - hd tl_xs, tl tl_xs)
in
.DS_STORE
<!DOCTYPE html>
<meta charset="utf-8">
<style>
div.tooltip {
position: absolute;
text-align: right;
z-index:9999;
width: 6px;
height: 18px;