Levels for netcdf files in brick, R - time

I have many .nc files with a variable "skt".
What I want to do is to load all these files in R as raster brick and have apart from the variable, the time, too.
I tried to do this with levels, but with no success.
I would really appreciate any help.
list.files(pattern="*.nc")
list_all=list.files(pattern="*.nc")
skt_all<- brick(list_all,varname="skt",levels=4)
Warning messages:
1: In if (x == "" | x == ".") { :
the condition has length > 1 and only the first element will be used
2: In if (!start %in% c("htt", "ftp")) { :
the condition has length > 1 and only the first element will be used
3: In if (fileext %in% c(".GRD", ".GRI")) { :
the condition has length > 1 and only the first element will be used
4: In if (!file.exists(x)) { :
the condition has length > 1 and only the first element will be used
5: In if ((fileext %in% c(".HE5", ".NC", ".NCF", ".NC4", ".CDF", ".NCDF", :
the condition has length > 1 and only the first element will be used

Related

Writing a function that returns true if given string has exactly 6 characters

I am trying to write a function that returns true or false if a given string has exactly 6 consecutive characters with the same value. If the string has more or less than 6, it will return false:
I am not allowed to use lists, sets or import any packages. I am only restricted to while loops, for loops, and utilizing basic mathematical operations
Two example runs are shown below:
Enter a string: 367777776
True
Enter a string: 3677777777776
False
Note that although I entered numbers, it is actually a string within the function argument for example: consecutive('3777776')
I tried to convert the string into an ASCII table and then try and filter out the numbers there. However, I
def consecutive(x):
storage= ' '
acc=0
count=0
for s in x:
storage+= str(ord(s)) + ' '
acc+=ord(s)
if acc == acc:
count+=1
for s in x-1:
return count
My intention is to compare the previous character's ASCII code to the current character's ASCII code in the string. If the ASCII doesnt match, I will add an accumulator for it. The accumulator will list the number of duplicates. From there, I will implement an if-else statement to see if it is greater or less than 6 However, I have a hard time translating my thoughts into python code.
Can anyone assist me?
That's a pretty good start!
A few comments:
Variables storage and acc play the same role, and are a little more complicated than they have to be. All you want to know when you arrive at character s is whether or not s is identical to the previous character. So, you only need to store the previously seen character.
Condition acc == acc is always going to be True. I think you meant acc == s?
When you encounter an identical character, you correctly increase the count with count += 1. However, when we change characters, you should reset the count.
With these comments in mind, I fixed your code, then blanked out a few parts for you to fill. I've also renamed storage and acc to previous_char which I think is more explicit.
def has_6_consecutive(x):
previous_char = None
count = 0
for s in x:
if s == previous_char:
???
elif count == 6:
???
else:
???
previous_char = ???
???
You could use recursion. Loop over all the characters and for each one check to see of the next 6 are identical. If so, return true. If you get to the end of the array (or even within 6 characters of the end), return false.
For more info on recursion, check this out: https://www.programiz.com/python-programming/recursion
would something like this be allowed?
def consecF(n):
consec = 1
prev = n[0]
for i in n:
if i==prev:
consec+=1
else:
consec=1
if consec == 6:
return True
prev = i
return False
n = "12111123333221"
print(consecF(n))
You can try a two pointer approach, where the left pointer is fixed at the first instance of some digit and the right one is shifted as long as the digit is seen.
def consecutive(x):
left = 0
while left != len(x):
right = left
while right < len(x) and x[right] == x[left]:
right += 1
length = (right - 1) - left + 1 # from left to right - 1 inclusive, x[left] repeated
if length == 6: # found desired length
return True
left = right
return False # no segment found
tests = [
'3677777777776',
'367777776'
]
for test in tests:
print(f"{test}: {consecutive(test)}")
Output
3677777777776: False
367777776: True
You should store the current sequence of repeated chars.
def consecutive(x):
sequencechar = ' '
repetitions = 0
for ch in x:
if ch != sequencechar:
if repetitions == 6:
break
sequencechar = ch
repetitions = 1
else:
repetitions += 1
return repetitions == 6
If I could, I would not have given the entire solution, but this still is a simple problem. However one has to take care of some points.
As you see the current sequence is stored, and when the sequence is ended and a new starts, on having found a correct sequence it breaks out of the for loop.
Also after the for loop ends normally, the last sequence is checked (which was not done in the loop).

Reduce binary string to an empty string by removing subsequences with alternative characters

This was a question asked in the coding round for NASDAQ internship.
Program description:
The program takes a binary string as input. We have to successively remove sub-sequences having all characters alternating, till the string is empty. The task was to find the minimum number of steps required to do so.
Example1:
let the string be : 0111001
Removed-0101, Remaining-110
Removed-10 , Remaining-1
Removed-1
No of steps = 3
Example2:
let the string be : 111000111
Removed-101, Remaining-110011
Removed-101, Remaining-101
Removed-101
No of steps = 3
Example3:
let the string be : 11011
Removed-101, Remaining-11
Removed-1 , Remaining-1
Removed-1
No of steps = 3
Example4:
let the string be : 10101
Removed-10101
No of steps = 1
The solution I tried, considered the first character of the binary string as first character for my sub-sequence. Then created a new string, where the next character would be appended if it wasn't part of the alternating sequence. The new string becomes our binary string. In this way, a loop continues till the new string is empty. (somewhat an O(n^2) algorithm). As expected, it gave me a timeout error. Adding a somewhat similar code in C++ to the one I had tried, which was in Java.
#include<bits/stdc++.h>
using namespace std;
int main() {
string str, newStr;
int len;
char c;
int count = 0;
getline(cin, str);
len = str.length();
//continue till string is empty
while(len > 0) {
len = 0;
c = str[0];
for(int i=1; str[i] != '\0';i++) {
//if alternative characters are found, set as c and avoid that character
if(c != str[i])
c = str[i];
//if next character is not alternate, add the character to newStr
else {
newStr.push_back(str[i]);
len++;
}
}
str = newStr;
newStr = "";
count++;
}
cout<<count<<endl;
return 0;
}
I also tried methods like finding the length of the largest sub sequence of same consecutive characters which obviously didn't satisfy every case, like that of example3.
Hope somebody could help me with the most optimized solution for this question. Preferably a code in C, C++ or python. Even the algorithm would do.
I found a more optimal O(NlogN) solution by maintaining a Min-Heap and Look-up hashMap.
We start with the initial array as alternating counts of 0, 1.
That is, for string= 0111001; lets assume our input-array S=[1,3,2,1]
Basic idea:
Heapify the count-array
Extract minimum count node => add to num_steps
Now extract both its neighbours (maintained in the Node-class) from the Heap using the lookup-map
Merge both these neighbours and insert into the Heap
Repeat steps 2-4 until no entries remain in the Heap
Code implementation in Python
class Node:
def __init__(self, node_type: int, count: int):
self.prev = None
self.next = None
self.node_type = node_type
self.node_count = count
#staticmethod
def compare(node1, node2) -> bool:
return node1.node_count < node2.node_count
def get_num_steps(S: list): ## Example: S = [2, 1, 2, 3]
heap = []
node_heap_position_map = {} ## Map[Node] -> Heap-index
prev = None
type = 0
for s in S:
node: Node = Node(type, s)
node.prev = prev
if prev is not None:
prev.next = node
prev = node
type = 1 - type
# Add element to the map and also maintain the updated positions of the elements for easy lookup
addElementToHeap(heap, node_heap_position_map, node)
num_steps = 0
last_val = 0
while len(heap) > 0:
# Extract top-element and also update the positions in the lookup-map
top_heap_val: Node = extractMinFromHeap(heap, node_heap_position_map)
num_steps += top_heap_val.node_count - last_val
last_val = top_heap_val.node_count
# If its the corner element, no merging is required
if top_heap_val.prev is None or top_heap_val.next is None:
continue
# Merge the nodes adjacent to the extracted-min-node:
prev_node = top_heap_val.prev
next_node = top_heap_val.next
removeNodeFromHeap(prev_node, node_heap_position_map)
removeNodeFromHeap(next_node, node_heap_position_map)
del node_heap_position_map[prev_node]
del node_heap_position_map[next_node]
# Created the merged-node for neighbours and add to the Heap; and update the lookup-map
merged_node = Node(prev_node.node_type, prev_node.node_count + next_node.node_count)
merged_node.prev = prev_node.prev
merged_node.next = next_node.next
addElementToHeap(heap, node_heap_position_map, merged_node)
return num_steps
PS: I havent implemented the Min-heap operations above, but the function-method-names are quite eponymous.
We can solve this in O(n) time and O(1) space.
This isn't about order at all. The actual task, when you think about it, is how to divide the string into the least number of subsequences that consist of alternating characters (where a single is allowed). Just maintain two queues or stacks; one for 1s, the other for 0s, where characters pop their immediate alternate predecessors. Keep a record of how long the queue is at any one time during the iteration (not including the replacement moves).
Examples:
(1)
0111001
queues
1 1 -
0 - 0
0 - 00
1 1 0
1 11 -
1 111 - <- max 3
0 11 0
For O(1) space, The queues can just be two numbers representimg the current counts.
(2)
111000111
queues (count of 1s and count of 0s)
1 1 0
1 2 0
1 3 0 <- max 3
0 2 1
0 1 2
0 0 3 <- max 3
1 1 2
1 2 1
1 3 0 <- max 3
(3)
11011
queues
1 1 0
1 2 0
0 1 1
1 2 0
1 3 0 <- max 3
(4)
10101
queues
1 1 0 <- max 1
0 0 1 <- max 1
1 1 0 <- max 1
0 0 1 <- max 1
1 1 0 <- max 1
I won't write the full code. But I have an idea of an approach that will probably be fast enough (certainly faster than building all of the intermediate strings).
Read the input and change it to a representation that consists of the lengths of sequences of the same character. So 11011 is represented with a structure that specifies it something like [{length: 2, value: 1}, {length: 1, value: 0}, {length: 2, value: 1}]. With some cleverness you can drop the values entirely and represent it as [2, 1, 2] - I'll leave that as an exercise for the reader.
With that representation you know that you can remove one value from each of the identified sequences of the same character in each "step". You can do this a number of times equal to the smallest length of any of those sequences.
So you identify the minimum sequence length, add that to a total number of operations that you're tracking, then subtract that from every sequence's length.
After doing that, you need to deal with sequences of 0 length. - Remove them, then if there are any adjacent sequences of the same value, merge those (add together the lengths, remove one). This merging step is the one that requires some care if you're going for the representation that forgets the values.
Keep repeating this until there's nothing left. It should run somewhat faster than dealing with string manipulations.
There's probably an even better approach that doesn't iterate through the steps at all after making this representation, just examining the lengths of sequences starting at the start in one pass through to the end. I haven't worked out what that approach is exactly, but I'm reasonably confident that it would exist. After trying what I've outlined above, working that out is a good idea. I have a feeling it's something like - start total at 0, keep track of minimum and maximum total reaches. Scan each value from the start of string, adding 1 to the total for each 1 encountered, subtracting 1 for each 0 encountered. The answer is the greater of the absolute values of the minimum and maximum reached by total. - I haven't verified that, it's just a hunch. Comments have lead to further speculation that doing this but adding together the maximum and absolute of minimum may be more realistic.
Time complexity - O(n)
void solve(string s) {
int n = s.size();
int zero = 0, One = 0, res = 0;
for (int i = 0; i < n; i++)
{
if (s[i] == '1')
{
if (zero > 0)
zero--;
else
res++;
One++;
}
else
{
if (One > 0)
One--;
else
res++;
zero++;
}
}
cout << res << endl;
}

Pseudocode - What is wrong about this

I AM TRYING TO FIND THE ERROR
The code is supposed to find out if a positive integer entered by a user is exactly divisible by the number 3.
n = userinput
WHILE n ≥ 0
n = n - 3
ENDWHILE
You're using greater than OR EQUAL TO so you won't break out of the loop on n = 0, only n = -3 which then triggers your ELSE statement. The EQUAL TO aspect takes you a step too far.
Answering the comment:
Use > instead of >=. Basically the code as written will never allow n to equal 0 at the time the condition is evaluated. Trace each step of the loop using a number like 3.
N = 3
//first pass
WHILE (3 >= 0) // true
n = 3-3 //n now 0
//second pass
WHILE (0 >= 0) //True, 0 is equal to 0
n = 0-3 //n now -3
//third pass
WHILE(-3 >= 0) //False break out of loop
IF(-3 == 0) // false so we jump to the else
ELSE: 3 is not divisible by 3.
One quick way to easily spot check your loops that aren't performing as expected is to just manually run through them with an easy input.

Finding out if a number in a cell is even or odd

Given that a number in the 0th cell of tape is filled and the rest are all just used as scratch cells (i.e. they all start at 0 and are temporaries -- I don't care what happens to them), I would like to replace the 0th cell with a 0 or a 1. 0 if even, 1 if odd.
Basically, what I want to do is (in C-esque pseudocode):
cell[0] = (cell[0] % 2)
I know that there exists a divmod algorithm defined as follows:
If one does not need to preserve n, use this variant:
# >n d
[->-[>+>>]>[+[-<+>]>+>>]<<<<<]
# >0 d-n%d n%d n/d
However, since X % 2 == X & 1, i.e. X mod 2 is the rightmost bit of X, I think that divmod might be overkill in terms of complexity of calculation.
Is there any better algorithm/technique for finding out if the cell is even or not?
You need an algorithm that keeps only parity, you could do it like that :
result=0
while(n > 0) {
result++;
n--;
if(n > 0) {
result--;
n--;
}
}
To test n without losing its value, you need to copy it : copy from A to B and C then move C to A. You could test B and keep n into A. Here is the brainfuck code :
[->+<] # move #0 to #1
> # goto #1
[-<+ # if #1 then decrements #1 and increments #0
> # goto #1
[->+>+<<] # if #1 then move #1 to #2 and #3
>> # goto #3
[-<<+>>] # if #3 then move #3 to #1
< # goto #2
[<-<->>[-]] # if #2 then decrements #0, decrements #1 and sets 0 into #2
< # go to #1
] # continue loop if #1 is not null
< # goto #0
light-form:
[->+<]>[-<+>[->+>+<<]>>[-<<+>>]<[<-<->>[-]]<]<
N odd or even:
>, load m1 with N
[-[->]<]+ set m0 = 1 if odd
set m1 = 1 if even
Here is a version that completely resolves P:
Is N odd or even?
>, ~load m1 with N (not counted for golf scoring)
>>+>+<<< ~set 'trail of breadcrumbs' so we can figure out
where P is
[-[->]<]+ ~if N is odd set m0 = 1
>>>[>] ~figure out where P is
<[-<]<[-]< ~go back to m1 and zero out if N is even
Pointer P ends on m0
odd: m0 = 1
even: m0 = 0
You have to check the modulus to know whether it's even or odd. It's the simplest way. But I'd be wary of that divmod algorithm you posted. It should work when checking modulo 2 but if I remember correctly don't try to divide by 1 using it.
On a PC you could just AND the number with 1 (assuming it's an integer). But brainfuck doesn't have an AND operator, so you're going to have to go the long way round. You know the computer stores numbers in binary, but that's none of brainfuck's concern. It doesn't give you an array of binary values to manipulate. It gives you an array of numbers which you can only increment, decrement or compare to 0.

Optimization of a function which look for combination - out-of-memory trouble + speed

Below is a function that creates all possible combination of splitting the elements of x into n groups (all groups have the same number of elements)
Function:
perm.groups <- function(x,n){
nx <- length(x)
ning <- nx/n
group1 <-
rbind(
matrix(rep(x[1],choose(nx-1,ning-1)),nrow=1),
combn(x[-1],ning-1)
)
ng <- ncol(group1)
if(n > 2){
out <- vector('list',ng)
for(i in seq_len(ng)){
other <- perm.groups(setdiff(x,group1[,i]),n=n-1)
out[[i]] <- lapply(seq_along(other),
function(j) cbind(group1[,i],other[[j]])
)
}
out <- unlist(out,recursive=FALSE)
} else {
other <- lapply(seq_len(ng),function(i)
matrix(setdiff(x,group1[,i]),ncol=1)
)
out <- lapply(seq_len(ng),
function(i) cbind(group1[,i],other[[i]])
)
}
out
}
Pseudo-code (explainations)
nb = number of groups
ning = number of elements in every group
if(nb == 2)
1. take first element, and add it to every possible
combination of ning-1 elements of x[-1]
2. make the difference for each group defined in step 1 and x
to get the related second group
3. combine the groups from step 2 with the related groups from step 1
if(nb > 2)
1. take first element, and add it to every possible
combination of ning-1 elements of x[-1]
2. to define the other groups belonging to the first groups obtained like this,
apply the algorithm on the other elements of x, but for nb-1 groups
3. combine all possible other groups from step 2
with the related first groups from step 1
This function (and pseudo-code) was first created by Joris Meys on this previous post:
Find all possible ways to split a list of elements into a a given number of group of the same size
Is there a way to create a function that returns a given number of randomly taken possible combinations ?
Such a function would take a third argument which is either percentage.possibilities or number.possiblities which fix the number of random different combinations the function returns.
Something like:
new.perm.groups(x=1:12,n=3,number.possiblities=50)
Working on #JackManey suggestion, you can sample one permutation group in an equiprobable fashion using
sample.perm.group <- function(ning, ngrp)
{
if( ngrp==1 ) return(seq_len(ning))
g1 <- 1+sample(ning*ngrp-1, size=ning-1)
g1 <- c(1, g1[order(g1)])
remaining <- seq_len(ning*ngrp)[-g1]
cbind(g1, matrix(remaining[sample.perm.group(ning, ngrp-1)], nrow=ning), deparse.level=0)
}
where ning is the number of elements per group and ngrp is the number of groups.
It returns indices, so if you have an arbitrary vector you can use it as a permutation:
> ind <- sample.perm.group(3,3)
> ind
[,1] [,2] [,3]
[1,] 1 2 5
[2,] 3 7 6
[3,] 4 8 9
> LETTERS[1:9][ind]
[1] "A" "C" "D" "B" "G" "H" "E" "F" "I"
To generate a sample of permutations of size N, you have two options: If you allow repetitions, i.e., a sample with replacement, all you have to do is run the preceding function N times. OTOH, if your sample is to be taken without replacent, then you can use a rejection mechanism:
sample.perm.groups <- function(ning, ngrp, N)
{
result <- list(sample.perm.group(ning, ngrp))
for( i in seq_len(N-1) )
{
repeat
{
y <- sample.perm.group(ning, ngrp)
if( all(vapply(result, function(x)any(x!=y), logical(1))) ) break
}
result[[i+1]] <- y
}
result
}
This is clearly an equiprobable sampling design, and it is unlikely to be inefficient, since the number of possible combinations is usually much larger than N.

Resources