vendredi 29 janvier 2021

How can I get the int i value in the javaFX for loop into if else?

LocalDate rst = LocalDate.of(2003, 1, 1);

LocalDate bdDate = dtDogum.getValue();

    for (int i = 0; i < personal.size(); i++) { 
        if (bdDate.isAfter(rst)) {
            hatamesajı.setTitle("Error");
            hatamesajı.setHeaderText("Personnel to be registered are under the age of 18");
            hatamesajı.show();
            return;
        }       
    }

I am coding a personnel registration program. A user under 18 gives a warning when registered. However, even though the first registration is under the age of 18, it does not give any warning. I think it is because I cannot place the int i value inside the for loop in the if block. How can I place it?

Bash scripting | Linux | if else statement [closed]

I'm having a bash script and it includes few config commands.

For example,

Command1 Command2 Command3 Command4 and so on...

The first time when the script is run, all the commands (Suppose there are 10 commands in that script) in that script should get executed. But after the first time, when the script get executed, only one command should get executed (For example, command5 only). How can I do this? Can I use "if statement" here? And how?

What is faster: adding a key-value pair or checking for the existence of a key?

Imagine a CSV file with 3 columns: individual name, group name, group ID. Obviously column 1 is different for every line while column 2 and 3 can be the same as before (every group name has an individual ID though). This is not sorted in any way.

For reasons I'm creating a dict to save: group ID (key) --> group name (value). Now what is faster of the following variants?

  1. checking if that key already exists and only saving if not.

    if ID not in group_dict:
       group_dict[ID] = name 
    
  2. just saving it every time again (replacing the value, which is the same anyway).

    group_dict[ID] = name 
    

Multi-operator Calculator With C

I want to make a calculator which takes 3+4/4*6 kinda input and I need to write it in C programming. I take numbers and operations separately from user, then I put numbers and operators to separate arrays. Then I wanted to loop through arrays respectively to do operations -I know operator priority is not considered- but It is not giving me correct result at all. To determine opeators, first I used switch case then tried If statement but both of them gave wrong results. I am gonna share both of them. How I can re-arrange this code to get correct result ?

By correct result I mean e.g 3+4/1*8 = 56

CODE WITH IF STATEMENT

#include <stdio.h>
int main(void){
    printf("You will enter your operations in number - operator order.\n");
    printf("How many numbers do you have to calculate?");
    int how_many; //how many numbers the user has in his/her whole operation
    scanf("%d",&how_many);
    int number_entry = how_many; //for example 1+2*9/5 has 4 numbers
    int operator_entry = how_many - 1; //and 3 operators
    int *number_in; //for every number as 1, 2 ,9 ,5 according to example
    //int *last;
    char *operator_in; // and for every operator as +, * , / according to example
    int numbers[number_entry];
    int ops[operator_entry];
    int j;
    int result;
    //int k;
    printf("\n");
    for(j=0;j<operator_entry;j++){
        printf("Enter the number : ");
        scanf("%d",&number_in);
        numbers[j] = number_in;
        printf("Enter the operation as + for add, - for substract, * for multiply and / for division: ");
        scanf(" %c", &operator_in);
        ops[j] = operator_in;
    };
    printf("Enter the number : ");
    scanf("%d" , &number_in);   
    numbers[number_entry-1] = number_in;
        
    
    int loop;

//wrote these loops to see if arrays works correctly or not

    for(loop = 0; loop < number_entry; loop++)
        printf("%d ", numbers[loop]);
    
    for(loop = 0; loop < operator_entry; loop++)
        printf(" %c", ops[loop]);
        
    for(loop = 0; loop<number_entry;loop++){
        
        if(ops[loop] == "+"){
            
            result = numbers[2*loop] + numbers[(2*loop)-1];
            
        }else if(ops[loop] == "-"){
            
            result = numbers[2*loop] - numbers[(2*loop)-1];
            
        }else if(ops[loop] == "*"){
            
            result = numbers[2*loop] * numbers[(2*loop)-1];
        }
        else if(ops[loop] == "/"){
            
            result = numbers[2*loop] / numbers[(2*loop)-1];
        }
        
    };
    
    printf("\n");
    
    printf("Result: ");
    
    printf("%d", result);
    
    return 0;
}

WITH SWITCH CASE

