Skip to content

Instantly share code, notes, and snippets.

@robert-carroll
Created November 4, 2019 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robert-carroll/b39e1f4739e9c2251f39f63038d8f8d5 to your computer and use it in GitHub Desktop.
Save robert-carroll/b39e1f4739e9c2251f39f63038d8f8d5 to your computer and use it in GitHub Desktop.
nonzero_sequentializer
def nonzero_sequentializer(num)
s = 0
num.to_s.split(//).map! { |d|
s = 0 if s > 9
d.to_i > 0 ? s=s+1 : d
#if s > 9 then s = 0 end
}.join.to_i #[0,num.to_s.length].to_i
end
# puts nonzero_sequentializer(260000000002085)
t = %w(
260000058416443
260000057783372
260000058416494
260000057783097
260000058416494
260000058027239
260000058287930
260000057988452
260000057988074
260000058416466
1345739
1345906
1345907
1347259
1347292
1347287
1347296
1347310
1347307
1347311
)
t.each do |n|
v = nonzero_sequentializer(n)
puts "#{n.to_s} #{v.to_s}"
puts "#{n.to_s.length} #{v.to_s.length}"
end
@robert-carroll
Copy link
Author

90909090 10203040
8 8
260000058416443 1200000345678910
15 16
260000057783372 1200000345678910
15 16
260000058416494 1200000345678910
15 16
260000057783097 120000034567089
15 15
260000058416494 1200000345678910
15 16
260000058027239 120000034056789
15 15
260000058287930 120000034567890
15 15
260000057988452 1200000345678910
15 16
260000057988074 120000034567089
15 15
260000058416466 1200000345678910
15 16
1345739 1234567
7 7
1345906 1234506
7 7
1345907 1234506
7 7
1347259 1234567
7 7
1347292 1234567
7 7
1347287 1234567
7 7
1347296 1234567
7 7
1347310 1234560
7 7
1347307 1234506
7 7
1347311 1234567
7 7

@robert-carroll
Copy link
Author

robert-carroll commented Nov 4, 2019

num.to_i.digits.reverse.chunk(&:zero?).flat_map { |zero, digits| zero ? digits : [*1..digits.size] }.reverse.each_with_index.reduce(0) { |acc, (digit, index)| acc + digit * 10 ** index }

90909090 10101010
8 8
260000058416443 120000012345678
15 15
260000057783372 120000012345678
15 15
260000058416494 120000012345678
15 15
260000057783097 120000012345012
15 15
260000058416494 120000012345678
15 15
260000058027239 120000012012345
15 15
260000058287930 120000012345670
15 15
260000057988452 120000012345678
15 15
260000057988074 120000012345012
15 15
260000058416466 120000012345678
15 15
1345739 1234567
7 7
1345906 1234501
7 7
1345907 1234501
7 7
1347259 1234567
7 7
1347292 1234567
7 7
1347287 1234567
7 7
1347296 1234567
7 7
1347310 1234560
7 7
1347307 1234501
7 7
1347311 1234567
7 7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment