I was reading SICP chapter 3 and thought of this (consider it a variation of the procedure integers that creates a stream of integers): how do you create a stream of two alternating values? For example you create this:
1 0 1 0 1 0 1 0 ...
and you can change the step to 2 (or more) and make it look like
1 1 0 0 1 1 0 0 1 1 ...
1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 ...
(define (make-alternating-values n)
(define (iter i)
(cons-stream
(if (> n 0)
1
0)
(if (= i (- 1 n))
(iter n)
(iter (- i 1)))))
(iter n))
(make-alternating-values 1)
; 1 0 1 0 1 0 1 0 ...
(make-alternating-values 2)
; 1 1 0 0 1 1 0 0 1 1 ...
(make-alternating-values 3)
; 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 ...
Related
I would like to create a sequence from two numbers, such that the occurrence of one of the numbers decreases (from n_1 to 1) while for the other number the occurrences are fixed at n_2.
I've been looking around for and tried using seq and rep to do it but I can't seem to figure it out.
Here is an example for c(0,1) and n_1=5, n_2=3:
0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,1,1,1,0,1,1,1
And here for c(0,1) and n_1=2, n_2=1:
0,0,1,0,1
Maybe something like this?
rep(rep(c(0, 1), n_1), times = rbind(n_1:1, n_2))
## [1] 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0 0 1 1 1 0 0 1 1 1 0 1 1 1
Here it is as a function (without any sanity checks):
myfun <- function(vec, n1, n2) rep(rep(vec, n1), times = rbind(n1:1, n2))
myfun(c(0, 1), 2, 1)
## [1] 0 0 1 0 1
inverse.rle
Another alternative is to use inverse.rle:
y <- list(lengths = rbind(n_1:1, n_2),
values = rep(c(0, 1), n_1))
inverse.rle(y)
## [1] 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0 0 1 1 1 0 0 1 1 1 0 1 1 1
An alternative (albeit slower) method using a similar concept:
unlist(mapply(rep,c(0,1),times=rbind(n_1:1,n_2)))
###[1] 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0 0 1 1 1 0 0 1 1 1 0 1 1 1
Here is another approach using upper-triangle of a matrix:
f_rep <- function(num1, n_1, num2, n_2){
m <- matrix(rep(c(num1, num2), times=c(n_1+1, n_2)), n_1+n_2+1, n_1+n_2+1, byrow = T)
t(m)[lower.tri(m,diag=FALSE)][1:sum((n_1:1)+n_2)]
}
f_rep(0, 5, 1, 3)
#[1] 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0 0 1 1 1 0 0 1 1 1 0 1 1 1
f_rep(2, 4, 3, 3)
#[1] 2 2 2 2 3 3 3 2 2 2 3 3 3 2 2 3 3 3 2 3 3 3
myf = function(x, n){
rep(rep(x,n[1]), unlist(lapply(0:(n[1]-1), function(i) n - c(i,0))))
}
myf(c(0,1), c(5,3))
#[1] 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0 0 1 1 1 0 0 1 1 1 0 1 1 1
How can I create the following vector?
vec = (0 1 1 0 0 0 1 1 1 1)
I already tried rep(0:1,times=1:4) which works with numbers other than 0 but does not here...=
For rep, 'times' and 'x' need to have the same length (unless the length of 'times' equals 1). Therefore, you need to make a vector 'x' with length 4 in this case.
> rep(rep(0:1,2),times=1:4)
[1] 0 1 1 0 0 0 1 1 1 1
Here's a generic solution:
> increp=function(n){rep(0:(n-1), times=1:n) %% 2}
> increp(4)
[1] 0 1 1 0 0 0 1 1 1 1
> increp(3)
[1] 0 1 1 0 0 0
> increp(2)
[1] 0 1 1
> increp(6)
[1] 0 1 1 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1
It generates 0,1,1,2,2,2,3,3,3 up to the required length and then just converts to 0/1 based on even or odd.
I want to find ALL the non-negative integer solutions to the equation i+j+k+l+m=n where n is a non-negative integer. That is, I want to find all possible 5-tuples (i,j,k,l,m) with respect to a certain n, in R.
I wrote a code which is not working. I am suspicious there is something wrong in the looping.
For your convenience, I have taken n=3, so I am basically trying to compute all vectors (i,j,k,l,m) which are 35 in number, and the matrix a(35 by 5) is the matrix that is supposed to display those vectors. The whole thing is in the function "sample(n)", where if I put n=3 i.e. sample(3) when called will give me the matrix a. Please note that a (35 by 5) is defined beforehand with all entries 0.
sample=function(n){
i=0
j=0
k=0
l=0
m=0
for(p in 1:35){
while(i<=3){
while(j<=3){
while(k<=3){
while(l<=3){
m=n-(i+j+k+l)
if(m>-1){
a[p,]=c(i,j,k,l,m)
}
l=l+1}
k=k+1}
j=j+1}
i=i+1}
}
return(a)
}
When I call sample(3), I get my original a i.e. the matrix with all elements 0. What is wrong with this code? Please rectify it.
I don't think a brute-force approach will bring you much joy for this task. Instead you should look for existing functions that can be used and are efficient (i.e. implemented in C/C++).
n <- 3
library(partitions)
blockparts(rep(n, 5), n)
#[1,] 3 2 1 0 2 1 0 1 0 0 2 1 0 1 0 0 1 0 0 0 2 1 0 1 0 0 1 0 0 0 1 0 0 0 0
#[2,] 0 1 2 3 0 1 2 0 1 0 0 1 2 0 1 0 0 1 0 0 0 1 2 0 1 0 0 1 0 0 0 1 0 0 0
#[3,] 0 0 0 0 1 1 1 2 2 3 0 0 0 1 1 2 0 0 1 0 0 0 0 1 1 2 0 0 1 0 0 0 1 0 0
#[4,] 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 3 0 0 0 0 0 0 1 1 1 2 0 0 0 1 0
#[5,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 3
I believe that your code isn't answering your stated problem (as I understand it), on top of possible errors in your code.
One way to think of the problem is that, given the quadruple (i,j,k,l), the value of m = n - (i + j + k + l), while noting that the quadruple (i,j,k,l) is constrained so that n >= i+j+k+l AND i,j,k,l >= 0. For example, consider the following algorithm:
Let i freely take any value between 0 and n.
Given i, j can take values between 0 and n-i.
Given (i,j), k takes values between 0 and n-i-j.
Given (i,j,k), l takes values between 0 and n-i-j-k.
Given (i,j,k,l), m is defined as m = n - i - j - k -l.
The following code ought to answer your question. Please comment if this is not what you were looking for.
sample.example = function(n){
a=array(0,c(0,5))
for(i in 0:n){
for(j in seq(from=0,to=n-i,by=1)){
for(k in seq(from=0,to=n-i-j,by=1)){
for(l in seq(from=0,to=n-i-j-k,by=1)){
m = n - i -j - k - l
a = rbind(a,c(i,j,k,l,m))
}}}}
return(a)
}
I have a problem with my sudoku-solver function, it's not doing the backtracking process and i can't see why, just when the backtracking comes, the function stops, here is the code:
(define (solve-sudoku grid)
(define blank-space (check-for-empty-space 0 '()))
(cond [(empty? blank-space) (begin (display "FIN\n") true)])
(define x (first blank-space))
(define y (first (rest blank-space)))
(display x) (display y) (display "\n")
(cond [(eqv? #f (try-value grid 1 x y )) false])
)
(define (try-value grid num x y )
(cond [(> num 9) false]
[(is-safe grid num x y)
(begin
(assign-to-pos grid num x y)
(cond [(solve-sudoku grid) true]
[else (begin (display "reset\n")
(assign-to-pos grid 0 x y))]
))]
[else (try-value grid (+ 1 num) x y)])
)
I have a matrix for tests:
;---------------+-----+-----+-----+--
(define row0 (vector 3 0 6 5 0 8 4 0 0 ))
(define row1 (vector 5 2 0 0 0 0 0 0 0 ))
(define row2 (vector 0 8 7 0 0 0 0 3 1 ))
;---------------+-----+-----+-----+--
(define row3 (vector 0 0 3 0 1 0 0 8 0 ))
(define row4 (vector 9 0 0 8 6 3 0 0 5 ))
(define row5 (vector 0 5 0 0 9 0 6 0 0 ))
;---------------+-----+-----+-----+--
(define row6 (vector 1 3 0 0 0 0 2 5 0 ))
(define row7 (vector 0 0 0 0 0 0 0 7 4 ))
(define row8 (vector 0 0 5 2 0 6 3 0 0 ))
;---------------+-----+-----+-----+--
(define grid (vector row0 row1 row2 row3 row4 row5 row6 row7 row8))
The output is:
empty-position: 0,1
empty-position: 0,4
empty-position: 0,7
empty-position: 0,8
empty-position: 1,2
empty-position: 1,3
empty-position: 1,4
empty-position: 1,5
empty-position: 1,6
empty-position: 1,7
empty-position: 1,8
reset
result
'#(#(3 1 6 5 2 8 4 9 7)
#(5 2 4 1 3 7 8 0 0)
#(0 8 7 0 0 0 0 3 1)
#(0 0 3 0 1 0 0 8 0)
#(9 0 0 8 6 3 0 0 5)
#(0 5 0 0 9 0 6 0 0)
#(1 3 0 0 0 0 2 5 0)
#(0 0 0 0 0 0 0 7 4)
#(0 0 5 2 0 6 3 0 0))
Your test case is enormous, and I can see why it would be really hard to figure out what's going wrong. Rather than trying to debug the whole huge thing, test small pieces first. I can't see all of your program, but it sounds like you need to write many small tests for all of the functions in your program.
Can you please advise a way of binary data fusion?
Here is a a task:
There are n (n is odd) sources of binary labels (0 | 1). So, every data "frame" contains n labels. The task is to produce a single label per frame based on the fusion of all labels. For example:
S1 0 0 0 1 1 1 0 0 0 1 1 0
S2 0 0 1 1 1 1 1 0 0 1 1 1
S3 0 0 0 0 1 1 1 0 0 0 1 0
--------------------------
0 0 0 1 1 1 1 0 0 1 1 0
The "major voting" was used in this case: 0 0 0 -> 0; 1 1 0 -> 1 etc.
The major voting could be extended in horizontal direction, so that it's done over k frames for every i-th frame E.g. for k=3:
F1 round( (0+0+0+0+0+0+0+1+0) / 9) = 0
F2 round( (0+0+0+0+1+0+1+1+0) / 9) = 0
F3 round( (0+1+0+1+1+0+1+1+1) / 9) = 1 # was 0
F4 round( (1+1+0+1+1+1+1+1+1) / 9) = 1
..
Are there any other fusion schemes that come to your mind?
Thank you!
It looks to me you might be interested in
The tradeoff between reliability, consistency and availability. Here you can read about it with Amazon's Dynamo as an example.
Forward Error Correction