Skip to content

Commit

Permalink
added some hashes programs
Browse files Browse the repository at this point in the history
  • Loading branch information
Karthikeyan A K committed Aug 25, 2012
1 parent 2963f60 commit 8e673bf
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 2 deletions.
7 changes: 7 additions & 0 deletions hash_creation_1_a.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/ruby
# hash_creation_1_a.rb
marks = { :English => 50, :Math => 70, :Science => 75 }
puts "Key => Value"
marks.each { |a,b|
puts "#{a} => #{b}"
}
4 changes: 2 additions & 2 deletions hash_creation_2.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/ruby
# hash_creation_2.rb
# this does not work, hence must omitted from book
marks = { 'English': 50, 'Math': 70, 'Science': 75 }

marks = { English: 50, Math: 70, Science: 75 }
puts "Key => Value"
marks.each { |a,b|
puts "#{a} => #{b}"
Expand Down
9 changes: 9 additions & 0 deletions x/2n100.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
n = 1

while (2**n) <= (100*n*n)
# n += 0.01
n = n + 0.1
puts n
end

puts n
37 changes: 37 additions & 0 deletions x/additive_combinations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Array
def sum
total = 0
for element in self
total += element
end
total
end

def increment base = 1
array = self.collect {|x| x }
array_length = array.length
i = 0
loop do
unless array[i]
array << 0
return array
end

if array[i] % base == 0 and array[i] !=0
array[i] = 0
i = i+1
else
array[i] = array[i]+1
return array
end
end
end
end

number = 5 # just change this to get various combinations
a = [0]
loop do
puts a.join ' + ' if a.sum == number
break if a.length > number
a = a.increment number
end
18 changes: 18 additions & 0 deletions x/diamond.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# printa a diamond of desired height

height = 7 # height should be minimu 3 and must be a odd number
h = height - 2
width = 2*h-3
j=1

(h-1).downto(0) { |i|
puts " "*(i) + "#"*j
j=j+2
}

(1).upto(h-1) { |i|
puts " "*i + "#" * width
width=width-2

}

1 change: 1 addition & 0 deletions x/factorials.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
re
12 changes: 12 additions & 0 deletions x/fixnum_prime.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Fixnum
def prime?
b = 0
a = self
return true if a <= 2
2.upto(a){ |n|
b = n
break if a%n == 0
}
b == a
end
end
17 changes: 17 additions & 0 deletions x/goldbatch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require './fixnum_prime.rb'

# display every odd number from 4 to 100 as sum of two different prime
# numbers

prime_array = [] #empty array
1.upto(100){ |x| prime_array << x if x.prime? } # gets all pime from 1 to 100 into prime_array

4.step(100,2){ |num|
for prime in prime_array
diff = num - prime
if prime_array.index diff
puts "#{num} = #{prime} + #{diff}"
break # comment break to have all possible combinations
end
end
}
4 changes: 4 additions & 0 deletions x/prime_from_1_to_100.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require './fixnum_prime.rb'

1.upto(100){ |x| puts x if x.prime? }

3 changes: 3 additions & 0 deletions x/pyramid 2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# program prints a pyramid
height = 15
height.times{ |j| puts " "*(height-(1+j)) + "#"*(2*j+1) }
9 changes: 9 additions & 0 deletions x/pyramid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# program prints a pyramid
height = 15
space = height -1
i = 0
height.times{
puts " "*space + "#"*(2*i+1)
space = space -1
i = i+1
}

0 comments on commit 8e673bf

Please sign in to comment.