Не было бы легче проверить корни куба? Начните с 20 (20 ** 3 = 8000) и до 30 (30 ** 3 = 27000). Затем вам нужно проверить менее 10 целых чисел.
for i in range(20, 30):
print("Trying {0}".format(i))
if i ** 3 > 12000:
print("Maximum integral cube root less than 12000: {0}".format(i - 1))
break
Следующее добавит неделю года к вводу строк в формате год-неделя и вернет вектор дат в виде символа. Функция lubridate package days () добавит даты, соответствующие концу соответствующей недели. Заметьте, к примеру, что я добавил дополнительный регистр в вашей переменной 'ex' к 52-й неделе, и он возвращает 31 декабря
library(lubridate)
ex <- c('2012-41','2016-4','2018-52')
dates <- strsplit(ex,"-")
dates <- sapply(dates,function(x) {
year_week <- unlist(x)
year <- year_week[1]
week <- year_week[2]
start_date <- as.Date(paste0(year,'-01-01'))
date <- start_date+weeks(week)
#note here: OP asked for beginning of week.
#There's some ambiguity here, the above is end-of-week;
#uncommment here for beginning of week, just subtracted 6 days.
#I think this might yield inconsistent results, especially year-boundaries
#hence suggestion to use end of week. See below for possible solution
#date <- start_date+weeks(week)-days(6)
return (as.character(date))
})
Выход:
> dates
[1] "2012-10-14" "2016-01-29" "2018-12-31"
И просто получить месяц из этих полных дат:
month(dates)
Доходность:
> month(dates)
[1] 10 1 12