tag:blogger.com,1999:blog-4228859841453048938.post1124097989918476174..comments2024-03-29T15:50:59.980+08:00Comments on Coding Interview Questions: No. 38 - Digits in a SequenceHarry Hehttp://www.blogger.com/profile/10363303096963693183noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-4228859841453048938.post-48258684647113783462017-09-23T13:11:43.915+08:002017-09-23T13:11:43.915+08:00Here is a more compact python code
def find_digit...Here is a more compact python code<br /><br />def find_digit_in_order(n):<br /> if n<10:<br /> return n<br /> <br /> p10 = 9<br /> rank = 1<br /> remain = n<br /> passed=0<br /> while remain-rank*p10>0:<br /> if rank == 1:<br /> remain = remain - 10<br /> passed = 9<br /> else:<br /> remain = remain - rank*p10<br /> passed = passed + p10<br /> p10 = p10 * 10<br /> rank = rank + 1<br /> <br /> num = passed + math.ceil(remain/rank)<br /> <br /> for i in range(math.floor(rank-remain%rank-1)):<br /> num = math.floor(num/10)<br /> return num%10<br />Anonymoushttps://www.blogger.com/profile/10332481178797961573noreply@blogger.comtag:blogger.com,1999:blog-4228859841453048938.post-47647422899107746202017-06-05T13:53:50.846+08:002017-06-05T13:53:50.846+08:00If anyone wants it, here's my slightly more co...If anyone wants it, here's my slightly more compact (less nicely broken out) solution that utilizes that Math library in java:<br /><br />public int FindDigit(int k) {<br /> if (k < 0) return -1;<br /> if (k < 10) return k;<br /> //keeps track of the number of digits in the solution's underlying number<br /> int digitCount = 1;<br /> //starts on the first ten digits as the possible range<br /> //then jumps to larger ranges if k is not in the range for the <br /> //set of numbers with that number of digits<br /> int tierStart = 1;<br /> int tierEnd = 10;<br /> while (k > tierEnd) {<br /> tierStart += tierEnd;<br /> tierEnd += (Math.Pow(10, digit) * 9 * (digit + 1));<br /> digit++;<br /> }<br /> //accounts for digits before that range<br /> int truePosition = k - tierStart;<br /> //readds the numbers before the range to get the actual number <br /> //that are represented in the digits that k is in<br /> int fullNumber = (truePosition / digits) + (Math.Pow(10, digit -1);<br /> //represents k's position in the full number<br /> int pos = tierMid % digits;<br /> while (pos > 0) {<br /> fullNumber = / 10;<br /> pos--;<br /> }<br /> return fullNumber % 10;<br />}Paige Andersonhttps://www.blogger.com/profile/13823677635499932225noreply@blogger.comtag:blogger.com,1999:blog-4228859841453048938.post-82933955589752769402015-10-11T17:24:22.856+08:002015-10-11T17:24:22.856+08:00The last line return -1; in digitAtIndex is never ...The last line return -1; in digitAtIndex is never calledalvarhttps://www.blogger.com/profile/11148996327596795748noreply@blogger.comtag:blogger.com,1999:blog-4228859841453048938.post-43185012057577835092013-02-06T09:04:15.118+08:002013-02-06T09:04:15.118+08:00Here the position starts from 0, so the digit at t...Here the position starts from 0, so the digit at the position 5 is 5. In this post, the position 5 is different from the 5th position.Harry Hehttps://www.blogger.com/profile/10363303096963693183noreply@blogger.comtag:blogger.com,1999:blog-4228859841453048938.post-16261921284675072082013-02-06T05:35:01.737+08:002013-02-06T05:35:01.737+08:00the 5th position is 4. :)the 5th position is 4. :)Anonymoushttps://www.blogger.com/profile/02141677073183861874noreply@blogger.com