# Lowest number in a numeric string

4 messages
Open this post in threaded view
|
Report Content as Inappropriate

## Lowest number in a numeric string

 Hi all, I've got a very long numeric string. I want to find the lowest number that isn't in that string. E.G String = 123456 Therefore 7 is the lowest number not in that string E.G.2 String = 1234567891011 Therefore 13 is the lowest number not in that string. Any thoughts? p.s. I'm an R noob.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Lowest number in a numeric string

 On 06/14/2012 06:08 PM, mogwai84 wrote: > Hi all, > > I've got a very long numeric string. I want to find the lowest number that > isn't in that string. > > E.G > > String = 123456 > Therefore 7 is the lowest number not in that string > > E.G.2 > > String = 1234567891011 > Therefore 13 is the lowest number not in that string. > > Any thoughts? p.s. I'm an R noob. > Hi mogwai84, Your second example is inconsistent with the first. If the answer is not 12, then you must be counting the first two digits as a pair. However, that can't be right, because there are several other pairs that are much larger than 13. Are you making the assumption that all strings of digits contain monotonically increasing integers? Without a better specification of the problem, one can only guess at a solution. Jim ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Lowest number in a numeric string

 In reply to this post by mogwai84 On Thu, Jun 14, 2012 at 01:08:53AM -0700, mogwai84 wrote: > Hi all, > > I've got a very long numeric string. I want to find the lowest number that > isn't in that string. > > E.G > > String = 123456 > Therefore 7 is the lowest number not in that string > > E.G.2 > > String = 1234567891011 > Therefore 13 is the lowest number not in that string. Hi. Try the following.   s <- "1234567891011"   n <- nchar(s)   x <- rep(NA, times=choose(n+1, 2))   k <- 0   for (i in 1:n) {       for (j in i:n) {           k <- k + 1           x[k] <- as.numeric(substr(s, i, j))       }   }   for (i in 1:1000) {       if (! i %in% x) break   }   print(i)   [1] 13 For a longer string, we may use the fact that the number of the numbers included in the string is at most choose(n+1, 2). This implies that the lowest missing number is at most choose(n+1, 2) + 1. Due to this, we can consider only sequencies of digits of length at most ceiling(log10(choose(n+1, 2) + 1)). Hope this helps. Petr Savicky. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.