Show the value, not the vector name in cat funciton print out - cat

This is using Rstudio. I have this line without syntax erro
cat("The\tdistance\tbetween\tthe\tstations\nthis\r5\tand\t7\nthis\ris\tresult")
It prints out below
The distance between the stations
5 and 7
is result
Two questions:
1. how to delete extra blank spaces between the words? I like to compress to 1
2. Result actually is the name of a vector. it has value =2.5. I want to show this value of 2.5, the name of the vector Result. Thanks

Related

Sumproduct with OR and Right function

In column A are IDs like "1.23.1". The first and last numbers are the ones I use to identify the IDs I am looking for. The middle numbers range from 1 to 999, so IDs could be "1.1.1" OR "1.231.1. This information is only important to show that not all IDs have the same amount of characters.
I need to add the numbers from column B that match the IDs I am looking for.
A ----------- B
1.21.1 ------------5
1.314.2 ----------6
2.2.1 -------------3
I am getting a #VALUE! error with the following formula.
=SUMPRODUCT(OR((RIGHT(A4:A6,1)="1")+0,RIGHT(A4:A6,1)="2")+0,LEFT(A4:A6,1)="1",B4:B6
I would like to add the 5 and 6 because their IDs both end with 1 or 2 and start with 1.
Example
=SUMPRODUCT((LEFT(A4:A6)="1")*((RIGHT(A4:A6)="1")+(RIGHT(A4:A6)="2"))*B4:B6)
Using the OR function will only return a single value; hence your arrays will not be all the same length; hence the #VALUE! error.
Using addition will return an array of the OR test for each cell, instead of a single OR for the entire array.
If you want to use the function with separate arrays, where you convert each test array to it's numeric equivalent, (as in your example) you can use:
=SUMPRODUCT(N(LEFT(A4:A6)="1"),N((RIGHT(A4:A6)="1")+(RIGHT(A4:A6)="2")),B4:B6)

How to count occurrence of substring elements in string in python (even when repeated)?

I tried the following code
s = "BANANA"
print(s.count('ANA'))
>> 1
The output was 1. It did not consider A which was previously counted.
I want the output to be 2 since 'ANA' occurs twice. Ia their a function I don't know about.

Find common subsets between "big" sets

So, I have a file that contains about 13000+ rows. Each row has a list of destinations separated by the char ";". I need to find between all those lists of destinations the 10 most common subsets (ignoring empty set or sets containing only 1 destination) between all the destinations, and the amount of times this subsets appear on the data:
An example may make this easier to understand:
This would be the file (each letter represents a destination)
A;B;C;D
A;B
A;B;C;D;E
A;B;C;D;E;F;G
A;B;C;D;E;F;G;H;L
C;G;B
K;H
So, the most common subsets of destinations together would be:
1. A;B : 5
2. A;C : 4
3. A;D : 4
4. A;B;C : 4
5. A;B;C;D : 4
6. A;E : 3
7. A;B;C;D;E : 3
8. B;C;D;E : 3
9. C;D;E : 3
10. A;B;C;D;E;F : 2
This problem seems very complex to me, I think it would be easier to solve it by limiting the size of the subsets to n (or a fixed number like 3).
Any ideas on how to solve it? I think I need something like FPGRowth but without the Association Rule generated.
Thanks!
you can solve this with one loop:
You have to generate a hashmap for saving the results...
you can give every destination a unique prime number and multiplicate the prime numbers of one line. the result is the key of the hashmap. if the key does not exist, you have to add it with a value of 1. If it exists, you can increase the value. This is called "Integer factorization". At the end you have to find the highest value number of your hashmap.
(hint: save the destination name also in the value of the hashmap,
then you do not have to recalculate the number to the destinations)
(2nd hint: remember the highest number and hashkey, so you don't have
to search at the end for the highest number and key...)
EDIT: for the combinations like A;B;C =>A;B and also B;C you can use 2 for loops to go through the line

How to print a number within a string in matlab

I would like to use the command text to type numbers within 57 hexagons. I want to use a loop:
for mm=1:57
text(x(m),y(m),'m')
end
where x(m) and y(m) are the coordinates of the text .
The script above types the string "m" and not the value of m. What am I doing wrong?
Jubobs pretty much told you how to do it. Use the num2str function. BTW, small typo in your for loop. You mean to use mm:
for mm=1:57
text(x(mm),y(mm),num2str(mm));
end
The reason why I've even decided to post an answer is because you can do this vectorized without a loop, which I'd also like to write an answer for. What you can do place each number into a character array where each row denotes a unique number, and you can use text to print out all numbers simultaneously.
m = sprintfc('%2d', 1:57);
d = reshape([m{:}], 2, 57).';
text(x, y, d);
The (undocumented!) function sprintfc takes a formatting specifier and an array and creates a cell array of strings where each cell is the string version of each element in the array you supply. In order to ensure that the character array has the same number of columns per row, I ensure that each string takes up 2 characters, and so any number less than 10 will have a blank space at the beginning. I then convert the cell array of strings into a character array by converting the cell array into a comma-separated list of strings and I reshape the matrix into an acceptable form, and then I call text with all of the pairs of x and y, with the corresponding labels in m together on the screen.

finding the shortest subsegment

I wanted to know which alogrithm should i apply.
Their is a sentence given and a list to words. We have to find the first shortest sub segment that contains all the words in the list of words.
eg:
Sentence - this is the best problem i have ever solved
List of words -
is
best
this
The answer should be:
this is the best
If there are many such sub segments then we have to print the one that contains the smallest number of words and appears first in sentence.
Here is my approach to solve the above problem.
1. Take 2 pointers head and tail both point to 0
Now move the head pointer until the word pointed to by the head pointer is a valid keyword; now mark it as head.
2. Now move tail pointer until the sentence contains all the given keywords at least once; now mark it as tail.
And this is the first valid subsegment with all valid keywords and calculate it's length
3. Now check word frequency at head - if it is greater than 1 now move head pointer to a word in the sentence which is a valid keyword, as well as it contains frequency of word as 1.
4. Now check whether all keywords are there or not - if yes, calculate it's length and store it as min sub-segment.
5. If it does not contains all valid keywords now move tail pointer until all keywords are found and calculate its length like (tail-head+1); if it is greater than min one then ignore it.
6. Now continue this process until last keyword of given sentence
The complexity of the above approach is o(n).
For example let us take this sentance
Hi this is a funny world this is a good experience with this world
and i need to find 3 keywords
this
is
world
at first consider 2 hash tables namely required,obtained
now store all required keywords in required table.
now take head and tail as 0 now check hi is a valid keyword since it it not move head
now check for next keyword i.e this ,now this is a valid keyword so make a count of 1 and store this word position as head .so now head is 1
now move tail pointer so next keyword is "is" ,it is a valid one hence increment count
now similarly check for a,funny keywords since they are not valid ones hence move tail to world
now world is a valid one as well as count is 3 and tail is 4 whenever count == no of required keywords(in our case it is 3) that means our segment contains all valid keywords
now it's length is (4-1+1)=4
now check frequency of word at head it is one hence if we move this head pointer then we won't get a valid segment
so now move tail pointer to next word this now update frequency of this to 2 from 1 and counter becomes 4
so now we can move our head pointer now move to a keyword is now update counter as 3 because our segment won't contain this at this moment because we have shifted the head pointer from this keyword
now again count is 3 hence calculate it's length again it is 4
so check freq of head keyword is it is 1 hence move tail pointer to next keyword is now is keyword freq is more than 1 hence now move head pointer until we get a valid keyword with freq as 1 now obtained keyword is world and head position is 5 and tail position is 7
and counter is 3 so calculate length as 7-5+1 which is 3 hence this is a min length that we found till now
now move tail until keyword freq at head is more than 1 now finally our tail become 13
now move head from 5 to 6 calculate it's length ,and it becomes 13-6+1 which is 8 so ignore it
now further we cannot move our tail hence print the words from min_head to min_tail as final result
in our case the answer is
world this is
Consider the following simple approach -
Make a dictionary mapping(enumeration) for each word in the sentence. Like -
this[1] is[2] the[3] best[4] problem[5] i[6] have[7] ever[8] solved[9]
Assuming all are distinct words in the sentence.
Now, taking one word at a time and keeping the record of max and min value of that word as key. In this case it would be 4 and 1, resp.
Return the string within the limits.

Resources