short-url-creator 0.1.0

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.
Files changed (2) hide show
  1. data/lib/short-url-creator.rb +75 -0
  2. metadata +55 -0
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # file: short-url-creator.rb
4
+
5
+ class ShortUrlCreator
6
+ def initialize()
7
+ @chars = ('a'..'z').to_a + ('1'..'9').to_a + ('A'..'Z').to_a
8
+ @array_size = @chars.size
9
+ @h = Hash.new
10
+ @chars.each {|c| @h[c] = '0'}
11
+ vowels = %w(a e i o u A E I O U 4 3 1 0)
12
+ vowels.each {|v| @h[v] = '1'}
13
+ nums = %w(2 5 6 7 8 9)
14
+ nums.each {|n| @h[n] = '2'}
15
+ @count = 0
16
+ @a = Array.new(7, -1)
17
+ @k = 0
18
+ end
19
+
20
+ def iterate_chars(array_size)
21
+ (0..array_size).each {|i|
22
+ increment_index(@k)
23
+ convert_to_chars()
24
+ }
25
+ end
26
+
27
+ def convert_to_chars()
28
+ buffer = ''
29
+ @a.each {|i|
30
+ buffer << @chars[i] if i >= 0
31
+ }
32
+ a = buffer.reverse.scan(/./)
33
+ k = a.length
34
+ if (k > 1)
35
+ if ((@h[a[k-2]] + @h[a[k-1]]) != '10')
36
+ puts buffer.reverse
37
+ end
38
+ else
39
+ puts buffer.reverse
40
+ end
41
+ end
42
+
43
+ def get_short_url(count)
44
+ if count > @array_size
45
+ new_count = count - @array_size
46
+ iterate_chars(@array_size)
47
+ get_short_url(new_count)
48
+ else
49
+ iterate_chars(count)
50
+ end
51
+ end
52
+
53
+ def increment_a(i)
54
+ if @a[i] < @array_size - 1
55
+ @a[i] = @a[i] + 1
56
+ return i
57
+ else
58
+ @a[i] = 0
59
+ return i += 1
60
+ end
61
+ end
62
+
63
+ def increment_index(k)
64
+ old_k = k
65
+ k = increment_a(k)
66
+
67
+ if k != old_k
68
+ increment_index(k)
69
+ else
70
+ k = 0
71
+ end
72
+ k
73
+ end
74
+
75
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: short-url-creator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors: []
7
+
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-10-10 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/short-url-creator.rb
26
+ has_rdoc: true
27
+ homepage:
28
+ licenses: []
29
+
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.3.5
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: short-url-creator
54
+ test_files: []
55
+