Search Results

Search found 15255 results on 611 pages for 'sparse array'.

Page 6/611 | < Previous Page | 2 3 4 5 6 7 8 9 10 11 12 13  | Next Page >

  • Card deck and sparse matrix interview questions

    - by MrDatabase
    I just had a technical phone screen w/ a start-up. Here's the technical questions I was asked ... and my answers. What do think of these answers? Feel free to post better answers :-) Question 1: how would you represent a standard 52 card deck in (basically any language)? How would you shuffle the deck? Answer: use an array containing a "Card" struct or class. Each instance of card has some unique identifier... either it's position in the array or a unique integer member variable in the range [0, 51]. Shuffle the cards by traversing the array once from index zero to index 51. Randomly swap ith card with "another card" (I didn't remember how this shuffle algorithm works exactly). Watch out for using the same probability for each card... that's a gotcha in this algorithm. I mentioned the algorithm is from Programming Pearls. Question 2: how to represent a large sparse matrix? the matrix can be very large... like 1000x1000... but only a relatively small number (~20) of the entries are non-zero. Answer: condense the array into a list of the non-zero entries. for a given entry (i,j) in the array... "map" (i,j) to a single integer k... then use k as a key into a dictionary or hashtable. For the 1000x1000 sparse array map (i,j) to k using something like f(i, j) = i + j * 1001. 1001 is just one plus the maximum of all i and j. I didn't recall exactly how this mapping worked... but the interviewer got the idea (I think). Are these good answers? I'm wondering because after I finished the second question the interviewer said the dreaded "well that's all the questions I have for now." Cheers!

    Read the article

  • How to structurally display a multi-dimensional array in PHP?

    - by Jaime Cross
    How can I display the contents of an array as follows: Company Name - Username1 - Username2 Another Company Name - Username3 The array I have created is as follows: $array[1]['company_id'] = '12'; $array[1]['company_name'] = 'ABC Company'; $array[1]['company_type'] = 'default'; $array[1]['user_id'] = '23'; $array[1]['user_name'] = 'Andrew'; $array[2]['company_id'] = '12'; $array[2]['company_name'] = 'ABC Company'; $array[2]['company_type'] = 'default'; $array[2]['user_id'] = '27'; $array[2]['user_name'] = 'Jeffrey'; $array[3]['company_id'] = '1'; $array[3]['company_name'] = 'Some Company'; $array[3]['company_type'] = 'default'; $array[3]['user_id'] = '29'; $array[3]['user_name'] = 'William'; $array[4]['company_id'] = '51'; $array[4]['company_name'] = 'My Company'; $array[4]['company_type'] = 'default'; $array[4]['user_id'] = '20'; $array[4]['user_name'] = 'Jaime';

    Read the article

  • Dynamic Array traversal in PHP

    - by Kristoffer Bohmann
    I want to build a hierarchy from a one-dimensional array and can (almost) do so with a more or less hardcoded code. How can I make the code dynamic? Perhaps with while(isset($array[$key])) { ... }? Or, with an extra function? Like this: $out = my_extra_traverse_function($array,$key); function array_traverse($array,$key=NULL) { $out = (string) $key; $out = $array[$key] . "/" . $out; $key = $array[$key]; $out = $array[$key] ? $array[$key] . "/" . $out : ""; $key = $array[$key]; $out = $array[$key] ? $array[$key] . "/" . $out : ""; $key = $array[$key]; $out = $array[$key] ? $array[$key] . "/" . $out : ""; return $out; } $a = Array(102=>101, 103=>102, 105=>107, 109=>105, 111=>109, 104=>111); echo array_traverse($a,104); Output: 107/105/109/111/104

    Read the article

  • PHP arrays - How to 1-dimensional array into nested multidimensional array?

    - by sombe
    When retrieving a hierarchical structure from MySQL (table with one ID column and one PARENT column signifying the hierarchical relationships), I map the result into an enumerated array as follows (for this example the numbers are arbitrary): Array ( [3] => Array ( [7] => Array () ), [7] => Array ( [8] => Array () ) ) Notice 3 is the parent of 7, and 7 is the parent of 8 (this could go on and on; and any parent could have multiple children). I wanted to shrink this array into a nested multidimensional array as follows: Array ( [3] => Array ( [7] => Array ( [8] => Array () ) ) ) That is, each NEW id is automatically assigned an empty array. Regardless, any ID's children will be pushed into their parent's array. Take a look at the following illustration for further clarification: This will probably result in a complicated recursive operation, since I always have to check whether a parent with any certain ID already exists (and if so, push the value into its array). Is there a built-in php function that can assist me with this? Do you have any idea as to how to go about constructing this? For what it's worth I'm using this to built a navigation bar in wordpress (which can contain categories, subcategories, posts... essentially anything).

    Read the article

  • Casting Type array to Generic array?

    - by George R
    The short version of the question - why can't I do this? I'm restricted to .NET 3.5. T[] genericArray; // Obviously T should be float! genericArray = new T[3]{ 1.0f, 2.0f, 0.0f }; // Can't do this either, why the hell not genericArray = new float[3]{ 1.0f, 2.0f, 0.0f }; Longer version - I'm working with the Unity engine here, although that's not important. What is - I'm trying to throw conversion between its fixed Vector2 (2 floats) and Vector3 (3 floats) and my generic Vector< class. I can't cast types directly to a generic array. using UnityEngine; public struct Vector { private readonly T[] _axes; #region Constructors public Vector(int axisCount) { this._axes = new T[axisCount]; } public Vector(T x, T y) { this._axes = new T[2] { x, y }; } public Vector(T x, T y, T z) { this._axes = new T[3]{x, y, z}; } public Vector(Vector2 vector2) { // This doesn't work this._axes = new T[2] { vector2.x, vector2.y }; } public Vector(Vector3 vector3) { // Nor does this this._axes = new T[3] { vector3.x, vector3.y, vector3.z }; } #endregion #region Properties public T this[int i] { get { return _axes[i]; } set { _axes[i] = value; } } public T X { get { return _axes[0];} set { _axes[0] = value; } } public T Y { get { return _axes[1]; } set { _axes[1] = value; } } public T Z { get { return this._axes.Length (Vector2 vector2) { Vector vector = new Vector(vector2); return vector; } public static explicit operator Vector(Vector3 vector3) { Vector vector = new Vector(vector3); return vector; } #endregion }

    Read the article

  • Understanding c-pointers for rows in 2-dimensional array

    - by utdiscant
    I have the following code: int main() { int n = 3, m = 4, a[n][m], i, j, (* p)[m] = a; for (i = 0; i < n; i++) for (j = 0; j < m; j++) a[i][j] = 1; p++; (*p)[2] = 9; return 0; } I have a hard time understanding what p is here, and the consequences of the operations on p in the end. Can someone give me a brief explanation of what happens. I know c-pointers in their simple settings, but here it get slightly more complicated.

    Read the article

  • Scipy sparse... arrays?

    - by spitzanator
    Hey, folks. So, I'm doing some Kmeans classification using numpy arrays that are quite sparse-- lots and lots of zeroes. I figured that I'd use scipy's 'sparse' package to reduce the storage overhead, but I'm a little confused about how to create arrays, not matrices. I've gone through this tutorial on how to create sparse matrices: http://www.scipy.org/SciPy_Tutorial#head-c60163f2fd2bab79edd94be43682414f18b90df7 To mimic an array, I just create a 1xN matrix, but as you may guess, Asp.dot(Bsp) doesn't quite work because you can't multiply two 1xN matrices. I'd have to transpose each array to Nx1, and that's pretty lame, since I'd be doing it for every dot-product calculation. Next up, I tried to create an NxN matrix where column 1 == row 1 (such that you can multiply two matrices and just take the top-left corner as the dot product), but that turned out to be really inefficient. I'd love to use scipy's sparse package as a magic replacement for numpy's array(), but as yet, I'm not really sure what to do. Any advice? Thank you very much!

    Read the article

  • k-means clustering in R on very large, sparse matrix?

    - by movingabout
    Hello, I am trying to do some k-means clustering on a very large matrix. The matrix is approximately 500000 rows x 4000 cols yet very sparse (only a couple of "1" values per row). The whole thing does not fit into memory, so I converted it into a sparse ARFF file. But R obviously can't read the sparse ARFF file format. I also have the data as a plain CSV file. Is there any package available in R for loading such sparse matrices efficiently? I'd then use the regular k-means algorithm from the cluster package to proceed. Many thanks

    Read the article

  • How to define a static array without a contant size in a constructor of a class? (C++)

    - by Keand64
    I have a class defined as: class Obj { public: int width, height; Obj(int w, int h); } and I need it to contain a static array like so: int presc[width][height]; however, I cannot define within the class, so it it possible to create a pointer to a 2D array (and, out of curiosity, 3, 4, and 5D arrays), have that as a member of the class, and intitalize it in the constructor like: int ar[5][6]; Obj o(5, 6, &ar); If that isn't possible, is there any way to get a static array without a contant size in a class, or am I going to have to use a dynamic array? (Something I don't want to do because I don't plan on ever changing the size of the array after it's created.)

    Read the article

  • How to remove duplicates from multidimensional array in php

    - by JackTurky
    i have an array like: Array ( [prom] => Array ( [cab] => Array ( [0] => Array ( [code] => 01 [price1] => 1000 [price2] => 2000 [available] => 2 [max] => 2 [gca] => 2 ) [1] => Array ( [code] => 04 [price1] => 870 [price2] => 2500 [available] => 3 [max] => 4 [gca] => 10 ) [2] => Array ( [code] => 01 [price1] => 1000 [price2] => 2000 [available] => 2 [max] => 2 [gca] => 2 ) [3] => Array ( [code] => 05 [price1] => 346 [price2] => 1022 [available] => 10 [max] => 2 [gca] => 20 ) ) [cab1] => Array........ ) [prom1] = Array.... ) What i have to do is to remove duplicates inside every [cab*] array.. so to have something like: Array ( [prom] => Array ( [cab] => Array ( [0] => Array ( [code] => 01 [price1] => 1000 [price2] => 2000 [available] => 2 [max] => 2 [gca] => 2 ) [1] => Array ( [code] => 04 [price1] => 870 [price2] => 2500 [available] => 3 [max] => 4 [gca] => 10 ) [2] => Array ( [code] => 05 [price1] => 346 [price2] => 1022 [available] => 10 [max] => 2 [gca] => 20 ) ) [cab1] => Array........ ) [prom1] = Array.... ) In know that there is array_unique combined with array_map to remove duplicates.. but i know that it works only on 2D array.. what can i do? can someone help me pls? thanks!!!

    Read the article

  • Sum Values in Multidimensional Array

    - by lemonpole
    Hello all. I'm experimenting with arrays in PHP and I am setting up a fake environment where a "team's" record is held in arrays. $t1 = array ( "basicInfo" => array ( "The Sineps", "December 25, 2010", "lemonpole" ), "overallRecord" => array ( 0, 0, 0, 0 ), "overallSeasons" => array ( "season1.cs" => array (0, 0, 0), "season2.cs" => array (0, 0, 0) ), "matches" => array ( "season1.cs" => array ( "week1" => array ("12", "3", "1"), "week2" => array ("8", "8" ,"0"), "week3" => array ("8", "8" ,"0") ), "season2.cs" => array ( "week1" => array ("9", "2", "5"), "week2" => array ("12", "2" ,"2") ) ) ); What I am trying to achieve is to add all the wins, loss, and draws, from each season's week to their respective week. So for example, the sum of all the weeks in $t1["matches"]["season1.cs"] will be added to $t1["overallSeasons"]["season1.cs"]. The result would leave: "overallSeasons" => array ( "season1.cs" => array (28, 19, 1), "season2.cs" => array (21, 4, 7) ), I tried to work this out on my own for the past hour and all I have gotten is a little more knowledge of for-loops and foreach-loops :o... so I think I now have the basics down such as using foreach loops and so on; however, I am still fairly new to this so bear with me! I can get the loop to point to $t1["matches"] key and go through each season but I can't seem to figure out how to add all of the wins, loss, and draw, for each individual week. For now, I'm only looking for answers concerning the overall seasons sum since I can work from there once I figure out how to achieve this. Any help will be much appreciated but please, try and keep it simple for me... or comment the code accordingly please! Thanks!

    Read the article

  • Specific complex array sorting

    - by TheDeadMedic
    Okay, a before; Array ( 'home' => array('order' => 1), 'about' => array(), 'folio' => array('order' => 2), 'folio/web' => array('order' => 2), 'folio/print' => array('order' => 1) 'contact' => array('order' => 2) ) And a desired after; Array ( 'home' => array('order' => 1), 'contact' => array('order' => 2), 'folio' => array('order' => 2), 'folio/print' => array('order' => 1), 'folio/web' => array('order' => 2), 'about' => array() ) I know, horrific (don't ask!) See how the slash in the key indicates children, and how the order is nested accordingly? And items without orders are simply shifted to the bottom. But also how multiple 'same level' items with the same order are merely sorted by key?

    Read the article

  • PHP: Recursive array function

    - by Industrial
    Hi everybody, I want to create a function that returns the full path from a set node, back to the root value. I tried to make a recursive function, but ran out of luck totally. What would be an appropriate way to do this? I assume that a recursive function is the only way? Here's the array: Array ( [0] => Array ( [id] => 1 [name] => Root category [_parent] => ) [1] => Array ( [id] => 2 [name] => Category 2 [_parent] => 1 ) [2] => Array ( [id] => 3 [name] => Category 3 [_parent] => 1 ) [3] => Array ( [id] => 4 [name] => Category 4 [_parent] => 3 ) ) The result I want my function to output when getting full path of node id#4: Array ( [0] => Array ( [id] => 1 [name] => Root category [_parent] => ) [1] => Array ( [id] => 3 [name] => Category 3 [_parent] => 1 ) [2] => Array ( [id] => 4 [name] => Category 4 [_parent] => 3 ) ) The notoriously bad example of my recursive skills: function recursive ($id, $array) { $innerarray = array(); foreach ($array as $k => $v) { if ($v['id'] === $id) { if ($v['_parent'] !== '') { $innerarray[] = $v; recursive($v['id'], $array); } } } return $innerarray; } Thanks!

    Read the article

  • Convert PostgreSQL array to PHP array

    - by EarthMind
    I have trouble reading Postgresql arrays in PHP. I have tried explode(), but this breaks arrays containing commas in strings, and str_getcsv() but it's also no good as PostgreSQL doesn't quote the Japanese strings. Not working: explode(',', trim($pgArray, '{}')); str_getcsv( trim($pgArray, '{}') );

    Read the article

  • Convert between python array and .NET Array

    - by dungema
    I have a python method that returns a Python byte array.array('c'). Now, I want to copy this array using System.Runtime.InteropServices.Marshal.Copy. This method however expects a .NET array. import array from System.Runtime.InteropServices import Marshal bytes = array.array('c') bytes.append('a') bytes.append('b') bytes.append('c') Marshal.Copy(bytes, dest, 0, 3) Is there a way to make this work without copying the data? If not, how do I convert the data in the Python array to the .NET array?

    Read the article

  • Formatting associative array declaration

    - by Drew Stephens
    When declaring an associative array, how do you handle the indentation of the elements of the array? I've seen a number of different styles (PHP syntax, since that's what I've been in lately). This is a pretty picky and trivial thing, so move along if you're interested in more serious pursuits. 1) Indent elements one more level: $array = array( 'Foo' => 'Bar', 'Baz' => 'Qux' ); 2) Indent elements two levels: $array = array( 'Foo' => 'Bar', 'Baz' => 'Qux' ); 3) Indent elements beyond the array constructor, with closing brace aligned with the start of the constructor: $array = array( 'Foo' => 'Bar', 'Baz' => 'Qux' ); 4) Indent elements beyond the array construct, with closing brace aligned with opening brace: $array = array( 'Foo' => 'Bar', 'Baz' => 'Qux' ); Personally, I like #3—the broad indentation makes it clear that we're at a break point in the code (constructing the array), and having the closing brace floating a bit to the left of all of the array's data makes it clear that this declaration is done.

    Read the article

  • Javascript array length incorrect on array of objects

    - by Serenti
    Could someone explain this (strange) behavior? Why is the length in the first example 3 and not 2, and most importantly, why is the length in the second example 0? As long as the keys are numerical, length works. When they are not, length is 0. How can I get the correct length from the second example? Thank you. a = []; a["1"] = {"string1":"string","string2":"string"}; a["2"] = {"string1":"string","string2":"string"}; alert(a.length); // returns 3 b = []; b["key1"] = {"string1":"string","string2":"string"}; b["key2"] = {"string1":"string","string2":"string"}; alert(b.length); // returns 0

    Read the article

  • Sort array by keys of another array.

    - by marbrun
    Hello There are 2 arrays, both with the same length and with the same keys: $a1 = [1=>2000,65=>1354,103=>1787]; $a2 = [1=>'hello',65=>'hi',103=>'goodevening']; asort($a1); The keys of a1 and a2 are id's from a database. a1 gets sorted by value. Once sorted, how can we use the same sorting order in a2? Thanks!

    Read the article

  • Echo a multidimensional array in PHP

    - by Jennifer
    I have a multidimensional array and I'm trying to find out how to simply "echo" the elements of the array. The depth of the array is not known, so it could be deeply nested. In the case of the array below, the right order to echo would be: This is a parent comment This is a child comment This is the 2nd child comment This is another parent comment This is the array I was talking about: Array ( [0] => Array ( [comment_id] => 1 [comment_content] => This is a parent comment [child] => Array ( [0] => Array ( [comment_id] => 3 [comment_content] => This is a child comment [child] => Array ( [0] => Array ( [comment_id] => 4 [comment_content] => This is the 2nd child comment [child] => Array ( ) ) ) ) ) ) [1] => Array ( [comment_id] => 2 [comment_content] => This is another parent comment [child] => Array ( ) ) )

    Read the article

  • get the array with html array path

    - by antpaw
    hey, i have this path name from an input element interesse[angebote][flurfuerderfahrzeuge] as a string in my php var. now i need convert it somehow (with regex or explode()) so it looks like this: $_POST['interesse']['angebote']['flurfuerderfahrzeuge'] and the use eval() to get the value. But I'm sure there must be a much easier way do this. Any ideas? Thanks!

    Read the article

  • This for array colllision function doesn't work with anything but first object in array

    - by Zee Bashew
    For some reason, this simple simple loop is totally broken. (characterSheet is my character Class, it's just a movieClip with some extra functionality) (hitBox, is basically a square movieclip) Anyway: every time hitBox make contact with a characterSheet in a different order than they were created: Nothing happens. The program only seems to be listening to collisions that are made with o2[0]. As soon as another hitBox is created, it pushes the last one out of o2[0] and the last one becomes totally useless. What's super weird is that I can hit characterSheets in any order I like.... public function collisions(o1:Array, o2:Array) { if((o1.lenght>=0)&&(o2.length>=0)){ for (var i = 0; i < o1.length; i++) { var ob1 = o1[i]; for (var f = 0; f < o1.length; f++) { var ob2 = o2[f]; if (ob1 is characterSheet) { if (ob2.hitTestObject(ob1)) { var right:Boolean = true; if (ob1.x < hitBox(ob2).origin.x) right = false; characterSheet(ob1).specialDamage(hitBox(ob2).damageType, hitBox(ob2).damage, right); }}}}}} Also it might be somewhat helpful to see the function for creating a new hitBox public function SpawnHitBox(targeted, following, atype, xoff, yoff, ... args) { var newHitBox = new hitBox(targeted, following, atype, xoff, yoff, args); badCollisionObjects.push(newHitBox); arraydictionary[newHitBox] = badCollisionObjects; addChild(newHitBox); }

    Read the article

  • Find key of parent in array / PHP

    - by 106691756905536410593
    Perhaps someone can help me out with this one: I'm using a basic search function to find an array deep within an array. The problem is, once that array is found, I'd also like to return it's parent key. Is there a PHP function that can determine the parent key of an array? Below is an example of the Search Function... Ideally I'd like to return the array that is found, as well as it's parent key. function search($array, $key, $value){ $results = array(); if (is_array($array)){ if ($array[$key] == $value){ $results[] = $array; } foreach ($array as $subarray){ $results = array_merge($results, search($subarray, $key, $value)); } } return $results; }

    Read the article

  • Message "Sparse file not allowed" after succesfull install without swap-partition

    - by FUZxxl
    I've installed Ubuntu without creating a swap partition and with / on a btrfs.# Now I get the message "Sparse file is not allowed" on each boot. This message appears before the splash-screen. Is there a way to kill this warning? # I use some programs that tend to get havoc on memory usage. To prevent them from killing my system, I let the OOM killer do the rest when out of memory rather than making my system unreasonably slow by excessive swapping.

    Read the article

  • PHP : How to insert an array into other child array by same index?

    - by airi
    i am new to array. Hope you can help me. i have 2 array. Let say: array 1 = [[50,60],[70,80]] array 2 = ["Fire", "Marine"] array 1 index : Array([0]=Array ([0]=50 [1]=60) [1]=Array ([0]=70 [1]=80)) array 2 index : Array ([0]= Fire [1]=Marine) How i can insert array 2 into array 1 like output below : Output that i want like below: array 3 = [["Fire",50,60],["Marine",70,80]] array 3 index = (Array([0]=Array ([0]=Fire [1]=50 [2]=60) [1]=Array ([0]=Marine [1]=70 [2]=80)) Thanks

    Read the article

< Previous Page | 2 3 4 5 6 7 8 9 10 11 12 13  | Next Page >