Skip to main content
added 1 character in body
Source Link
bigyihsuan
  • 10.8k
  • 1
  • 23
  • 66

Rust, 195195 181 bytes

|s:&str|s.chars().map(|c|c.to_digit(10).unwrap()).rev().enumerate().map(|(i,n)|if i%2>0{2*n}else{n}|n<<i%2).map(|n|format!("{n}").chars().map(|c|c.to_digit(10).unwrap()).sum::<u32>()).sum::<u32>()%10<1;

Attempt This Online!Attempt This Online!

I'm pretty unfamiliar with Rust golf, so feel free to suggest savings.

  • -14 by @ceilingcat

Explanation

|s:&str|                           // input is a &str
s                                  // original string
.chars()                           // get the characters
.map(|c|c.to_digit(10).unwrap())   // convert each character into a digit
.rev()                             // reversed
.enumerate()                       // into pairs of (index, digit)
.map(|(i,n)|if i%2>0{2*n}else{n}|n<<i%2)                // into n or 2*n if the index is odd
.map(                              // getting the digit sum...
  |n|format!("{n}")                // to string
  .chars()                         // get the chars chars
  .map(|c|c.to_digit(10).unwrap()) // convert to digits
  .sum::<u32>())                   // sum them up
.sum::<u32>()                      // sum all the numbers up
 %10<1;                            // check if divisible by 10

Rust, 195 bytes

|s:&str|s.chars().map(|c|c.to_digit(10).unwrap()).rev().enumerate().map(|(i,n)|if i%2>0{2*n}else{n}).map(|n|format!("{n}").chars().map(|c|c.to_digit(10).unwrap()).sum::<u32>()).sum::<u32>()%10<1;

Attempt This Online!

I'm pretty unfamiliar with Rust golf, so feel free to suggest savings.

Explanation

|s:&str|                           // input is a &str
s                                  // original string
.chars()                           // get the characters
.map(|c|c.to_digit(10).unwrap())   // convert each character into a digit
.rev()                             // reversed
.enumerate()                       // into pairs of (index, digit)
.map(|(i,n)|if i%2>0{2*n}else{n})  // into n or 2*n if the index is odd
.map(                              // getting the digit sum...
  |n|format!("{n}")                // to string
  .chars()                         // get the chars chars
  .map(|c|c.to_digit(10).unwrap()) // convert to digits
  .sum::<u32>())                   // sum them up
.sum::<u32>()                      // sum all the numbers up
 %10<1;                            // check if divisible by 10

Rust, 195 181 bytes

|s:&str|s.chars().map(|c|c.to_digit(10).unwrap()).rev().enumerate().map(|(i,n)|n<<i%2).map(|n|format!("{n}").chars().map(|c|c.to_digit(10).unwrap()).sum::<u32>()).sum::<u32>()%10<1;

Attempt This Online!

I'm pretty unfamiliar with Rust golf, so feel free to suggest savings.

  • -14 by @ceilingcat

Explanation

|s:&str|                           // input is a &str
s                                  // original string
.chars()                           // get the characters
.map(|c|c.to_digit(10).unwrap())   // convert each character into a digit
.rev()                             // reversed
.enumerate()                       // into pairs of (index, digit)
.map(|(i,n)|n<<i%2)                // into n or 2*n if the index is odd
.map(                              // getting the digit sum...
  |n|format!("{n}")                // to string
  .chars()                         // get the chars chars
  .map(|c|c.to_digit(10).unwrap()) // convert to digits
  .sum::<u32>())                   // sum them up
.sum::<u32>()                      // sum all the numbers up
 %10<1;                            // check if divisible by 10
Source Link
bigyihsuan
  • 10.8k
  • 1
  • 23
  • 66

Rust, 195 bytes

|s:&str|s.chars().map(|c|c.to_digit(10).unwrap()).rev().enumerate().map(|(i,n)|if i%2>0{2*n}else{n}).map(|n|format!("{n}").chars().map(|c|c.to_digit(10).unwrap()).sum::<u32>()).sum::<u32>()%10<1;

Attempt This Online!

I'm pretty unfamiliar with Rust golf, so feel free to suggest savings.

Explanation

|s:&str|                           // input is a &str
s                                  // original string
.chars()                           // get the characters
.map(|c|c.to_digit(10).unwrap())   // convert each character into a digit
.rev()                             // reversed
.enumerate()                       // into pairs of (index, digit)
.map(|(i,n)|if i%2>0{2*n}else{n})  // into n or 2*n if the index is odd
.map(                              // getting the digit sum...
  |n|format!("{n}")                // to string
  .chars()                         // get the chars chars
  .map(|c|c.to_digit(10).unwrap()) // convert to digits
  .sum::<u32>())                   // sum them up
.sum::<u32>()                      // sum all the numbers up
 %10<1;                            // check if divisible by 10