samedi 27 juin 2020

why is my code not giving me the correct output (i only want to receive integers as key)?

int main(int argc, string argv[])                           //command line argument
{

  for ( int i = 0; i< strlen(argv[1]); i++)
  {
  if (argc != 2)                                            //correct argument count
  {
    printf ("usage: ./caesar key\n");
    return 1;
  }
  else if(isdigit(argv[1][i]))
  {
     printf("success\n");
  }
  else if(isalpha(argv[1][i]))
  {
    printf ("usage: ./caesar key\n");
    return 1;
  }
  }  

in this part of code what i'm trying to say to the computer is to 'only accept ints as key, otherwise reject the key'.

however, when i tried different inputs to test the accuracy of my code this is what happened:

~/caesar/ $ ./caesar 4r       //<--mix of outputs.i don't want 'success' here bc input has a letter
success
usage: ./caesar key
~/caesar/ $ ./caesar g5       //correct output
usage: ./caesar key
~/caesar/ $ ./caesar r        //correct output
usage: ./caesar key

as you can see, when i input a letter and then an int, it works fine, but when i do the opposite (int and then letter), it would give me a mix of outputs, both rejecting and accepting the output.

How exactly does the .any() Python method work?

I'm trying to write a script that simulates a system of chemical reactions over time. One of the inputs to the function is the following array:

popul_num = np.array([200, 100, 0, 0])

Which contains the number of discrete molecules of each species in the system. Part of the main function has an if statement that's meant to check that number of molecules is positive. if it is processed to the next iteration, else break out of the whole simulation

if popul_num.any() < 0: # Any method isn't working! --> Does .any() work on arrays or just lists? 
        print("Break out of loop negative molecule numbers")
        tao_all = tao_all[0:-1]
        popul_num_all = popul_num_all[0:-1]       
    else:
        break

I've used the .any() to try find if any element of the popul_num array is negative. But it doesn't work, it doesn't throw an error, the system just never enters the if statement and I can't figure out why?

I've just ran the program and the final number of molecules the system returned was: [135 -19 65 54] the program should have broken out before the second element got to -19.

Any suggestions?

Cheers

Return true if the array contains either 3 even or 3 odd values all next to each other

While I was practicing Java Problems on coding bat I came across the following problem statement:-

Problem:-

Given an array of integers, return true if the array contains either 3 even or 3 odd values all next to each other.

Example:-

modThree([2, 1, 3, 5]) → true
modThree([2, 1, 2, 5]) → false
modThree([2, 4, 2, 5]) → true

My Solution:-

public boolean modThree(int[] nums) {
  for(int i=0;i<nums.length-2;i++){
    if((nums[i]%2==0&&nums[i+1]%2==0&&nums[i+2]%2==0)||(nums[i]%2==1 && nums[i+1]%2==1 && nums[i+2]%2==1)){
      return true;
    }
  }
    return false;
}

Though my solution works, my solution looks a bit long(especially the if statement condition). So, I am looking for a solution with fewer lines of code. Can you help me with this?

How many words from the array A and array B are used in the user input?

This is a program where I need some help

   A = ["having", "had", "is"]
   B = ["will", "would", "should"]

   sentence = input("Enter a sentence") #E.g. 'I will be having it in the future'

   if A in sentence:
      ...

   elif B in sentence:
      ...

Here I need to know how many words from the array A and array B are used in sentence.

The output here should be:
There is 1 word from A & 1 word from B in the sentence

Can you help me out please?

Why does this code result in an infinite loop? Also, how can I completely restart my number baseball game?(How can I re-generate random numbers?)

I'm new to C, and is trying to make a number baseball game. The functions I wish to include are, when I press x, the game ends, and when I press m, a series of instructions are printed. Finally, I've added a re-game function, so that when I press the number 1 after the game ends, one could play it again. The number baseball game itself works fine, but whenever I press x or m, the program goes into an infinite loop. I can't figure out what's wrong. Also, when I use the re-game function, my program works, but it doesn't generate a new set of numbers, and just uses the set it used before. Could you please help??

#include <stdio.h>
#include <time.h>
#include <windows.h>

int main (void) {
    
    int computerBall[4];
    int X = 0;
    int i, j = 1;
    int userBall[4];
    int x, m;
    int o, q;
    int flag = 1;
    q = 1;
    x = 2;
    m = 3;
    srand (time (NULL));

    computerBall[0] = rand () % 31;

    while (computerBall[0] == 0)
        computerBall[0] = rand () % 31;

    for (i = 1; i < 4; i++) {
        computerBall[i] = rand () % 31;

        while (q == 1) {
            flag = 0;
            for (j = 0; j < i; j++) {
                if (computerBall[j] == computerBall[i]) {
                    computerBall[i] = rand () % 31;
                    flag = 1;
                    break;
                }
            }
            if (flag == 0)
                break;
        }
    }                           // Code to Generate Random Numbers
    while (q == 1) {
        printf ("Choose 4 numbers between 1 and 30\n");
        printf ("Press x to exit, and press m for help\n");
        while (q == 1) {
            printf ("Input numbers:");
            scanf_s ("%d %d %d %d\n", &userBall[0], &userBall[1],
                     &userBall[2], &userBall[3]);
            //To prevent numbers outside of range
            if (userBall[0] < 1 || userBall[0] > 30 || userBall[1] < 1 || 
                userBall[1] > 30 || userBall[2] < 1 || userBall[2] > 30 || 
                userBall[3] < 1 || userBall[3] > 30) {
                printf ("You cannot input numbers outside the range.\n");
                continue;
            } //To prevent repeating numbers
            else if (userBall[0] == userBall[1] || userBall[0] == userBall[2] ||
                     userBall[0] == userBall[3] || userBall[1] == userBall[2] ||
                     userBall[1] == userBall[3] || userBall[2] == userBall[3]) {
                printf ("You cannot input same numbers at once.\n");
                continue;
            }
            else if (userBall[0] == m) {        //Doesn't work
                printf ("Help\n\n");
                continue;
            }
            else if (userBall[0] == x) {        //Doesn't work
                printf ("\n Game Over\n");
                printf ("Answer : ");
                printf ("%d ", X);
                printf ("Press 1 to restart");
                scanf_s ("%d", &o);
                if (o == 1)
                    continue;
                else
                    q = 0;
            }
            break;
        }
    }
    return 0;
}

vendredi 26 juin 2020

How can i split a column based on a condition in Python pandas

My one of the columns consist following data -

Numbers
100 K
25.20 K
250 K
33.45 K
250
100
10
5
4
1

In the above Numbers column, I want to multiply numbers with K with 1000's and the other numbers without K, i want to leave them as it is. How can I perform this conditional splitting and multiplication of column Numbers

Thanks in advance.

IF condition in where clause in SQL Server

I need to write the if condition in the where clause of SQL where I wanted to define that if one column is null then search in another column in a row. I have used union but it's making the query slow to execute, so help me write this statement in the proper way.

This is the code I have right now:

SELECT * 
FROM ACCOUNT 
WHERE (IF ACCOUNTID IS NULL THEN REF_ACC_ID = 12 ELSE ACCOUNTID = 12)