lundi 1 novembre 2021

i need to find the distance between two characters in string js. i have a solution but i can't understand the snippet of code related to if statement

The task was to write a function subLength() that takes 2 parameters, a string and a single character. The function should search the string for the two occurrences of the character and return the length between them including the 2 characters. If there are less than 2 or more than 2 occurrences of the character the function should return 0.

const subLength = (str, char) => {
let charCount = 0;
let len = -1;

for (let i=0; i<str.length; i++) {
  if (str[i] == char) {
    charCount++;
    if (charCount > 2) {
      return 0;
    }
// could somebody explain why -1 is equal to len and then len is reassigned to i???       
if (len == -1) {
      len = i;
    } else {
      len = i - len + 1
    }
  }
}
if (charCount < 2) {
  return 0;
}

return len;
};

java return if else [duplicate]

package org.otsi.test;

public class Test {

    static int count = 0;

    public static void main(String[] args) {
        System.out.println("Main Started");
        if (count < 3) {
            count++;
            main(null);
            System.out.println("After Main(null)");
        } else {
            System.out.println("In else");
            return;
        }
        System.out.println("Hello World!");

    }

}

Output :

Main Started
Main Started
Main Started
In else
After Main(null)
Hello World!
After Main(null)
Hello World!
After Main(null)
Hello World!

How the return is behaving here? Why Hello World printed thrice?

Exit from loop on while true

i need to understand why when i run this script this happens.

The script selects n number of words from the list of keywords and starts making the request to the API of the service and once it has obtained the answer it posts the result on telegram.

To make a new request to the API, it must wait a number of minutes specified in the PAUSE_MINUTES variable

The script pauses at the hours defined by the MIN_HOUR and variables MAX_HOUR

I would like that once made the request to the API and after waiting for the minutes I have established, it would make a new request with new keywords.

What happens instead is that if the first request was made with the word "Bosch", the following ones will also be made with this word.

In short, the script never leaves the loop.

How can I modify the script in such a way as to obtain a new keyword for each new request, respecting the waiting times between one call and another and the night sleep time?

from amazon_api import search_items
from create_messages import create_item_html
import time
from datetime import datetime
from itertools import chain
import random


# ***** Telegram TOKEN and parameters Definition *****
TOKEN = 'mytoken'
CHANNEL_NAME = '@mychannel'

# ***** BOT ACTIVITY TIME *****
MIN_HOUR = 8 
MAX_HOUR = 24 
PAUSE_MINUTES = 5

# ***** SEARCH CATEGORY DEFINITION *****
CATEGORY = "ToolsAndHomeImprovement"

# Create the bot instance
bot = telegram.Bot(token=TOKEN)

# Search keywords definition
keywords = ["Bosch", "Beta", "Dewalt", "USAG", "Karcher", "Bahco"]
#print (keywords)

# run bot function
def run_bot(bot, keywords):
    random.shuffle(keywords)
while True:
    try:
        items_full = []
        # shuffling keywords list
        random.shuffle(keywords)
        number_of_keywords = 1
        for el in keywords[:number_of_keywords]:
            print (keywords[:number_of_keywords:])    
            for i in range(1, 10):
                items = search_items(el, "ToolsAndHomeImprovement", item_page=i)
                time.sleep(1)
                items_full.append(items)
        items_full = list(chain(*items_full))
        print(f'{5 * "*"} Requests Completed {5 * "*"}')
        
        random.shuffle(items_full)
        random.shuffle(items_full)
        res = create_item_html(items_full)
        while len(res) > 3:
            now = datetime.now().time()
            if MIN_HOUR - 2 < now.hour < MAX_HOUR - 2:
                try:
                    print(f'{5*"*"} Sending posts to channel {5*"*"}')
                    
                    bot.send_message(chat_id="@mychannel", text=res[0], reply_markup=res[1],parse_mode=telegram.ParseMode.HTML)
                    bot.send_message(chat_id="@mychannel", text=res[2], reply_markup=res[3],parse_mode=telegram.ParseMode.HTML)
                    res.pop(0)
                    res.pop(0)
                    res.pop(0)
                    res.pop(0)

                except Exception as e:
                    print(e)
                    res.pop(0)
                    res.pop(0)
                    res.pop(0)
                    res.pop(0)
                    continue

                time.sleep(60* PAUSE_MINUTES)

            else:
                print(f'{5 * "*"} Inactive Bot, between  {MIN_HOUR}AM and {MAX_HOUR}PM {5 * "*"}')
                time.sleep(60 * 5)
    except Exception as e:
        print(e)


# running bot
run_bot(bot, keywords)

Multiple IF statements using dates and blank cells

I currently have a spreadsheet showing when work needs to be complete by (Finish No later than - N2) and the date it was completed by (Actual Finish Date - L2). In the formula I have written it will bring back the words Non Compliant or Compliant if the Actual Finish date was before or after the Finish No later than.

=IF(N2>$L$2,"Non Compliant - Complete","Compliant - Complete")

This works fine but I also need to add two further words based on whether there is any date in the Actual Finish Date column.

Can someone help me add an extra If function to the end of mine with following rules?

If the date is blank and the Finish No later than date is before today then it returns - Non Compliant - Open or If the date is blank and the Finish No later than date is grater than or equal to today then it returns - Compliant - Open

Thank you for looking

How to fix this illegal star of expression?

enter image description here

/Latihan1.java:13: error: illegal start of expression } else if(umur >=26 && <= 50){ ^ 1 error

How to edit columns values based on another value?

My data fame looks like this:

val    type
10     new
70     new
61     new
45     old
32     new
77     mid
11     mid

For values in column "type", if value is new, I want to edit it depending on val column. If value in val is =< 20, it must be new-1, if > 20 and < 50, it must be new-2,if >= 50, it must be new-3. So desired result is:

val    type
10     new-1
70     new-3
61     new-3
45     old
32     new-2
77     mid
11     mid

How to do that?

My program won't stop executing the if statement yet the condition is not met

It's supposed to be a multiplication table from 1 to 6 on the x axis and 1 to 5 on the y axis, so I have this if statement that is supposed to jump to the nezt line once the multiplication reaches 6 but it keeps on executing even though I reset the condition that is to be met within it.

#include <stdio.h>
#include <stdlib.h>
int main(){
int mult = 1;
int check = 0;
int res;
while(mult != 6){


        if (check <= 6){
            res = mult * check;
            printf("%d ",res);
            check ++;
        }
        if (check > 6 ){
            printf("\n ");
            check = 0;
        }

}}