#!/usr/bin/env ruby #Programming Challenges 2003 #isbn: 0-387-00163-8 require "pp" #1.6.1 the 3n+1 problem def cycle_length(number,count=0) #number==1 is the defined end case return count+1 if number==1 # if even divid else *3+1 return number%2 == 0 ? cycle_length(number/2,count+1) : cycle_length((number*3)+1,count+1) end def n3plus1(min,max) raise "min must be less then max" if max=lines.size) || (col+x<0 || col+y>=lines[y].size) lines[y+line][x+col] = lines[y+line][x+col].to_i+1 } } end } } return lines end def minesweper boards = "4 4\n*...\n....\n.*..\n....".split(/\d\s\d\s/).reject!{|x| x.empty?} count = 1 boards.each{|board| puts "Field ##{count}" puts get_chart(board).collect{|x| x.join("")}.join("\n").gsub(".","0") puts "" count+=1 } end #1.6.3 def money_exchange input =<<-BILL 3 10 20 30 4 15 15.01 3 3.01 0 BILL input = input.split loop{ count = input.shift.to_i break if count==0 values = input[0...count] input = input[count..-1] avg = 0.0 values.each{|number| avg+= number.to_f} avg=avg/count change = 0.0 values.select{|number| number.to_f