Ubuntu 16.04. Thunderbird 60.2.1 (64 bits) Calendar has disappeared.
That change in Thunderbird, which is automatically updated, costed me 1 hour of research and work. How many hours these guys took from ppl around the world? Is seems they just don't care. Because, maybe, they become big.
For now:
1. Remove Lighting from Thunderbird.
2. Close Thunderbird
3. sudo apt install xul-ext-lightning
4. Start Thunderbird again.
Calendar is here again.
Or... you may try to download and install appropriate version of Lighting:
- versions: https://developer.mozilla.org/en-US/docs/Mozilla/Calendar/Calendar_Versions#Stable_Releases
- downloads: https://ftp.mozilla.org/pub/calendar/lightning/candidates/
For tomorrow, seriously consider different pim tool, because you may find yourself in not comfortable situation.
czwartek, 8 listopada 2018
czwartek, 25 października 2018
Codility FibFrogg
Idea: This is a search graph task. A used this explanation of the algorithm LINK.
In first step you check all possible single jumps from start point. This creates a new "row" of starting positions.
In next step you check all possible single jumps from every start position in the "row".
Sum of found new positions create another row.
You skip if you find a possible jump to the position which was "visited" previously, because there is a shorter or equal path to that position.
You stop when any jump will reach "end position" or you cant find a position to jump anymore. Code:
In first step you check all possible single jumps from start point. This creates a new "row" of starting positions.
In next step you check all possible single jumps from every start position in the "row".
Sum of found new positions create another row.
You skip if you find a possible jump to the position which was "visited" previously, because there is a shorter or equal path to that position.
You stop when any jump will reach "end position" or you cant find a position to jump anymore. Code:
wtorek, 16 października 2018
Codility-CommonPrimeDivisors version by PrimeFactorization
It's much slower solution than by using Greatest Common Divisor, but it's different and still gets 100% on Codility platform
Codility-CommonPrimeDivisors version by GCD
It's tricky task. I used some help via internet.
Idea: Greatest common divisor of A and B consists of EVERY prime divisor of A & B. Consists or contains. There is no such a number E that would be prime, and divisor of A and B that GCD(A,B) * E = A && GCD(A,B) * E =B
Once you have GCD, you divide a number by it. That number can be considered as a "row" of prime factors and their exponents. So dividing number by GCD will remove some factors or their exponents.
Then you create a new GCD value form divided number and old GCD value. And repeat dividing. Eventually GCD will become = 1. Then there are two possibilities:
(last) divided number = 1 number consisted only with prime divisors contained in original GCD
(last) divided number != 1 number consists of some more prime divisors than original GCD
Idea: Greatest common divisor of A and B consists of EVERY prime divisor of A & B. Consists or contains. There is no such a number E that would be prime, and divisor of A and B that GCD(A,B) * E = A && GCD(A,B) * E =B
Once you have GCD, you divide a number by it. That number can be considered as a "row" of prime factors and their exponents. So dividing number by GCD will remove some factors or their exponents.
Then you create a new GCD value form divided number and old GCD value. And repeat dividing. Eventually GCD will become = 1. Then there are two possibilities:
(last) divided number = 1 number consisted only with prime divisors contained in original GCD
(last) divided number != 1 number consists of some more prime divisors than original GCD
wtorek, 9 października 2018
sobota, 6 października 2018
piątek, 5 października 2018
Codility - CountNonDivisible
Idea:
Take an element.
Count amount divisors it has.
Non-divisors = N - amount_of_divisors
Take an element.
Count amount divisors it has.
Non-divisors = N - amount_of_divisors
import static java.lang.Integer.max; import java.util.Arrays; class Solution { public int[] solution(int[] A) { final int N = A.length; final int MAX_VALUE_TBL = 2*50000; int[] r = new int[N]; // result table int[] AS_AV = new int[MAX_VALUE_TBL + 1]; // number of cell with values int[] AS_AR = new int[MAX_VALUE_TBL + 1]; // results yet counted for values boolean[] b = new boolean[MAX_VALUE_TBL + 1]; // if value has been counted if (N == 1) return r; for (int i = 0; i < N; i++) { int v = A[i]; AS_AV[v]++; } for (int i = 0; i < N; i++) { int cu_val = A[i]; if (!b[cu_val]) { int am_div = getAmDivisors(cu_val, AS_AV); int am_all = N; r[i] = am_all - am_div; b[cu_val] = true; AS_AR[cu_val] = r[i]; } else { r[i] = AS_AR[cu_val]; } } return r; } private int getAmDivisors(int cu_val, int[] AS_AV) { int r = 0; int sqr = (int) Math.sqrt(cu_val); for (int divisor = sqr; divisor > 0; divisor--) { if (cu_val % divisor == 0) { r += AS_AV[divisor]; if (divisor * divisor != cu_val) { r += AS_AV[cu_val / divisor]; } } } return r; } }
czwartek, 4 października 2018
Codility - Flags
This is a solution for Codility Flags task:
Idea.
Idea.
- Register all peaks.
- Maximum flag number is Math.sqrt(idxOfLastPeak - idxOfFirstPeak)+1
- Go through possible flag numbers, starting from maximum downwards. Check every time number of possible flags.
- If number of possible flags >= number tested (taken) than return number.
import static java.lang.Integer.min; class Solution { public int solution(int[] A) { int r = 0; final int N = A.length; int[] P = new int[N / 2]; int iP = -1; int max_flag_number; for (int i = 1; i < N - 1; i++) { if (A[i] > A[i - 1] && A[i] > A[i + 1]) { P[++iP] = i; } } if (iP == -1) return 0; if (iP == 0) return 1; max_flag_number = (int) (Math.sqrt(P[iP] - P[0])+1); for (int flag_taken = max_flag_number; flag_taken > 0; flag_taken--) { int possible_nb_flag = countFlag(P, flag_taken, iP); if (possible_nb_flag >= flag_taken) { r = min(possible_nb_flag, flag_taken); break; } } return r; } private int countFlag(int[] P, int space, int iP) { int r = 1; int start_flag; start_flag = P[0]; for (int i = 1; i <= iP; i++) { if (P[i] - start_flag >= space) { r++; start_flag = P[i]; } } return r; } }
środa, 3 października 2018
Codility Peaks v2
This is the second solution for Codility task, named Peaks.
The idea is simple:
I collect all the "peaks", and meanwhile I notice prefix sum of peak encounters.
I go through possible sizes of division as long, as I find that one, that has at least one peak in every "block" aka division.
============================================
public int solution(int[] A) {
int r = 0; // result
final int N = A.length; // size of input table
int[] P = new int[N/2]; // peaks
int pInd = -1; // last index of peaks table
int[] S = new int[N]; // sumprefix of peak count
for(int i = 1; i < N-1; i++){
if(A[i] > A[i-1] && A[i] > A[i+1]){
P[++pInd] = i; // remember peak index
S[i] = S[i-1]+1; // remember amount of peaks till this idx
} else {
S[i] = S[i-1];
}
}
if(pInd == -1) return 0; // no peaks
if(pInd == 0) return 1; // one peak means one block
S[N-1] = S[N-2]; // filling the last value of S
int minSize = max(N - P[pInd], P[0]+1); // first and last block has to have a peak
for(int size = minSize; size <=N; size++){
boolean noPeak = false;
if(N % size == 0){
int blocksAmount = N/size;
for(int blockNb = 1; blockNb < blocksAmount; blockNb++){
if( !(S[(blockNb+1)*size -1] > S[blockNb*size -1]) ){
noPeak = true;
break;
}
} // end of loop for blocks
if (!noPeak) {
r = N / size;
break;
}
}
} // end of loop for sizes
return r;
}
The idea is simple:
I collect all the "peaks", and meanwhile I notice prefix sum of peak encounters.
I go through possible sizes of division as long, as I find that one, that has at least one peak in every "block" aka division.
============================================
public int solution(int[] A) {
int r = 0; // result
final int N = A.length; // size of input table
int[] P = new int[N/2]; // peaks
int pInd = -1; // last index of peaks table
int[] S = new int[N]; // sumprefix of peak count
for(int i = 1; i < N-1; i++){
if(A[i] > A[i-1] && A[i] > A[i+1]){
P[++pInd] = i; // remember peak index
S[i] = S[i-1]+1; // remember amount of peaks till this idx
} else {
S[i] = S[i-1];
}
}
if(pInd == -1) return 0; // no peaks
if(pInd == 0) return 1; // one peak means one block
S[N-1] = S[N-2]; // filling the last value of S
int minSize = max(N - P[pInd], P[0]+1); // first and last block has to have a peak
for(int size = minSize; size <=N; size++){
boolean noPeak = false;
if(N % size == 0){
int blocksAmount = N/size;
for(int blockNb = 1; blockNb < blocksAmount; blockNb++){
if( !(S[(blockNb+1)*size -1] > S[blockNb*size -1]) ){
noPeak = true;
break;
}
} // end of loop for blocks
if (!noPeak) {
r = N / size;
break;
}
}
} // end of loop for sizes
return r;
}
Codility - Peaks
This is the first solution of the peaks task form Codility platform:
Idea is to determine min and max size of possible block, then go through peaks and increase minimum size of the block if some peak is "out of the division". Additionally there is a list of forbidden value of sizes to avoid situation that increasing minimum size on one peak would negatively interfere with any of previous peaks.
It's very long in implementation, but intuitive, very fast && maybe original. Faster than widely published version with checking all the peaks in a loop of consecutive size of the block, if one don't use prefix sum for counting blocks. Of course 100% in Codility test.
Here is a code:
public int solution(int[] A) {
int r;
final int N = A.length;
int[] B = new int[N]; // space between peaks +1
int[] P = new int[N]; // peaks idxs
int iP; // max index of values in table P
int max_bl, min_bl;
List<Integer> list_impossible_min = new ArrayList<>();
int[] min_max_ip = new int[3]; // temporary storage for variables
// detemine B, P, iP, max_bl, min_bl
getMinMaxBl(A, B, P, min_max_ip);
min_bl = min_max_ip[0];
max_bl = min_max_ip[1];
iP = min_max_ip[2];
if(iP == -1){ // no peaks
return 0;
} else if(iP == 0){ // one peak
return 1;
}
// applaying last block size
int last_bl = N-1 -P[iP] + 1;
if( last_bl > min_bl){
min_bl = last_bl;}
if(last_bl > B[P[iP]]){
B[P[iP]] = last_bl;}
// applying condition of possible divide
while ((N % min_bl) != 0) {
min_bl++;}
while ((N % max_bl) != 0) {
max_bl++;}
for (int i = 1; i <= iP; i++) {
int idx_peak = P[i];
if (B[idx_peak] > min_bl) {
int pp = P[i-1]; // previous peak
int a = (idx_peak + 1) / min_bl;
int b = a * min_bl;
if (b != (idx_peak + 1)) { // border of division is in between peaks
if (b - pp > min_bl) { // border of division is too far from pp
// seraching for first possible new min_bl
for (int j = min_bl + 1; j <= max_bl; j++) {
a = (idx_peak + 1) / j;
b = a * j;
if (list_impossible_min.isEmpty() || !list_impossible_min.contains(j)) {
if (N % j == 0 && (b - pp <= j)) {
min_bl = j;
break;
}
}
}
}
}
for (int j = min_bl + 1; j <= B[idx_peak]; j++) {
a = (idx_peak + 1) / j;
b = a * j;
if (N % j != 0 || ((pp + j < idx_peak) && ( b*a-pp > j))) {
if (pp + j < idx_peak) {
if (list_impossible_min.isEmpty() || !list_impossible_min.contains(j)) {
list_impossible_min.add(j);
}
}
}
}
}
}
r = N / min_bl;
return r;
}
private void getMinMaxBl(int[] A, int[] B, int[] P, int[] min_max_ip) {
int N = A.length;
int f_peak = -1, l_peak = -1;
int iP = -1, min_bl = 0, max_bl = 0;
// detemine max_bl, min_bl
// first min_bl & max_bl
for (int i = 1; i < N - 1; i++) {
if (peak(A, i)) {
max_bl = i+1;
min_bl = i+1;
f_peak = i;
l_peak = i;
B[i] = i+1;
P[++iP] = i;
break;
}
}
if(f_peak > 0){
for(int i = f_peak+2; i < N-1; i++){
int t_max_block;
int t_min_block;
if (peak(A, i)) {
t_max_block = i - l_peak; // covers cells max empty space + 1 for peak
t_min_block = (t_max_block + 1)/2; // covers two peaks + space between
if((t_max_block+1)%2 != 0){
t_min_block++;
}
min_bl = max(min_bl, t_min_block);
max_bl = max(max_bl, t_max_block);
B[i] = t_max_block;
l_peak = i;
P[++iP] = i;
}
}
}
min_max_ip[0] = min_bl;
min_max_ip[1] = max_bl;
min_max_ip[2] = iP;
}
boolean peak(int[] A, int i) {
return A[i] > A[i - 1] && A[i] > A[i + 1];
}
Idea is to determine min and max size of possible block, then go through peaks and increase minimum size of the block if some peak is "out of the division". Additionally there is a list of forbidden value of sizes to avoid situation that increasing minimum size on one peak would negatively interfere with any of previous peaks.
It's very long in implementation, but intuitive, very fast && maybe original. Faster than widely published version with checking all the peaks in a loop of consecutive size of the block, if one don't use prefix sum for counting blocks. Of course 100% in Codility test.
Here is a code:
public int solution(int[] A) {
int r;
final int N = A.length;
int[] B = new int[N]; // space between peaks +1
int[] P = new int[N]; // peaks idxs
int iP; // max index of values in table P
int max_bl, min_bl;
List<Integer> list_impossible_min = new ArrayList<>();
int[] min_max_ip = new int[3]; // temporary storage for variables
// detemine B, P, iP, max_bl, min_bl
getMinMaxBl(A, B, P, min_max_ip);
min_bl = min_max_ip[0];
max_bl = min_max_ip[1];
iP = min_max_ip[2];
if(iP == -1){ // no peaks
return 0;
} else if(iP == 0){ // one peak
return 1;
}
// applaying last block size
int last_bl = N-1 -P[iP] + 1;
if( last_bl > min_bl){
min_bl = last_bl;}
if(last_bl > B[P[iP]]){
B[P[iP]] = last_bl;}
// applying condition of possible divide
while ((N % min_bl) != 0) {
min_bl++;}
while ((N % max_bl) != 0) {
max_bl++;}
for (int i = 1; i <= iP; i++) {
int idx_peak = P[i];
if (B[idx_peak] > min_bl) {
int pp = P[i-1]; // previous peak
int a = (idx_peak + 1) / min_bl;
int b = a * min_bl;
if (b != (idx_peak + 1)) { // border of division is in between peaks
if (b - pp > min_bl) { // border of division is too far from pp
// seraching for first possible new min_bl
for (int j = min_bl + 1; j <= max_bl; j++) {
a = (idx_peak + 1) / j;
b = a * j;
if (list_impossible_min.isEmpty() || !list_impossible_min.contains(j)) {
if (N % j == 0 && (b - pp <= j)) {
min_bl = j;
break;
}
}
}
}
}
for (int j = min_bl + 1; j <= B[idx_peak]; j++) {
a = (idx_peak + 1) / j;
b = a * j;
if (N % j != 0 || ((pp + j < idx_peak) && ( b*a-pp > j))) {
if (pp + j < idx_peak) {
if (list_impossible_min.isEmpty() || !list_impossible_min.contains(j)) {
list_impossible_min.add(j);
}
}
}
}
}
}
r = N / min_bl;
return r;
}
private void getMinMaxBl(int[] A, int[] B, int[] P, int[] min_max_ip) {
int N = A.length;
int f_peak = -1, l_peak = -1;
int iP = -1, min_bl = 0, max_bl = 0;
// detemine max_bl, min_bl
// first min_bl & max_bl
for (int i = 1; i < N - 1; i++) {
if (peak(A, i)) {
max_bl = i+1;
min_bl = i+1;
f_peak = i;
l_peak = i;
B[i] = i+1;
P[++iP] = i;
break;
}
}
if(f_peak > 0){
for(int i = f_peak+2; i < N-1; i++){
int t_max_block;
int t_min_block;
if (peak(A, i)) {
t_max_block = i - l_peak; // covers cells max empty space + 1 for peak
t_min_block = (t_max_block + 1)/2; // covers two peaks + space between
if((t_max_block+1)%2 != 0){
t_min_block++;
}
min_bl = max(min_bl, t_min_block);
max_bl = max(max_bl, t_max_block);
B[i] = t_max_block;
l_peak = i;
P[++iP] = i;
}
}
}
min_max_ip[0] = min_bl;
min_max_ip[1] = max_bl;
min_max_ip[2] = iP;
}
boolean peak(int[] A, int i) {
return A[i] > A[i - 1] && A[i] > A[i + 1];
}
piątek, 17 sierpnia 2018
Codility - NumberOfDiscIntersections
O(N) Time complexity.
Main idea: For every next circle, the number of possible pairs is equal difference between all open to this "moment" cicrcles and all that have been processed.
public int solution04(int[] A) {
final int N = A.length;
final int M = N + 2;
int[] left = new int[M]; // values of nb of "left" edges of the circles in that point
int[] sleft = new int[M]; // prefix sum of left[]
int il, ir; // index of the "left" and of the "right" edge of the circle
for (int i = 0; i < N; i++) { // counting left edges
il = tl(i, A);
left[il]++;
}
sleft[0] = left[0];
for (int i = 1; i < M; i++) {// counting prefix sums for future use
sleft[i]=sleft[i-1]+left[i];
}
int o, pairs, total_p = 0, total_used=0;
for (int i = 0; i < N; i++) { // counting pairs
ir = tr(i, A, M);
o = sleft[ir]; // nb of open till right edge
pairs = o -1 - total_used;
total_used++;
total_p += pairs;
}
if(total_p > 10000000){
total_p = -1;
}
return total_p;
}
int tl(int i, int[] A){
int tl = i - A[i]; // index of "begin" of the circle
if (tl < 0) {
tl = 0;
} else {
tl = i - A[i] + 1;
}
return tl;
}
int tr(int i, int[] A, int M){
int tr; // index of "end" of the circle
if (Integer.MAX_VALUE - i < A[i] || i + A[i] >= M - 1) {
tr = M - 1;
} else {
tr = i + A[i] + 1;
}
return tr;
}
piątek, 20 lipca 2018
Codility - MinAvgTwoSlice
public int solution03(int[] A) {
final int N = A.length;
double minSlc = (A[0] + A[1]) / 2.0;
int indMinSlc = 0;
for (int i = 0; i < N - 2; i++) {
double slc = (A[i] + A[i + 1]) / 2.0;
if (A[i + 2] < slc) {
slc = (A[i] + A[i + 1] + A[i + 2]) / 3.0;
}
if (slc < minSlc) {
minSlc = slc;
indMinSlc = i;
}
}
if ((A[N - 2] + A[N - 1]) / 2.0 < minSlc) {
indMinSlc = N - 2;
}
return indMinSlc;
}
=============
Explanation:
1. First slice consist of two elements.
2. You can lover the value of original slice by adding some new elements.
3. In order to lover the value of original slice, added elements have to have "slice value" lesser than original slice.
4. If pt. 3 is fullfilled than value of new slice created that way is bigger than slice of added elements.
5. So it has no sense try to create a new slice with lesser value by adding more than ONE element, because added elements will create a slice of even lesser value than one we try to create.
6. You can omit using double and dividing, by using multiplying and type int. But solution will less clear.
final int N = A.length;
double minSlc = (A[0] + A[1]) / 2.0;
int indMinSlc = 0;
for (int i = 0; i < N - 2; i++) {
double slc = (A[i] + A[i + 1]) / 2.0;
if (A[i + 2] < slc) {
slc = (A[i] + A[i + 1] + A[i + 2]) / 3.0;
}
if (slc < minSlc) {
minSlc = slc;
indMinSlc = i;
}
}
if ((A[N - 2] + A[N - 1]) / 2.0 < minSlc) {
indMinSlc = N - 2;
}
return indMinSlc;
}
=============
Explanation:
1. First slice consist of two elements.
2. You can lover the value of original slice by adding some new elements.
3. In order to lover the value of original slice, added elements have to have "slice value" lesser than original slice.
4. If pt. 3 is fullfilled than value of new slice created that way is bigger than slice of added elements.
5. So it has no sense try to create a new slice with lesser value by adding more than ONE element, because added elements will create a slice of even lesser value than one we try to create.
6. You can omit using double and dividing, by using multiplying and type int. But solution will less clear.
środa, 14 lutego 2018
How to compare folders
1. diff -rq folder1 folder2
-r - means recursive
-q - means give only differences
2. rsync -n -avrc /abc/home/sample1/* server2:/abc/home/sample2/
https://stackoverflow.com/a/19410791
3. using "meld".
It is slow, but graphical interface
4. using "find"
https://stackoverflow.com/questions/16787916/difference-between-two-directories-in-linux
-r - means recursive
-q - means give only differences
2. rsync -n -avrc /abc/home/sample1/* server2:/abc/home/sample2/
https://stackoverflow.com/a/19410791
rsync -n -avr --size-only --delete /abc/home/sample1/ server2:/abc/home/sample2/
3. using "meld".
It is slow, but graphical interface
4. using "find"
A good way to do this comparison is to use
find with md5sum , then a diff .Example: Use find to list all the files in the directory then calculate the md5 hash for each file and pipe it to a file:
Do the same procedure to the another directory:
Then compare the result two files with "diff":
This strategy is very useful when the two directories to be compared
are not in the same machine and you need to make sure that the files are
equal in both directories. |
Subskrybuj:
Posty (Atom)