kaprekar 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/bin/kaprekar +20 -0
- data/lib/kaprekar.rb +66 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dcd25c24ae2f0604b3d311be4be259fa28d90a5d
|
4
|
+
data.tar.gz: 51c869f2788a3936e994458e65aa4d6e2ff07d8f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5008860caa1b2b70596f8db5759e46107137b404baf01c4ec555c8781843732b116ebd509d0e363c34b59627050163a808a8a57326bf08f637c90d8400a88430
|
7
|
+
data.tar.gz: 2a1ef7e28ee20e037ef4ee825d35e39fdd8a1cd8c4f50c232b35360c9d887cff126bb9cb591525f81ef785e063656b1165dcc45cb3d1834650e61f38c21e94fa
|
data/bin/kaprekar
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require './lib/kaprekar'
|
4
|
+
|
5
|
+
abort if ARGV[0].nil?
|
6
|
+
input = ARGV[0][/[^A-z]+/]
|
7
|
+
|
8
|
+
if input.size > 4
|
9
|
+
puts 'Too many digits!'
|
10
|
+
puts 'Please try again with a four digit number with 4 unique digits:'
|
11
|
+
abort
|
12
|
+
end
|
13
|
+
|
14
|
+
if input.split('').uniq.join != input
|
15
|
+
puts 'error: non-unique digits in number, try again'
|
16
|
+
puts 'Please enter a four digit number with 4 unique digits:'
|
17
|
+
abort
|
18
|
+
end
|
19
|
+
|
20
|
+
Kaprekar.new(ARGV[0]).pretty
|
data/lib/kaprekar.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require_relative 'kaprekar/num'
|
2
|
+
require_relative 'kaprekar/errors'
|
3
|
+
|
4
|
+
# Class for exploring Kaprekar's constant.
|
5
|
+
class Kaprekar
|
6
|
+
attr_accessor :input
|
7
|
+
attr_reader :result, :iterations
|
8
|
+
alias_method :count, :iterations
|
9
|
+
|
10
|
+
def initialize(input = nil)
|
11
|
+
return self if input.nil?
|
12
|
+
@input = input
|
13
|
+
end
|
14
|
+
|
15
|
+
def calculate(input = nil)
|
16
|
+
@input = input unless input.nil?
|
17
|
+
sanitize_input
|
18
|
+
fail InvalidInput unless valid_input?
|
19
|
+
kaprekar
|
20
|
+
@result = { input: @input,
|
21
|
+
iterations: @iterations,
|
22
|
+
intermediaries: @intermediaries }
|
23
|
+
end
|
24
|
+
alias_method :run, :calculate
|
25
|
+
alias_method :start, :calculate
|
26
|
+
|
27
|
+
def pretty
|
28
|
+
calculate
|
29
|
+
puts "Input: #{@input}"
|
30
|
+
puts "Total iterations to 6174: #{@iterations}"
|
31
|
+
@result[:intermediaries].each do |int|
|
32
|
+
puts "Number: #{int[:num]}, Count: #{int[:count]}"
|
33
|
+
end
|
34
|
+
nil
|
35
|
+
end
|
36
|
+
alias_method :pp, :pretty
|
37
|
+
|
38
|
+
def valid_input?(input = nil)
|
39
|
+
@input = input unless input.nil?
|
40
|
+
return false if @input.to_i.zero?
|
41
|
+
return false if @input.nil?
|
42
|
+
return false unless @input.to_s.split('').count == 4
|
43
|
+
return false unless @input.to_s.split('').uniq.join == @input.to_s
|
44
|
+
true
|
45
|
+
end
|
46
|
+
alias_method :valid?, :valid_input?
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def sanitize_input
|
51
|
+
@input = @input.to_i if @input.is_a? String
|
52
|
+
end
|
53
|
+
|
54
|
+
def kaprekar
|
55
|
+
input = @input
|
56
|
+
@iterations, result = 0, 0
|
57
|
+
@intermediaries = [{ num: @input, count: iterations }]
|
58
|
+
until result == 6174
|
59
|
+
result = input.backward_sort - input.forward_sort
|
60
|
+
input = result
|
61
|
+
@iterations += 1
|
62
|
+
@intermediaries << { num: result, count: @iterations }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kaprekar
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Topher Saunders
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-31 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Tool for exploring Kaprekar's routine.
|
14
|
+
email: topher6345@gmail.com
|
15
|
+
executables:
|
16
|
+
- kaprekar
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- bin/kaprekar
|
21
|
+
- lib/kaprekar.rb
|
22
|
+
homepage: https://github.com/topher6345/kaprekar
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 2.2.2
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: Exploring Kaprekar routines.
|
46
|
+
test_files: []
|