Page 15 of 15 FirstFirst ... 5131415
Results 141 to 141 of 141

Thread: Beginner Programming Challenge #9

  1. #141
    Join Date
    Nov 2009
    Location
    Ukraine
    Beans
    11

    Re: Beginner Programming Challenge #9

    Written in Ruby

    PHP Code:
    def read_from_file(path)
      
    lines IO.readlines(path) {|ll.chomp}
    end

    lines 
    read_from_file("collector.txt")
    numbers_sum 0
    letters 
    = {}

    lines.each do |line|
      if 
    line =~ /^[a-z]$/
        
    key line.chomp.to_sym
        letters
    [key] = 0 unless letters.keys.include?(key)
        
    letters[key] += 1
      end
      numbers_sum 
    += line.to_i
    end

    puts 
    "Sum = #{numbers_sum}"
    letters.each do |kv|
      
    puts "#{k} = #{v}"
    end 
    Source file:
    PHP Code:
    ~% cat collector.txt 
    a
    a
    a
    c
    b
    b
    3
    5
    0
    d
    a
    c
    d
    1
    1
    0
    0
    d

    Result of executing:
    PHP Code:
    ~% ruby collector.rb
    Sum 
    10
    4
    3
    2

    Last edited by CuracaoThe; July 16th, 2011 at 03:57 PM.

Page 15 of 15 FirstFirst ... 5131415

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •