body
stringlengths
25
86.7k
comments
list
answers
list
meta_data
dict
question_id
stringlengths
1
6
<p>As an exercise, I have implemented a circular bounded queue using arrays as the base data structure. I'd appreciate it if someone can review it for me.</p> <pre><code> class BoundedQueue&lt;T&gt; { T[] que; int head; // remove from head int tail; // insert at tail public BoundedQue...
[]
[ { "body": "<ol>\n<li>Exception handling should be added\nto the constructor, handling a negative\n<code>quesize</code>. Or take a look at Code\nContracts.</li>\n<li>Instead of initializing <code>head</code> and\n<code>tail</code> to <code>-1</code>, you could use\n<a href=\"http://msdn.microsoft.com/en-us/libra...
{ "AcceptedAnswerId": "1217", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-09T23:21:17.183", "Id": "1216", "Score": "14", "Tags": [ "c#", "queue", "circular-list" ], "Title": "Circular Bounded Queue using C#" }
1216
<p>This Common Lisp program is an exercise to print an integer and its digits reversed to the screen:</p> <pre><code>(defun read-number () (format t "Enter a number: ~%") (read)) (defun reverse-string (the-string) (if (eq (length the-string) 0) "" (concatenate 'string (reverse-string (subseq the-string 1...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-10T02:24:08.913", "Id": "2149", "Score": "0", "body": "Wow, lots of Lisp exercises. Is this for school, or did you just pick it up for the hell of it? :)" }, { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-10T0...
[ { "body": "<p>You're talking about digits and integer, but to me, as an non-lisper, it looks as if you operate on Strings. </p>\n\n<ul>\n<li>I would take the number, modulo 10, print that digit. </li>\n<li>If the rest is > 0 recursively call the function with the (number - digit) / 10. </li>\n</ul>\n\n<p>In mos...
{ "AcceptedAnswerId": "1221", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-10T02:10:39.440", "Id": "1219", "Score": "4", "Tags": [ "strings", "lisp", "common-lisp" ], "Title": "Print an integer and its digits reversed" }
1219
<p>I need to place a Google map in Colorbox overlay and am wondering if there's a better way to do this. I've just started out with Google's tutorial so there's nothing fancy going on.</p> <p>Right now I'm hiding the div that Google loads the map into:</p> <pre><code>&lt;div style="width:0; height: 0; overflow: hidd...
[]
[ { "body": "<p>If you want to dynamically insert the Google Map, instead of simply linking to an href, you can do as follows:</p>\n\n<p>In your HTML, have the basic outline for the colorbox ready:</p>\n\n<pre><code>&lt;div id=\"box\"&gt;\n &lt;div class=\"google-map\"&gt;&lt;/div&gt;\n&lt;/div&gt;\n</code></p...
{ "AcceptedAnswerId": "1290", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-10T04:22:50.250", "Id": "1223", "Score": "6", "Tags": [ "google-apps-script", "google-maps" ], "Title": "Placing a Google map in Colorbox" }
1223
<p>I have two helper methods that return li for view with the li for current view having id as selected. </p> <pre><code>def get_li(a) li = "&lt;li" li += ' id=selected' if (a[:controller] == params[:controller] &amp;&amp; a[:action] == params[:action]) li += "&gt;" + link_to(a[:text], { :controller =&gt; a[:con...
[]
[ { "body": "<p>First of all let's talk about method names:</p>\n\n<p>The <code>get_</code> prefix is usually not used in ruby - you'd just leave it off.</p>\n\n<p>Further the name <code>get_li</code> (or just <code>li</code>) is not very descriptive of what the method does. It tells me that it will produce an <c...
{ "AcceptedAnswerId": "1232", "CommentCount": "0", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-10T04:54:24.250", "Id": "1224", "Score": "7", "Tags": [ "ruby", "ruby-on-rails" ], "Title": "Refactor Helper methods" }
1224
<p>I needed a function that would take a list of folders and removed all sub folders, so that only the top level folders stayed in the list. </p> <p>For example, given the list:</p> <pre><code>c:\stuf\morestuff c:\stuf\morestuff\sub1 c:\otherstuf c:\otherstuf\sub1 c:\otherstuf\sub2 </code></pre> <p>I wanted the list...
[]
[ { "body": "<pre><code>is there an issue with using variables inside anonymous methods that were declared outside?\n</code></pre>\n\n<p>no, if an anonymous method accesses a local variable then C# will create a separate class to host the method and variable allowing it all to \"just work\". (*)</p>\n\n<pre><code...
{ "AcceptedAnswerId": "1231", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-10T05:04:50.357", "Id": "1225", "Score": "11", "Tags": [ "c#", "strings", "collections" ], "Title": "Filtering a recursive directory listing, discarding subfolders" }
1225
<p>This code is intended to find all possible solutions to a cryptoarithmetic problem. The description of the problem I was trying to solve is here:</p> <blockquote> <p>In cryptoarithmetic problems, we are given a problem wherein the digits are replaced with characters representing digits. A solution to such a ...
[]
[ { "body": "<p>Some preliminary notes for now (I'll add later):</p>\n\n<p>Whenever you need to write <code>(if n n 2)</code> or <code>(if (not n) 2 n)</code>, you can instead write <code>(or n 2)</code>. <code>or</code> will take any number of arguments and return either <code>nil</code> or the first argument th...
{ "AcceptedAnswerId": "1251", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-10T06:26:11.830", "Id": "1227", "Score": "4", "Tags": [ "lisp", "cryptography", "common-lisp" ], "Title": "Finding a cryptoarithmetic solution" }
1227
<p>I saw a sassy CSS for a rails app like this:</p> <pre><code>$std-margin: 30px; @mixin solid-bg($color: #EEEEEE) { background: none repeat scroll 0 0 $color; } @mixin solid-border($direction, $width: 1px, $color: black) { @if $direction == "" { border: { color: $color ; width: $width; sty...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-10T11:39:18.790", "Id": "2160", "Score": "1", "body": "Optimization usually depends on the HTML. Can you provide it with use cases for all the rules? Also could you point out the advantages you see in your mixins over just using `backg...
[ { "body": "<p>One optimization you should do in any case is not to have all rules inside <code>html { ... }</code> and <code>body { ... }</code>, because the resulting CSS rules all require the renderer to unnecessarily check every single element if it is actually a decedent of <code>html</code> and <code>body<...
{ "AcceptedAnswerId": null, "CommentCount": "3", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-10T08:40:48.553", "Id": "1229", "Score": "5", "Tags": [ "css", "sass" ], "Title": "Good practice using SCSS" }
1229
<p>I have this code in a class sharing a private static array of data. Internally it's encoded, but I need the iterator for a <code>foreach</code> loop to show the decoded values.</p> <p>Is there a native array function that can remap the keys and values via a callback function?</p> <pre><code>public function getIter...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-10T23:51:12.673", "Id": "2187", "Score": "0", "body": "I just had a thought to possibly implement `Iterator` instead of `IteratorAggregate` which would reduce the need for duplicating the array before iterating, and instead, decode dur...
[ { "body": "<p>Implementing <code>Iterator</code> is the way to go in my opinion. As for your question, <a href=\"http://php.net/manual/en/function.array-map.php\" rel=\"nofollow\"><code>array_map</code></a> would allow you to decode the values but not the keys. But you can combine that with other array operatio...
{ "AcceptedAnswerId": "1246", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-10T17:48:35.670", "Id": "1238", "Score": "3", "Tags": [ "php", "php5" ], "Title": "Decoding name/value pairs" }
1238
<p>I have a function that takes in a controller instance, a method, and parameters (this is for a little MVC experiment I'm working on). The purpose of the function is to execute the controller method with the given parameters.</p> <p>However, there are a few caveats. First, if the controller has a "router" method, it...
[]
[ { "body": "<p>That's certainly a bastardization of the <code>switch</code> statement.</p>\n\n<p>What you should use is an <code>if...else if...</code> group:</p>\n\n<pre><code>$response = null; //initialize\nif ( $controller-&gt;using_router)\n{\n $response = $controller-&gt;router();\n}\nelse\n{\n $response ...
{ "AcceptedAnswerId": "1240", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-10T17:53:09.507", "Id": "1239", "Score": "5", "Tags": [ "php", "url-routing" ], "Title": "Switch statement in PHP - cleaner way to determine priority?" }
1239
<p>I am locating the find method of List in C# with the single Expression type parameter</p> <p>I am using the following ( where T is a List, in my case a SubSonic IActiveRecord )</p> <pre><code>MethodInfo info = typeof(T).GetMethods(BindingFlags.Static | BindingFlags.Public) .FirstOrDef...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-10T22:06:08.313", "Id": "2179", "Score": "0", "body": "IEnumerable expose one single method named GetEnumerator..." }, { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-10T22:23:10.513", "Id": "2180", "Sc...
[ { "body": "<p>When searching for methods using reflection I found that the most future-proof way is by searching exactly for the method, so not only the Count() is correct but I would also have checked both what is this parameter type and what is the return type of the function.</p>\n", "comments": [ ...
{ "AcceptedAnswerId": "1245", "CommentCount": "6", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-10T22:00:18.193", "Id": "1244", "Score": "5", "Tags": [ "c#" ], "Title": "Best way to locate Find on List<T> with reflection" }
1244
<p>I would like to have my code reviewed, I'm trying to get a general model I'm using for my stuff going and I don't want to keep using wrong or inefficient code if I can.</p> <p>My file structure is like this: </p> <p><img src="https://i.stack.imgur.com/0equs.png" alt="File structure"></p> <p>In conf there's a file...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-11T13:51:36.463", "Id": "2189", "Score": "0", "body": "Saw your comment about indenting 4 spaces to get it into a code block. Just paste it next time, highlight the code, and hit the little 101010 button. It'll auto-indent it the 4 s...
[ { "body": "<p>That's a good split to start off with, although I strongly encourage you to place your database conf file outside the public html area on your server.</p>\n", "comments": [ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-11T15:04:12.267", "Id": "2191"...
{ "AcceptedAnswerId": "1295", "CommentCount": "1", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-11T12:28:45.490", "Id": "1247", "Score": "5", "Tags": [ "php", "classes" ], "Title": "Splitting code, am I doing it right?" }
1247
<p>I need to subclass <code>UIButton</code>, however the design of <code>UIButton</code> makes this really painful. This is because to create a button you really need to call:</p> <pre><code>[UIButton buttonWithType:UIButtonTypeRoundedRect] </code></pre> <p>So if I want to subclass I really need to do whatever it is ...
[]
[ { "body": "<p>I think I've realised the problem myself. Memory management gets very confused because we are now essentially tracking two objects that are actually the same object.</p>\n", "comments": [], "meta_data": { "CommentCount": "0", "ContentLicense": "CC BY-SA 2.5", "CreationDat...
{ "AcceptedAnswerId": "1919", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-11T15:41:49.007", "Id": "1249", "Score": "2", "Tags": [ "object-oriented", "objective-c", "memory-management", "constructor" ], "Title": "Subclassing UIButton" }
1249
<p>I'm making a simple container class for fun and education, but my rebuilding/resizing method seems rather inefficient. Is there an easier way to do this?</p> <pre><code>// If FromFront is true, cells should be added or // subtracted from the front of the array rather than the back. void Rebuild(std::size_t NewSize...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-11T18:41:52.283", "Id": "2193", "Score": "0", "body": "I must be missing something...why are you even using a temporary array?" }, { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-11T20:00:38.553", "Id": "21...
[ { "body": "<pre><code>void Rebuild(std::size_t NewSize, bool FromFront = false)\n{\n const std::size_t OldSize = Size;\n\n Datatype* NewStorage = new Datatype[NewSize]; \n</code></pre>\n\n<p>Rather then creating a temporary array and copying the existing data into it, just create the new array and copy in...
{ "AcceptedAnswerId": "1253", "CommentCount": "2", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-11T18:27:55.153", "Id": "1252", "Score": "8", "Tags": [ "c++", "algorithm", "classes" ], "Title": "Simplifying a Resizing Method" }
1252
<pre><code>#include&lt;stdafx.h&gt; #include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; #include&lt;string&gt; #include&lt;iostream&gt; using namespace std; string temp[10]; void extract(char * c) { int ind = 0; //char *temp = (char *)malloc(sizeof(char)); while(*c!= NULL) { if(*c!= ' ') // read a...
[]
[ { "body": "<h3>In response to your specific questions:</h3>\n\n<ol>\n<li>Use a vector. Using a vector you can just append to the end and it will resize itself as necessary.</li>\n<li>You can not use <code>printf</code> with <code>string</code>s as <code>printf</code> is a C function and does not know C++'s <cod...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-11T22:08:19.547", "Id": "1254", "Score": "4", "Tags": [ "c++", "array" ], "Title": "Reverse a word using string manipulation" }
1254
<p>I have developed an application that interacts with IBM ClearQuest. The problem is that when I run everything locally, such as, run the webservice local and then ASP page local, everything is at the speed I expect. When I post the webservice (precompiled) to the server and run the web page through the server, the ca...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-12T19:52:58.103", "Id": "2210", "Score": "3", "body": "Try profiling your application on server." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-11-28T23:03:45.927", "Id": "59517", "Score": "0", "b...
[ { "body": "<p>This being the oldest <a href=\"/questions/tagged/c%23\" class=\"post-tag\" title=\"show questions tagged &#39;c#&#39;\" rel=\"tag\">c#</a> <a href=\"https://codereview.meta.stackexchange.com/a/1511/23788\">zombie</a> on the site, I thought the code could use a bit of reviewing. I don't think I ca...
{ "AcceptedAnswerId": null, "CommentCount": "5", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-11T22:52:36.640", "Id": "1257", "Score": "5", "Tags": [ "c#", ".net", "vb.net" ], "Title": "WebMethod slower on execution than Windows application of the same code" }
1257
<p>I'm trying to figure out a way to simplify implementing paged searching in sql server.</p> <p>My restrictions are: </p> <ol> <li>has to be in a proc. </li> <li>can't use dynamic sql.</li> </ol> <p>Here's my sample query:</p> <pre><code>DECLARE @TenantId int DECLARE @MemberStatusId int DECLARE @SearchTerm varcha...
[]
[ { "body": "<p>It looks pretty good to me. With a cost of 100% on the <code>FullTextMatch</code>, I'd consider running this as a standard statement and hard code the variables <strong>just to see what the execution plan says.</strong> \nI do have one question though. Your case statement for <code>@ColumnName = '...
{ "AcceptedAnswerId": "23795", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-11T23:24:00.897", "Id": "1258", "Score": "4", "Tags": [ "sql", "sql-server", "search" ], "Title": "SQL paged search results" }
1258
<p>What do you think about this code?</p> <pre><code>#include &lt;iostream&gt; using std::cout; using std::endl; #include &lt;vector&gt; using std::vector; #include &lt;ctime&gt; #include &lt;cstdlib&gt; template &lt; typename T &gt; vector &lt; T &gt; concatenate( vector &lt; T &gt; _a_, T piv, vector &lt; T &gt; ...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-12T03:43:23.883", "Id": "2199", "Score": "0", "body": "Sorry v_list shouldn't be there" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-12-25T11:09:48.327", "Id": "11146", "Score": "0", "body": "My ...
[ { "body": "<p>If this is not just a learning exercise, I have to point out that C++ already has a sorting function in its standard library and in real code you should use that. So for the rest of this answer I'm going to assume that this is in fact just a learning exercise.</p>\n\n<p>Now first a note on the alg...
{ "AcceptedAnswerId": "1262", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-12T03:36:27.133", "Id": "1259", "Score": "5", "Tags": [ "c++", "algorithm", "sorting", "reinventing-the-wheel", "quick-sort" ], "Title": "Quicksort application" }
1259
<p>I have the following code:</p> <pre><code>print o # {'actor_name': [u'Keanu Reeves', u'Laurence Fishburne', u'Carrie-Anne Moss', u'Hugo Weaving', u'Glor # ia Foster', u'Joe Pantoliano', u'Marcus Chong', u'Julian Arahanga', u'Belinda McClory', u'Matt Doran # '], 'played': [u'Neo (Thomas Anderson)', u'Morfeusz', u'Tr...
[]
[ { "body": "<p>You're looking for <a href=\"http://docs.python.org/library/functions.html#zip\"><code>zip()</code></a>.</p>\n\n<pre><code>print zip(o['actor_name'], o['played'])\n</code></pre>\n\n<p>or to make it look like your output</p>\n\n<pre><code>for entry in zip(o['actor_name'], o['played']):\n print e...
{ "AcceptedAnswerId": "1261", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-12T09:47:08.833", "Id": "1260", "Score": "4", "Tags": [ "python" ], "Title": "Merging two lists from dictionary. How to do this in a better way?" }
1260
<p>Is this the proper method for creating an OpenGL 3.3+ forward-compatible context?</p> <pre><code>// Create window WNDCLASSEX wc; ZeroMemory( &amp;wc, sizeof( wc ) ); wc.cbSize = sizeof( wc ); wc.lpfnWndProc = _eventHandler; wc.hInstance = GetModuleHandle( NULL ); wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); wc.hIc...
[]
[ { "body": "<p>This <em>looks</em> right to me. I am a bit tired right now and usually use <a href=\"http://www.glfw.org/\" rel=\"nofollow\">GLFW</a>.</p>\n\n<p>You might be interested in <a href=\"http://www.opengl.org/wiki/Tutorial%3a_OpenGL_3.1_The_First_Triangle_%28C%2B%2B/Win%29\" rel=\"nofollow\">this page...
{ "AcceptedAnswerId": "1377", "CommentCount": "0", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-12T14:34:39.043", "Id": "1263", "Score": "9", "Tags": [ "c++" ], "Title": "OpenGL forward-compatible context creation on Windows" }
1263
<p>The title sucks, but it's really very simple - given an array and a starting element, cycle between one element above, below, then two element above, and below the starting element. For example: </p> <pre><code>Start: 5 5 &gt; 6 &gt; 4 &gt; 7 &gt; 3 &gt; 8 &gt; 2 ... </code></pre> <p>It doesn't matter if the code...
[]
[ { "body": "<p>First off, your code was a bit difficult to read, because you combined the algorithm you need with your functionality, that you didn't explain. It's not only easier for people helping you, but also yourself, if you try to separate your problem from the application.</p>\n\n<p>Your attempt was to ca...
{ "AcceptedAnswerId": "1269", "CommentCount": "0", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-12T17:11:02.237", "Id": "1264", "Score": "4", "Tags": [ "javascript", "array" ], "Title": "Cycling between element above and below current array element" }
1264
<pre><code>$($(this).parent().parent().parent().parent().parent().parent().children()[0]).html() </code></pre>
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-11-25T02:44:44.640", "Id": "58826", "Score": "2", "body": "This question appears to be off-topic because it is about how bad the code is." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-11-25T03:46:37.177", "...
[ { "body": "<pre><code>.parent().parent().parent().parent().parent().parent()\n</code></pre>\n\n<p>This can be much simplified by using <code>parents()</code> which returns an array, which you can just access with an index. I.e. you can replace the above with:</p>\n\n<pre><code>.parents()[5]\n</code></pre>\n\n<p...
{ "AcceptedAnswerId": "1266", "CommentCount": "3", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-12T17:53:06.013", "Id": "1265", "Score": "2", "Tags": [ "javascript", "jquery" ], "Title": "How bad is this jQuery?" }
1265
<p>A while back I wrote a <a href="http://brandontilley.com/2011/01/31/gist-tag-for-jekyll.html" rel="nofollow">Fluid tag for Jekyll</a> that inserts the contents of a <a href="https://gist.github.com/" rel="nofollow">GitHub Gist</a> into the content of the page in <code>&lt;noscript&gt;</code> tags for RSS readers. I'...
[]
[ { "body": "<p>A couple of mostly minor notes and improvements:</p>\n\n<pre><code>return \"\" unless @text =~ /([\\d]*) (.*)/\ngist, file = $1.strip, $2.strip\n</code></pre>\n\n<p>I generally try to avoid magic variables where ever possible. Using <code>match</code> and the resulting <code>MatchData</code> objec...
{ "AcceptedAnswerId": "1272", "CommentCount": "0", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-12T18:45:20.550", "Id": "1268", "Score": "4", "Tags": [ "ruby", "jekyll", "liquid" ], "Title": "Fluid tag for displaying GitHub Gists with noscript tags in a Jekyll post" }
1268
<p>I generally feel that the pattern I use for development is kind of wrong. I start with simple functionality, then when I realize that some pieces can be reused I usually start to write "scope root level" functions in order to be more DRY. I usually place these functions at the bottom of my "scope". Would these funct...
[]
[ { "body": "<p>That's a bit to much code for me to go through in one go but I'll take a stab at one part:</p>\n\n<pre><code>// for each user initiated scroll event \n// poll current section\nsetInterval(function() {\n if ( didScroll &amp;&amp; !scriptScroll ) {\n didScroll = false;\n\n var curScroll = co...
{ "AcceptedAnswerId": "1275", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-12T20:45:48.200", "Id": "1271", "Score": "5", "Tags": [ "javascript", "jquery", "event-handling" ], "Title": "Window-scrolling controller" }
1271
<p>I just realized that I could turn:</p> <pre><code>import MySQLdb def main(): db = MySQLdb.connect( .. ) try: while True: do_thingy() finally: db.close() </code></pre> <p>into:</p> <pre><code>import MySQLdb, atexit def main(): db = MySQLdb.connect( .. ) atexit.regis...
[]
[ { "body": "<p>Firstly, an alternative:</p>\n\n<pre><code>import MySQLdb, contextlib\n\ndef main():\n db = MySQLdb.connect( .. )\n with contextlib.closing(db):\n do_thingy()\n</code></pre>\n\n<p>The database connection will be closed after the with block even in the case of exceptions.</p>\n\n<p>Th...
{ "AcceptedAnswerId": "1277", "CommentCount": "0", "ContentLicense": "CC BY-SA 4.0", "CreationDate": "2011-03-12T22:10:10.403", "Id": "1274", "Score": "4", "Tags": [ "python", "comparative-review" ], "Title": "Should I use atexit to close db connection?" }
1274
<p>I have written these two functions that have a similar process. The first is meant to "split" a string on a given character and the second is meant to "replace-all" instances of a character in a given string. The behavior between the two seems very similar - can you think of a good way to abstract that behavior in...
[]
[ { "body": "<p>I think the later versions are unnecessarily complicated and verbose.</p>\n\n<p>My first idea was to just concatenate the splitted list parts in replace-all – for you can do <code>(concatenate 'string seqs ... )</code> – but that may not be very efficient.</p>\n\n<p>You can use map or loop for rep...
{ "AcceptedAnswerId": "1294", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-14T04:21:39.820", "Id": "1282", "Score": "2", "Tags": [ "strings", "lisp", "common-lisp" ], "Title": "Abstract the (duplicate?) behavior between \"sort\" and \"replace\"" }
1282
<p>I'm learning Python and have found <a href="http://code.google.com/codejam/contest/dashboard?c=635101#s=p0" rel="nofollow noreferrer">this problem</a> from Google Code Jam:</p> <blockquote> <p>How many <code>mkdir</code> commands does it take to construct a given directory tree:</p> <h3>Input</h3> <p>The first line ...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-14T10:46:28.507", "Id": "2236", "Score": "3", "body": "You should really comment your code. Now we will have to basically solve the problem for you before we even understand what your code does. That is a lot of work..." }, { "...
[ { "body": "<p>Rather than using a list, consider approaching the problem from a graph perspective. Build a tree from the root directory using the existing directories, then traverse it with the new directories, and output a result line each time you have to add a leaf to the graph. This should be a little fas...
{ "AcceptedAnswerId": "1284", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-14T05:43:46.670", "Id": "1283", "Score": "6", "Tags": [ "python", "performance", "programming-challenge", "tree", "file-system" ], "Title": "\"File Fix-it\" challenge" }
1283
<p>I initially posted this on StackOverflow, and was redirected here. I have been playing around with Bouncy Castle (Java) for the last few days, and I've reached the point where I believe I can securely exchange secret keys with a Diffie-Hellman exchange.</p> <p>Having read many posts underlining the difficulty of pr...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-14T19:17:35.093", "Id": "2246", "Score": "2", "body": "I think you should use SSL for \"real\" encryption, simply because it's a field-proven protocol, but it would be instructive for you to see how your implementation differs from SSL...
[ { "body": "<p>It's very difficult to answer you, because what you've posted is a test-bed where your byte arrays are passed around in the context of a high-level language, as opposed to a real situation where you'd have (at the very least) two processes communicating down a pipe, TCP socket or similar. The deta...
{ "AcceptedAnswerId": null, "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-14T19:02:46.027", "Id": "1286", "Score": "4", "Tags": [ "java", "security", "cryptography" ], "Title": "Cryptographic key exchange" }
1286
<p>I have a heading, which is an integer and from it I would like to figure out the compass direction (North, North-East,...) and return the appropriate icon. I feel like I'm probably already at the cleanest solution but I would love to be proven wrong.</p> <pre><code>public GetHeadingImage(string iconName, int headin...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2012-08-09T21:30:20.857", "Id": "23555", "Score": "0", "body": "Is it possible at all that the thing, whatever it may be, is not moving at all?" } ]
[ { "body": "<p>Small suggestion: the GetHeadingImage() has a lot of duplicate code.<br/>\nWhy not something like:</p>\n\n<pre><code>public GetHeadingImage(string iconName, int heading){\n return iconName + GetHeadingName(heading) + ICON_FILE_EXTENTION;\n}\n</code></pre>\n\n<p>You could then have the heading ...
{ "AcceptedAnswerId": "1292", "CommentCount": "1", "ContentLicense": "CC BY-SA 4.0", "CreationDate": "2011-03-14T20:18:13.207", "Id": "1287", "Score": "13", "Tags": [ "c#" ], "Title": "Calculating compass direction" }
1287
<p>I'll try to keep this as short as I can without losing context - we have a system for online education, where presentations often have a test.</p> <p>Management has access to a report used for measuring compliance. The report has a few paramaters that need to be selected:</p> <ul> <li>A date range to look in (e.g....
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-15T23:23:40.453", "Id": "2275", "Score": "1", "body": "Yeesh! Is it `Set objResultsRS = objSQLDB.Execute(,,adCmdText)` that's taking so long? If so then capture the SQL before executing. People will find that a lot easier to analyse." ...
[ { "body": "<p>It makes me sad how few people seem to know this basic advice: If you have a program that's slow <em>profile it</em>. It takes five minutes to download a trial of a profiler, hook it up to your app, and see <em>exactly</em> where the bottlenecks are. Computers are much too complicated to reason ab...
{ "AcceptedAnswerId": "1303", "CommentCount": "5", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-15T22:05:39.067", "Id": "1298", "Score": "3", "Tags": [ "optimization", "vbscript", "asp-classic" ], "Title": "Trying to speed up a report that takes freaking forever" }
1298
<p>One of my side projects is a table driven scanner/parser generator, in an effort to reduce the size of the transition table I represent the generated table <code>A[x, y]</code> as <code>A'[x + I[y]]</code> instead of <code>A'[x + y * c]</code>. This allows me to overlap identical parts of the table.</p> <p>The foll...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-16T11:45:04.467", "Id": "2285", "Score": "1", "body": "I feel kinda bad about posting such a large piece of code for review, but I think trimming too much more would compromise the review." }, { "ContentLicense": "CC BY-SA 2.5"...
[ { "body": "<p>Did some reading during the past few days about clean code and I can spot a bit of complexity in yours. Just some things that I think would improve the readability:</p>\n\n<p>Multiple similar blocks like :</p>\n\n<pre><code> states[write] = states1[read1];\n offsets[write] = offsets1...
{ "AcceptedAnswerId": null, "CommentCount": "3", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-16T11:38:43.833", "Id": "1308", "Score": "6", "Tags": [ "c#", "collections", "hash-map" ], "Title": "Overlapping table fragments" }
1308
<p>As you review this, please keep in mind I'm using Jython 2.1 as shipped with IBM's WebSphere 7.0 (their latest-and-greatest). I don't have decorators, properties, new style objects, or any other Python feature invented in the last decade.</p> <p>Problem:</p> <p>Using WebSphere's Jython interface to determine app ...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-18T05:29:10.360", "Id": "2337", "Score": "4", "body": "Were I prone to making uneducated and accusatory statements, I would venture to say that WebSphere is the WTF. Instead, I will just wonder what their rationale is in shipping such...
[ { "body": "<p>I'd say calling <code>self.setattr</code> from <code>__getattr__(self, )</code> is borderline abuse in itself.</p>\n\n<p>I guess you are trying to implement lazy attributes, right?</p>\n\n<p>However in your case valid attributes are constant and their values appear constant too, so why not set the...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-16T19:33:06.480", "Id": "1314", "Score": "10", "Tags": [ "jython" ], "Title": "Encapsulating the WebSphere Jython API" }
1314
<p>I have a word-wrap algorithm that basically generates lines of text that fit the width of the text. Unfortunately, it gets slow when I add too much text.</p> <p>I was wondering if I oversaw any major optimizations that could be made. Also, if anyone has a design that would still allow strings of lines or string poi...
[]
[ { "body": "<p>I have some high-level advice based on a cursory read of your code. It would really help if you could refactor it into multiple methods as this function is too long and does too much. That won't help your speed, but it will make it easier for others to grok your code and provide more advice.</p>\n...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-16T23:55:27.277", "Id": "1316", "Score": "6", "Tags": [ "c++", "optimization" ], "Title": "Optimizing WordWrap Algorithm" }
1316
<blockquote> <p><strong>3.1</strong></p> <p>Write a program that computes the arithmetic sum of a constant difference sequence:</p> <pre><code>D0 = A Dn+1 = Cdn + B </code></pre> <p>Run the following values and compare to the closed form solution:</p> <pre><code>Maximum index = 10,000, a = 1:0; c = 5; b...
[]
[ { "body": "<p>I notice three things about your code:</p>\n\n<ol>\n<li>You're relying on tail-recursion. This is okay if you know your program will only ever run on implementations that perform tail call optimization, but you should be aware that the Common Lisp standard does not require TCO (and more practicall...
{ "AcceptedAnswerId": "1328", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-17T05:56:07.507", "Id": "1318", "Score": "3", "Tags": [ "lisp", "common-lisp" ], "Title": "Compute arithmetic sum of a constant difference sequence" }
1318
<p>I was asked to update a business rule to allow a specific column to be added. Because our datalayer uses only DataSets as an interface to the database that is what we work with in the business layer.</p> <p>Here I have created a couple of <code>DataTable</code> and <code>DataRow</code> extensions to use for this bu...
[]
[ { "body": "<ol>\n<li><p><code>hasColumnChanged</code> method. Inside first <code>if</code> you have two almost same lines. Duplicated code should be extracted:</p>\n\n<pre><code>if (row[col, DataRowVersion.Original] != DBNull.Value &amp;&amp; row[col, DataRowVersion.Current] != DBNull.Value) \n{ \n string ...
{ "AcceptedAnswerId": "1321", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-17T09:39:36.007", "Id": "1319", "Score": "16", "Tags": [ "c#" ], "Title": "Verify which columns have changed in a datatable or datarow" }
1319
<p>I have the following 'custom' tree data structure. This concept was to be thought of as, if given ("abb", 20) and ("abc", 30), then</p> <blockquote> <pre><code>Tree['a']['b']['b'] == 20 </code></pre> </blockquote> <p></p> <blockquote> <pre><code>Tree['a']['b']['c'] == 30 </code></pre> </blockquote> <p>Or</p> <...
[]
[ { "body": "<p>Some suggestions:</p>\n\n<p>You are using a vector for your branches but you are using an index loop to iterate through the vector.<br/>\nI'd consider using the vector iterator.<br/>\nThis will make your code a bit clearer and versatile (if you decide to change the container type for example). Wil...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-17T11:29:19.817", "Id": "1322", "Score": "6", "Tags": [ "c++", "tree" ], "Title": "Optimising a custom tree data structure" }
1322
<blockquote> <p><strong>4.2 Machine Epsilon</strong></p> <p>Find the floating point number epsi that has the the following properties:</p> <ol> <li>1.0+epsi is greater than 1.0 and</li> <li>Let m b e any number less than epsi. Then 1.0+m is equal to 1.0.</li> </ol> <p>epsi is called machine ep...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-17T22:55:06.343", "Id": "2334", "Score": "0", "body": "As a general note you should get used to writing comments and (at least in this case) shorter lines." } ]
[ { "body": "<p>If we assume that a float is represented in memory as a (sign, mantissa, exponent) tuple, and assume a radix of 2, then we can find the machine epsilon exactly. That is, if we can assume the machine stores floats using base-2 representations of the mantissa and exponent, then we know that:</p>\n\...
{ "AcceptedAnswerId": "1337", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-17T13:49:47.917", "Id": "1324", "Score": "2", "Tags": [ "lisp", "common-lisp", "floating-point" ], "Title": "Machine epsilon - find epsi" }
1324
<p>How can I improve this code?</p> <pre><code>public class Address { public string house_no { get; set; } public string street { get; set; } public string zip { get; set; } } public class Contact { public string email { get; set; } public string ph_no { get; set; } publ...
[]
[ { "body": "<p>It is already short and to the point. One minor formatting change; I would make your class properties upper camel case, which is typical for C#. I also dropped the abbreviations.</p>\n\n<pre><code>public class Address { \n public string HouseNumber { get; set; } \n public string Street...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-17T19:17:15.110", "Id": "1329", "Score": "3", "Tags": [ "c#", "json" ], "Title": "Getting contact information" }
1329
<p>I have a model I have created to generate iCal and the necessary JSON for the jQuery calendar plugin <a href="http://arshaw.com/fullcalendar/">Full Calendar</a> to work. When I display the page, the JSON generation takes about 7 seconds. The query itself has been running in only a few microseconds. So I know my code...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-17T19:39:51.803", "Id": "2322", "Score": "0", "body": "This is for `[asp.net-mvc-2]` but I can't tag it as such." }, { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-17T21:08:31.850", "Id": "2331", "Scor...
[ { "body": "<p>Couple of tricks to improve performance (i've got around +10%, not that much though): </p>\n\n<p>1) Preallocate <code>stringBuilder</code> size. You can roughly calculate it's total size as <code>Events.Count * charsPerEvent</code><br>\n 2) In this line: <code>sb.AppendFormat(\"\\\"end\\\":\\\"{...
{ "AcceptedAnswerId": "1338", "CommentCount": "12", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-17T19:39:16.483", "Id": "1330", "Score": "8", "Tags": [ "c#", "asp.net", "asp.net-mvc-2" ], "Title": "My Calendar generation code is slow. How do I improve it?" }
1330
<p>The below code feels ugly, how can I make it better? (The part i don't like is the ||=. I also don't know if I like the resulting data structure. (An array of hashes)</p> <pre><code>def survey_results @survey_results = [] unless params[:step_survey_id].blank? SurveyResult.find_by_step_survey_id(params...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-17T21:31:41.877", "Id": "2332", "Score": "0", "body": "Could you please describe the relevant models (especially SurveyResult and StepSurvey) and their relation to each other? Also which class is `survey_results` a method of?" } ]
[ { "body": "<p>Regarding the data structure: In cases like this where you always use the same keys for the hashes (i.e. <code>:answers</code> and <code>:question</code> in this case), it is often a good idea to use a simple class instead, which has accessors for the given properties. You can use <code>Struct</co...
{ "AcceptedAnswerId": "1335", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-17T20:32:06.763", "Id": "1333", "Score": "2", "Tags": [ "ruby", "ruby-on-rails" ], "Title": "Setting responses on survey results" }
1333
<blockquote> <p><strong>3.23 Palindromes</strong></p> <p>A palindrome is a number that reads the same forwards and backwards, like 12321. Write a program that tests input integers for the palindrome property.</p> <p>Hint: This should not be done with the numbers in their input (character) format.</p...
[]
[ { "body": "<p>This one is easy! Remember the <a href=\"https://codereview.stackexchange.com/questions/1219/common-lisp-print-an-integer-and-its-digits-reversed\"><code>reverse-digits</code> function</a>? If a number is the same as its reverse, then it's a palindrome.</p>\n", "comments": [], "meta_data":...
{ "AcceptedAnswerId": "1341", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-18T05:38:26.310", "Id": "1339", "Score": "3", "Tags": [ "lisp", "common-lisp", "palindrome" ], "Title": "Testing input integers for the palindrome property" }
1339
<p>Typical tutorials suggest simple CoreLocation code similar to the following:</p> <p>Header</p> <pre><code>#import&lt;CoreLocation/CoreLocation.h&gt; @interface MyCLController : NSObject &lt;CLLocationManagerDelegate&gt; { CLLocationManager *locManager; } @property (nonatomic, retain) CLLocationManager *locMana...
[]
[ { "body": "<p>Apple's LocateMe sample has been kept up to date and shows how to properly handle getting a fix with accuracy evalutation and timeout. It's quite an excellent sample so why not start there?</p>\n\n<p>Devices without cellular won't have the benefit of cell triangulation but that's all abstracted an...
{ "AcceptedAnswerId": "2726", "CommentCount": "0", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-18T13:56:07.550", "Id": "1342", "Score": "0", "Tags": [ "objective-c", "ios" ], "Title": "Core Location - Practices for Supporting Common iOS Devices" }
1342
<p>After growing tired of all the ways in which a file loop can be broken (<code>find -print | while read</code>) or unreadable (<code>find -exec</code> with complex commands), I think I've managed to build a <a href="https://github.com/l0b0/tilde/blob/master/examples/safe-find.sh"><code>find</code> template</a> which ...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-23T10:59:01.850", "Id": "2435", "Score": "0", "body": "Either nobody here knows bash, or nobody can break it. ;)" }, { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-24T05:48:49.457", "Id": "2476", "Scor...
[ { "body": "<pre><code>find . -type f -execdir echo START{}END \";\"\nSTART./--$`\\! *@\n\n \\\"' \n/--$`\\! *@\n\n \\\"' \nEND\n</code></pre>\n\n<p>In which cases to you fail with find?</p>\n\n<pre><code>find . -type f -execdir md5sum {} \";\"\n\\d41d8cd98f00b204e9800998ecf8427e ./--$`\\\\! *@\n...
{ "AcceptedAnswerId": "1575", "CommentCount": "7", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-18T14:33:49.160", "Id": "1343", "Score": "12", "Tags": [ "security", "bash" ], "Title": "'find' template for handling any file" }
1343
<p>I have a section of code I use to extract an event log out of a large text file. It works well, it's just my use of <code>list(itertools.takewhile(...))</code> that feels a little sketchy to me.</p> <p>Is there a nicer way of doing this? </p> <pre><code>import itertools testdata = ''' Lots of other lines... Reall...
[]
[ { "body": "<p>Yes, it is indeed a bit sketchy/confusing to use <code>takewhile</code> when you really don't want to take the lines, but discard them. I think it's better to use <code>dropwhile</code> and then use its return value instead of discarding it. I believe that that captures the intent much more clearl...
{ "AcceptedAnswerId": "1345", "CommentCount": "0", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-18T20:15:13.427", "Id": "1344", "Score": "3", "Tags": [ "python" ], "Title": "Extracting lines from a file, the smelly way" }
1344
<p>I figured it's about time to jump into some Haskell, so here's a first attempt at an oddly specific SVG parser:</p> <pre><code>import System.Environment import Text.ParserCombinators.Parsec parseFile fname = parseFromFile (manyTill (try tag) (try readEnd)) fname tag = do manyTill anyChar . try $ lookAhead tagSta...
[]
[ { "body": "<p>Ok, here are some things I noticed:</p>\n\n<p>First of all, while the code seems easy enough to follow as it is, a couple of comments here and there certainly couldn't hurt.</p>\n\n<hr>\n\n<pre><code>tag = do manyTill anyChar . try $ lookAhead tagStart\n char '&lt;'\n name &lt;- ma...
{ "AcceptedAnswerId": "1348", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-19T05:53:38.157", "Id": "1347", "Score": "8", "Tags": [ "beginner", "haskell", "svg", "parsec" ], "Title": "First attempt at a SVG parser" }
1347
<p>The idea here is to implement a simple, threadsafe, observable collection that clients can bind to, whilst background threads can update. Changes in the contained items raise the <code>CollectionChanged</code> event. </p> <p>Feedback appreciated:</p> <pre><code>public class ThreadSafeObservableCollection&lt;T&gt; ...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-20T09:55:55.860", "Id": "2372", "Score": "1", "body": "why item's `propertyChanged` raises `Reset` on the entire collection?" } ]
[ { "body": "<p>Your <code>Items</code> initialization I believe isn't threadsafe. Use <a href=\"https://docs.microsoft.com/en-us/dotnet/api/system.lazy-1?redirectedfrom=MSDN&amp;view=net-5.0\" rel=\"nofollow noreferrer\"><code>Lazy&lt;T&gt;</code></a> for thread safe lazy initialization instead.</p>\n<p>The <a h...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-19T17:10:18.933", "Id": "1349", "Score": "7", "Tags": [ "c#", ".net", "thread-safety" ], "Title": "ThreadSafeObservableCollection of (T)" }
1349
<p>How can this code be improved?</p> <pre><code>using System; //using System.Linq; using System.Collections; using System.Collections.Generic; namespace Combinatorics { public class Permutation { private int[] data = null; private int order = 0; public Permutation(int n) { this.data = new int...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-20T02:35:02.527", "Id": "2367", "Score": "3", "body": "Did you tag this as functional-programming because you want to take the code into a more functional direction? Otherwise I don't see the reason for the tag, this code is as imperat...
[ { "body": "<ol>\n<li><p>Why have you commented out <code>Linq</code> namespace? Use it at least here:</p>\n\n<pre><code>this.data = new int[n];\nfor (int i = 0; i &lt; n; ++i)\n{\n this.data[i] = i;\n}\n</code></pre>\n\n<p>It can be replaced with:</p>\n\n<pre><code>this.data = Enumerable.Range(0, n).ToArray(...
{ "AcceptedAnswerId": null, "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-19T20:07:13.287", "Id": "1352", "Score": "4", "Tags": [ "c#", "functional-programming", "combinatorics" ], "Title": "Generating permutations and combinations" }
1352
<p>The following block of code needs to be as fast as possible as it likely to be called many thousands of times in a run.</p> <p>I am also conscious that my thinking style is leaning towards a more procedural style from time to time and hence I may not be taking full advantage of all the benefits that functional prog...
[]
[ { "body": "<p>You should indent the body of the <code>qarray</code> function to make it more obvious where it starts and where it's end (I mean I know it ends at the end of the file in this case, but that's not immediately apparent at first glance).</p>\n\n<p>You should also rename your function <code>f</code> ...
{ "AcceptedAnswerId": "1363", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-20T19:44:40.553", "Id": "1362", "Score": "4", "Tags": [ "performance", "functional-programming", "csv", "f#" ], "Title": "Processing a tab-delimited file with vertexes and ages...
1362
<p>I'm working on my graduate project and I had stumbled upon a somewhat dilemma, I've managed to solve it with some workarounds but I have my doubts that this is the most efficient way to deal with this problem.</p> <p>I'm writing a class to deal with the Plesk API and making it as flexible as possible for easy use.<...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-23T14:30:24.693", "Id": "2441", "Score": "0", "body": "Just out of curiosity, in your execution script (that last bit), why did you close and open your script to output a one line string? Why not just `echo 'SSL needs to be active';`? ...
[ { "body": "<p>Generally speaking, I like your code. I think it's well planned with few inefficiencies. I'm also impressed by your use of PHP's <code>DOMDocument</code> class. When creating XML via PHP, I usually opt for <code>foreach</code> loops and output buffering. I've been aware of this object class, but y...
{ "AcceptedAnswerId": null, "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-21T13:46:52.077", "Id": "1368", "Score": "7", "Tags": [ "php", "api", "xml" ], "Title": "Generating code with XML" }
1368
<p>While working with DirectShow, I came across the need to easily recognize different known 'sets' of GUIDs. E.g.: There are different GUIDs to indicate the possible time formats: <code>None, Byte, Field, Frame, MediaTime, Sample</code>. Working with enums instead of GUIDs would be a lot more useful in my opinion.</p>...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-21T20:11:30.077", "Id": "2397", "Score": "0", "body": "Do you have to use `Guid`? I can imagine easier ways to map `enum` into `Guid` if you do not it to be truly random." }, { "ContentLicense": "CC BY-SA 2.5", "CreationDat...
[ { "body": "<p>I would avoid casting the same thing more then once. You can simply use <code>as</code>, it will return null if the object is not of the correct type.</p>\n\n<pre><code>public override bool Equals( object obj )\n{\n AbstractGuidEnum&lt;T&gt; guidObj = obj as AbstractGuidEnum&lt;T&gt;;\n if (...
{ "AcceptedAnswerId": null, "CommentCount": "4", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-21T19:28:35.770", "Id": "1371", "Score": "9", "Tags": [ "c#" ], "Title": "Linking GUIDs to enums" }
1371
<p>Given the following problem:</p> <pre><code>;3.3 Tabulate the function k = 2^n for n = 1..50. ;Do this for the fewest possible multiplications.[3] </code></pre> <p>I wrote this answer:</p> <pre><code>(defun k (n) (ash 2 (1- n))) (loop for n from 1 to 50 do (format t "k(~a) = ~a ~%" n (k n))) </code></pre>...
[]
[ { "body": "<p>If you use <code>1</code> instead of <code>2</code> as the number to shift, you won't have to subtract one from <code>n</code>. I.e. you can just define <code>k</code> as <code>(ash 1 n)</code>.</p>\n\n<p>Other than that your code looks fine.</p>\n", "comments": [], "meta_data": { "C...
{ "AcceptedAnswerId": "1392", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-22T06:32:47.253", "Id": "1374", "Score": "2", "Tags": [ "lisp", "common-lisp" ], "Title": "Tabulate k = 2^n minimizing multiplication" }
1374
<p>Given the following problem:</p> <blockquote> <p>It is conjectured that for any \$n &gt; 0\$, \$n^2 + 3n + 5\$ is never divisible by 121. Test this conjecture for \$n = 1,2,...,9999,10000\$.</p> </blockquote> <p>I wrote the following solution:</p> <pre><code>(defpackage :one-twenty-one-divisible (:use :cl)) (...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-04-10T13:24:03.053", "Id": "3062", "Score": "0", "body": "Are you asking if it is evenly divisible? It would have to be a zero of the function to be evenly divisible." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2...
[ { "body": "<p>I don't know Lisp, but you can speed up the calculation by observing that n² + 3n + 5 is never divisible by 11 (and hence never divisible by 121), except when n mod 11 == 4. </p>\n\n<p>If you check the equation for the remaining candidates (11*k+4) in the \"full\" module 121 (e.g. using Lisp, but ...
{ "AcceptedAnswerId": "1712", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-22T06:56:15.970", "Id": "1375", "Score": "6", "Tags": [ "mathematics", "lisp", "common-lisp" ], "Title": "Determining if f(n) = n^2 + 3n + 5 is ever divisible by 121" }
1375
<p>I have successfully placed multiple jquery ui slider on a page and its transalation text too. you can check it live on: <a href="http://outsourcingnepal.com/projects/jQuery%20slider/" rel="nofollow">http://outsourcingnepal.com/projects/jQuery%20slider/</a></p> <p>I have my jquery code as :</p> <pre><code>var arrLa...
[]
[ { "body": "<p>In my option your code has two problems:</p>\n\n<ol>\n<li><p>You unnecessarily fill your HTML with empty elements which you could just create in your script. Changing that would remove the necessity for the <code>rel</code> attribute.</p></li>\n<li><p>more importantly (even if there are people tha...
{ "AcceptedAnswerId": "1387", "CommentCount": "0", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-22T08:01:36.423", "Id": "1376", "Score": "7", "Tags": [ "javascript", "jquery", "jquery-ui" ], "Title": "Review on my multiple JQuery Slider on a page" }
1376
<p>I've written a plugin that takes a generic approach to creating sticky headers and would like to have the code reviewed. Is this acceptable JavaScript?</p> <p><strong><a href="http://robertfall.github.com/Stuck-On-Top/" rel="nofollow">Demo Page</a></strong></p> <p>It's my first plugin however and I'd like feedbac...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-22T16:10:02.420", "Id": "2414", "Score": "0", "body": "A working example would be great, because currently I can't imagine what \"sticky headers\" are supposed to be..." }, { "ContentLicense": "CC BY-SA 2.5", "CreationDate"...
[ { "body": "<p>At the risk of nitpicking there's quite a fiew small/micro-optimizations that could be done:</p>\n\n<ul>\n<li><p>group multiple <code>var</code> declarations using commas</p>\n\n<p><code>var containerTop = container.offset().top, headerOrigin = header.offset().top;</code></p></li>\n<li><p>chain me...
{ "AcceptedAnswerId": "1403", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-22T12:55:21.630", "Id": "1381", "Score": "4", "Tags": [ "javascript", "jquery", "plugin" ], "Title": "jQuery plugin to create sticky headers" }
1381
<p>I have been a programmer for 12 years, mainly ERP software and C development and am looking to make a career/specialty change to Java. I've read it countless times if you want to learn a new language you need to write code, then write some more code and then finally write more code. So I've written some code!</p> ...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-22T13:54:07.423", "Id": "2411", "Score": "1", "body": "Please include the main part of the code that you want reviewed in the post as per the FAQ. Thanks" }, { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-22T...
[ { "body": "<p>Let's start with the most basic, the <code>Card</code> class.</p>\n\n<blockquote>\n<pre><code>import java.util.*;\n</code></pre>\n</blockquote>\n\n<p>Except during development, it's custom to explicitly import only the classes you need instead of using wildcards.</p>\n\n<blockquote>\n<pre><code>pu...
{ "AcceptedAnswerId": "1390", "CommentCount": "5", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-22T13:26:16.540", "Id": "1382", "Score": "24", "Tags": [ "java", "beginner", "game", "playing-cards" ], "Title": "Texas Hold'em in Java" }
1382
<p>I wrote a simple PHP script which allows the user to upload a file to the server. It works, but I'm not very sure about security. Could you give me some hints about security mistakes?</p> <p>In a nutshell: The user uploads a file from form.html, then a file is saved in uploads/ directory on the server, and a direct...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-25T14:15:55.740", "Id": "2528", "Score": "1", "body": "Most php functions return a sensible response or FALSE. I suggest you return FALSE instead of NULL." } ]
[ { "body": "<p>One thing that immediately stands out to me is whether <code>$_FILES['uploadfile']</code> actually exists for each upload request. Do an <code>isset</code> on it first to make sure a client doesn't send a post with fabricated input values - for example: some custom html form of their own making. ...
{ "AcceptedAnswerId": "1418", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-22T15:25:48.460", "Id": "1384", "Score": "5", "Tags": [ "php", "security", "network-file-transfer" ], "Title": "Uploading a file to the server" }
1384
<p>Just as the question states and to be sure, am I missing anything on this? Something important/obvious that I overlooked? As is, I can save any functor, function pointer and member function pointer using this delegate class (copy ctor and assignment operator left out for brevity, as were the versions with parameters...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-22T22:43:14.233", "Id": "2423", "Score": "0", "body": "Have you looked at 'Fastest possible delegates' in codeproject? It's just so you're not reinventing the wheel." }, { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "...
[ { "body": "<p>You are missing const and volatile qualified method pointer versions.</p>\n\n<p>Example:</p>\n\n<pre><code>template&lt;class Obj, class R&gt;\nstruct DelegateHelper&lt;R (Obj::*)() const&gt;{\n typedef R (Obj::*FuncPtr)() const;\n\n DelegateHelper(const Obj* obj_ptr, FuncPtr func)\n :...
{ "AcceptedAnswerId": null, "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-22T16:50:15.437", "Id": "1388", "Score": "4", "Tags": [ "c++", "delegates" ], "Title": "Anything I'm missing on this delegate implementation?" }
1388
<p>Inside of a folder named <code>txt</code> I have 138 text files (totaling 349MB) full of email addresses. I have no idea (yet) how many addresses there are. They are separated from one another by line breaks. I created the following script to read all of these files into an array, dismiss the duplicates, then sort a...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-23T13:28:16.157", "Id": "2438", "Score": "0", "body": "You can probably make the script already twice as fast by utilizing both cores. Separate the processing across two processes, or use two threads. I find it strange that it indicate...
[ { "body": "<p>It's been a long time since I've written PHP, but I think I can give you a few pointers anyhow.</p>\n\n<p>I believe the following line is problematic speed-wise:</p>\n\n<pre><code>if (in_array($val, $preventRepeat) || !strpos($val, '@') || !$val) {\n...\n</code></pre>\n\n<p>The <code>in_array</cod...
{ "AcceptedAnswerId": "1410", "CommentCount": "7", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-22T23:07:17.403", "Id": "1393", "Score": "4", "Tags": [ "php", "csv", "parsing" ], "Title": "73 Lines of Mayhem - Parse, Sort and Save to CSV in PHP CLI" }
1393
<p>I was trying to do a very common interview problem "Finding the max repeated word in a string " and could not find much resources in net for c/c++ implementation. So I coded it myself here. I have tried to do most of the coding from scratch for better understanding. Could you review my code and provide comments on m...
[]
[ { "body": "<p>Some general comments first:</p>\n\n<ol>\n<li><p>Comment, comment, comment. Although it seems pretty clear now, it doesn't hurt to make it really clear for the guy who has to maintain it when you've moved on.</p></li>\n<li><p>You might want to have separate functions for doing the counting of th...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-23T00:44:04.093", "Id": "1394", "Score": "5", "Tags": [ "c++", "array", "interview-questions" ], "Title": "Max repeated word in a string" }
1394
<p>Given the following problem (SICP Exercise 1.3):</p> <blockquote> <p>Define a procedure that takes three numbers as arguments and returns the sum of squares of the two largest numbers.</p> </blockquote> <p>I wrote the following (somewhat clumsy) solution in Scheme. How can I make it better?</p> <pre><cod...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-23T03:50:21.590", "Id": "60463", "Score": "0", "body": "I posted [my solution](http://stackoverflow.com/questions/161666/sicp-exercise-1-3-request-for-comments/161675#161675) to this question some time ago. It doesn't involve any condi...
[ { "body": "<p>Scheme is a lot more functional than Common Lisp. The way you can apply that to this situation is by making more use of passing functions around (to the point that this problem is almost a one-liner). For the puzzle as written, I'd do something like</p>\n\n<pre><code>(define (big-squares a b c)\n ...
{ "AcceptedAnswerId": "1399", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-23T02:08:16.997", "Id": "1395", "Score": "9", "Tags": [ "lisp", "scheme", "sicp" ], "Title": "Sum of squares of two largest of three numbers" }
1395
<p>Given the following task:</p> <blockquote> <p>Use Newton's method to compute the square root of a number. Newton's method involves successive approximation. You start with a guess, and then continue averaging successive guesses until you reach a satisfactory level of precision.</p> </blockquote> <p>I wro...
[]
[ { "body": "<p><code>abs</code> is mentioned in R6RS, so I'm inclined to believe that you can treat it as a primitive.</p>\n\n<hr>\n\n<p>The scheme convention is to end predicates with <code>?</code>, so that should really be <code>almost-equal?</code></p>\n", "comments": [], "meta_data": { "Commen...
{ "AcceptedAnswerId": "1412", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-23T02:40:31.550", "Id": "1396", "Score": "3", "Tags": [ "lisp", "scheme", "numerical-methods" ], "Title": "Use Newton's Method to compute sqrt(x)" }
1396
<p>I've implemented a <code>let-with</code> macro that takes an association list, keyed by symbols, that enables code to bind their corresponding values to local variables.</p> <p>It's designed to have a similar effect to JavaScript's <code>with</code>, but without <a href="http://yuiblog.com/blog/2006/04/11/with-stat...
[]
[ { "body": "<p>JavaScript's <code>with</code> statement allows one to modify object members. This may not be possible for certain types of values (such as strings) using the <code>let-with</code> macro as defined above.</p>\n\n<hr>\n\n<p>One could generalize <code>let-with</code> to bind values from several ass...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-23T04:12:24.613", "Id": "1398", "Score": "4", "Tags": [ "scheme", "macros" ], "Title": "Is this a good way to implement let-with?" }
1398
<p>Newton's method for finding cube roots states that for any given \$x\$ and a guess \$y\$, a better approximation is \$\dfrac{(\dfrac{x}{y^2} + 2y)}{3}\$.</p> <p>What do you think of this code for finding a cube root in Scheme?</p> <pre><code>(define (improveguess y x) ; y is a guess for cuberoot(x) (/ (+ (/ x ...
[]
[ { "body": "<p>Your <code>improve-guess</code> is probably better written like this:</p>\n\n<pre><code>(/ (+ (/ x y y) y y) 3)\n</code></pre>\n\n<p>Or, if you define a <code>mean</code> function:</p>\n\n<pre><code>(define (mean . xs)\n (/ (apply + xs) (length xs)))\n</code></pre>\n\n<p>then you can make <code>i...
{ "AcceptedAnswerId": "1416", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-23T06:41:00.277", "Id": "1401", "Score": "6", "Tags": [ "mathematics", "lisp", "scheme" ], "Title": "Cube root (Newton's method)" }
1401
<p>Here's my function for getting the length of a file, in characters:</p> <pre><code>unsigned int GetFileLength(std::string FileName) { std::ifstream InFile(FileName.c_str()); unsigned int FileLength = 0; while (InFile.get() != EOF) FileLength++; InFile.close(); return FileLength; } </code></pre> <p>How can this be...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2016-09-21T20:18:48.920", "Id": "266271", "Score": "0", "body": "of course you are assuming this is an ASCII file. 1 byte == 1 char. Lets hope there are no foreigners around with their pesky unicode junk :-). Also note the returning uint is a ...
[ { "body": "<p>Is accuracy important? I believe you could try to get the filesize and divide this by the amount of bytes the formatting uses. This could give a good estimate of the amount of characters in the file.</p>\n\n<p>I say estimate because I might be forgetting about possible headers and such, although I...
{ "AcceptedAnswerId": "1407", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-23T13:10:57.700", "Id": "1405", "Score": "8", "Tags": [ "c++", "file" ], "Title": "Getting the length of a file in characters" }
1405
<p>I wrote the following JPA method that queries a database to return a distinct list of a String field (called mtfcc) of a Location object. I then took the list of strings to query for the associated name for that MTFCC.</p> <p>Could I just modify the first query to get the results, without having to use the for loop...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-23T15:05:13.053", "Id": "2444", "Score": "0", "body": "I don't know about JPA and which queries it supports, but wouldn't it already be faster to do `SELECT location FROM Location location` and do the distinct programmatically? Not say...
[ { "body": "<p>Ok, I've got a possible simplification for you. The goal I had was to get to using one query:</p>\n\n<pre><code>SELECT DISTINCT location.mftcc, location.name FROM Location location\n</code></pre>\n\n<p>It turns out that <code>Query.getResultList()</code> behaves funny: It returns a <code>List</cod...
{ "AcceptedAnswerId": "1457", "CommentCount": "7", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-23T14:32:07.183", "Id": "1409", "Score": "11", "Tags": [ "java" ], "Title": "JPA Query to Return a Map" }
1409
<p>In Python, <code>itertools.combinations</code> yields combinations of elements in a sequence sorted by lexicographical order. In the course of solving certain math problems, I found it useful to write a function, <code>combinations_by_subset</code>, that yields these combinations sorted by subset order (for lack of...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2016-05-10T22:24:12.120", "Id": "239403", "Score": "0", "body": "I think this ordering can be very useful and should be added to standard `itertools` library as an option." } ]
[ { "body": "<p>I should preface with the fact that I don't know python. It's likely that I've made a very clear error in my following edit. Generally though, I prefer your second function to your first. If python allows, I would break it up a bit for readability. Why do I like it? It doesn't need the <code>yield...
{ "AcceptedAnswerId": "1429", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-24T04:08:29.953", "Id": "1419", "Score": "5", "Tags": [ "python", "algorithm", "generator", "combinatorics" ], "Title": "Python generator function that yields combinations of e...
1419
<p>I just found out about the existence of this forum and it's exactly what I needed. I just asked <a href="https://stackoverflow.com/questions/5386795/how-to-safely-save-data-to-an-existing-file-with-c">this</a> question on Stack Overflow and after reading the answers I came up with a code (not the one I posted there ...
[]
[ { "body": "<ul>\n<li><p>I don't like discarding all exceptions from File.Delete(). I think it's fine to discard a DirectoryNotFoundException here, but I think the others should be allowed to propagate up and be shown to the user. I find that hiding the fact that the user does not have write access or that anoth...
{ "AcceptedAnswerId": "1423", "CommentCount": "0", "ContentLicense": "CC BY-SA 4.0", "CreationDate": "2011-03-24T04:22:39.120", "Id": "1420", "Score": "11", "Tags": [ "c#" ], "Title": "Overwriting an existing file in C#" }
1420
<p>The following code has one of the most confusing lines I've ever wrote. I can imagine ten other way to write it but I do not know which else could be any better.</p> <p>Note:</p> <p><code>Db()</code> is very similar to <code>PDO()</code>, it just extends it adding few features I'm not using here.</p> <p><code>Pos...
[]
[ { "body": "<p>It appears that the only difference between these queries lies in the where clause. If this is the case, you can clean up the code a bit by removing redundant SQL, like so:</p>\n\n<pre>\n$stmt = \n\"SELECT p.PostPID, p.PostUID, p.PostText, p.PostTime, u.UserUID, u.UserName, u.UserImage, u.UserRep,...
{ "AcceptedAnswerId": "1461", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-24T13:15:14.900", "Id": "1426", "Score": "5", "Tags": [ "php", "mysql" ], "Title": "Gathering data from database" }
1426
<p>I have the following left join LINQ query that is returning the results that I expect, but it does not "feel" right. I need <strong>ALL</strong> records from the <code>UserProfile</code> table.</p> <p>Then the <code>LastWinnerDate</code> is a single record from the winner table (possible multiple records) indicatin...
[]
[ { "body": "<p>What LINQ is this? Entities? ESQL, linq to sql? linq to objects?</p>\n\n<p>If Linq to SQL then:</p>\n\n<p>Try this and see what the resulting SQL is then decide based on the sql, or profile the sql itself on your db?</p>\n\n<pre><code> /// &lt;summary&gt;\n /// From BReusable\n /// &lt;/s...
{ "AcceptedAnswerId": "1613", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-24T14:23:25.310", "Id": "1428", "Score": "10", "Tags": [ "c#", ".net", "linq" ], "Title": "Left join query of user profiles" }
1428
<p>The following working function seems a bit hacky to me as it has been pieced together from a handful of (probably poorly written) online tutorials, and I want to know if there is a better (or more standard) way to approach this.</p> <pre><code>/*** * GET HTTP Operation * * @param request Request URI * @retu...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-04-15T07:12:12.903", "Id": "75149", "Score": "0", "body": "Have a look at http://stackoverflow.com/questions/221442/rest-clients-for-java" }, { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-24T15:37:34.533", "...
[ { "body": "<p>Adding another answer since I'm actually looking at the code now...</p>\n\n<p>You are swallowing every exception that gets thrown. The calling code has no idea whether it tried to hit a page that didn't exist, its request was malformed, etc. If you don't want to expose the exceptions (probably a...
{ "AcceptedAnswerId": "1433", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-24T15:13:47.267", "Id": "1430", "Score": "6", "Tags": [ "java", "beginner", "api", "http" ], "Title": "Interfacing with RESTful web services" }
1430
<p>The program finds the non repeated number in a <code>int</code> array. I am using a <code>count</code> for the duplicates and making a <code>Numcounts[]</code> to store the count values. I am assuming the array is sorted. Then I can check the <code>Numcounts[]</code> for <code>1</code>, and that would be the non-rep...
[]
[ { "body": "<p>A hashmap is a structure that maps a key (the hash) to a value.<br>\nIn your example you'd want the key to be the number and the value to be the count of each number.\nIf you are using a compiler that that supports C++0x, you can add </p>\n\n<pre><code>#include &lt;unordered_map&gt;\n</code></pre>...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-24T19:23:03.027", "Id": "1434", "Score": "7", "Tags": [ "c++", "array" ], "Title": "Find the non repeating number in the integer array" }
1434
<p>I have this for loop for creating a list of variables:</p> <pre><code>vars &lt;- c( 'beta.o', 'sd.y') for (x in c('ghs', 'site', 'trt')) { if(model.parms[[x]] == 1) { data &lt;- data[, which(names(data) != x)] } else { data &lt;- data if(x!='ghs') { vars &lt;- c(vars, paste('sd.', x, sep = ''...
[]
[ { "body": "<p>This bit:</p>\n\n<pre><code>for (i in 1:m) {\n if(i == 1 &amp;&amp; x == 'site') {\n vars &lt;- c(vars, 'beta.site[1]')\n }\n if (i &gt; 1) {\n vars &lt;- c(vars, paste('beta.', x, '[', i, ']', sep=''))\n }\n}\n</code></pre>\n\n<p>Says handle the first pass differently from all the other...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-24T21:13:38.340", "Id": "1437", "Score": "2", "Tags": [ "r" ], "Title": "How to simplify nested conditional for loop in R?" }
1437
<blockquote> <p>A function f is defined by the rule that f(n) = n if n &lt; 3 and f(n) = f(n-1) + 2f(n-2) + 3f(n-3) if n>=3. Write a recursive and an iterative process for computing f(n).</p> </blockquote> <p>I wrote the following:</p> <pre><code>(define (f_r n) ; recursive (cond ((&lt; n 3) n) ...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-25T02:41:09.400", "Id": "2514", "Score": "0", "body": "Do you mind if I ask where you're getting these puzzles from? Based on the frequency with which one-letter function names/variables are defined, I'm assuming it's either a math boo...
[ { "body": "<p>Your iterative definition will not produce correct results for most values of n.</p>\n\n<p>When rewriting a pure recursive function as an iterative one, one needs to keep as many accumulators as there are base cases. In the iterative step, compute the next value, store it in the first accumulator...
{ "AcceptedAnswerId": "1446", "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-25T01:24:45.397", "Id": "1440", "Score": "1", "Tags": [ "recursion", "lisp", "scheme", "iteration" ], "Title": "Both an Iterative and a Recursive f(x)" }
1440
<blockquote> <pre><code> 1 1 1 1 2 1 1 3 3 1 </code></pre> </blockquote> <p>What do you think of this solution for computing Pascal's triangle?</p> <pre><code>(define (pascal row column) (cond ((or (= row column) (= 1 column)) 1) (else (+ (pascal (- row 1) (- column 1)) (pascal (-...
[]
[ { "body": "<p>You may use <a href=\"http://community.schemewiki.org/?memoization\" rel=\"nofollow\">memoization</a> to reduce your algorithm's complexity from O(2 ^ n) to O(n ^ 2).</p>\n", "comments": [], "meta_data": { "CommentCount": "0", "ContentLicense": "CC BY-SA 2.5", "CreationDa...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-25T02:33:49.953", "Id": "1441", "Score": "3", "Tags": [ "lisp", "scheme" ], "Title": "Pascal's triangle" }
1441
<p>I've always seen the <code>IPrincipal</code> and <code>IIdentity</code> interfaces separately, but I haven't seen any compelling reason for it, so I have my own interface that combines the two:</p> <pre><code>public interface IUser : System.Security.Principal.IIdentity, System.Security.Principal.IPrincipal { st...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-25T11:35:07.237", "Id": "2519", "Score": "0", "body": "I doubt this is `code review`. It looks more like a design question." }, { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-25T12:28:51.053", "Id": "2520"...
[ { "body": "<p>These interfaces are meant to <a href=\"http://msdn.microsoft.com/en-us/library/ftx85f8x.aspx\" rel=\"nofollow\">work with the user’s identity information</a>.</p>\n\n<ul>\n<li><a href=\"http://msdn.microsoft.com/en-us/library/system.security.principal.iidentity.aspx\" rel=\"nofollow\"><code>IIden...
{ "AcceptedAnswerId": "1616", "CommentCount": "6", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-25T05:12:48.417", "Id": "1442", "Score": "11", "Tags": [ "c#", "security" ], "Title": "IIdentity and IPrincipal Interfaces" }
1442
<p>From SICP's 1.24: (<a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html" rel="nofollow">Exponentiation</a>) </p> <p>(you may need to click through and read ~1 page to understand)</p> <blockquote> <p>Exercise 1.16. Design a procedure that evolves an iterative exponentiation process that uses...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2012-06-19T02:59:17.320", "Id": "20605", "Score": "1", "body": "`even?` is a Scheme builtin, so you should not define your own version. :-) Also, your bottom version isn't iterative." } ]
[ { "body": "<p>Your implementation will not produce correct results in general because your recursive definitions are erroneous.</p>\n\n<p>One should note that in case n = 0, result is 1. In case of even n (or n = 2 i), one may write b ^ n = (b * b) ^ i. In case of odd n (or n = 2 i + 1), one may write b ^ n =...
{ "AcceptedAnswerId": "1448", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-25T06:22:50.580", "Id": "1443", "Score": "3", "Tags": [ "lisp", "scheme" ], "Title": "Iterative exponentiation process" }
1443
<p>Given the following task:</p> <blockquote> <p><strong>Exercise 1.17</strong></p> <p>The exponentiation algorithms in this section are based on performing exponentiation by means of repeated multiplication. In a similar way, one can perform integer multiplication by means of repeated addition. The...
[]
[ { "body": "<p>Your implementation will not produce correct results in general.</p>\n\n<p>In the base case of b = 0, result should be 0. In the case of even b, result is double a times half b (which you have done correctly). In the case of odd b, result should be a + (double a times half (b - 1)).</p>\n\n<pre>...
{ "AcceptedAnswerId": "1449", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-25T06:41:28.990", "Id": "1444", "Score": "1", "Tags": [ "lisp", "scheme" ], "Title": "Multiplication in terms of addition" }
1444
<p>Just thinking if using code like this</p> <pre><code>&lt;?php abstract class helper { private static $_cycles = array(); public static function isOdd($v) { return (0 == ($v % 2)) ? false: true; } public static function isEven($v) { return !self::isOdd($v); } publi...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-25T18:12:46.983", "Id": "2535", "Score": "0", "body": "I'm guessing that the second code block is what the HTML/CSS people will need to write?" }, { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-25T18:26:36.490...
[ { "body": "<p>Since this is a code review site, let me point out that the following is an abomination:</p>\n\n<pre><code>return (0 == ($v % 2)) ? false: true;\n</code></pre>\n\n<p>The constants are redundant. Much simpler:</p>\n\n<pre><code>return (0 != ($v % 2));\n</code></pre>\n\n<p>(The parentheses are also ...
{ "AcceptedAnswerId": "1463", "CommentCount": "2", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-25T15:06:29.313", "Id": "1452", "Score": "6", "Tags": [ "php", "functional-programming" ], "Title": "Is this good approach to use debug functions for implementing features?" }
1452
<p>I am relatively new to JavaScript and JQuery. I wanted a Datepicker that has the following functionality:</p> <ol> <li>Weekends are not selectable </li> <li>Non working days (bank holidays etc.) are not selectable </li> <li>The first selectable day must be x number of full working days in the future, taking into ...
[]
[ { "body": "<p>for <strong>1)</strong></p>\n\n<pre><code>var today = new Date();\ntoday.setTime(0); // resets the time. (midnight)\n</code></pre>\n\n<p>I believe this will fix the date comparison issue</p>\n\n<p>for <strong>2)</strong></p>\n\n<p>It just sucks that there isn't a built-in for this sort of thing. I...
{ "AcceptedAnswerId": "3139", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-25T15:30:33.643", "Id": "1454", "Score": "7", "Tags": [ "javascript", "jquery", "jquery-ui" ], "Title": "JQuery UI Datepicker with beforeShowDay function" }
1454
<p>I created a few forms for faculty members to nominate colleagues for awards. Many of the nominations require documentation to be submitted in order to validate the nomination. I created one form then modified it to fit the needs of many different award forms. I created it rather hastily and would like input as to ho...
[]
[ { "body": "<p>Considering that you're doing this:</p>\n\n<pre><code> &lt;!---Upload document to the destination. Accept only MSWord, PDF, RTF and plain text files.---&gt;\n &lt;cffile action=\"upload\"\n filefield=\"recommendation4\"\n accept=\"application/msword, application/pdf, ...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-25T18:23:00.543", "Id": "1460", "Score": "4", "Tags": [ "form", "network-file-transfer", "coldfusion", "cfml" ], "Title": "Uploading multiple files for faculty nomination system"...
1460
<p>As much as I try, I cannot seem to get this Coffeescript code to look beautiful (I'd like to think it is possible). I have tried both Javascript and Coffeescript. Just to be clear, this code works fine, but it is hard to read, for reasons that I am unable to pinpoint.</p> <p><strong>How can it be refactored, reorga...
[]
[ { "body": "<p>Here are some simple suggestions:</p>\n\n<ol>\n<li>Put some spaces in between lines of code. Some whitespace will make things easier to read</li>\n<li>I noticed you are using two spaces for indentation. In general, four spaces (or a tab) makes for more readable code</li>\n<li>Don't break lines on ...
{ "AcceptedAnswerId": "3333", "CommentCount": "0", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-25T20:40:59.300", "Id": "1462", "Score": "11", "Tags": [ "javascript", "jquery", "jquery-ui", "coffeescript" ], "Title": "Coffeescript beautification and refactoring" }
1462
<p>This code comes from a <a href="http://wordpress.org/support/topic/plugin-vote-it-up-show-top-voted-post-in-index-page" rel="nofollow">Wordpress plugin's forum</a> called Vote it Up. It sort posts by vote. A lotof people say that there is more code than is needed. So I was wondering if someone have any idea about ho...
[]
[ { "body": "<p>These are my suggestions</p>\n\n<ol>\n<li><p>Don't use mysql functions to connect and query Wordpress posts. \nYou can replace the first SQL statement with this WP_Query. </p>\n\n<pre><code>$query = new WP_Query('post_type=post&amp;post_status=publish&amp;orderby=date&amp;order=DESC');\n$pageposts...
{ "AcceptedAnswerId": "1492", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-25T21:07:39.553", "Id": "1464", "Score": "3", "Tags": [ "php", "mysql", "wordpress" ], "Title": "Function from WordPress VoteItUp function" }
1464
<p>I've been experimenting with Perl and MySql and wrote this code for connecting and writing to a database:</p> <pre><code># MySQL DDL to create database used by code # # CREATE DATABASE sampledb; # # USE sampledb; # # CREATE TABLE `dbtable` ( # `id` int(11) NOT NULL AUTO_INCREMENT, # `demo` long...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-28T15:38:24.490", "Id": "2603", "Score": "0", "body": "Is this a question?" }, { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-28T15:49:43.870", "Id": "2605", "Score": "0", "body": "Would you descri...
[ { "body": "<p>Couple of things I noticed:</p>\n\n<p>When you connect to the database, you don't check that the connection succeeded. The most common way I've seen to do this in Perl is:</p>\n\n<pre><code>my $connect = DBI-&gt;connect($dsn, $username, $password)\n or die \"Connection Error: DBI::errstr\\n\";\...
{ "AcceptedAnswerId": "1523", "CommentCount": "6", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-25T23:30:43.523", "Id": "1465", "Score": "5", "Tags": [ "mysql", "perl" ], "Title": "Perl DBI Sample with MySQL DDL" }
1465
<p>This is a collection of reasonably useful functions I put together for writing my blog.</p> <pre><code>(require 'htmlize) (defvar blog-mode-map nil "Keymap for blog minor mode") (unless blog-mode-map (let ((map (make-sparse-keymap))) (define-key map "\C-cl" 'insert-link) (define-key map "\C-cp" 'inser...
[]
[ { "body": "<p>Not sure if this is relevant as it is a year old but, you can look at <a href=\"https://github.com/Neil-Smithline/defassoclist\" rel=\"nofollow\">https://github.com/Neil-Smithline/defassoclist</a> to see how to create multiple functions in a macro. </p>\n\n<p>You didn't include your failed attempt...
{ "AcceptedAnswerId": "11673", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-26T05:00:11.500", "Id": "1467", "Score": "10", "Tags": [ "elisp" ], "Title": "Minor mode for blog writing in HTML" }
1467
<p>This function changes a HashSet in the root of the class called 'commands' to a set of strings found in a file at "plugins/UC/Commands.txt" (it's what was requested by the people using the plugin). If the file doesn't exist, it copies a default file found in the zip file called "Commands.txt".</p> <p>This is a wei...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-26T21:33:47.757", "Id": "2561", "Score": "0", "body": "In regard to \"Community's\" comment added to the question, The FileReader's constructor can throw a FileNotFoundException and is not contained within an inner exception." }, {...
[ { "body": "<p>I have not coded in Java for some time, so I encourage you to use your own discretion while reading this. It should at-least give some general direction though.</p>\n\n<p>I noticed that you read lines into the hash set in several places, so I started by extracting it to a separate method.</p>\n\n<...
{ "AcceptedAnswerId": null, "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-26T03:36:16.183", "Id": "1468", "Score": "2", "Tags": [ "java", "strings", "file" ], "Title": "Change HashSet to a set of strings found in a file" }
1468
<p>I'm cleaning build directories produced by GNOME build tool, <a href="http://live.gnome.org/Jhbuild" rel="nofollow">JHBuild</a>. This tool either downloads tarballs or clones git repositories, depending on set-up. After that, it proceeds to compilation (and then installation). Once in a while, something gets screwed...
[]
[ { "body": "<pre><code>full_path = \"{}/{}\".format(top_level, filename)\n</code></pre>\n\n<p>You can use <code>os.path.join(top_level, filename)</code> for that. That way it will also work on any system which does not use <code>/</code> as a directory separator (that's not really a realistic concern in this cas...
{ "AcceptedAnswerId": "1477", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-26T16:33:28.870", "Id": "1476", "Score": "3", "Tags": [ "python", "git", "make" ], "Title": "Cleaning multiple build directories" }
1476
<p>For an interview, I was asked to write code allocate memory for a rows*cols matrix, using a <em>single</em> <code>malloc()</code>. I want your comments about this code that I wrote:</p> <pre><code>/* Allots (in effect) space for a matrix of size rows*cols using a single malloc. The whole space allotted consists of...
[]
[ { "body": "<p>I would use <code>calloc</code> instead of <code>malloc</code> since you're anyway clearing the matrix after allocation. </p>\n\n<pre><code>void* cmatrix = calloc(1, matsize);\n...\n//memset((void*) cmatrix, 0, matsize); this is not needed anymore because of 'calloc'\n</code></pre>\n\n<p>Also I d...
{ "AcceptedAnswerId": "1481", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-26T22:51:36.817", "Id": "1479", "Score": "4", "Tags": [ "c", "interview-questions", "memory-management", "matrix" ], "Title": "Allocating memory for a matrix with a single mall...
1479
<p>I wrote an implementation of binary search in JavaScript earlier for kicks, but I noticed my version was significantly different than those found on Google. <a href="http://www.dweebd.com/javascript/binary-search-an-array-in-javascript/">Here</a> is an example of binary search I found on Google (copied here to comba...
[]
[ { "body": "<p>I would just like to hi-light a few points of difference in the two and how I <em>think</em> they are reflected in what you are seeing.</p>\n\n<ul>\n<li><p>When making a comparison between <code>arr[target]</code> and <code>ele</code>, if they are not the same then you do not need to include <code...
{ "AcceptedAnswerId": "1488", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-26T23:32:47.953", "Id": "1480", "Score": "38", "Tags": [ "javascript", "algorithm", "search", "binary-search" ], "Title": "JavaScript binary search" }
1480
<p>This is a classic linked list problem.</p> <ol> <li>Deleting a node from linked list given the data to be deleted.</li> <li>Inserting a node in a sorted linked list.</li> </ol> <p>I saw various versions of this and here is my version. Could you check and let me know if this code is efficient?</p> <pre><code>void ...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-27T02:06:40.350", "Id": "2567", "Score": "0", "body": "In the future please indent your code by 4 spaces or use the code button (the one with the ones and zeros) to properly format your code." }, { "ContentLicense": "CC BY-SA 2...
[ { "body": "<p>The efficiency of your algorithm is fine, however there are a couple of other things you should take care of:</p>\n\n<p>First of all do <strong>not</strong> free memory, which was allocated with <code>malloc</code>, using <code>delete</code>. <code>delete</code> is for freeing memory which was all...
{ "AcceptedAnswerId": null, "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-27T01:35:01.030", "Id": "1484", "Score": "3", "Tags": [ "c++", "linked-list" ], "Title": "Deleting a linked list node" }
1484
<p>I have built a "reset and confirm password" section on a site. It works great. I just have this suspicion that maybe it isn't the most efficient way. I am relatively new to jQuery but I can usually make things work well. I am just wanting to make sure I am doing them right. </p> <p>Any suggestions are appreciated....
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-27T02:06:18.227", "Id": "2566", "Score": "4", "body": "Just in glancing, I would not use a `GET` request to change a user's password - this leaves you open for [CSRF attacks](http://en.wikipedia.org/wiki/Cross-site_request_forgery). U...
[ { "body": "<p>Looks fine to me but one way to write a lot less code for these relatively simple ajax calls is to use the <a href=\"http://jquery.malsup.com/form/\" rel=\"nofollow\">Jquery Form</a> plugin that converts a form tag to an ajax call. I believe you'll find it a huge time save going forward.</p>\n", ...
{ "AcceptedAnswerId": "1508", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-27T01:38:40.543", "Id": "1485", "Score": "6", "Tags": [ "javascript", "jquery", "ajax" ], "Title": "jQuery password confirmation" }
1485
<p>I'm working on writing a scripting language with ANTLR and C++. This is my first actual move from ANTLR grammars into the C++ API, so I'd like to know if this would be a good way to structure the grammar (later I will be adding a tree parser or tree rewriting rules though).</p> <pre><code>grammar dyst; options { ...
[]
[ { "body": "<ol>\n<li><p>The grammar itself is pretty unreadable \"as is\". A rule like:</p>\n\n<pre><code>statement : stopUsingNamespaceStm|usingNamespaceStm|namespaceDefineStm|functionStm|defineStm|assignStm|funcDefineStm|ifStm|whileStm|returnStm|breakStm|eventDefStm|eventCallStm|linkStm|classDefStm|exitStm|im...
{ "AcceptedAnswerId": "1582", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-27T03:24:11.313", "Id": "1487", "Score": "8", "Tags": [ "c++", "grammar" ], "Title": "Parsing a basic scripting language" }
1487
<p>Here is my PHP class for solving Sudoku:</p> <p><a href="https://gist.github.com/889044" rel="nofollow">GitHub</a></p> <p><strong>Sudoku</strong></p> <pre><code>&lt;?php /** * Bootstrap file * * This is an example of how to use the SudokuSolver Class * Here the input array has been hardcoded. It could be send * ...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-27T15:19:09.917", "Id": "2579", "Score": "0", "body": "Please include the source code you want reviewed in your post next time, don't just link to it." }, { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-27T23:4...
[ { "body": "<p>Here's a few comments on the code rather than the algorithm - I'll leave that to someone else :)</p>\n\n<p><strong>Commenting</strong></p>\n\n<ul>\n<li><p>Quite a few comments repeat what the code does. For example:</p>\n\n<pre><code>// initially set the $probable as array 1 to 9. \n$probable = r...
{ "AcceptedAnswerId": null, "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-27T12:48:52.290", "Id": "1489", "Score": "4", "Tags": [ "php", "php5", "sudoku" ], "Title": "Sudoku solving class in PHP" }
1489
<p>In the absence of a full-fledged tutorial, I am teaching myself to program in the <a href="http://factorcode.org/">Factor</a> language by writing a bunch of functions that I will eventually use in command-line scripts.</p> <p>One common operation I like to perform is to change the case of names of files contained i...
[]
[ { "body": "<p>Comments! Comments are good, especially to people who are just learning Factor right now. Admittedly, for Factor, it's pretty readable -- I finally got it, after staring a while and doing lots of Googling -- but comments are always nice.</p>\n\n<p><del>I can't find anything about the <code>: name&...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-28T00:13:49.270", "Id": "1493", "Score": "33", "Tags": [ "file-system", "factor-lang" ], "Title": "Factor script to change case of all filenames in a directory" }
1493
<p>After reading an article on Skull Security mentioning the potential weakness of php's mt_rand function because of weak auto-seeding (<a href="http://ow.ly/4nrne" rel="nofollow">http://ow.ly/4nrne</a>), I decided to see what -- if any -- entropy I could find available from within php. The idea is to have enough (weak...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-28T10:34:43.660", "Id": "2596", "Score": "0", "body": "Just realized, while reviewing my own code the day after, that the whole thing is currently useless because the mt_srand function expects an integer as seed." }, { "Content...
[ { "body": "<p>I actually don't get the point of injecting so much system information into the <code>mt_srand</code> function. Looks like a total (and maybe even pointless) paranoia :)</p>\n\n<p>But here you go with a cleaner code:</p>\n\n<pre><code>&lt;?php\n/**\n* Random password generator\n* v2.0\n*/\n\ndefin...
{ "AcceptedAnswerId": "1584", "CommentCount": "5", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-28T02:19:28.757", "Id": "1494", "Score": "4", "Tags": [ "php", "security", "random" ], "Title": "Password-generation function using custom seed" }
1494
<p>I'm making a scripting language parser and it's shaping up well but it takes about 30ms to do 30 or so odd/even checks.</p> <p><code>Profiler</code> tells me that it's taking most of the time in the <code>InvokeOperator.Operate</code> function but that doesn't tell me a lot because the Operate call the Run method o...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-28T09:06:33.807", "Id": "2594", "Score": "3", "body": "see `Make sure you include your code in your question` part in [FAQ](http://codereview.stackexchange.com/faq)" }, { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2...
[ { "body": "<p>I suggest you try using a profiler of some sort in order to determine exactly where the bottleneck is. Once you do that, it will much easier to find how to avoid it.</p>\n", "comments": [ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-29T09:12:49.770", ...
{ "AcceptedAnswerId": null, "CommentCount": "8", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-28T07:14:24.023", "Id": "1495", "Score": "7", "Tags": [ "c#", "parsing" ], "Title": "Scripting language parser" }
1495
<p>I have the following code for reading <a href="http://htk.eng.cam.ac.uk/" rel="nofollow">HTK</a> feature files. The code below is working completely correct (verified it with unit tests and the output of the original HTK toolkit).</p> <pre><code>from HTK_model import FLOAT_TYPE from numpy import array from struct i...
[]
[ { "body": "<pre><code> data = in_f.read(12)\n num_samples, sample_period, sample_size, param_kind = unpack('&gt;iihh', data)\n A = B = 0\n if compressed:\n A = array('f')\n A.fromfile(in_f, sample_size/2)\n B = array('f')\n B.fromfile(in_f, sample_size/2)\n #The fi...
{ "AcceptedAnswerId": "1500", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-28T07:57:03.323", "Id": "1496", "Score": "5", "Tags": [ "python", "performance", "file", "numpy", "serialization" ], "Title": "Reading a binary file containing periodic sam...
1496
<p>I have an extension that implements all the BinaryReader/BinaryWriter in the Stream class. I want to create generic methods for them. Is there a nicer way of doing this?</p> <pre><code>public static void Write&lt;T&gt;(this Stream stream, T num) { if (num is byte) { stream.Write((byte)(object)num); ...
[]
[ { "body": "<p>I'm guessing you defined more write methods than this? Otherwise the <code>Write()</code> would call itself recursively infinitely. You probably have overloads for every supported type, <strong>making the generic write method superfluous</strong>. This would also explain the weird casting you are ...
{ "AcceptedAnswerId": "1501", "CommentCount": "0", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-28T11:22:37.440", "Id": "1498", "Score": "5", "Tags": [ "c#" ], "Title": "Generic BinaryReader/Writer Read/Write methods" }
1498
<p>I have the following code which uses four if statements to check the value in four different combo boxes. </p> <pre><code>if (!String.IsNullOrEmpty(tx_cmb_Circuits1.Text.Trim())) emp.CircuitEmail_Group_Rels.Add( new Circuit_Email_Group_Rel { Circuits = (Circuits) tx_cmb_Circuits1.Sel...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-31T15:54:38.913", "Id": "2751", "Score": "0", "body": "your tabs are very annoying. only 4 spaces are needed for indentation." }, { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-04-07T14:25:23.647", "Id": "297...
[ { "body": "<p>Add all items to a list and do a foreach instead.</p>\n\n<pre><code>var buttons = new List&lt;Button&gt;\n{\n tx_cmb_Circuits1,\n tx_cmb_Circuits2,\n ...\n};\n\nforeach ( var button in buttons )\n{\n if ( !String.IsNullOrEmpty( button.Text.Trim() ) )\n {\n emp.CircuitEmail_Gr...
{ "AcceptedAnswerId": "1503", "CommentCount": "4", "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-28T15:17:02.837", "Id": "1502", "Score": "6", "Tags": [ "c#" ], "Title": "Is there a shorter way to write the following if statements?" }
1502
<p>I have been trying to learn more about using bits/bitfields/bitmask or whatever you call them exactly in PHP for different settings and permissions. With the help of others, I have come up with this class. It has come a long way from when I started it but I am looking for any ideas on how to improve it more.</p> <...
[]
[ { "body": "<p>You are kind of missing the point of using bitmasks.</p>\n\n<p>You should be making use of simple bit operations.</p>\n\n<p>If each bit represents a privacy settings, then the operations should be:</p>\n\n<ol>\n<li><p>Check if a value is set, using &amp; operator.</p>\n\n<p><code>($this-&gt;value ...
{ "AcceptedAnswerId": "1510", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-29T00:02:47.023", "Id": "1509", "Score": "4", "Tags": [ "php", "php5", "bitwise" ], "Title": "PHP Bitmask class" }
1509
<blockquote> <p>Given an array that is first sorted non-decreasing and then rotated right by an unspecified number of times, find the index of its minimal element efficiently. If multiple such minimal elements exist, return the index of any one.</p> <p>Idea: Conceptually, divide the array into two parts: t...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-03-29T04:22:17.397", "Id": "2624", "Score": "0", "body": "I made a small error here: the return type of the getMinimIndex() should be size_t not int , and the printf formatting should change accordingly to %ud" } ]
[ { "body": "<p>Your algorithm works for sequences that are strictly increasing (as @Moron points out). If efficiency is a consideration, you may wish to employ iteration instead of recursion.</p>\n\n<pre><code>int getMinimIndex (const int *const a, size_t left, size_t right)\n{\n while (1)\n {\n as...
{ "AcceptedAnswerId": "1566", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-29T02:19:39.403", "Id": "1514", "Score": "11", "Tags": [ "c", "algorithm", "interview-questions" ], "Title": "Finding the minimum in a sorted, rotated array" }
1514
<p>Using the property that GCD(a, b) = GCD(b, r) where r is the remainder when you compute (a / b), you can write a recursive function as follows:</p> <pre><code>(define (gcd a b) ; recursive (if (= 0 b) a (gcd b (remainder a b)))) </code></pre> <p>I also tried to write the following as an iterative functio...
[]
[ { "body": "<p>Indeed, both versions you have are iterative. I'm not actually sure that a recursive solution makes sense in this case---usually a recursive approach is for the classical pattern of holding the result of operating on the first item, then recursing into the remaining items. Calculating the GCD does...
{ "AcceptedAnswerId": "1522", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-03-29T05:40:38.573", "Id": "1517", "Score": "4", "Tags": [ "lisp", "scheme" ], "Title": "GCD - is this solution iterative?" }
1517