12
\$\begingroup\$

If you place the positive integers together and read each set of two adjacent digits at the same time, you get: (A136414)

12, 23, 34, 45, 56, 67, 78, 89, 91, 10, 1, 11, ...

However, if you squash that sequence again:

12, 22, 23, 33, 34, 44, 45, 55, 56, 66, 67, 77, 78, 88, 89, 99, 91, 11, 10, 1, 11, 11

and so on.

Your task is to output the sequence of the positive integers squashed m times.

Squashing process

[1, 2, 3, 4, 5]
       ↓
    "12345"
       ↓
[12, 23, 34, 45]

Python squashing script

Rules

  • You have to receive m (a positive integer) as the input or you can output the sequences for all m
  • Numbers in the sequence can appear more than once
  • Leading zeros must be removed (eg. 04 is 4)
  • Default sequence rules
  • Default Loopholes apply

Test cases

[In]: 1
[Out]: [12, 23, 34, 45, 56, 67, 78, 89, 91, 10, 1, 11, 11, 12, 21, 13, 31, 14, 41, 15, ...]

[In]: 2
[Out]: [12, 22, 23, 33, 34, 44, 45, 55, 56, 66, 67, 77, 78, 88, 89, 99, 91, 11, 10, 1, 11, 11, 11, 11, 11, 12, 22, 21, 11, 13, 33, 31, 11, 14, 44, 41, 11, 15, ...]

[In]: 3
[Out]: [12, 22, 22, 22, 23, 33, 33, 33, 34, 44, 44, 44, 45, 55, 55, 55, 56, 66, 66, 66, 67, 77, 77, 77, 78, 88, 88, 88, 89, 99, 99, 99, 91, 11, 11, 11, 10, 1, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 22, 22, 22, 21, 11, 11, 11, 13, 33, 33, 33, 31, 11, 11, 11, 14, 44, 44, 44, 41, 11, 11, 11, 15, ...]
\$\endgroup\$
1

16 Answers 16

13
\$\begingroup\$

Haskell, 49 bytes

f(x:z)=read[x,z!!0]:f z
q=[1..]:map(f.(>>=show))q

Attempt This Online!

Haskell loves infinite sequences. Here we get to make an infinite sequence of infinite sequences.

The function f takes infinite string of digits and produces numbers formed from the pairs in order. We combine this with (>>=show) to get a function which takes an infinite sequence and produces the next. We use recursion to create the infinite list of such sequences starting with [1..].

\$\endgroup\$
6
\$\begingroup\$

Python 3.8, 121 119 bytes

lambda m,n:m<1and n or(n*0==[]or(n:=range(1,n)))and(d:=''.join(map(str,n)))and z(m-1,[int(i+j)for i,j in zip(d,d[1:])])

Try it online!

m is the number of squashes applied and n controls the length of the output.


Same idea, except split over two functions (and somehow the exact same number of characters):

lambda m,n:y(m,range(1,n))
y=lambda m,a:m<1and a or(d:=''.join(map(str,a)))and y(m-1,[int(i+j)for i,j in zip(d,d[1:])])

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$ Jul 8, 2022 at 19:02
6
\$\begingroup\$

05AB1E, 7 bytes

∞IFSü«ï

Given \$m\$, output the infinite \$m^{th}\$ sequence.

Try it online or verify the first 25 results of the first 5 \$m\$.

Explanation:

∞        # Push the infinite list of positive integers: [1,2,3,...]
 IF      # Loop the input `m` amount of times:
   S     #  Convert the list of integers to a flattened list of digits
    ü    #  For each overlapping pair of digits:
     «   #   Append them together
      ï  #  Convert it back to an integer to remove the leading 0s
         # (after which the result is output implicitly)
\$\endgroup\$
5
\$\begingroup\$

APL(Dyalog Unicode), 21 bytes SBCS

Anonymous infix lambda, taking m as left argument and the number of elements of the sequence to generate as right argument.

{⍵↑{⍎¨2,/∊⍕¨⍵}⍣⍺⍳1+⍵}

{} "dfn"; is m and is the number of elements to generate.

1+⍵ increment the number of elements (to handle the case of 1)

 generate the integers 1…m+1

{}⍣⍺ repeat application of the following function, m times:

  ⍕¨⍵ format each number in the sequence

   enlist (flatten)

  2,/ concatenate adjacent characters

  ⍎¨ evaluate each

⍵↑ take as many elements as requested

Try it on APLgolf!

\$\endgroup\$
4
\$\begingroup\$

JavaScript (Node.js), 64 bytes

