How do I write a loop like this in Ruby?  hashTable = [node(None, -1, None, None, -1) for i in range(0, tableLength)]   I already tried the following:  hash_table = [table_length.times do |x|   x = NODE.new(@key, @val, @next, @prev, @pos)   x end]   Where I'm passing the values for @key, @val... when I'm calling the class, as follows:  this = NODE.new(nil, -1, nil, nil, -1) this.read_file("alice.txt")   But it's not working.
121693
To do the equivalent of Python list comprehensions, I'm doing the following:  some_array.select{|x| x % 2 == 0 }.collect{|x| x * 3}   Is there a better way to do this...perhaps with one method call?
8608
It's clear that a search performance of the generic HashSet&lt;T&gt; class is higher than of the generic List&lt;T&gt; class. Just compare the hash-based key with the linear approach in the List&lt;T&gt; class.  However calculating a hash key may itself take some CPU cycles, so for a small amount of items the linear search can be a real alternative to the HashSet&lt;T&gt;.  My question: where is the break-even?  To simplify the scenario (and to be fair) let's assume that the List&lt;T&gt; class uses the element's Equals() method to identify an item.
64302
I frequently find myself writing code like this:  List&lt;int&gt; list = new List&lt;int&gt; { 1, 3, 5 }; foreach (int i in list) {     Console.Write("{0}\t", i.ToString()); } Console.WriteLine();   Better would be something like this:  List&lt;int&gt; list = new List&lt;int&gt; { 1, 3, 5 }; Console.WriteLine("{0}\t", list);   I suspect there's some clever way of doing this, but I don't see it. Does anybody have a better solution than the first block?
4327
Is there a way in Java's for-each loop  for(String s : stringArray) {   doSomethingWith(s); }   to find out how often the loop has already been processed?  Aside from using the old and well-known for(int i=0; i &lt; boundary; i++) - loop, is the construct  int i = 0; for(String s : stringArray) {   doSomethingWith(s);   i++; }   the only way to have such a counter available in a for-each loop?
111486
So if I have to choose between a hash table or a prefix tree what are the discriminating factors that would lead me to choose one over the other. From my own naive point of view it seems as though using a trie has some extra overhead since it isn't stored as an array but that in terms of run time (assuming the longest key is the longest english word) it can be essentially O(1) (in relation to the upper bound). Maybe the longest english word is 50 characters?  Hash tables are instant look up once you get the index. Hashing the key to get the index however seems like it could easily take near 50 steps.  Can someone provide me a more experienced perspective on this? Thanks!
77207
I would like to write a function GetHashCodeOfList() which returns a hash-code of a list of strings regardless of order. Given 2 lists with the same strings should return the same hash-code.   ArrayList list1 = new ArrayList()     list1.Add("String1"); list1.Add("String2"); list1.Add("String3");      ArrayList list2 = new ArrayList()     list2.Add("String3");     list2.Add("String2");  list2.Add("String1");  GetHashCodeOfList(list1) = GetHashCodeOfList(list2) //this should be equal.   I had a few thoughts:    I can first sort the list, then combine the sorted list into 1 long string and then call GetHashCode(). However sorting is a slow operation. I can get the hash of each individual string (by calling string.GetHashCode()) in the list, then multiplying all hashes and calling Mod UInt32.MaxValue.  For example: "String1".GetHashCode() * "String2".GetHashCode * … MOD UInt32.MaxValue. But this results in a number overflow.   Does anyone have any thoughts?  Thanks in advance for your help.
123912
Recently I wrote a bit of Lua code something like:  local a = {} for i = 1, n do    local copy = a    -- alter the values in the copy end   Obviously, that wasn't what I wanted to do since variables hold references to an anonymous table not the values of the table themselves in Lua.  This is clearly laid out in , but I'd forgotten about it.  So the question is what should I write instead of copy = a to get a copy of the values in a?
123088
I am writing a SQL query creator using some parameters. In Java, it's very easy to detect the last element of an array from inside the for loop by just checking the current array position with the array length.  for(int i=0; i&lt; arr.length;i++){      boolean isLastElem = i== (arr.length -1) ? true : false;         }   In PHP they have non-integer indexes to access arrays. So you must iterate over an array using a foreach loop. This becomes problematic when you need to take some decision (in my case to append or/and parameter while building query).   I am sure there must be some standard way of doing this.   How do you solve this in PHP?
61002
for (i=0 ; i&lt;=10; i++) {     ..     .. }  i=0; while(i&lt;=10) {     ..     ..     i++; }   In for and while loop, which one is better, performance wise?
89126
I need to create a loop to answer part c of the following linear algebra question. I'm new to Matlab so this is extremely confusing. I'm not sure how to make a loop that "counts" from 0-25 in order to generate a new matrix for each value. If someone could help me out it'd be great.
48764
Does anyone know how the built in dictionary type for python is implemented?  My understanding is that it is some sort of hash table, but I haven't been able to find any sort of definitive answer.
61295
I have a class.  Public Class Foo     Private _Name As String     Public Property Name() As String         Get             Return _Name         End Get         Set(ByVal value As String)             _Name = value         End Set     End Property      Private _Age As String     Public Property Age() As String         Get             Return _Age         End Get         Set(ByVal value As String)             _Age = value         End Set     End Property      Private _ContactNumber As String     Public Property ContactNumber() As String         Get             Return _ContactNumber         End Get         Set(ByVal value As String)             _ContactNumber = value         End Set     End Property   End Class   I want to loop through the properties of the above class. eg;  Public Sub DisplayAll(ByVal Someobject As Foo)     For Each _Property As something In Someobject.Properties         Console.WriteLine(_Property.Name &amp; "=" &amp; _Property.value)     Next End Sub
106
I need a good explanation (references are a plus) on Python's slice notation.   To me, this notation needs a bit of picking up.   It looks extremely powerful, but I haven't quite got my head around it.
13955
Unless I am missing an obvious built-in method, what is the quickest way to get the nth occurrence of a string within a string?  I realize that I could loop the  method by updating its start index on each iteration of the loop. But doing it this way seems wasteful to me.
23996
I am a newbie to Python and have come across the following example in my book that is not explained very well. Here is my print out from the interpreter:  &gt;&gt;&gt; s = 'spam' &gt;&gt;&gt; s[:-1] 'spa'   Why does slicing with no beginning bound and a '-1' return every element except the last one? Is calling s[0:-1] logically the same as calling s[:-1]? They both return the same result. But I'm not sure what python is doing exactly. Any help would be greatly appreciated.
36918
Is the singleton class in Ruby a class in and of itself? Is it the reason why all objects belong to "class?" The concept is fuzzy, but I believe it has something to do with why I can define a class method at all (class foo; def foo.bar ...).  What is the singleton class in Ruby?
116900
Is there an easy way to create tree nodes from data in a file? I need the tree to have nodes in the format 1,2,3,4 rather than 1,2,4,8 etc. I can't create the nodes in a loop as there would be two child nodes for each node.  I'm picking up the data in each cell and declaring a variable for each entry. Is there an easier way to create the following tree? Each row in the file is a node, and I will eventually be filling each node with columns from each row.  \documentclass{standalone} \usepackage{catchfile,tikz} \usepackage{pgfplotstable} \usepackage{filecontents}   \begin{filecontents*}{\jobname.dat} a&amp;1&amp;2&amp;3\\ b&amp;1&amp;2&amp;3\\ c&amp;1&amp;2&amp;3\\ d&amp;1&amp;2&amp;3\\ e&amp;1&amp;2&amp;3\\ f&amp;1&amp;2&amp;3\\ \end{filecontents*}  \begin{document}  \pgfplotstableread[col sep=&amp;,header=false]{\jobname.dat}{\firsttable}     \pgfplotstablegetelem{0}{[index]0}\of{\firsttable} \let\RowZeroColZero\pgfplotsretval \pgfplotstablegetelem{1}{[index]0}\of{\firsttable} \let\RowOneColZero\pgfplotsretval \pgfplotstablegetelem{2}{[index]0}\of{\firsttable} \let\RowTwoColZero\pgfplotsretval \pgfplotstablegetelem{3}{[index]0}\of{\firsttable} \let\RowThreeColZero\pgfplotsretval \pgfplotstablegetelem{4}{[index]0}\of{\firsttable} \let\RowFourColZero\pgfplotsretval \pgfplotstablegetelem{5}{[index]0}\of{\firsttable} \let\RowFiveColZero\pgfplotsretval  \begin{tikzpicture} [grow=right, sloped,dot/.style={circle,fill,inner sep=0.5pt}] \coordinate  node{\RowZeroColZero}     child {     node {\RowTwoColZero}             child {node {\RowFiveColZero}}             child {node[text opacity = 0] {\RowFiveColZero}}     }     child {     node {\RowOneColZero}             child {node {\RowFourColZero}}             child {node {\RowThreeColZero}}     } ;  \end{tikzpicture} \end{document}     At each node, if I could even extract the node coordinate and locate a row in the file with the node reference, that would be a great help in reducing the amount of code.