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
| /** | |
| * returns a transformed list (of strings) of the | |
| * 1-to-100 integer list passed in such that: | |
| * integers evenly divisible by 3 are replaced by "fizz" | |
| * integers evenly divisible by 5 are replaced by "buzz" | |
| * integers evenly divisible by both 3 & 5 are replaced by "fizzbuz" | |
| * all other integers are replaced by their string representation | |
| * */ | |
| def f[Int](q:List[Int]=(1 to 100).toList):List[String] = { | |
| fb.foldLeft(List[String]())( (u, v) => |
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
| require(zoo) | |
| require(xts) | |
| #---------------------- mock an irregular (non-uniform interval) time series -----------------------# | |
| start <- Sys.time() - (20 * 60 * 60) | |
| end <- Sys.time() | |
| # create the time series index | |
| idx_ts = seq.POSIXt(start, end, by='hours') |
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 xtab(*cols, apply_wt=False): | |
| ''' | |
| returns: | |
| (i) xt, NumPy array storing the xtab results, number of dimensions is equal to | |
| the len(args) passed in | |
| (ii) unique_vals_all_cols, a tuple of 1D NumPy array for each dimension | |
| in xt (for a 2D xtab, the tuple comprises the row and column headers) | |
| pass in: | |
| (i) 1 or more 1D NumPy arrays of integers | |
| (ii) if wts is True, then the last array in cols is an array of weights |
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
| /** | |
| * Solves the n-Queen puzzle in O(n!) | |
| * Let p[r] be the column of the queen on the rth row (must be exactly 1 queen per row) | |
| * There also must be exactly 1 queen per column and hence p must be a permuation of (0 until i) | |
| * There must be n distinct (col + diag) and n distinct (col - diag) for each queen (else bishop attacks) | |
| * @return a Vector p of length n such that p[i] is the column of the queen on the ith row | |
| */ | |
| def nQueens(n: Int) = (0 until n).permutations filter {p => | |
| p.zipWithIndex.flatMap{case (c, d) => Seq(n + c + d, c - d)}.distinct.size == 2*n | |
| } |
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
| # at the R REPL prompt | |
| devtools::install_github('IRkernel/repr') | |
| devtools::install_github("IRkernel/IRdisplay") | |
| devtools::install_github("IRkernel/IRKernel") | |
| require(IRdisplay) |
OlderNewer