f=(m,n)=>n?[...f(m,n-1),m--?+f(m,n+2).join``.substr(n-1,2):n]:[]

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Husk, 11 bytes

!¡ȯmdẊeṁdN→

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Jelly, 11 bytes

ị+D€FVƝƊ⁹¡¥

Try it online!

1-indexed, takes \$m\$ on the right and \$n\$ on the left to give the \$n\$th term of the sequence.

It seems like there should be a non-naive method as well, but the patterns that manifest in the single digits may not be as real as they seem.

        ⁹¡     Repeat m times
 +        ¥    starting with n + m:
  D€           digits of each
   €           (implicitly rangifies on first iteration),
    F          flatten,
     VƝ        concatenate each pair of adjacent digits into an integer.
ị              Get the n'th element of the result.
\$\endgroup\$
4
\$\begingroup\$

Python 2, 96 bytes (@math junkie)

def f(n,c=0):
 while n<1:c+=1;yield c
 for s in f(n-1):
  for b in`s`:
    if c:yield int(c+b)
    c=b

Attempt This Online!

TIL: Apparently one can mix spaces and tabs in Python 2.

Old Python 2, 101 bytes

def f(n,c=0):
 while n<1:c=c+1;yield c
 for s in f(n-1):
  for b in`s`:
   if c:yield int(c+b)
   c=b

Attempt This Online!

\$\endgroup\$
3
  • \$\begingroup\$ You can save some bytes by mixing tabs and spaces: ATO \$\endgroup\$ Jul 9, 2022 at 22:10
  • \$\begingroup\$ Wow, is that a Python 2 thing? I'm pretty sure it is illegal in Python 3.@mathjunkie \$\endgroup\$
    – loopy walt
    Jul 9, 2022 at 23:13
  • 1
    \$\begingroup\$ @loopywalt Yes, see this tip. \$\endgroup\$
    – naffetS
    Jul 10, 2022 at 2:14
4
\$\begingroup\$

Burlesque, 20 bytes

J+.ro{im2CO}3MVE!j.+

Try it online!

J    # Duplicate M
+.ro # Range [1..M+1]
{
 im  # Implode (concatenate digits)
 2CO # Chunks with overlap length 2 ("ABC"2CO -> "AB" "BC")
}
3MV  # Move the 3rd item (N) to top of stack
E!   # Evaluate that many times
j.+  # Keep the top M numbers

Burlesque, 13 bytes (invalid violates default sequence rules)

ro{im2CO}x/E!

Try it online!

ro    # Range 1..N
{
 im   # Implode (concatenate digits)
 2CO  # Chunks with overlap length 2 ("ABC"2CO -> "AB" "BC")
}
x/    # Reorder stack putting `m` on top
E!    # Eval `m` times
\$\endgroup\$
4
  • \$\begingroup\$ So N is the size of the original list? I don't see how that could be allowed by the rules \$\endgroup\$ Jul 8, 2022 at 22:17
  • \$\begingroup\$ I believe this answer is invalid. Note that a different number of elements are returned for the same N and different m: Try it online!. Also, the input 2 1 gives an error: Try it online! \$\endgroup\$ Jul 10, 2022 at 21:53
  • \$\begingroup\$ It is more the fact that the infinite list cannot easily be operated on (in this way) in a way which will return an answer in finite time. It generates the list for [1..N] input elements "squashed" M times. As N approaches infinity, it produces the full sequence. \$\endgroup\$ Jul 11, 2022 at 21:30
  • \$\begingroup\$ I understand that but I don't think it's allowed by the rules. n should either be the number of elements returned or the index of an element in the mth list, not the upper bound of the starting list. See Default Sequence Rules \$\endgroup\$ Jul 11, 2022 at 21:35
2
\$\begingroup\$

Vyxal, 10 bytes

Tɾ$(ṅ2lĖ)Ẏ

Try it Online!

Takes number of items to generate then how many times to squash

\$\endgroup\$
2
\$\begingroup\$

Rust + Itertools, 249 bytes

use itertools::*;let f=|s:Box<dyn std::iter::Iterator<Item=u32>>|Box::new(s.flat_map(|a|(0..=a.log10()).rev().map(move|t|a/10u32.pow(t)%10)).tuple_windows().map(|k:(u32,u32)|k.0*10+k.1));let g=|n|{let mut k=f(Box::new(1..));for _ in 0..n{k=f(k)};k};

Playground link: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=11fa065d958a13e4a9d68dc2de7b9b12

\$\endgroup\$
2
\$\begingroup\$

Java 10, 143 139 bytes

