生成以000000为起始值的数字序列

问题描述

我正在编写一个函数,该函数以dhhmmss(day-hour-minutes-seconds)的格式开始和结束一天,并计算开始和结束{{1 }}。
根据定义,开始dhhmmss为000000,结束hhmmss为235959。

我的函数必须仅采用开始hhmmss和结束d并计算这两者之间回文数的长度

这是我的做法

d

但是,由于行的原因,如果n1 = 0和n1 = 1,则上述功能将不起作用

Reverse.numberAsString <- function(x){ # Reverse using string manipulation

  x.out <- as.character(x)   # convert number to a character string 
  x.out <- unlist(strsplit(x.out,'')) # break the string up into a vector 
  x.out <- rev(x.out)   # reverse it 
  x.out <- paste(x.out,collapse='')  # join it back together 
  x.out <- as.numeric(x.out)  # turn it back to a number 
  return(x.out) 
}  

is.Palindrome <- function(x){
  x == sapply(x,Reverse.numberAsString)
 }      


palindrom_fun <- function(n1,n2){

  if (n1 > n2) { print('n1 cannot be > n2') 

   } else {

   n1.mod <- as.numeric(paste(c(n1,"000000"),collapse = ""))
   n2.mod <- as.numeric(paste(c(n2,"235959"),collapse = ""))

   x <- seq(from = n1.mod,to = n2.mod,by = 1)
  palindrome_number <- x[is.Palindrome(x)]
  length.palindrom <- length(palindrome_number)

  return(length.palindrom)
 }
}

 palindrom_fun(1,2)
 # 1236

因为R无法创建从0000000到1235959的数字序列。在这种情况下如何使函数起作用?

解决方法

您可以使用:比较字符向量的头尾和尾巴(因为headtail很慢)。对于所需的序列,您可以使用sprintf生成前导零。

isPalindrome <- Vectorize(function(x) {
  s <- el(strsplit(as.character(x),""))
  ll <- length(s)
  l2 <- pmax(floor(ll / 2),1)
  # out <- all(head(s,l) == rev(tail(s,l)))  ## slower
  out <- all(s[1:l2] == s[ll:(ll - l2 + 1)])
  return(out)
})

## Test
x <- c("0000000","1123456","1231321","0000","1234","11","12","1")
isPalindrome(x)
# 0000000 1123456 1231321    0000    1234      11      12       1 
#    TRUE   FALSE    TRUE    TRUE   FALSE    TRUE   FALSE    TRUE

