lundi 28 décembre 2020

How to use for loop when I have different length in R? [duplicate]

I have two dataframes df1 and df2 and `df2':

df1 = structure(list(Day = c(19L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L), 
    Month = c(9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 
    9, 9, 9, 9), Year = c(2004, 2004, 2004, 2004, 2004, 2004, 
    2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 
    2004, 2004, 2004, 2004), Date = structure(c(12680, 12681, 
    12681, 12681, 12681, 12682, 12682, 12682, 12682, 12682, 12682, 
    12682, 12683, 12683, 12683, 12683, 12683, 12683, 12683, 12683
    ), class = "Date")), row.names = c("text1", "text2", "text3", 
"text4", "text5", "text6", "text7", "text8", "text9", "text10", 
"text11", "text12", "text13", "text14", "text15", "text16", "text17", 
"text18", "text19", "text20"), class = "data.frame")

       Day Month Year       Date
text1   19     9 2004 2004-09-19
text2   20     9 2004 2004-09-20
text3   20     9 2004 2004-09-20
text4   20     9 2004 2004-09-20
text5   20     9 2004 2004-09-20
text6   21     9 2004 2004-09-21
text7   21     9 2004 2004-09-21
text8   21     9 2004 2004-09-21
text9   21     9 2004 2004-09-21
text10  21     9 2004 2004-09-21
text11  21     9 2004 2004-09-21
text12  21     9 2004 2004-09-21
text13  22     9 2004 2004-09-22
text14  22     9 2004 2004-09-22
text15  22     9 2004 2004-09-22
text16  22     9 2004 2004-09-22
text17  22     9 2004 2004-09-22
text18  22     9 2004 2004-09-22
text19  22     9 2004 2004-09-22
text20  22     9 2004 2004-09-22

df2 = structure(list(days = structure(c(12680, 12681, 12682, 12683, 
 12684, 12685, 12686, 12687, 12688, 12689, 12690, 12691, 12692, 
 12693, 12694), class = "Date"), week = c(3, 3, 3, 4, 4, 4, 4, 
 4, 4, 4, 4, 4, 1, 1, 1)), row.names = c(NA, -15L), class = 
 "data.frame")

         days week
1  2004-09-19    3
2  2004-09-20    3
3  2004-09-21    3
4  2004-09-22    4
5  2004-09-23    4
6  2004-09-24    4
7  2004-09-25    4
8  2004-09-26    4
9  2004-09-27    4
10 2004-09-28    4
11 2004-09-29    4
12 2004-09-30    4
13 2004-10-01    1
14 2004-10-02    1
15 2004-10-03    1

As you can see df1 is longer than df2 and contains all the "dates" in df2. What I would like to get is a code that says: if the date is df1 is the same as the date in df2, then give me the weeknumber. I manage to do it individually:

if (df1$Date[1] == df2$days[1]) df2$week[1]

if (df1$Date[2] == df2$days[2]) df2$week[2] 

if (df1$Date[3] == df2$days[2]) df2$week[2] # this is the issue: df1 is longer than df2 since it repeats dates more than ones

if (df1$Date[4] == df2$days[2]) df2$week[2] # same as above
...

The problem is that when I try to loop for every element in df1 and df2 I fail since the two have different length since df1 repeats the same date n times. So I would need a loop that takes care of the repetions of dates in df1. Yet I got stuck here:

for (i in length(# don't know how to handle the different length)) {
if (df1$Date[1] == df2$days[1]) df2$week[1]
}

Can anyone help me sort this out?

Thanks!

Python syntax for “if a and b or a and c” where a, b and c are words in a sentence

I have a python script, and I should check if two words are present in a sentence separated by dash

test_dash_sentence="wordtest-wordtest-word3-word4"

if ("word1" and "word2") or ("word1" and "word3") in test_dash_sentence:
    print("FOUND")
else:
    print("NOT FOUND")

The issue I have is that is return me "FOUND" even if word1 is not present, so it should return me "NOT FOUND"

I have tried different models (with simple quotation, without parentheses) and it seems to work if I only look for one word in the sentence, but when I look for two words, it doesn't work.

Python function return None. Can someone explain Why? [duplicate]

hello guys I wrote a function for this task:

Task

You've just moved into a perfectly straight street with exactly n identical houses on either side of the road. Naturally, you would like to find out the house number of the people on the other side of the street.

The street looks something like this:

Street
1|   |6
3|   |4
5|   |2
  • Evens increase on the right;
  • Odds decrease on the left. H
  • House numbers start at 1 and increase without gaps.
  • When n = 3, 1 is opposite 6, 3 opposite 4, and 5 opposite 2.

Example

Given your house number address and length of street n, give the house number on the opposite side of the street.

over_the_road(1, 3) = 6

My solution:

def over_the_road(address, n):
    even = list(range(n + 1))
    for number in even:
        if number % 2 == 0:
            even.remove(number)
    odds = list(range(n + 1))
    odds.pop(0)
    for number in odds:
        if not number % 2 == 0:
            odds.remove(number)
    odds.reverse()
    if address == even:
        return odds[even.index(address)]
    elif address == odds:
        return even[odds.index(address)]

Can someone please explain why is my function returning none?

Java - If-statement returns false

I've got a button "addspieler" and when I first press it, the second if statement returns true and view.addbox is executed (I want to add an JComboBox every time the button is pressed). But after the execution the if-statement returns always "false" and view.addbox is not going to be executed anymore. I don't know why. Thanks for advice!

public void actionPerformed(ActionEvent e) {

            if (e.getSource() == view.button1) {
                view.zeige_AuswahlNeuesSpiel(modell.getSpieler());

            }

            if (e.getSource() == view.button_addSpieler) {
                view.add_Box(modell.spieler_Anzahl_NeuesSpiel, modell.getSpieler());

            }

    }

view.addbox =


 public void add_Box(int anz_spieler, ArrayList<Spieler> spieler_Liste)
        {
            if(ct >= anz_spieler)
                return;



            jcombo_Liste = erstelle_Box(anz_spieler);

            GridBagConstraints gbc = new GridBagConstraints();
            GridBagLayout layout = new GridBagLayout();
            setLayout(layout);


            jcombo_Liste.get(ct).addItem(spieler_Liste.toArray());






            button_addSpieler = new Button("Spieler hinzufügen");
            button_addSpieler.addActionListener(darts_controller);

            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 0;
            gbc.gridy = 0;
            f1.add(button_addSpieler,gbc);


            gbc.gridx = ct;
            gbc.gridy = 1;
            f1.add(jcombo_Liste.get(ct),gbc);

            ct = ct+1;
            f1.repaint();


        }

How do I add a command (tkinter) in an if statement?

I have created an if statement to check if the username and password and username entered is the same to the username and password in the textile. I have been able to display if the data inputted is correct or not. I now want to add a command into the if statement e.g. command = main_menu if the data inputted is correct (I am using tkinter)

This is the if code I have so far:

def validation(username1, password1):
    file = open("Accounts.txt", "r")
    for line in file:
        line = line.split("\n")
        line = line[0].split(" ")

        if username1 == line[0] and password1 == line[1]:
            valid = Label(root, text="Data: Found")
            valid.place(x=290, y=130)
        else:
            valid = Label(root, text="Data: Not found")
            valid.place(x=280, y=130)

    file.close()

Any ideas on how to do this?

Thanks xx

weird syntax Javascript [duplicate]

I saw this piece of code in a book :

let book = {
  subtitle : {
    title : "nothing",
    }
}
let len = undefined;

// the first one
if (book) { 
  if (book.subtitle) len = book.subtitle.length;
}

// the second one (which i have no idea what is)
len = book && book.subtitle && book.subtitle.length

can anyone explain how does the second done work?

IF always returns FALSE after comparing data of a database

I'm trying to create a website that has a login page. I had connected a database which contains everyone name and passwords (we are 10 people, so I put all data on a database). After the login it should redirect you to another page, but it always returns false; despite this, I inserted all data correctly. Here's the code of the "if", thanks in advance for the help.

session_start(); 
                  if ($ArrRow["NomeUtente"]==$_POST["NomeUtente"] and $ArrRow["Password"]==$_POST["Password"]) {
                  $_SESSION["login"]=true;
                  echo "Login corretto";
                  echo "<meta http-equiv=refresh content=2;url=https://tpvgruppo.altervista.org/turni-settimanali/>";
                } else {
                  $_SESSION["login"]=false;
                  echo "Login non corretto, riprova.";
                  echo "<meta http-equiv=refresh content=2;url=https://tpvgruppo.altervista.org/area-riservata/>";
             }