Skip to main content
deleted 71 characters in body
Source Link
Sundar R
  • 6.6k
  • 19
  • 33

MATL, 1818 17 bytes

t4Y2m)48-t1YSof!Ut1YSof)sE

Try it online! or with several test casesTry it online!

(-1 byte thanks to Luis Mendo/Giuseppe/both!)

Explanation:

     % Implicit input
 t   % duplicate input
     % stack: ['r5e6o9mm!/3708dvc' 'r5e6o9mm!/3708dvc']
 4Y2 % push inbuilt literal, characters '0':'9'
     % stack: ['r5e6o9mm!/3708dvc' 'r5e6o9mm!/3708dvc' '0123456789']
 m)  % extract only characters from input that belong to '0':'9'
     % stack: ['5693708']
 48-!U % subtract% '0'transpose toand turnconvert asciieach value tofrom numericstring valueto number
     % stack: [5 6 9 3 7 0 8]
 t   % duplicate that
 1YS % circular shift by 1
     % stack: [[5 6 9 3 7 0 8] [8 5 6 9 3 7 0]]
 o   % parity check - 1 for odd, 0 for even
     % stack: [[5 6 9 3 7 0 8] [0 1 0 1 1 1 0]]
 f   % find non-zero value indices in last array
     % stack: [[5 6 9 3 7 0 8] [2 4 5 6]]
 )   % index at those places in the first array
 s   % sum
 E   % multiply by 2
     % (implicit) convert to string and display

The basic idea is that numbers that follow even numbers can be ignored, while those that follow odd numbers are doubled - and the final result is the sum of those doubled values.

I didn't think f after the parity check o would be necessary, but for some reason MATL doesn't see the array of 0's and 1's that result from the o as a logical array, instead takes them as numerical indices and indexes into positions 1 and end.

MATL, 18 bytes

t4Y2m)48-t1YSof)sE

Try it online! or with several test cases

Explanation:

     % Implicit input
 t   % duplicate input
     % stack: ['r5e6o9mm!/3708dvc' 'r5e6o9mm!/3708dvc']
 4Y2 % push inbuilt literal, characters '0':'9'
     % stack: ['r5e6o9mm!/3708dvc' 'r5e6o9mm!/3708dvc' '0123456789']
 m)  % extract only characters from input that belong to '0':'9'
     % stack: ['5693708']
 48- % subtract '0' to turn ascii value to numeric value
     % stack: [5 6 9 3 7 0 8]
 t   % duplicate that
 1YS % circular shift by 1
     % stack: [[5 6 9 3 7 0 8] [8 5 6 9 3 7 0]]
 o   % parity check - 1 for odd, 0 for even
     % stack: [[5 6 9 3 7 0 8] [0 1 0 1 1 1 0]]
 f   % find non-zero value indices in last array
     % stack: [[5 6 9 3 7 0 8] [2 4 5 6]]
 )   % index at those places in the first array
 s   % sum
 E   % multiply by 2
     % (implicit) convert to string and display

The basic idea is that numbers that follow even numbers can be ignored, while those that follow odd numbers are doubled - and the final result is the sum of those doubled values.

I didn't think f after the parity check o would be necessary, but for some reason MATL doesn't see the array of 0's and 1's that result from the o as a logical array, instead takes them as numerical indices and indexes into positions 1 and end.

MATL, 18 17 bytes

t4Y2m)!Ut1YSof)sE

Try it online!

(-1 byte thanks to Luis Mendo/Giuseppe/both!)

Explanation:

     % Implicit input
 t   % duplicate input
     % stack: ['r5e6o9mm!/3708dvc' 'r5e6o9mm!/3708dvc']
 4Y2 % push inbuilt literal, characters '0':'9'
     % stack: ['r5e6o9mm!/3708dvc' 'r5e6o9mm!/3708dvc' '0123456789']
 m)  % extract only characters from input that belong to '0':'9'
     % stack: ['5693708']
 !U  % transpose and convert each value from string to number
     % stack: [5 6 9 3 7 0 8]
 t   % duplicate that
 1YS % circular shift by 1
     % stack: [[5 6 9 3 7 0 8] [8 5 6 9 3 7 0]]
 o   % parity check - 1 for odd, 0 for even
     % stack: [[5 6 9 3 7 0 8] [0 1 0 1 1 1 0]]
 f   % find non-zero value indices in last array
     % stack: [[5 6 9 3 7 0 8] [2 4 5 6]]
 )   % index at those places in the first array
 s   % sum
 E   % multiply by 2
     % (implicit) convert to string and display

