Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Apply an arithmetic operation using 2 hashes

   1   users_hash = {"tom" => 1,  
   2   "adi" => 1,
   3   "adi2" => 1,    
   4   "aaron" => 1 }
   5  
   6   users_hash2 = {"tom" => 2,  
   7   "adi" => 3,  
   8   "aaron" => 4 }
   9  
  10  class Hash
  11    def +(hash2)
  12      self.each do |key, value|
  13        self[key] += hash2[key] if hash2.has_key? key
  14      end
  15    end
  16  end
  17  
  18  c = users_hash + users_hash2


=> {"adi2"=>1, "adi"=>4, "tom"=>3, "aaron"=>5}

update: 14:35 5-Sep-08
In addition, the following code copies over items from the right hash which are not present in the left hash:
   1  class Hash
   2    def +(hash2)
   3      temp = Hash.new
   4      self.each do |key, value|
   5        temp[key] = self[key] + hash2[key] unless hash2[key].nil?
   6      end
   7  
   8      hash2.update(temp)
   9      self.update(hash2)
  10    end
  11  end
  12  
  13  c = users_hash + users_hash2

==> {"adi2"=>1, "adi3"=>3, "adi"=>1, "tom"=>3, "aaron"=>5}

Reference:
- Ruby hash merge (Add element to Ruby hash) - by Ruby on Rails [rubyonrailsexamples.com]
- Manipulating Structured Data in Ruby > Working with Hashes [informit.com]

A solution for the "Primary Arithmetic" problem

A solution for the "Primary Arithmetic" problem.

Problem description:
http://icpcres.ecs.baylor.edu/onlinejudge/external/100/10035.html

Author: Joana Matos Fonseca da Trindade
Date: 2008.04.04

   1  
   2  /**
   3   * Solution for the "Primary Arithmetic" problem.
   4   * UVa ID: 10035
   5   */ 
   6  #include <iostream.h> 
   7  #include <stdlib.h> 
   8  
   9  using namespace std; 
  10  
  11  int main() {
  12      unsigned long long n1; /* 1st number */ 
  13      unsigned long long n2; /* 2nd number */ 
  14      int carry = 0; /* carry */ 
  15      int sum = 0; /* temporary sum */ 
  16      int count = 0; /* carry counter */ 
  17  
  18      while(cin >> n1 >> n2 && ((n1 > 0) || (n2 > 0))) { 
  19          carry = 0; 
  20  	count = 0; 
  21  	sum = 0; 
  22  
  23  	/* while there's still something.. */ 
  24  	while ((n1 > 0) || (n2 > 0)) { 
  25  	    /* sum the two right-most digits */ 
  26  	    sum = carry + (n1 % 10) + (n2 % 10); 
  27  
  28  	    if (sum >= 10) { 
  29  		count++; 
  30  	    } 
  31  			
  32  	    /* get the carry by dividing the sum of the two digits */ 
  33  	    carry = sum / 10; 
  34  
  35  	    /* 'reduce' the numbers by ten, to update the right-most digits */ 
  36  	    n1 /= 10; 
  37              n2 /= 10; 
  38  	} 
  39  		
  40  	if (count == 0) { 
  41              cout << "No carry operation." << endl; 
  42  	} else if (count == 1) { 
  43  	    cout << "1 carry operation." << endl; 
  44  	} else { 
  45              cout << count << " carry operations." << endl; 
  46          } 
  47      } 
  48  
  49      return 0;
  50  } 
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS