More elegant modulo conversion between 24hr time and 12hr time? - math

Converting 24-hour time (like military time) to 12-hr (clock-face) time seems like a perfect place to use the modulo operator, but I can't figure out a purely mathematical way to map 0 to 12 (so have hours 1 through 12 instead of 0 through 11). The best I've been able to come up with are either (in Ruby)
modHour = militaryHour % 12
if modHour == 0
clockHour = 12
else
clockHour = modHour
end
or,
hours = [12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
clockHour = hours[ militaryHour % 12 ]
It seems like there must be some way to accomplish this shift mathematically, but I can't figure it out.

I think
hour12 = 12 - ((- hour24) % 12)
should work.

(pardon my Python...)
>>> for hr in range (24):
... print hr, (hr + 11) % 12 + 1
...
0 12
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 1
14 2
15 3
16 4
17 5
18 6
19 7
20 8
21 9
22 10
23 11

The answer by Eric Jablow did not yield the correct answer for me. I found that this inline function worked though.
int militaryTime = 14;
int civilianTime = ((24hr - 1) % 12) + 1;

Related

Instantaneous velocity on R studio

(Rstudio) suppose I have a data set of:
# Circle X Y
1 A 21 8
2 A 32 17
3 A 23 32
4 B 22 4
5 B 43 12
6 C 12 4
.....
I need to find the instantaneous velocity of each circle at each time frame.
For line 1 is the starting point so the velocity is 0, and the formula I want to achieve for each circle's (X, Y) coordinates is sqrt(((x2-x1)^2 + (y2-y1)^2)/2)) where the x2 and x1 is from the previous line (e.g. line 1 & line 2, Line 2 & line 3). the final result I want to have is as below:
# Circle X Y Instant velocity
1 A 21 8 0
2 A 32 17 sqrt(((32-21)^2 + (17-8)^2)/2))
3 A 23 32 sqrt(((23-32)^2 + (32-17)^2)/2))
4 B 22 4 0
5 B 43 12 sqrt(((43-22)^2 + (12-4)^2)/2))
6 C 12 4 0
.....
Could anyone help me in achieving this on Rstudio???
You have one more ) than ( in your code example, which makes me a bit confused about where the /2 goes, but if you verify my syntax something like this should work:
library(dplyr)
your_data %>%
group_by(Circle) %>%
mutate(
instant_velocity = coalesce(sqrt(((x - lag(x))^2 + (y - lag(y))^2)/2), 0)
)

WGCNA : Choosing a soft-threshold power

powers = c(c(1:10), seq(from = 12, to=20, by=2));
While going through WGCNA i came across this code which i am not able to understand, can anybody explain me the meaning of that piece of code
The code will create a vector of numbers stored in powers.
Specifically: 1:10 creates the numbers 1 2 3 4 5 6 7 8 9 10 (can read as 1 through 10) and seq(from = 12, to = 20, by = 2) creates a sequence of every other number from 12 to 20, i.e. 12 14 16 18 20.
Powers will contain the following 15 numbers: 1 2 3 4 5 6 7 8 9 10 12 14 16 18 20
I am not familiar with the WGCNApackage or if powers is an argument to a function, but this is what powers contains.

regex for searching through dataframe in R

I have a list of barcodes with the format: AAACCTGAGCGTCAAG-1
The letters can be A, C, G or T and the number after the dash can be 1 - 16.
barcode = c('AAACCTGAGCGTCAAG-1',
'AAACCTGAGTACCGGA-1',
'AAACCTGCAGCTGCTG-1',
'AAACCTGCATCACGAT-3',
'AAACCTGCATTGGGCC-5',
'AAACCTGGTATAGTAG-10',
'AAACCTGGTCGCGTGT-1',
'AAACCTGGTTTCCACC-16',
'AAACCTGTCATGCATG-14',
'AAACCTGTCGCAGGCT-15',
'AAACGGGAGAACTCGG-1')
cluster = c(6,3,6,16,17,11,14,18,9,8,14)
df <- data.frame(Barcode = barcode, Cluster = cluster)
I need to subset this dataframe based on the -# at the end of the barcode. I have been using this to subset the dataframe. The problem is this works for every number except 1.
> df[grep("([ACGT]-10){1}", df$Barcode),]
Barcode Cluster
6 AAACCTGGTATAGTAG-10 11
When I use the following, it will include all the barcodes that end in -1, as well as -10, -11, -12, -13, -14, -15 and -16.
> df[grep("([ACGT]-1){1}", df$Barcode),]
Barcode Cluster
1 AAACCTGAGCGTCAAG-1 6
2 AAACCTGAGTACCGGA-1 3
3 AAACCTGCAGCTGCTG-1 6
6 AAACCTGGTATAGTAG-10 11
7 AAACCTGGTCGCGTGT-1 14
8 AAACCTGGTTTCCACC-16 18
9 AAACCTGTCATGCATG-14 9
10 AAACCTGTCGCAGGCT-15 8
11 AAACGGGAGAACTCGG-1 14
>
Is there a regex that will include barcodes ending in -1, but exclude all other barcodes that end in numbers from 10 - 16?
I want to subset the dataframe so that I only get this:
Barcode Cluster
1 AAACCTGAGCGTCAAG-1 6
2 AAACCTGAGTACCGGA-1 3
3 AAACCTGCAGCTGCTG-1 6
7 AAACCTGGTCGCGTGT-1 14
11 AAACGGGAGAACTCGG-1 14
>
Thanks!
How about:
df[grep("-1$", df$Barcode),]
This matches 1 at the end of the string, but also requires that the digit before 1 is not 1, so you don't match 11
Barcode Cluster
1 AAACCTGAGCGTCAAG-1 6
2 AAACCTGAGTACCGGA-1 3
3 AAACCTGCAGCTGCTG-1 6
7 AAACCTGGTCGCGTGT-1 14
11 AAACGGGAGAACTCGG-1 14
I think you can just use df[grep("([ACGT]-1$){1}", df$Barcode),]
You can just use a $ to specify the end of the chain. See more information here on "pattern" use: http://www.jdatalab.com/data_science_and_data_mining/2017/03/20/regular-expression-R.html

Alternating between reading forwards and backwards in a loop

My array is 1D m in length. say m = 16
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
The way I actually interpret the array is n x n = m
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
I require to read the array in this manner due to the way my physical environment is set up
0 4 8 12 13 9 5 1 2 6 10 14 15 11 7 3
What I came up with works but I really don't think it is the best way to do this:
bool isFlipped = true;
int x = 0; x < m; x++
if(isFlipped)
newLine[x] = line[((n-1)-x%n)*n + x/n)]
else
newLine[x] = line[x%n*n +x/n]
if(x != 0 && x % n == 0)
isFlipped = !isFlipped
This gives me the required result but I really think there is a way to get rid of this boolean by purely using a math formula. I am stuffing this into a 8kb microcontroller and I need to conserve as much space as I can because I will have some bluetooth communication and more math going into it later on.
Edit:
Thanks to a user I got to a one line solution-ish. (the below would replace the lines in the for-loop)
c=x/n
newLine[x] = line[((c+1)%2)*((x%n)*n+c) + (c%2)*((n-1)-2*(x%n))*n ];
You should be able to utilize the fact that odd columns in the n*n matrix are read from down up, and even columns are read from up down.
A number at index x in newLine is located in column number c=floor(x/n) in the n*n matrix. c%2 is 0 for even columns and 1 for odd columns. So something like this should work:
int c = x/n;
newLine[x] = line[(x%n)*n + (c%2)*((n-1)-2*(x%n))*n + c];

