samedi 28 mars 2020

how does a break function in the inner loop in R?

while (statement 1){
  ......
  ......
  if (statement 2){
    x <- x + 1
    break
  }
  if (statement 3){
    y <- y + 1
  }
}

I have a pseudocode as shown above, I want to verify my understanding whether is correct or not. Is it when the statement 2 is fulfilled, the equation in the 1st if loop will still run, then it will break the if loopand never come back again even the while loop still continue going on?

I hope I can get an explanation about the break function in this senario. Appreciate for your help!

How To I Get The Input from incoming_message

'''''''''''''''''''''''''''''''''''''''''''''''''''''' @app.route('/bot', methods=['GET','POST'])

    incoming_msg = request.values.get('Body', '')
    #print(incoming_msg)
    resp = MessagingResponse()
    msg = resp.message()

if  '/ex %s'.startswith('https') in incoming_msg:
        r = requets.get('exam.com'
        responded = True

Keep an index for how many times a if statement had passed inside a list comprehension

I want for a vector with ones and zeros to check if the element in the vector is 1. If it is one I want to label this element 2 and the next one is equal to 3 etc.. This is easily done by a for loop.

x=np.array([1., 0., 0., 1., 0., 0., 1., 0., 1., 0.])

def label(vector):
U=np.copy(vector)
l=2
for i in range(len(U[:])):
    if occupied(U[i]):
        U[i]=l
        l+=1
return U

And then

label(x)

returns

array([2., 0., 0., 3., 0., 0., 4., 0., 5., 0.])

However since ths arrays become larger and eventually become matrices I thought I would be a good idea to use list comprehensions.

def count_clusters(vector):
U=np.copy(vector)
label=2
U[:] = [label, label+=1 if el==1. else el for el in U[:]]
return U

Which should look something like this. The problem is ofcourse to do the label+=1 at the same time in the list comprehension as to change the value of the element in the array. And then afterwards use the label+1 for the following label.

So I was wondering if this is possible or should I just stick to the for loop.

List index out of range and i got error in my if block which is inside the while loop

def merge(a1,a2):
    if len(a1)<1:
        return a2
    if len(a2)<1:
        return a1
    a1item=a1[0]
    a2item=a2[0]
    i=1
    j=1
    merge=[]
    while(a1item or a2item):
        print(a1item,a2item)
        if a1item<a2item:
            merge.append(a1item)
            a1item=a1[i]
            i+=1
        else:
            merge.append(a2item)
            a2item=a2[j]
            j+=1
        print(merge)


merge([1,2,3],[3,54,100])

I got error in a1item=a1[i] how to stop when the loop is pointing to last element. Suggest me without using inbuilt function

if/else if equivalent for qmake/pro Qt file

I sometimes have to write qmake pro file like this:

QMAKE_EXTRA_TARGETS += activate

macos {
    clear_cache.commands += defaults write io.delille.$$TARGET activated 1;
}

win32 {
    clear_cache.commands += another working command;
}


linux {
    clear_cache.commands += echo unsupported;
}

ios {
    clear_cache.commands += echo unsupported;
}

Is there a way to have avoid listing all unsupported plateform in a simpler way like most language allow if if/else if statement?

How can I check a column for a sub-string and update another column if found?

I am quite new to python and my searches haven't been productive, so I hope someone could help or direct me to where it has been answered. Basically, I am trying to search a particular column for a substring and if that substring exists, update the corresponding row on a different column.

I will illustrate with this dataset

    A   B            C
    1   CYESC        THIS
    2   ABCD         0
    3   YES BOY      THIS
    4   BOY YES CAN  THIS
    5   ryrruei      0

I want to "THIS" to show up in Column C if column "B" contains the substring "YES" and "0" if it doesn't.

I tried running this code, which came out with an error message:

import pandas as pd 
import numpy as np

df['C'] = pd.np.where(df['B'].str.contains('YES'), 'THIS')

I also tried using if:

if 'YES' in [df['B']]:
     df['C'] = "THIS"
Else:
    df['C'] = 0    

Still wasn't successful. I know there is something I am missing...please any suggestion would be appreciated. Cheers

vendredi 27 mars 2020

Loop for creating counter

For python loop, I want to write this logic: High = 120, Low = 100, Counter = 0 Live stock price = LTP If LTP > high, then counter should be 1 (first breakout), then if LTP < low, then counter should be 2 (second breakout) If LTP is in between high and low, then counter should show previous counter value. Help please!