mercredi 28 octobre 2020

how to reduce run tiime for a simple if-else-while program

i had just started learning python,so i was practising some code at codechef one of the problem i was trying was https://www.codechef.com/problems/CHEFEZQ

"Chef published a blog post, and is now receiving many queries about it. On day i, he receives Qi queries. But Chef can answer at most k queries in a single day.

Chef always answers the maximum number of questions that he can on any given day (note however that this cannot be more than k). The remaining questions (if any) will be carried over to the next day.

Fortunately, after n days, the queries have stopped. Chef would like to know the first day during which he has some free time, i.e. the first day when he answered less than k questions.

Input: First line will contain T, the number of testcases. Then the testcases follow. The first line of each testcase contains two space separated integers n and k. The second line of each testcase contains n space separated integers, namely Q1,Q2,...Qn. Output: For each testcase, output in a single line the first day during which chef answers less than k questions.

Constraints 1≤T≤105 1≤ sum of n over all testcases ≤105 1≤k≤108 0≤Qi≤108"

so i wrote my code like this:

i=0

while (i<T):
    nk=input()
    q=[int(a) for a in input().split(" ")]
    n_k=nk.split(" ")
    n=int(n_k[0])
    k=int(n_k[1])
    j=0
    l=0
    while j<n:
        l=l+q[j]-k
        if l<0:
            print(j+1)
            break
        j+=1
    while j>=n:
        l=l-k
        if l<0:
            print(j+1)
            break
        j+=1
    i+=1

the total time limit is 1sec but my code takes 5 sec i dont find any method to reduce time,can someone pls help me out thank you in advance

mardi 27 octobre 2020

If x and y are true do this, else do this. Pandas

enter code here if fluadf['CLINICAL_STATUS'] = 'DRAFT' and fluadf['CLINICAL_DATE_RECEIVED'] = " / /

" fluadf['CLINICAL_STATUS'].str.replace( 'DRAFT', 'IN TRANSIT') else fluadf['CLINICAL_STATUS'].str.replace( 'DRAFT', 'IN PROCESS')

Not sure if this code makes sense. I am trying to test for two values and if they are both true (clinical status is draft and there is not a date for clinical date received) then change draft to in transit. Else, change draft to in process. I'd really appreciate any help coaching me through this.

how can I loop through a tuple?


def grade(*score):
  for i in range(0, score):
    if score >= 90:
      return "A"
    elif score >=80:
      return "B"
    elif score >=70:
      return "C"
    elif score >=60:
      return "D"
    else:
      return "F"

print(grade(87, 92, 100, 54, 72, 84, 81, 74))

I want to be able to loop through the arguments and return the correct grade on each loop

Call function if-statement is true before the function has declared

I am trying to call a function if-statement is true before the function has declared as shown in the example below

This is the general idea of the code, any suggestion? let me know if you have any questions. Thanks

function start(){

var a=Math.random();
var b=Math.random();

if (a>b) function one();
else function two();

function one(){ //do something}
function two(){//do something}
}

start(); 

Two dimensional arrays(dynamically) if condition for null in c

Hi people I created an array that is two-dimensional dynamically. I want to determine which row empty but my syntax knowledge is insufficient. Thank you in advance for your interest.

There is my code:

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    int i,j,*x,y=0;
    int** p=malloc(sizeof(int*)*4);
    for(i=0;i<3;i++)
        p[i]=malloc(sizeof(int)*3);
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            p[i][j]=(y++);
    }
    x=NULL;
    for(i=0;i<4;i++)
    {
        // Fourth index shouldn't be NULL?
        if(p[i]==x)
            printf("NULL");
        else
            printf("Not null");
    }
    for(i=0;i<4;i++)
        free(p[i]);
    free(p);
    return(0);
}

How to assign a new column based on conditions of other columns?

I am trying to add a column, "flag_column" based on the value present in A, B, C, D.

i.e if there is a value in A / B / C / D I would like to create a new column, 'flag' indicating the column name that contains a value.

  A B C D counts flag
0 1 0 0 0  1     A
1 0 1 0 0  1     B
2 1 0 0 0  1     A
3 0 0 1 0  1     C
4 0 1 0 0  1     B

Note: There will only be one column (A through D) that contains a value, so counts will always be 1.

I've tried:

if [df['A'] == 1] == True:
    df['flag'] = 'A'
elif [df['B'] == 1] == True:
    df['flag'] = 'B'
elif [df['C'] == 1] == True:
    df['flag'] = 'C'  
else:
    df['flag'] = 'D'    

I have also tried:

df['flag'] = np.where(df['A'] == 1, 'A', False)
df['flag'] = np.where(df['B'] == 1, 'B', False)
df['flag'] = np.where(df['C'] == 1, 'C', False)
df['flag'] = np.where(df['D'] == 1, 'D', False)

I've also tried doing this iteratively looping through each "category" and assigning a flag value, however it overwrites in these cases as well.

If there is a way in which I could iteratively do this, that would be ideal. However, any help on this (simple) question would be greatly appreciated!

how to stop getting errors when trying to turn all certain squares dark blue on a grid

I'm not sure how to fix this issue. my program is basically randomly generated green blocks on a 32 by 32 grid(it does not fill the whole grid). if i click on an empty spot, it turns blue and is supposed to spread to all other empty spots but not changing green(land). if I click on an area that is connected to the edge of the screen, it will turn a dark green and also spread to all other empty spots. my problem is that if i click on a square that is connected to the edge of the screen, i just get an error and although it spreads, it doesn't always spread to every spot, and it's not the same color

this is the code to make it spread

    void findLakes(int x, int y) {
    if (board[x][y] != EMPTY)
        return;
    board[x][y] = LAKE;
    if (y > 0 && board[x][y - 1] == EMPTY)
        findLakes(x, y - 1);
    if (y < GRID - 1 && board[x][y + 1] == EMPTY)
        findLakes(x, y + 1);
    if (x > 0 && board[x - 1][y] == EMPTY)
        findLakes(x - 1, y);
    if (x < GRID - 1 && board[x + 1][y] == EMPTY)
        findLakes(x + 1, y);
    if (x == 0) findOceans(x,y);
}
// if (... square is on the edge of the board) findOceans(x,y);
void findOceans(int x, int y) {
            if (board[x][y] == LAKE) board [x][y] = OCEAN;

            if (((y > 0) && (board[x][y - 1] == LAKE)) ||  (board[x][y - 1] == EMPTY ))
                findOceans(x, y - 1);
            if (((y < GRID - 1) && (board[x][y + 1] == LAKE)) ||  (board[x][y + 1] == EMPTY))
                findOceans(x, y + 1);
            if (((x > 0) && (board[x - 1][y] == LAKE)) || (board[x][x - 1] == EMPTY))
                findOceans(x - 1, y);
            if (((x < GRID - 1 ) && (board[x + 1][y] == LAKE)) ||  (board[x][x + 1] == EMPTY))
                findOceans(x + 1, y);
}

and if you wanted, this is how i generate my map

void makeRandomMap() {
    int i, j;
    boolean done = false;
    int landTiles = 0;
    while (!done) {
        i = (int) (Math.random() * GRID);
        j = (int) (Math.random() * GRID);
        if (board[i][j] == EMPTY) {
            board[i][j] = LAND;
            landTiles++;
            if (landTiles == NUM_LAND)
                done = true;
        }
    }
}

randomly generated map

its kind of confusing how I described it so if you have any questions, I can try and answer them!