#include <stdio.h>
int main(void){
    printf("You will enter your operations in number - operator order.\n");
    printf("How many numbers do you have to calculate?");
    int how_many; //how many numbers the user has in his/her whole operation
    scanf("%d",&how_many);
    int number_entry = how_many; //for example 1+2*9/5 has 4 numbers
    int operator_entry = how_many - 1; //and 3 operators
    int *number_in; //for every number as 1, 2 ,9 ,5 according to example
    //int *last;
    char *operator_in; // and for every operator as +, * , / according to example
    int numbers[number_entry];
    int ops[operator_entry];
    int j;
    int *result = 0;
    //int k;
    printf("\n");
    for(j=0;j<operator_entry;j++){
        printf("Enter the number : ");
        scanf("%d",&number_in);
        numbers[j] = number_in;
        printf("Enter the operation as + for add, - for substract, * for multiply and / for division: ");
        scanf(" %c", &operator_in);
        ops[j] = operator_in;
    };
    printf("Enter the number : ");
    scanf("%d" , &number_in);   
    numbers[number_entry-1] = number_in;
        
    
    int loop;

//wrote these loops to see if arrays works correctly or not

    for(loop = 0; loop < number_entry; loop++)
        printf("%d ", numbers[loop]);
    
    for(loop = 0; loop < operator_entry; loop++)
        printf(" %c", ops[loop]);
        
    for(loop = 0; loop<number_entry;loop++){
        switch(ops[loop]){
            case '+':
                result = numbers[2*loop] + numbers[(2*loop)-1];
                break;
            case '-':
                result = numbers[2*loop] - numbers[(2*loop)-1];
                break;
            case '*':
                result = numbers[2*loop] * numbers[(2*loop)-1];
                break;
            case '/':
                result = numbers[2*loop] / numbers[(2*loop)-1];
                break;      
        };

        resulu = result + result;
    };
    
    printf("\n");
    
    printf("Result: ");
    
    printf("%d", result);
    
    return 0;
}

Create new, grouped conditional variable in R

Im trying to create a new variable, which is grouped and has several conditions underlying it. E.g. using the iris dataset:

I want to add a new column, grouped by species and contains information about the size. The outcome is expected to be a factor variable with levels "small", "medium" and "large".

Here's an example:

data(iris)
data %>% 
  dplyr::group_by(Species) %>% 
  dplyr::mutate(Size =
                  if_else(Sepal.Length <= 5.1 ~ "small",
                          Sepal.Length <= 5.8 & > 5.1 ~ "medium",
                          Sepal.Length > 5.8 ~ "large")
                ) 

It doesn't work. Apparently there's

an unmatched closed bracket ")"

Now, I am aware of other solutions, e.g. using Base R's ifelse function, but I'm trying to find an elegant way to express myself using tidyverse-style code. Can anyone help? I would be happy about any solutions using tidyverse code. Doesn't matter what function it is.

how do I take out index and not all of the char in a string?

I want to instruct the computer so that if a character in string 'a' is also a character in string 'b', to remove that character (just that one character), however what the code below is doing is removing all of the same characters from the string at one go. Any help?

I have tried to use a[I] and b[I] to replace the index for each loop, but then the code below needs to be changed entirely, because I can no longer do 'for I in a' and 'if I in b', as I need integer references for the 'i's.

e.g. a = "askhfidshf" b = "fhsdhfojej"

def permutation_checker(a,b):
    if a==b:
        print("a is a permutation of b")

    if a != b:
        while a and b:
            for i in a:
                if i in b:
                    new_a = a.replace(i,"")
                    print(new_a)
                    a = new_a
                    new_b = b.replace(i,"")
                    print(new_b)
                    b =new_b

                    if (a=="") and b == "":
                        print("a is a permutation of b")

Does Flutter has the possibility to use only a if condition inside the build method?

I worked with flutter for almost a year and since it wasn't production-ready for web development I needed to switch to react.

To render object conditionally inside my build method I had to put an if-else statement rendering my custom widget if the condition was met or a random empty container in the other case.

@override
Widget build(BuildContext context) {
 return Container(
   child: condition == true 
       ? new MyCustomWidget() 
       : new Container() //Random empty container to fill the else statement
 )
)

Even if this doesn't cause any rendering problem, it's still can be ambiguous for someone reading the code. After working with react I saw that it provides a way to display only the if block using the following notation:

<div>
  {condition == true && <MyCustomReactComponent/>} //this block is displayed only if the condition is met
</div>

I was guessing if flutter has a way to do something similar.