So I want to know how to set this up to get the correct answer in R.
If you roll two standard 6-sided die, estimate the probability that the difference between them is 3, 4, or 5 (and not 0, 1, or 2).
I know how to set up the basic model of finding a sum but am not sure on how I can find the difference. PLEASE HELP!!!
I don't know exactly what you are doing. I guess you simulate the die rolls? So you have a data.frame with two columns, each containing the rolls of one die?
Like this:
die_rolls <- data.frame(die1 = c(1, 2, 6, 3, 1, 4), die2 = c(3, 5, 6, 2, 1, 1))
Then you could create a new column that calculates the difference. And then uses the absolute value of it, i.e. you won't have negative values.
die_rolls$difference <- abs(die_rolls$die1 - die_rolls$die2)
Related
def bubbleSort(array):
swapped = False
for i in range(len(array)-1,0,-1):
print(i)
for j in range(i):
print(j)
if array[j]>array[j+1]:
array[j], array[j+1] = array[j+1], array[j]
swapped= True
if swapped:
swapped=False
else:
break
print(array)
bubbleSort([5, 2, 1, 3])
How should I interpret this line: for i in range(len(array)-1,0,-1)? I'm particularly confused about the need for the 0 and -1 parameters.
That line has a couple of things happening, which I will give simplified explanations of (I'm assuming this code is written in Python).
First, for i in iterable will loop through iterable, meaning the code in the for loop will repeat as many times as there are elements in iterable, which could be an array, a list, a string etc, and each time it loops, i will be the next element of iterable, starting with the first. For example, for i in [1, 2, 3] will loop 3 times; the first time, i will be equal to 1; the second, 2, etc.
Next, the range function produces an iterable that is a range of numbers, for example from 0-9. With a single argument, range will produce a range from 0 to that number, but stopping just before it, e.g. range(5) will give you [0, 1, 2, 3, 4]. Thus if you were to use for i in range(5), your code would repeat 5 times, with i incrementing from 0 to 4.
With two arguments, the range will start at the first and stop before the second, which must be greater than the first. For example, range(3, 8) would give you [3, 4, 5, 6, 7]. range(8, 3), however, will not work, as the start number is greater than the stop number. This means you cannot count down with only 2 arguments.
The third optional argument for range is the step size; how much you want the numbers to increase or decrease by each step. For example, range(0, 10, 2) will give you the output [0, 2, 4, 6, 8], stopping before 10. Here is where you can produce a descending range, by setting the step argument to a negative number. range(10, 0, -2) will give you [10, 8, 6, 4, 2], again stopping before the second argument, and range(10, 0, -1) will give you the full [10, 9, 8, 7, 6, 5, 4, 3, 2, 1].
Finally, the len(iterable) function will give you the length of whatever you give it, or the number of items contained in say a list. For example len("Hello!") will give you 6, and len([1, 2, 3, 4, 5]) will give you 5.
Putting this all together, the line for i in range(len(array)-1, 0, -1) will do the following:
the code will repeat as many times as there are items in a list, with i taking on each value in the list
that list is a range of numbers
that start number of the range is the length of array minus one
the end of the range is 0
the range is descending, with a step size of -1
Thus if array were ["fish", "banana", "pineapple", "onion"], len(array) will return 4, so you will have for i in range(3, 0, -1), which will loop 3 times, with i being 3, then 2, then 1.
This was a rather simplified answer, so I suggest you find some tutorials on any functions you don't understand.
For simplicity lets assume that I have a list of X,Y coordinates. X has a limited range of 0-100 mm, and Y has 0 - inf mm. Every iteration a new list of coordinates is received. All the coordinates where the delta between their Y to the last Y is bigger then a configurable value (lets say 300M) are removed from the list.
I need to find an algorithm that will help me select all the co-ordinates that are repeating in a pattern.
I have a way of solving this problem but I would prefer a more "methodical" way.
For example, Lets say I have a list with the following coordinates (X, Y):
(2, 9), (2, 18), (2, 27), (2, 54), (2, 63), ...
Y delta is configured as 9mm.
Almost all Y deltas are 9 except between the 3rd and the 4th coordinates. Those coordinates are missing but still it is a pattern and the algorithm should be able to tell that all those coordinates are from the same pattern.
Another example:
(2, 9), (2, 13), (2, 18), (2, 22), (2, 27), (2, 54), (2, 63), ...
Here I expect the same result as the first example even though there are more coordinates that are not part of the pattern.
I need to be able to know exactly which coordinates are in the detected pattern.
The deltas of the first example are
9 9 27 9
and you can observe that the most frequent is 9, which is a divisor of 27, and with some good will, you can infer that two data points are missing.
The deltas of the second are
-5 14 4 5 27 9
and it makes no sense to see a pattern there.
From what I can see, the patterns you want are in small-shift deltas and GCDs. Since the first example solves pretty quickly, let's look at the second.
9, 13, 18, 22, 27, 54, 63
Here, the shift-1 (e.g. 13-9, 54-27) and shift-2 (18-9, 54-22) deltas are
4, 5, 4, 5, 27, 9
9, 9, 9, 32, 36
and the GCDs of those adjacent deltas (e.g. (4,5), (27,9), or (9, 9) ) are
1, 1, 1, 1, 9
9, 9, 1, 1
This makes 9 a very strong suspect, being the only non-unity value in the list. A second pass to look for multiples of 9 will easily reveal the preponderance of such values. Non-conforming values get removed; multiples of 9 indicate that we need to impute missing values.
You can hard-code those searches in your favorite language. Many modern languages have one or two packages that will handle such a shift with a simple operator; in Python, both numpy and pandas will do this.
Can you take it from there?
I have a single value that, over time, increases from 1 to 6 and then returns to 1. So over several frames the sequence seen would be:
1, 2, 3, 4, 5, 6, 1, 2, 3, 4...
If a hardware device is defective one of those numbers may not read correctly and so the sequence may look like this:
1, 2, 6, 4, 5, 6, 1, 2, 6, 4...
What is a fast, short method of detecting this inconsistency?
Note:
This is designed for a hard real-time environment
My current method is to check that each new value every frame has a difference no greater than 1 from the last value, then there is a special case for the difference between 1 and 6 which is allowed to be 5
I am wondering whether there is a simple algorithm or trick to solving this problem
The sequence is also valid if it is in reverse, e.g.:
6, 5, 4, 3, 2, 1, 6, 5, 4, 3....
You can use the modulo operator:
(current_value - previous_value + 6) mod 6 == 1
The +6 is there to ensure a positive argument to mod. Different languages treat negative values differently, but if -5 mod 6 evaluates to 1 in yours, you can omit the +6.
Given a Pseq similar to the following:
Pseq([1, 2, 3, 4, 5, 6, 7, 8], inf)
How would I randomise the values slightly each time? That is, not just randomly alter the 8 values once at initialisation time, but have a random offset added each time a value is sent to the stream?
Here's a neat way:
(Pseq([1, 2, 3, 4, 5, 6, 7, 8], inf) + Pgauss(0, 0.1))
First you need to know that Pgauss is just a pattern that generates gaussian random numbers. You can use any other kind of pattern such as Pwhite.
Then you need to know the really pleasant bit: performing basic math operations on Patterns (as above) composes the patterns (by wrapping them in Pbinop).
I found this previously answered here, Best way to loop over a python string backwards
I need to use OP's original idea, but I don't quite understand the meaning of the stop argument in the range function. What does 0-1 mean? From 0-1? Wouldn't that be the step?
Why can't you use reversed as accepted in the previously answered question?
However, to answer your original question:
The 0-1 is actually just a number 0-1, which is equal to -1
The documentation for range says the following: range(start[, end[, step]]). His call looks like the following: range(len(string)-1, 0-1, -1)
start = len(string)-1, which is last letter of the string.
end = 0-1 which is equal to -1, so the last letter being handled is 0. Remember that range(0, 3) will actually give 0, 1, 2 and stop right before the 3. Same goes for negative numbers, it will stop right before -1, so it will stop at 0.
step = -1. Step stands for how many numbers to step over at once. -1 means it will lower i by one every time.
The 0-1 is -1, it can be written either way:
>>> -1
-1
>>> 0-1
-1
Let's try a string length of 10, stop of 0-1 ad step of -1:
>>> range(10, 0-1, -1)
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
you'll see the same result with -1 instead of 0-1:
>>> range(10, -1, -1)
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
The step of -1 causes range to count backward, let's try a step of 1:
>>> range(10, 0-1, 1)
[]
When in doubt, shell it out