The basic idea is that numbers that follow even numbers can be ignored, while those that follow odd numbers are doubled - and the final result is the sum of those doubled values.

I didn't think f after the parity check o would be necessary, but for some reason MATL doesn't see the array of 0's and 1's that result from the o as a logical array, instead takes them as numerical indices and indexes into positions 1 and end.

add explanation
Source Link
Sundar R
  • 6.6k
  • 19
  • 33

MATL, 18 bytes

t4Y2m)48-t1YSof)sE

Try it online! or with several test cases

Explanation:

     % Implicit input
 t   % duplicate input
     % stack: ['r5e6o9mm!/3708dvc' 'r5e6o9mm!/3708dvc']
 4Y2 % push inbuilt literal, characters '0':'9'
     % stack: ['r5e6o9mm!/3708dvc' 'r5e6o9mm!/3708dvc' '0123456789']
 m)  % extract only characters from input that belong to '0':'9'
     % stack: ['5693708']
 48- % subtract '0' to turn ascii value to numeric value
     % stack: [5 6 9 3 7 0 8]
 t   % duplicate that
 1YS % circular shift by 1
     % stack: [[5 6 9 3 7 0 8] [8 5 6 9 3 7 0]]
 o   % parity check - 1 for odd, 0 for even
     % stack: [[5 6 9 3 7 0 8] [0 1 0 1 1 1 0]]
 f   % find non-zero value indices in last array
     % stack: [[5 6 9 3 7 0 8] [2 4 5 6]]
 )   % index at those places in the first array
 s   % sum
 E   % multiply by 2
     % (implicit) convert to string and display

The basic idea is that numbers that follow even numbers can be ignored, while those that follow odd numbers are doubled - and the final result is the sum of those doubled values.

I didn't think f after the parity check o would be necessary, but for some reason MATL doesn't see the array of 0's and 1's that result from the o as a logical array, instead takes them as numerical indices and indexes into positions 1 and end.

MATL, 18 bytes

t4Y2m)48-t1YSof)sE

Try it online! or with several test cases

MATL, 18 bytes

t4Y2m)48-t1YSof)sE

Try it online! or with several test cases

Explanation:

     % Implicit input
 t   % duplicate input
     % stack: ['r5e6o9mm!/3708dvc' 'r5e6o9mm!/3708dvc']
 4Y2 % push inbuilt literal, characters '0':'9'
     % stack: ['r5e6o9mm!/3708dvc' 'r5e6o9mm!/3708dvc' '0123456789']
 m)  % extract only characters from input that belong to '0':'9'
     % stack: ['5693708']
 48- % subtract '0' to turn ascii value to numeric value
     % stack: [5 6 9 3 7 0 8]
 t   % duplicate that
 1YS % circular shift by 1
     % stack: [[5 6 9 3 7 0 8] [8 5 6 9 3 7 0]]
 o   % parity check - 1 for odd, 0 for even
     % stack: [[5 6 9 3 7 0 8] [0 1 0 1 1 1 0]]
 f   % find non-zero value indices in last array
     % stack: [[5 6 9 3 7 0 8] [2 4 5 6]]
 )   % index at those places in the first array
 s   % sum
 E   % multiply by 2
     % (implicit) convert to string and display

The basic idea is that numbers that follow even numbers can be ignored, while those that follow odd numbers are doubled - and the final result is the sum of those doubled values.

I didn't think f after the parity check o would be necessary, but for some reason MATL doesn't see the array of 0's and 1's that result from the o as a logical array, instead takes them as numerical indices and indexes into positions 1 and end.

add multiple test cases link
Source Link
Sundar R
  • 6.6k
  • 19
  • 33

MATL, 18 bytes

t4Y2m)48-t1YSof)sE

Try it online! or with several test cases

MATL, 18 bytes

t4Y2m)48-t1YSof)sE

Try it online!

MATL, 18 bytes

t4Y2m)48-t1YSof)sE

Try it online! or with several test cases

Source Link
Sundar R
  • 6.6k
  • 19
  • 33
Loading