在下面的palindromFun函数中,我将实际的palindroms添加为属性,以便它们由函数返回。 (要关闭此行为,只需用## mark注释掉该行)。

palindromFun <- function(n1,n2) {
  if (n1 > n2) { 
    print('n1 cannot be > n2') 
  } else {
    tm <- sprintf("%06d",0:235959)
    dy <- n1:n2
    r <- paste0(rep(dy,each=length(tm)),tm)
    pd <- isPalindrome(r)
    out <- sum(pd)
    out <- `attr<-`(out,"palindroms",r[pd])  ## mark
    return(out)
  }
}

结果1

r1 <- palindromFun(n1=0,n2=1)
r1
# [1] 472
# attr(,"palindroms")
# [1] "0000000" "0001000" "0002000" "0003000" "0004000" "0005000" "0006000"
# [8] "0007000" "0008000" "0009000" "0010100" "0011100" "0012100" "0013100"
# [15] "0014100" "0015100" "0016100" "0017100" "0018100" "0019100" "0020200"
# [22] "0021200" "0022200" "0023200" "0024200" "0025200" "0026200" "0027200"
# [29] "0028200" "0029200" "0030300" "0031300" "0032300" "0033300" "0034300"
# [36] "0035300" "0036300" "0037300" "0038300" "0039300" "0040400" "0041400"
# [43] "0042400" "0043400" "0044400" "0045400" "0046400" "0047400" "0048400"
# [50] "0049400" "0050500" "0051500" "0052500" "0053500" "0054500" "0055500"
# [57] "0056500" "0057500" "0058500" "0059500" "0060600" "0061600" "0062600"
# [64] "0063600" "0064600" "0065600" "0066600" "0067600" "0068600" "0069600"
# [71] "0070700" "0071700" "0072700" "0073700" "0074700" "0075700" "0076700"
# [78] "0077700" "0078700" "0079700" "0080800" "0081800" "0082800" "0083800"
# [85] "0084800" "0085800" "0086800" "0087800" "0088800" "0089800" "0090900"
# [92] "0091900" "0092900" "0093900" "0094900" "0095900" "0096900" "0097900"
# [99] "0098900" "0099900" "0100010" "0101010" "0102010" "0103010" "0104010"
# [106] "0105010" "0106010" "0107010" "0108010" "0109010" "0110110" "0111110"
# [113] "0112110" "0113110" "0114110" "0115110" "0116110" "0117110" "0118110"
# [120] "0119110" "0120210" "0121210" "0122210" "0123210" "0124210" "0125210"
# [127] "0126210" "0127210" "0128210" "0129210" "0130310" "0131310" "0132310"
# [134] "0133310" "0134310" "0135310" "0136310" "0137310" "0138310" "0139310"
# [141] "0140410" "0141410" "0142410" "0143410" "0144410" "0145410" "0146410"
# [148] "0147410" "0148410" "0149410" "0150510" "0151510" "0152510" "0153510"
# [155] "0154510" "0155510" "0156510" "0157510" "0158510" "0159510" "0160610"
# [162] "0161610" "0162610" "0163610" "0164610" "0165610" "0166610" "0167610"
# [169] "0168610" "0169610" "0170710" "0171710" "0172710" "0173710" "0174710"
# [176] "0175710" "0176710" "0177710" "0178710" "0179710" "0180810" "0181810"
# [183] "0182810" "0183810" "0184810" "0185810" "0186810" "0187810" "0188810"
# [190] "0189810" "0190910" "0191910" "0192910" "0193910" "0194910" "0195910"
# [197] "0196910" "0197910" "0198910" "0199910" "0200020" "0201020" "0202020"
# [204] "0203020" "0204020" "0205020" "0206020" "0207020" "0208020" "0209020"
# [211] "0210120" "0211120" "0212120" "0213120" "0214120" "0215120" "0216120"
# [218] "0217120" "0218120" "0219120" "0220220" "0221220" "0222220" "0223220"
# [225] "0224220" "0225220" "0226220" "0227220" "0228220" "0229220" "0230320"
# [232] "0231320" "0232320" "0233320" "0234320" "0235320" "1000001" "1001001"
# [239] "1002001" "1003001" "1004001" "1005001" "1006001" "1007001" "1008001"
# [246] "1009001" "1010101" "1011101" "1012101" "1013101" "1014101" "1015101"
# [253] "1016101" "1017101" "1018101" "1019101" "1020201" "1021201" "1022201"
# [260] "1023201" "1024201" "1025201" "1026201" "1027201" "1028201" "1029201"
# [267] "1030301" "1031301" "1032301" "1033301" "1034301" "1035301" "1036301"
# [274] "1037301" "1038301" "1039301" "1040401" "1041401" "1042401" "1043401"
# [281] "1044401" "1045401" "1046401" "1047401" "1048401" "1049401" "1050501"
# [288] "1051501" "1052501" "1053501" "1054501" "1055501" "1056501" "1057501"
# [295] "1058501" "1059501" "1060601" "1061601" "1062601" "1063601" "1064601"
# [302] "1065601" "1066601" "1067601" "1068601" "1069601" "1070701" "1071701"
# [309] "1072701" "1073701" "1074701" "1075701" "1076701" "1077701" "1078701"
# [316] "1079701" "1080801" "1081801" "1082801" "1083801" "1084801" "1085801"
# [323] "1086801" "1087801" "1088801" "1089801" "1090901" "1091901" "1092901"
# [330] "1093901" "1094901" "1095901" "1096901" "1097901" "1098901" "1099901"
# [337] "1100011" "1101011" "1102011" "1103011" "1104011" "1105011" "1106011"
# [344] "1107011" "1108011" "1109011" "1110111" "1111111" "1112111" "1113111"
# [351] "1114111" "1115111" "1116111" "1117111" "1118111" "1119111" "1120211"
# [358] "1121211" "1122211" "1123211" "1124211" "1125211" "1126211" "1127211"
# [365] "1128211" "1129211" "1130311" "1131311" "1132311" "1133311" "1134311"
# [372] "1135311" "1136311" "1137311" "1138311" "1139311" "1140411" "1141411"
# [379] "1142411" "1143411" "1144411" "1145411" "1146411" "1147411" "1148411"
# [386] "1149411" "1150511" "1151511" "1152511" "1153511" "1154511" "1155511"
# [393] "1156511" "1157511" "1158511" "1159511" "1160611" "1161611" "1162611"
# [400] "1163611" "1164611" "1165611" "1166611" "1167611" "1168611" "1169611"
# [407] "1170711" "1171711" "1172711" "1173711" "1174711" "1175711" "1176711"
# [414] "1177711" "1178711" "1179711" "1180811" "1181811" "1182811" "1183811"
# [421] "1184811" "1185811" "1186811" "1187811" "1188811" "1189811" "1190911"
# [428] "1191911" "1192911" "1193911" "1194911" "1195911" "1196911" "1197911"
# [435] "1198911" "1199911" "1200021" "1201021" "1202021" "1203021" "1204021"
# [442] "1205021" "1206021" "1207021" "1208021" "1209021" "1210121" "1211121"
# [449] "1212121" "1213121" "1214121" "1215121" "1216121" "1217121" "1218121"
# [456] "1219121" "1220221" "1221221" "1222221" "1223221" "1224221" "1225221"
# [463] "1226221" "1227221" "1228221" "1229221" "1230321" "1231321" "1232321"
# [470] "1233321" "1234321" "1235321"

结果2

r2 <- palindromFun(n1=0,n2=2)
r2
# [1] 708
# attr(,"palindroms")
# [1] "0000000" "0001000" "0002000" "0003000" "0004000" "0005000" "0006000"
# [8] "0007000" "0008000" "0009000" "0010100" "0011100" "0012100" "0013100"
# [15] "0014100" "0015100" "0016100" "0017100" "0018100" "0019100" "0020200"
# [22] "0021200" "0022200" "0023200" "0024200" "0025200" "0026200" "0027200"
# [29] "0028200" "0029200" "0030300" "0031300" "0032300" "0033300" "0034300"
# [36] "0035300" "0036300" "0037300" "0038300" "0039300" "0040400" "0041400"
# [43] "0042400" "0043400" "0044400" "0045400" "0046400" "0047400" "0048400"
# [50] "0049400" "0050500" "0051500" "0052500" "0053500" "0054500" "0055500"
# [57] "0056500" "0057500" "0058500" "0059500" "0060600" "0061600" "0062600"
# [64] "0063600" "0064600" "0065600" "0066600" "0067600" "0068600" "0069600"
# [71] "0070700" "0071700" "0072700" "0073700" "0074700" "0075700" "0076700"
# [78] "0077700" "0078700" "0079700" "0080800" "0081800" "0082800" "0083800"
# [85] "0084800" "0085800" "0086800" "0087800" "0088800" "0089800" "0090900"
# [92] "0091900" "0092900" "0093900" "0094900" "0095900" "0096900" "0097900"
# [99] "0098900" "0099900" "0100010" "0101010" "0102010" "0103010" "0104010"
# [106] "0105010" "0106010" "0107010" "0108010" "0109010" "0110110" "0111110"
# [113] "0112110" "0113110" "0114110" "0115110" "0116110" "0117110" "0118110"
# [120] "0119110" "0120210" "0121210" "0122210" "0123210" "0124210" "0125210"
# [127] "0126210" "0127210" "0128210" "0129210" "0130310" "0131310" "0132310"
# [134] "0133310" "0134310" "0135310" "0136310" "0137310" "0138310" "0139310"
# [141] "0140410" "0141410" "0142410" "0143410" "0144410" "0145410" "0146410"
# [148] "0147410" "0148410" "0149410" "0150510" "0151510" "0152510" "0153510"
# [155] "0154510" "0155510" "0156510" "0157510" "0158510" "0159510" "0160610"
# [162] "0161610" "0162610" "0163610" "0164610" "0165610" "0166610" "0167610"
# [169] "0168610" "0169610" "0170710" "0171710" "0172710" "0173710" "0174710"
# [176] "0175710" "0176710" "0177710" "0178710" "0179710" "0180810" "0181810"
# [183] "0182810" "0183810" "0184810" "0185810" "0186810" "0187810" "0188810"
# [190] "0189810" "0190910" "0191910" "0192910" "0193910" "0194910" "0195910"
# [197] "0196910" "0197910" "0198910" "0199910" "0200020" "0201020" "0202020"
# [204] "0203020" "0204020" "0205020" "0206020" "0207020" "0208020" "0209020"
# [211] "0210120" "0211120" "0212120" "0213120" "0214120" "0215120" "0216120"
# [218] "0217120" "0218120" "0219120" "0220220" "0221220" "0222220" "0223220"
# [225] "0224220" "0225220" "0226220" "0227220" "0228220" "0229220" "0230320"
# [232] "0231320" "0232320" "0233320" "0234320" "0235320" "1000001" "1001001"
# [239] "1002001" "1003001" "1004001" "1005001" "1006001" "1007001" "1008001"
# [246] "1009001" "1010101" "1011101" "1012101" "1013101" "1014101" "1015101"
# [253] "1016101" "1017101" "1018101" "1019101" "1020201" "1021201" "1022201"
# [260] "1023201" "1024201" "1025201" "1026201" "1027201" "1028201" "1029201"
# [267] "1030301" "1031301" "1032301" "1033301" "1034301" "1035301" "1036301"
# [274] "1037301" "1038301" "1039301" "1040401" "1041401" "1042401" "1043401"
# [281] "1044401" "1045401" "1046401" "1047401" "1048401" "1049401" "1050501"
# [288] "1051501" "1052501" "1053501" "1054501" "1055501" "1056501" "1057501"
# [295] "1058501" "1059501" "1060601" "1061601" "1062601" "1063601" "1064601"
# [302] "1065601" "1066601" "1067601" "1068601" "1069601" "1070701" "1071701"
# [309] "1072701" "1073701" "1074701" "1075701" "1076701" "1077701" "1078701"
# [316] "1079701" "1080801" "1081801" "1082801" "1083801" "1084801" "1085801"
# [323] "1086801" "1087801" "1088801" "1089801" "1090901" "1091901" "1092901"
# [330] "1093901" "1094901" "1095901" "1096901" "1097901" "1098901" "1099901"
# [337] "1100011" "1101011" "1102011" "1103011" "1104011" "1105011" "1106011"
# [344] "1107011" "1108011" "1109011" "1110111" "1111111" "1112111" "1113111"
# [351] "1114111" "1115111" "1116111" "1117111" "1118111" "1119111" "1120211"
# [358] "1121211" "1122211" "1123211" "1124211" "1125211" "1126211" "1127211"
# [365] "1128211" "1129211" "1130311" "1131311" "1132311" "1133311" "1134311"
# [372] "1135311" "1136311" "1137311" "1138311" "1139311" "1140411" "1141411"
# [379] "1142411" "1143411" "1144411" "1145411" "1146411" "1147411" "1148411"
# [386] "1149411" "1150511" "1151511" "1152511" "1153511" "1154511" "1155511"
# [393] "1156511" "1157511" "1158511" "1159511" "1160611" "1161611" "1162611"
# [400] "1163611" "1164611" "1165611" "1166611" "1167611" "1168611" "1169611"
# [407] "1170711" "1171711" "1172711" "1173711" "1174711" "1175711" "1176711"
# [414] "1177711" "1178711" "1179711" "1180811" "1181811" "1182811" "1183811"
# [421] "1184811" "1185811" "1186811" "1187811" "1188811" "1189811" "1190911"
# [428] "1191911" "1192911" "1193911" "1194911" "1195911" "1196911" "1197911"
# [435] "1198911" "1199911" "1200021" "1201021" "1202021" "1203021" "1204021"
# [442] "1205021" "1206021" "1207021" "1208021" "1209021" "1210121" "1211121"
# [449] "1212121" "1213121" "1214121" "1215121" "1216121" "1217121" "1218121"
# [456] "1219121" "1220221" "1221221" "1222221" "1223221" "1224221" "1225221"
# [463] "1226221" "1227221" "1228221" "1229221" "1230321" "1231321" "1232321"
# [470] "1233321" "1234321" "1235321" "2000002" "2001002" "2002002" "2003002"
# [477] "2004002" "2005002" "2006002" "2007002" "2008002" "2009002" "2010102"
# [484] "2011102" "2012102" "2013102" "2014102" "2015102" "2016102" "2017102"
# [491] "2018102" "2019102" "2020202" "2021202" "2022202" "2023202" "2024202"
# [498] "2025202" "2026202" "2027202" "2028202" "2029202" "2030302" "2031302"
# [505] "2032302" "2033302" "2034302" "2035302" "2036302" "2037302" "2038302"
# [512] "2039302" "2040402" "2041402" "2042402" "2043402" "2044402" "2045402"
# [519] "2046402" "2047402" "2048402" "2049402" "2050502" "2051502" "2052502"
# [526] "2053502" "2054502" "2055502" "2056502" "2057502" "2058502" "2059502"
# [533] "2060602" "2061602" "2062602" "2063602" "2064602" "2065602" "2066602"
# [540] "2067602" "2068602" "2069602" "2070702" "2071702" "2072702" "2073702"
# [547] "2074702" "2075702" "2076702" "2077702" "2078702" "2079702" "2080802"
# [554] "2081802" "2082802" "2083802" "2084802" "2085802" "2086802" "2087802"
# [561] "2088802" "2089802" "2090902" "2091902" "2092902" "2093902" "2094902"
# [568] "2095902" "2096902" "2097902" "2098902" "2099902" "2100012" "2101012"
# [575] "2102012" "2103012" "2104012" "2105012" "2106012" "2107012" "2108012"
# [582] "2109012" "2110112" "2111112" "2112112" "2113112" "2114112" "2115112"
# [589] "2116112" "2117112" "2118112" "2119112" "2120212" "2121212" "2122212"
# [596] "2123212" "2124212" "2125212" "2126212" "2127212" "2128212" "2129212"
# [603] "2130312" "2131312" "2132312" "2133312" "2134312" "2135312" "2136312"
# [610] "2137312" "2138312" "2139312" "2140412" "2141412" "2142412" "2143412"
# [617] "2144412" "2145412" "2146412" "2147412" "2148412" "2149412" "2150512"
# [624] "2151512" "2152512" "2153512" "2154512" "2155512" "2156512" "2157512"
# [631] "2158512" "2159512" "2160612" "2161612" "2162612" "2163612" "2164612"
# [638] "2165612" "2166612" "2167612" "2168612" "2169612" "2170712" "2171712"
# [645] "2172712" "2173712" "2174712" "2175712" "2176712" "2177712" "2178712"
# [652] "2179712" "2180812" "2181812" "2182812" "2183812" "2184812" "2185812"
# [659] "2186812" "2187812" "2188812" "2189812" "2190912" "2191912" "2192912"
# [666] "2193912" "2194912" "2195912" "2196912" "2197912" "2198912" "2199912"
# [673] "2200022" "2201022" "2202022" "2203022" "2204022" "2205022" "2206022"
# [680] "2207022" "2208022" "2209022" "2210122" "2211122" "2212122" "2213122"
# [687] "2214122" "2215122" "2216122" "2217122" "2218122" "2219122" "2220222"
# [694] "2221222" "2222222" "2223222" "2224222" "2225222" "2226222" "2227222"
# [701] "2228222" "2229222" "2230322" "2231322" "2232322" "2233322" "2234322"
# [708] "2235322"

尽管如此,我的palindroms数量似乎与您不同。

,

这是使用R的内置时间和日期函数创建所需序列的快速方法。

#create the time sequence for every second for 1 day
dateseq <- seq(as.POSIXct("2020-08-15"),as.POSIXct("2020-08-16"),by="1 sec")

#remove the last element (midnight the next day)
dateseq <- dateseq[-86401]

#format the desire
answer <- format(dateseq,"%H%M%S")

tail(answer)
#[1] "235954" "235955" "235956" "235957" "235958" "235959"
,

这是使用功能性方法(仅使用基数R)解决整个问题的一种方法。也就是说,将每个问题分解为一个任务并建立所需的功能:

# Converts strings in the format "1234556" to date times
as_time <- function(chr) {
  chr[nchar(chr) == 7] <- paste0("0",chr[nchar(chr) == 7])
  strptime(chr,"%d%H%M%S")
}

# Converts date-times to strings in format "1234556"
as_chr <- function(t) {
  paste0(as.numeric(substr(t,9,10)),strftime(t,"%H%M%S"))
}

# Gets a sequence of valid strings between to strings in format "1234556"
seq_times <- function(t1,t2)
{
  as_chr(seq(as_time(t1),as_time(t2),by = "1 sec"))
}

# Reverse strings in a character vector
rev_string <- function(s) {
  sapply(s,function(x) intToUtf8(rev(utf8ToInt(x))),USE.NAMES = FALSE)
}

# Returns only the subset of a given character vector that are palindromes
get_palindromes <- function(t1,t2) {
  str <- seq_times(t1,t2)
  str[str == rev_string(str)]
}

现在我们可以做:

get_palindromes("1000000","2000000")
#>   [1] "1000001" "1001001" "1002001" "1003001" "1004001" "1005001" "1010101"
#>   [8] "1011101" "1012101" "1013101" "1014101" "1015101" "1020201" "1021201"
#>  [15] "1022201" "1023201" "1024201" "1025201" "1030301" "1031301" "1032301"
#>  [22] "1033301" "1034301" "1035301" "1040401" "1041401" "1042401" "1043401"
#>  [29] "1044401" "1045401" "1050501" "1051501" "1052501" "1053501" "1054501"
#>  [36] "1055501" "1060601" "1061601" "1062601" "1063601" "1064601" "1065601"
#>  [43] "1070701" "1071701" "1072701" "1073701" "1074701" "1075701" "1080801"
#>  [50] "1081801" "1082801" "1083801" "1084801" "1085801" "1090901" "1091901"
#>  [57] "1092901" "1093901" "1094901" "1095901" "1100011" "1101011" "1102011"
#>  [64] "1103011" "1104011" "1105011" "1110111" "1111111" "1112111" "1113111"
#>  [71] "1114111" "1115111" "1120211" "1121211" "1122211" "1123211" "1124211"
#>  [78] "1125211" "1130311" "1131311" "1132311" "1133311" "1134311" "1135311"
#>  [85] "1140411" "1141411" "1142411" "1143411" "1144411" "1145411" "1150511"
#>  [92] "1151511" "1152511" "1153511" "1154511" "1155511" "1160611" "1161611"
#>  [99] "1162611" "1163611" "1164611" "1165611" "1170711" "1171711" "1172711"
#> [106] "1173711" "1174711" "1175711" "1180811" "1181811" "1182811" "1183811"
#> [113] "1184811" "1185811" "1190911" "1191911" "1192911" "1193911" "1194911"
#> [120] "1195911" "1200021" "1201021" "1202021" "1203021" "1204021" "1205021"
#> [127] "1210121" "1211121" "1212121" "1213121" "1214121" "1215121" "1220221"
#> [134] "1221221" "1222221" "1223221" "1224221" "1225221" "1230321" "1231321"
#> [141] "1232321" "1233321" "1234321" "1235321"

get_palindromes("2235000","3060000")
#>  [1] "2235322" "3000003" "3001003" "3002003" "3003003" "3004003" "3005003"
#>  [8] "3010103" "3011103" "3012103" "3013103" "3014103" "3015103" "3020203"
#> [15] "3021203" "3022203" "3023203" "3024203" "3025203" "3030303" "3031303"
#> [22] "3032303" "3033303" "3034303" "3035303" "3040403" "3041403" "3042403"
#> [29] "3043403" "3044403" "3045403" "3050503" "3051503" "3052503" "3053503"
#> [36] "3054503" "3055503"
,

长度是什么意思?如果您指的是计数,那么我认为我们可以使用简单的数学方法来查看有多少种可能性。 假设对于n1 = 1和n2 = 2,在7个可用位置(dhhmmss)中,第1和第7位只能有2个选择。现在,对于剩下的6个地方,我们只需要考虑前3个地方,因为其余地方将与前3个地方相同(根据回文逻辑)。

现在,对于第二名,我们只能有3个选择(0、1、2,因为我们只能将小时从00到23,只考虑十位)。让我们将第二位的值存储到变量h中。接下来,我们获得第三名,对于h = {0,1,2},它们可以分别具有10、10和4个选择。接下来,我们排在第4位,只能有6个选择(范围从00到59,这里仅是10位)。

因此,总选项为2 * [10 + 10 + 4] * 6 = 288个选项。

,

您可以使用rep()创建各种时间元素(天,小时等),然后使用expand.grid()获取所有元素的组合。来自stringi的stri_reverse()可用于比较字符串的反向,从而确定它是否是回文。

    find_palindrome<-function(day_start,day_end){
          day<-rep(day_start:day_end) 
          hour<-rep(0:23) 
          min_sec<-rep(0:59) 
          #expand.grid() finds every combination of inputs
          #min_sec is used twice within expand.grid(),once for minutes and once for seconds.
          # The "%02d" within sprint() preserves a 2-digit length (e.g. '01' instead of '1'.)
          df<-expand.grid(day,sprintf("%02d",hour),min_sec),min_sec)) 
          df<-as.data.frame(df) 
          #create a column concatinating the values
          df$compare1<-paste(df[,1],df[,2],3],4],sep="") 
          #reverse the order in another column
          df$compare2<-stringi::stri_reverse(df$compare1) 
          #compare the numbers to find your palendromes
          palindrone<-df$compare1[df$compare1 == df$compare2]
          return(palindrone)
        }

然后运行功能:

#example using day 0 to day 2
find_palindrome(0,2)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...