jeudi 1 avril 2021

Function to search a text document for a word in C

I wrote this function to search a text file (which currently just says "cube") for one of the words and then call the corresponding function. However, I am getting the 'Error: No Function Found.' message.

Could anyone help me in identifying my problem?

void read(){
    const char cube[4] = "cube";
    const char cone[4] = "cone";
    const char sphere[6] = "sphere";
    const char cylinder[8] = "cylinder";

    char line[1024] ;
    FILE* fp = fopen("instructions.txt", "r") ;
    while (fgets(line , sizeof(line) , fp )!= NULL)
    {
        if (strstr(line , cube)!= NULL){
            void cube();
        }
        else if (strstr(line , cone)!= NULL){
            void cone();
        }
        else if (strstr(line , sphere)!= NULL){
            void sphere();
        }
        else if (strstr(line , cylinder)!= NULL){
            void cylinder();
        }
        else {
            printf("Error: No Function Found./n");
        }
    }
    fclose(fp);
    free(line);
    return;
}

If class doesn't exist do this else ignore

Hi what I'm trying to do in Javascript is to find if a class '.promo' exists in the '.productDetails' div, if it does then no action needs to be taken, but if it doesn't exist then i want it to print the copy in the span tags. I know this can be done in jQuery but looking to do this in plain Javascript instead.

var elem = document.querySelector('.product-details');
if(!elem.classlist.contains('.promo')){
   document.querySelector('.title').innerHTML += "<span class="promo">Add Promo Banner</span>";
 }
.product-details {border: 1px black solid; margin: 10px 0}
<div class="product-details">
  <div class="title">Title</div>
  <div class="promo">Promo 20% off</div>
</div>

<div class="product-details">
  <div class="title">Title</div>
</div>

Conditional argument across vectors

I wish to change one of my column values relative to several columns under a condition. If columns y2010 to y2019 all have zeros, then the column slope_median has a zero, otherwise, leave it as it is.

My expected output:

# A tibble: 6 x 13
     id y2010 y2011 y2012 y2013 y2014 y2015 y2016 y2017 y2018 y2019 slope_median slope_sd
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>        <dbl>    <dbl>
1     1   0     0     0     0       0   0       0   0       0   0          0       0     
2     2   0.1   0     0.2   0.1     0   0.1     0   0       0   0          0.243   0.0676
3     3   0     0.3   0.3   0.2     0   0       0   0.1     0   0          0.212   0.0621
4     4   0     0     0     0       0   0       0   0       0   0.1        0.354   0.197 
5     5   0     0.4   0     0       0   0       0   0       0   0          0.516   0.156 
6     6   0     0     0     0       0   0       0   0       0   0          0       0   

What I have tried:

data$slope_median ifelse(data[,2:11] >0, data$slope_median, 0)

and

when(tt[,2:11] >0, tt$slope_median)

neither work as expected.

reproducible code:

structure(list(id = c(1, 2, 3, 4, 5, 6), y2010 = c(0, 0.1, 0, 
0, 0, 0), y2011 = c(0, 0, 0.3, 0, 0.4, 0), y2012 = c(0, 0.2, 
0.3, 0, 0, 0), y2013 = c(0, 0.1, 0.2, 0, 0, 0), y2014 = c(0, 
0, 0, 0, 0, 0), y2015 = c(0, 0.1, 0, 0, 0, 0), y2016 = c(0, 0, 
0, 0, 0, 0), y2017 = c(0, 0, 0.1, 0, 0, 0), y2018 = c(0, 0, 0, 
0, 0, 0), y2019 = c(0, 0, 0, 0.1, 0, 0), slope_median = c(0.474975518924233, 
0.243454068541322, 0.212280103001613, 0.35383141799496, 0.516334127008962, 
0.356965572683185), slope_sd = c(0.111160151030104, 0.0675751738339188, 
0.0621022972843367, 0.196846417185408, 0.155712046330888, 0.102009447727955
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))

How can I solve if-else problem in for loops python3?

list = [["alex","tiffany"],["richard","bella"],["bruno","jessica"]]

enter = input("enter name: ")

for i in list:

    if enter == i[0]:

        print(i[1])

    else:

        print("the name that you searched for is absent!")

    break

Output 1:

enter name: alex

->tifanny

Output 2:

enter name: richard

->the name that you searched for is absent!

I want to print "bella" but my program doesn't do this. How can I solve this problem?

Why is this if statement not fufilled when all the conditions are met?

private int getNextNodesDirection(Pair<Integer, Integer> current, Pair<Integer, Integer> nextNode, Robot target) {
    System.out.println("current x:" + current.getSecond());
    System.out.println("current y:" + current.getFirst());
    System.out.println("next x:" + nextNode.getSecond());
    System.out.println("next y:" + nextNode.getFirst());

    if (target.getCurrentDirection().equals(IWorld.Direction.UP)) {
        //if y increases and x stays the same
       
        if (nextNode.getFirst() > current.getFirst() && nextNode.getSecond().equals(current.getFirst())) {
            return 0;
        }
        // if x increases and y stays the same
        else if (nextNode.getSecond() > current.getSecond() && nextNode.getFirst().equals( current.getFirst())) {
            return 1;
        }




while (index < endIndex){
        int fSteps = 0;
        int direction = getNextNodesDirection(nodeList.get(index), nodeList.get(index+1), target);
        if (direction == 1){
            cost+=1;
            target.handleCommand(new RightCommand());
        }
        else if (direction == 2){
            cost+=2;
            target.handleCommand(new RightCommand());
            target.handleCommand(new RightCommand());
        }
        else if (direction == 3){
            cost+=1;
            target.handleCommand(new LeftCommand());
        }
        while (direction == 0){
            fSteps += 1;
            cost+=fSteps;
            index+=1;
            if(nodeList.get(index).getSecond() == (end.getX()) && nodeList.get(index).getFirst() == end.getY()){
                break;
            }
            direction = getNextNodesDirection(nodeList.get(index), nodeList.get(index+1), target);

So when the program runs it enters into getNextNodesDirection and executes as expected but on the second time round, although all the conditions are fulfilled it just goes passed the statement. Why? I would like it to return 0 while nextNode.getFirst() is greater than current.getFirst and nextNode.getSecond() is equal to current.getSecond()

I've screenshotted here in IntelliJ, while debugging, you can clearly see that y is increasing and x staying the same yet it passes the "fulfilled" condition and carries on. Why?

The language is Java 11, OS is Debian on a virtual machine.

Why does the if statement not recognize what is in the list?

I want the if statement to correctly compare what is in the list to what the input is in the enter_password variable.

user_password = []
password = input("Enter what you would like your password to be. \n ENTER: ")
user_password.append(password)
print(f"Password created! Your password is: {user_password}")
enter_password = input("Enter password: ")
if enter_password == user_password:
    print("Welcome")
else:
    print(f"{user_password} is wrong password")

Python - Problem with checking if element is in list

i came across a problem in Python and since i do this for fun and not as a professional i dont get why this does not work. I have a list that contains other lists with two numbers in each one. This function should check wether the element +1 and the element -1 are elements of my tp list. If yes, append them to stack. Code:

def check():
    tp = [[0, 1], [1, 1], [1, 2], [2, 2], [2, 3], [3, 3]]
    stack = []
    for i in tp:
        a = i[0]
        b = i[1]
        if [(a - 1), (b - 1)] and [(a + 1), (b + 1)] in tp:
            stack.append(i)
    return stack

Unfortunately the output is:

[[0, 1], [1, 1], [1, 2], [2, 2]]

[1, 2] is correct because [0, 1] and [2, 3] are elements of tp.

[2, 2] is correct because [1, 1] and [3, 3] are elements of tp.

Why does this function give me the other two also? For instance: first element of tp is [0,1] -> [-1,0] and [1,2] should be the calculated outputs but obviously [-1,0] is no in this list. Where is my (probably obvious) mistake? Thanks in advance.