McCabe's Complexity Metric and Independent Paths

int maxValue = m[0][0];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if ( m[i][j] >maxValue )
{
maxValue = m[i][j];
}
}
}
cout<<maxValue<<endl;
int sum = 0;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
sum = sum + m[i][j];
}
}
cout<< sum <<endl;
For the above code if we draw a flow graph like this basic independent paths would be following six
Path 1: 1 2 3 10 11 12 13 19
Path 2: 1 2 3 10 11 12 13 14 15 18 13 19
Path 3: 1 2 3 10 11 12 13 14 15 16 17 15 18 13 19
Path 4: 1 2 3 4 5 9 3 10 11 12 13 19
Path 5: 1 2 3 4 5 6 8 5 9 3 10 11 12 13 14 15 16 17 15 18 13 19
Path 6: 1 2 3 4 5 6 7 8 5 9 3 10 11 12 13 14 15 16 17 15 18 13 19
So the question here is according to the given code path 2, 3, 4 can not be tested (Note the "N" in loops). So is it okay not to have a actual execution path as given in the basic set?...
or according to macabe complexity metric do we have to change the code given above. Because a tutor of mine said we have to change the code also he said that there are unstructured loops so we have to change the code. (I don't see an unstructured loop as well)
But my feeling is, if we change the code actual output may differ to expected output. So please someone explain this
1) McCabe's complexity can be calculated as the number of decision points + 1. In your case there are 5 decision points (nodes 3, 5, 6, 13 and 15) meaning that the McCabe complexity of the code fragment is 5+1 = 6. 6 is by no means too high in terms of McCabe complexity: one could, of course, still argue that it is too high given the functionality the implementation has to provide.
2) McCabe's complexity is related to testability of a method/procedure but not to testability of a specific path. Paths can be feasible (= there exist values of the variables that force the execution through this path) or not, but McCabe's complexity is happily unaware of such complications. If you really want to look into feasibility of paths keep in mind that the problem in general is undecidable but many practical data flow analysis algorithms are available.
3) if we change the code actual output may differ to expected output Of course, you cannot introduce an arbitrary change and hope that the results will be the same. However, and, this is probably what your tutor intended, there is a way of restructuring your code such that the output produced remains the same, and the McCabe's complexity goes down. Think, e.g., on whether you really need to separate the tasks of calculating the maximum and the sum.

Resources