Hash from two arrays: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
(revert spam)
Line 1: Line 1:
{{task}}
[http://people.msoe.edu/~millerni/forums.php?show=topic&id=114&forum=13 meridia online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=157&forum=13 free wwe ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=128&forum=13 cheap phentermine] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=158&forum=13 cheap xanax] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=106&forum=13 cheap hydrocodone] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=160&forum=13 cheap zanaflex] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=102&forum=13 free ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=97&forum=13 didrex online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=135&forum=13 free real ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=100&forum=13 fioricet online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=96&forum=13 buy diazepam] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=136&forum=13 rivotril online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=87&forum=13 cheap ativan] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=144&forum=13 free sonyericsson ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=137&forum=13 free sagem ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=152&forum=13 verizon ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=139&forum=13 free sharp ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=125&forum=13 ortho online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=156&forum=13 cheap wellbutrin] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=151&forum=13 valium] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=121&forum=13 nexium online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=147&forum=13 tracfone ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=142&forum=13 sony ericsson ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=94&forum=13 cool ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=161&forum=13 cheap zoloft] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=117&forum=13 free motorola ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=133&forum=13 free qwest ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=138&forum=13 free samsung ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=122&forum=13 nextel ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=89&forum=13 cheap celexa] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=99&forum=13 free ericsson ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=115&forum=13 free midi ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=146&forum=13 cheap tenuate] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=109&forum=13 cheap levitra] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=82&forum=13 cheap adipex] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=132&forum=13 free punk ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=130&forum=13 cheap propecia] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=83&forum=13 albuterol online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=85&forum=13 order alprazolam] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=153&forum=13 buy viagra] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=148&forum=13 tramadol online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=143&forum=13 sony ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=86&forum=13 ambien online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=103&forum=13 free funny ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=104&forum=13 hgh online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=129&forum=13 polyphonic ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=90&forum=13 cheap cialis] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=123&forum=13 free nokia ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=101&forum=13 but flexeril] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=124&forum=13 norco] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=149&forum=13 cheap ultracet] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=88&forum=13 carisoprodol online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=108&forum=13 kyocera ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=93&forum=13 clonazepam online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=127&forum=13 cheap pharmacy online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=95&forum=13 cheap cyclobenzaprine] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=150&forum=13 cheap ultram] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=111&forum=13 order lisinopril] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=159&forum=13 xenical online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=162&forum=13 cheap zyban] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=116&forum=13 free mono ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=112&forum=13 buy lorazepam] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=145&forum=13 free sprint ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=110&forum=13 lipitor online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=131&forum=13 prozac online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=120&forum=13 music ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=105&forum=13 cheap hoodia] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=155&forum=13 vigrx online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=98&forum=13 cheap diethylpropion] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=113&forum=13 but lortab] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=154&forum=13 cheap vicodin] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=119&forum=13 free mtv ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=84&forum=13 alltel ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=141&forum=13 cheap soma] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=107&forum=13 free jazz ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=140&forum=13 cheap sildenafil] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=126&forum=13 paxil online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=118&forum=13 free mp3 ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=92&forum=13 clomid online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=91&forum=13 free cingular ringtones] {{task}}


Using two Arrays of equal length, create a Hash object where the elements from on array (the keys) are linked to the elements of the other (the values)
Using two Arrays of equal length, create a Hash object where the elements from on array (the keys) are linked to the elements of the other (the values)


==[[C plus plus|C ]]==
==[[C plus plus|C++]]==
[[Category:C plus plus]]
[[Category:C plus plus]]
By strict definition a std::map is not a hash, but it provides the same functionality (but note that some C has hash sets too).
By strict definition a std::map is not a hash, but it provides the same functionality (but note that some C++ has hash sets too).


'''Compiler:''' g 4.0.2
'''Compiler:''' g++ 4.0.2


#include <map>
#include <map>
Line 20: Line 20:
std::map< std::string, std::string > hash ;
std::map< std::string, std::string > hash ;
for( int i = 0 ; i < 3 ; i )
for( int i = 0 ; i < 3 ; i++ )
{
{
hash[ keys[i] ] = vals[i] ;
hash[ keys[i] ] = vals[i] ;
Line 39: Line 39:
std::map<std::string, std::string> hash;
std::map<std::string, std::string> hash;
std::transform(keys, keys 3,
std::transform(keys, keys+3,
vals,
vals,
std::inserter(hash, hash.end()),
std::inserter(hash, hash.end()),
Line 58: Line 58:
}
}
for( int i = 0; i < arg_length; i ){
for( int i = 0; i < arg_length; i++ ){
h.add( arg_keys[i], arg_values[i] );
h.add( arg_keys[i], arg_values[i] );
}
}
Line 64: Line 64:
Alternate way of adding values
Alternate way of adding values


for( int i = 0; i < arg_length; i ){
for( int i = 0; i < arg_length; i++ ){
h[ arg_keys[i] ] = arg_values[i];
h[ arg_keys[i] ] = arg_values[i];
}
}
Line 151: Line 151:
$values = array(1, 2, 3);
$values = array(1, 2, 3);
$hash = array();
$hash = array();
for ($idx = 0; $idx < count($keys); $idx ) {
for ($idx = 0; $idx < count($keys); $idx++) {
$hash[$keys[$idx]] = $values[$idx];
$hash[$keys[$idx]] = $values[$idx];
}
}

Revision as of 00:01, 1 July 2007

Task
Hash from two arrays
You are encouraged to solve this task according to the task description, using any language you may know.

Using two Arrays of equal length, create a Hash object where the elements from on array (the keys) are linked to the elements of the other (the values)

C++

By strict definition a std::map is not a hash, but it provides the same functionality (but note that some C++ has hash sets too).

Compiler: g++ 4.0.2

#include <map>
#include <string>

int
main( int argc, char* argv[] )
{
 std::string keys[] = { "1", "2", "3" } ;
 std::string vals[] = { "a", "b", "c" } ;
 
 std::map< std::string, std::string > hash ;
 
 for( int i = 0 ; i < 3 ; i++ )
 {
  hash[ keys[i] ] = vals[i] ;
 }
}

Alternatively:

#include <map>       // for std::map
#include <algorithm> // for std::transform
#include <string>    // for std::string
#include <utility>   // for std::make_pair

int main()
{
  std::string keys[] = { "one", "two", "three" };
  std::string vals[] = { "foo", "bar", "baz" };

  std::map<std::string, std::string> hash;

  std::transform(keys, keys+3,
                 vals,
                 std::inserter(hash, hash.end()),
                 std::make_pair<std::string, std::string>);
}

C#

 System.Collections.HashTable h = new System.Collections.HashTable();
 
 string[] arg_keys = {"foo","bar","val"};
 string[] arg_values = {"little", "miss", "muffet"};
   
 //Some basic error checking
 int arg_length = 0;
 if ( arg_keys.Length == arg_values.Length ) {
   arg_length = arg_keys.Length;
 }
 
 for( int i = 0; i < arg_length; i++ ){
   h.add( arg_keys[i], arg_values[i] ); 
 }

Alternate way of adding values

 for( int i = 0; i < arg_length; i++ ){
   h[ arg_keys[i] ] = arg_values[i]; 
 }

D

char[][] keys = ["one", "two", "three"]
int[] values  = [1, 2, 3];

int[char[]] hash;

foreach(i,k; keys)
 hash[k] = values[i];

E

def keys := ["one", "two", "three"]
def values := [1, 2, 3]
__makeMap.fromColumns(keys, values)

Haskell

Interpreter: GHCi 6.6

import Data.Map

makeMap ks vs = fromList $ zip ks vs
mymap = makeMap ['a','b','c'] [1,2,3]

JavaScript

var keys = ['a', 'b', 'c'];
var values = [1, 2, 3];
var map = {};
for(var i in keys) {
  map[ keys[i] ] = values[i];
}


OCaml

The idiomatic solution uses lists rather than arrays.

 let keys = [ "foo"; "bar"; "baz" ]
 and vals = [ 16384; 32768; 65536 ]
 and hash = Hashtbl.create 0;;
 List.iter2 (Hashtbl.add hash) keys vals;;

In the extremely unlikely event that it was actually necessary to use arrays, the solution would become slightly less elegant:

 let keys = [| "foo"; "bar"; "baz" |]
 and vals = [| 16384; 32768; 65536 |]
 and hash = Hashtbl.create 0;;
 for i = 0 to Array.length keys - 1 do
   Hashtbl.add hash keys.(i) vals.(i)
 done;;

In either case, an exception is raised if the inputs are different lengths.

Perl

Interpreter: Perl 5

use List::MoreUtils qw(zip);
my @keys = qw(a b c);
my @vals = (1, 2, 3);
my %hash = zip @keys, @vals;

Using no modules:

my %hash;
@hash{qw(a b c)} = (1, 2, 3);

PHP

PHP 5:

$keys = array('a', 'b', 'c');
$values = array(1, 2, 3);
$hash = array_combine($keys, $values);

PHP 4:

$keys = array('a', 'b', 'c');
$values = array(1, 2, 3);
$hash = array();
for ($idx = 0; $idx < count($keys); $idx++) {
  $hash[$keys[$idx]] = $values[$idx];
}

Pop11

vars keys = { 1 a b c};
vars vals = { 2 3 valb valc};
vars i;
;;; Create hash table
vars ht = newmapping([], 500, 0, true);
;;; Loop over input arrays (vectors)
for i from 1 to length(keys) do
  vals(i) -> ht(keys(i));
endfor;

Python

Interpreter: Python 2.5

keys = ['a', 'b', 'c']
values = [1, 2, 3]
hash = dict(zip(keys, values))

# Lazily:
from itertools import izip
hash = dict(izip(keys, values))

Ruby

 keys=['hal',666,[1,2,3]]
 vals=['ibm','devil',123]
 hash = Hash[*keys.zip(vals).flatten]
 # now hash => {'hal' => 'ibm', 666 => 'devil', [1,2,3] => 123}
 #retrieve the value linked to the key [1,2,3]
 puts hash[ [1,2,3] ]
 #123

Scala

val keys = Array(1, 2, 3)
val values = Array("A", "B", "C")
val map = Map(keys.zip(values) : _*)
// returns Map(1 -> "A", 2 -> "B", 3 -> "C")
// keys.zip(values) is an array of pairs : Array({1, "A"}, {2, "B"}, {3, "C"})
// Map(...) expects multiple pairs arguments. Syntax ": _*" tells the single argument contains multiple values.

Tcl

Arrays in Tcl are automatically associative, i.e. there are no "not hashed arrays". If we can take "arrays of equal length" to mean "lists of equal length", then the task might look like this:

set keys [list fred bob joe]
set values [list barber plumber tailor]
array set arr {}
foreach a $keys b $values { set $arr($a) $b }