n->m->{int r[]=new int[n],i,j=0;for(;j++<m;){var s="";for(i=0;i<n;r[i]=new Byte(s.substring(i,i+++2)))s+=j<2?i-~i+""+-~i*2:r[i];}return r;}

Takes inputs \$n,m\$ and outputs the first \$n\$ values of the \$m^{th}\$ sequence.

Try it online.

Explanation:

n->m->{                  // Method with two integer parameters & integer-array result
  int r[]=new int[n],    //  Create the result-list of `n` amount of 0s
      i,                 //  Index-integer `i`, uninitialized for now
  j=0,i;for(;j++<m;){    //  Loop `j` in the range [1,m):
    var s="";            //   String `s`, starting empty
    for(i=0;i<n          //   Inner loop `i` in the range [0,n):
        ;                //     After every iteration:
         r[i]=           //      Replace the i'th value in the result-array to:
           new Byte(     //       Convert the following String to an integer,
                         //       (to put it in the integer-array & to remove leading 0s)
            s.substring( //        A substring of String `s`,
              i,i+++2))) //        in the character index-range [i,i+2)
                         //        (and increase `i` by 1 afterwards with `i++`)
      s+=                //    Append to String `s`:
         j<2?            //     If `j` is 1, thus the first iteration of loop `j`:
             i-~i        //      Append (2*i+1) to the String
             +""+-~i*2   //      as well as (2*i+2)
                         //      (so the .substring-indices won't be out-of-bounds)
           :             //     Else:
            r[i];        //      Append the i'th integer of the result-array instead
  }return r;}            //  After the loops: return the result-array
\$\endgroup\$
2
\$\begingroup\$

Jelly, 12 bytes

‘RDFṡ2ḌƲ⁴¡³ị

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Seems to be incorrect for 9, 1? \$\endgroup\$ Jul 8, 2022 at 15:43
  • 1
    \$\begingroup\$ @UnrelatedString Fixed. \$\endgroup\$
    – Leaky Nun
    Jul 8, 2022 at 20:23
1
\$\begingroup\$

Pyth, 16 bytes

LsM.:jkb2<yFShQE

Try it online!

Takes n (number of elements to generate) then m on separate lines.

LsM.:jkb2<yFShQE
L                 Create a lambda with variable b:
     jkb           - Concatenate elements of b
                     (e.g. [1,2,3,4] -> "1234")
   .:   2          - Produce all 2-element sub-strings
                     (e.g. "1234" -> ["12","23","34"])
 sM                - Convert to integers
                     (e.g. ["12","23","34"] -> [12,23,34])
            ShQ   Range from 1 to n+1
          yF   E  Apply the above lambda m times
         <        Keep the first n elements
\$\endgroup\$
1
\$\begingroup\$

lin, 41 bytes

$`1`d \; `it
\ns `' `flat2`xp \; `'
`_` N

Try it here! Generates an infinite list of infinite lists.

Pretty-printed output version (run with -i flag if using local interpreter):

; ( 40`t ) `' 10`t `__ wrap_
$`1`d \; `it
\ns `' `flat2`xp \; `'
`_` N

lin is a stack-based language I began working on a few years back, but have recently been dusting off with new features and inspirations.

Explanation

  • $` 1`d push infinite list [1..]
  • \; `it iterate infinitely...
    • \ns `' `flat convert each number to digit list and flatten
    • 2`xp \; `' pairwise map...
      • `_` N convert pair to number
\$\endgroup\$
0
\$\begingroup\$

Python 2, 102 bytes

def z(m,n):a=range(1,n);exec'd="".join(map(str,a));a=[int(i+j)for i,j in zip(d,d[1:])];'*m;return a[m]

Try it online!

It's not the shortest Python 2 script here, but the approach is very different to @loopy walt's so I thought it was worth sharing anyways.

The first version of the code, when given an input n, returned a sequence containing at least the first n elements, but @math junkie pointed out that this is not allowed in the sequence rules.

\$\endgroup\$
3
  • \$\begingroup\$ Hi, welcome to Code Golf and interesting answer. Unfortunately, I believe this answer is invalid. According to the Default Sequence Rules, if your function takes a parameter n, it should return either the nth element of the sequence, or the first n elements, and this answer does neither \$\endgroup\$ Jul 10, 2022 at 21:48
  • \$\begingroup\$ For example, note that n=4 using your answer returns a different number of elements for each m: Try it online! \$\endgroup\$ Jul 10, 2022 at 21:49
  • \$\begingroup\$ I interpreted the rule "Given some index n it can return all entries up to the n-th one in the sequence" as meaning it has to return at least the first n entries, but your interpretation is clearly more in line with the intent. \$\endgroup\$ Jul 10, 2022 at 22:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.