Champernowne up to me

5

Inspired by this question.

Champernowne's constant is an infinite decimal number that consists of "0." followed by all natural numbers concatenated together. It begins like so: 0.123456781011121314151617181920212223242526272829303132333435363738394041424344454647484950, etc. The digits of Chamernowne's constant are sequence A033307 in the OEIS.

Your Task:

Write a program or function that, when given an integer as input, outputs/returns Champernowne's constant up to the first occurance of the input. For example, if the input is 13, you should output 0.12345678910111213. If you wish, for inputs greater than 0, you may drop the leading "0." in your output.

Input:

An integer, or a string containing only numbers.

Output:

Champernowne's constant up to the first occurance of the input.

Test Cases:

0   -> 0
1   -> 0.1 OR 1
14  -> 0.1234567891011121314 OR 1234567891011121314
91  -> 0.1234567891 OR 1234567891
12  -> 0.12 OR 12
101 -> 0.123456789101 OR 123456789101 

Scoring:

This is , lowest score in bytes wins!

Gryphon

Posted 2017-08-03T23:40:46.143

Reputation: 6 697

Question was closed 2017-08-04T00:02:13.587

1Very related. – ETHproductions – 2017-08-03T23:57:30.460

@ETHproductions, thanks, I hadn't seen that. It is very closely related, but I don't think it is quite close enough to qualify as a dupe. However, if you disagree, feel free to CV. – Gryphon – 2017-08-03T23:58:50.213

1@ETHproductions I can't believe I'm getting to the point where I'm forgetting about questions that I actually answered. Anyway to me that seems like a dupe, so I'll close this for now. – FryAmTheEggman – 2017-08-04T00:01:43.630

Answers

2

05AB1E, 6 bytes

Code

LJ¹¡н«

Uses the 05AB1E encoding. Try it online!

Explanation

L         # Create the range [1, 2, ..., input]
 J        # Join into a single string
  ¹¡      # Split at occurences of the input
    н     # Take the first element
     «    # Append the input to the first element and implicitly output

Adnan

Posted 2017-08-03T23:40:46.143

Reputation: 41 965

2And I thought this would actually be somewhat difficult :( – Gryphon – 2017-08-03T23:55:46.693

2

Python 3, 70 63 61 bytes

A bunch of bytes removed thanks to @Rod, and some more thanks to @WheatWizard

lambda x:''.join(map(str,range(1,x))).split(str(x))[0]+str(x)

Try it online!

Luis Mendo

Posted 2017-08-03T23:40:46.143

Reputation: 87 464

you can save 13 bytes using map instead list comprehension, and backticks instead str

– Rod – 2017-08-04T00:31:16.070

@Rod Thanks! I've used your map suggestion – Luis Mendo – 2017-08-04T14:48:14.663

@WheatWizard Good idea, thanks! – Luis Mendo – 2017-08-04T14:53:41.200

1

MATL, 20 15 14 bytes

U:VXzG&Yb1X)Gh

Takes the input as a string. Try it online!

Explanation

U      % Implicitly input a string. Convert to number
:      % Range from 1 to that
V      % Convert to string. Includes spaces as separators
Xz     % Remove spaces
G      % Push input again
&Yb    % Split at that. Gives a cell array
1X)    % Get contents of the first cell
G      % Push input again
h      % Concatenate horizontally. Implicitly display

Luis Mendo

Posted 2017-08-03T23:40:46.143

Reputation: 87 464

1

Haskell, 82 73 55 bytes

x!b|or$zipWith(==)x b=x
x!(a:b)=a:x!b
(!(show=<<[1..]))

Try it online!

Explanation

First we define !. x!b truncates b to the first appearance of x. It does this by checking if b starts with x (or$zipWith(==)x b) returning x if it does and moving one down the string otherwise. Then we define our main function. Our main function is a point-free function that takes the constant (show=<<[1..]) and truncates it to the first appearance of x. This takes x as a string.

Post Rock Garf Hunter

Posted 2017-08-03T23:40:46.143

Reputation: 55 382

1

Pyth, 11 8 bytes

+hcjkSQ`

Try it online! or run a Test Suite.

Simply creates the constant (jkSQ) and then prints the constant up to the input (c ... `) followed by the input (+ ...).

FryAmTheEggman

Posted 2017-08-03T23:40:46.143

Reputation: 16 206

1

Mathematica, 111 bytes

(m=Min@SequencePosition[s=Flatten[IntegerDigits/@Range[0,#]],t=IntegerDigits@#];FromDigits@Join[s[[;;m-1]],t])&

J42161217

Posted 2017-08-03T23:40:46.143

Reputation: 15 931