Why does Python return -1 for string.rfind(last character of string, 0, len(string) – 1)?

For Python str.rfind(sub[, start[, end]]) method, I think these are the default values for the start and end parameters: start = 0 end = the index of the last character of the string, which is equivalent to the length of the string minus 1. However, Python returns different results for lines 3 and 4 below.… Read More Why does Python return -1 for string.rfind(last character of string, 0, len(string) – 1)?

Find location of a character and write location to a data frame in r

Suppose this dataframe: col1<- c(‘a_b_c_fv’, ‘ff_g1_h_fv’,’e_ii_jjjj_fv’ ) df <- data.frame(col1) Want to find the location of the third occurrence of the underscore _ and then write that location back to the data frame. So final result would look like: df$location <- c(6,8,10) How can I do this pleaase? I have tried unlist(gregexpr(‘_’, df$col1))[3] but I… Read More Find location of a character and write location to a data frame in r

How to define a macro that take the file's name and read, write to same name but different extensions using freopen()

Im trying to create some #define in c++, but now im stuck in this part #include<bits/stdc++.h> #define FASTRW ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define IO(filename) freopen(filename + ".inp", "r", stdin); freopen(filename + "out", "w", stdout) using namespace std; int main(){ FASTRW; IO("lmao"); return 0; } the first define worked just fine, but my vs code showed the… Read More How to define a macro that take the file's name and read, write to same name but different extensions using freopen()

Why NULL terminated string's length is smaller than expected in C?

There is code: #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char *str = calloc(3, sizeof(char)); str[2] = ‘\0′; printf("%ld", strlen(str)); free(str); } Expected output 2 Actual output 0 Why? As far as I understood strlen counts symbols before \0. Maybe str[0] and str[1] are already ‘\0’? >Solution : calloc() initializes allocated memory with… Read More Why NULL terminated string's length is smaller than expected in C?

Replace words in string with values of string array

I have the following string with placeholders ‘test {{1}} this {{2}} {{3}}’ these placeholders later should be replaced in same order by the values from input fields [‘now’, ‘code’, ‘block’] so the final string looks like this ‘test now this code block’ what’s the best approach here? >Solution : You can use .replace() on your… Read More Replace words in string with values of string array

How replace just leading number for a string where those characters appear also as unit

I have the following vector vector <- c(31, 41, 51, 61, 32, 42, 52, 62, 33, 43, 53, 63, 34, 44, 54, 64, 35, 45, 55, 65, 36, 46, 56, 66) I would like just to replace the leading number without convert that number when it is found as unit digit str_replace_all(vector, c(‘3’ = ‘Air’,… Read More How replace just leading number for a string where those characters appear also as unit

How to take specific set of characters out of overall string and save to array or list?

I have a string with Unicodes inside of it, and I am trying to extract each unicode from the overall string and save it to a list/array.. This is the overall string: "test &#128311; test &#128153; test &#128313;" I want the following list: 1. &#128311; 2. &#128153; 3. &#128313; Right now I am trying the… Read More How to take specific set of characters out of overall string and save to array or list?