Skip to main content
Added an alternative
Source Link
Kamil Drakari
  • 4.4k
  • 11
  • 21

Japt v2.0a0, 21 bytes

qW ÔË+WpE>VÃw
qWpUʧV

Try it

Input as string, n, substring

v2.0a0 is necessary because v1.4.6 errors if you try to use p this way.

Explanation:

qW ÔË+WpE>VÃw
qW            # Split <string> where <substring> appears
   Ô          # Reverse the array
    Ë      Ã  # For each item in the array:
     +        #  Append:
      Wp      #   <substring> repeated a number of times equal to:
        E>V   #    The current index is greater than <n>
              #    (Boolean gets converted to 1 or 0)
            w # Reverse the new array
              # Store as U

qWpUʧV
q             # Turn U into a string by inserting this between each item:
 Wp           #  <substring> repeated a number of times equal to:
   UÊ         #   Length of U (i.e. 1 + number of times <substring> appeared)
     §V       #   Is less than or equal to <n>
              # Output that string

I've tried an alternate using ð but the best I got was 25 bytes.

Japt v2.0a0, 21 bytes

qW ÔË+WpE>VÃw
qWpUʧV

Try it

Input as string, n, substring

v2.0a0 is necessary because v1.4.6 errors if you try to use p this way.

Explanation:

qW ÔË+WpE>VÃw
qW            # Split <string> where <substring> appears
   Ô          # Reverse the array
    Ë      Ã  # For each item in the array:
     +        #  Append:
      Wp      #   <substring> repeated a number of times equal to:
        E>V   #    The current index is greater than <n>
              #    (Boolean gets converted to 1 or 0)
            w # Reverse the new array
              # Store as U

qWpUʧV
q             # Turn U into a string by inserting this between each item:
 Wp           #  <substring> repeated a number of times equal to:
   UÊ         #   Length of U (i.e. 1 + number of times <substring> appeared)
     §V       #   Is less than or equal to <n>
              # Output that string

Japt v2.0a0, 21 bytes

qW ÔË+WpE>VÃw
qWpUʧV

Try it

Input as string, n, substring

v2.0a0 is necessary because v1.4.6 errors if you try to use p this way.

Explanation:

qW ÔË+WpE>VÃw
qW            # Split <string> where <substring> appears
   Ô          # Reverse the array
    Ë      Ã  # For each item in the array:
     +        #  Append:
      Wp      #   <substring> repeated a number of times equal to:
        E>V   #    The current index is greater than <n>
              #    (Boolean gets converted to 1 or 0)
            w # Reverse the new array
              # Store as U

qWpUʧV
q             # Turn U into a string by inserting this between each item:
 Wp           #  <substring> repeated a number of times equal to:
   UÊ         #   Length of U (i.e. 1 + number of times <substring> appeared)
     §V       #   Is less than or equal to <n>
              # Output that string

I've tried an alternate using ð but the best I got was 25 bytes.

Improvements
Source Link
Kamil Drakari
  • 4.4k
  • 11
  • 21

Japt v2.0a0, 2721 bytes

qW ÔË+(E§V?P:WÃwÔË+WpE>VÃw
Ê>V?Uq:UqWqWpUʧV

Try itTry it

Input as string, n, substring

Explanation coming later, I think there's still more golfingv2.0a0 is necessary because v1.4.6 errors if you try to douse p this way.

Explanation:

qW ÔË+WpE>VÃw
qW            # Split <string> where <substring> appears
   Ô          # Reverse the array
    Ë      Ã  # For each item in the array:
     +        #  Append:
      Wp      #   <substring> repeated a number of times equal to:
        E>V   #    The current index is greater than <n>
              #    (Boolean gets converted to 1 or 0)
            w # Reverse the new array
              # Store as U

qWpUʧV
q             # Turn U into a string by inserting this between each item:
 Wp           #  <substring> repeated a number of times equal to:
   UÊ         #   Length of U (i.e. 1 + number of times <substring> appeared)
     §V       #   Is less than or equal to <n>
              # Output that string

Japt, 27 bytes

qW ÔË+(E§V?P:WÃw
Ê>V?Uq:UqW

Try it

Input as string, n, substring

Explanation coming later, I think there's still more golfing to do.

Japt v2.0a0, 21 bytes

qW ÔË+WpE>VÃw
qWpUʧV

Try it

Input as string, n, substring

v2.0a0 is necessary because v1.4.6 errors if you try to use p this way.

Explanation:

qW ÔË+WpE>VÃw
qW            # Split <string> where <substring> appears
   Ô          # Reverse the array
    Ë      Ã  # For each item in the array:
     +        #  Append:
      Wp      #   <substring> repeated a number of times equal to:
        E>V   #    The current index is greater than <n>
              #    (Boolean gets converted to 1 or 0)
            w # Reverse the new array
              # Store as U

qWpUʧV
q             # Turn U into a string by inserting this between each item:
 Wp           #  <substring> repeated a number of times equal to:
   UÊ         #   Length of U (i.e. 1 + number of times <substring> appeared)
     §V       #   Is less than or equal to <n>
              # Output that string
Saved bytes
Source Link
Kamil Drakari
  • 4.4k
  • 11
  • 21

Japt, 3127 bytes

qW Ë+ÔË+(V+E+FÊn ¨JE§V?P:WWÃw
Ê>V?Uq:UqW

Try itTry it

Input as string, n, substring

Explanation coming later, I think there's still more golfing to do.

Japt, 31 bytes

qW Ë+(V+E+FÊn ¨J?P:W
Ê>V?Uq:UqW

Try it

Input as string, n, substring

Explanation coming later, I think there's still more golfing to do.

Japt, 27 bytes

qW ÔË+(E§V?P:WÃw
Ê>V?Uq:UqW

Try it

Input as string, n, substring

Explanation coming later, I think there's still more golfing to do.

Old algorithm did not meet clarified specifications
Source Link
Kamil Drakari
  • 4.4k
  • 11
  • 21
Loading
Source Link
Kamil Drakari
  • 4.4k
  • 11
  • 21
Loading