body stringlengths 25 86.7k | comments list | answers list | meta_data dict | question_id stringlengths 1 6 |
|---|---|---|---|---|
<p>I'm currently working on a project where I have a punch of raw data and multiple components that use that data for various tasks. I would like to separate the raw data from the components as much as possible and keep the raw data as only data (So no logic or functionality) but the components should be able to read a... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-27T14:41:10.420",
"Id": "37692",
"Score": "1",
"body": "Do you really need a GUID? It seems like all you need is a dirty flag when data changes so the components know to re-sync."
},
{
"ContentLicense": "CC BY-SA 3.0",
"Cre... | [
{
"body": "<p>I think you have two main choices, have the components throw events when they dirty the data. </p>\n\n<p>The plus here is that events are extremely multithreaded friendly.\nthe minus is the program might have to know about how to sync the dirty data.</p>\n\n<p>The other option is to have an abstra... | {
"AcceptedAnswerId": null,
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-27T11:14:50.140",
"Id": "24414",
"Score": "7",
"Tags": [
"c++"
],
"Title": "Efficent way of synchronizing between data"
} | 24414 |
<p>I created a <em>class EmailParser</em> and check if a supplied string is an email or not with
the help of the standard librarie's email module. If the supplied string is not an
email I raise an exception. </p>
<ul>
<li><strong>Is this a pythonic way to deal with inappropiate input values an assertation error?</str... | [] | [
{
"body": "<p>Assertions are meant to help find bugs in the program. See <a href=\"http://wiki.python.org/moin/UsingAssertionsEffectively\" rel=\"nofollow\">Python Wiki: Using assertion effectively</a>. Quote:</p>\n\n<blockquote>\n <p>Assertions should <em>not</em> be used to test for failure cases that can\n ... | {
"AcceptedAnswerId": "24426",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-27T13:23:24.860",
"Id": "24419",
"Score": "3",
"Tags": [
"python",
"exception"
],
"Title": "Raising an assertation error if string is not an email"
} | 24419 |
<p>I am implementing a program that takes commands from a user and based on those commands, drives a Robot on a grid. To implement this program I have used the Command pattern. I have used this pattern because, most importantly, I want to be able to add to the list of possible commands in the future.</p>
<p>The progra... | [] | [
{
"body": "<p>I think you've added an unnecessary layer of abstraction here.</p>\n\n<p>To me, <code>populateCommandList</code> looks completely unnecessary. You are converting a list of commands in one format (character string) into another format (an array of <code>Command</code> objects). Then, presumably, ... | {
"AcceptedAnswerId": "24422",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-27T13:59:48.453",
"Id": "24421",
"Score": "1",
"Tags": [
"java",
"design-patterns"
],
"Title": "Opinions on my Command pattern design - is it extensible?"
} | 24421 |
<p>My quick searching did not reveal anyone sharing their solution to <a href="http://tour.golang.org/#40" rel="nofollow">this exercise</a>, so am not sure if there are other (and better) approaches to mine:</p>
<pre><code>package main
import (
"code.google.com/p/go-tour/wc"
"strings"
)
func WordCount(s stri... | [] | [
{
"body": "<p>You can be more concise and readable using <code>range</code> and the fact that the default value of the int is <code>0</code> :</p>\n\n<pre><code>func WordCount(s string) map[string]int {\n m := make(map[string]int)\n for _, w := range(strings.Fields(s)) {\n m[w]++\n }\n return... | {
"AcceptedAnswerId": "24427",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-27T14:29:21.847",
"Id": "24423",
"Score": "3",
"Tags": [
"go"
],
"Title": "exercise #40 in A Tour of Go"
} | 24423 |
<p>I am writing an email parser and cannot decide how much logic should be included in the <code>__init__</code> function. I know that a constructor should make the object ready for use, but are there best practices?</p>
<p>I've added several statements to my constructor:</p>
<pre><code>def __init__(self, message):
... | [] | [
{
"body": "<p>Notes:</p>\n\n<ul>\n<li><p>Personally I prefer to keep the initialization method simple and lean. Just setup the minimum state of the object, let subsequent calls to methods do the real job when it's needed. Otherwise, why use a class? Maybe you don't need a class at all and just a function would ... | {
"AcceptedAnswerId": "24442",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-27T14:44:43.513",
"Id": "24425",
"Score": "3",
"Tags": [
"python",
"parsing",
"constructor",
"email"
],
"Title": "Logic for an init function in email parser"
} | 24425 |
<p>I have a meta-search engine system, i implemented mainly using static classes. The query pre processing module consists of the following static methods</p>
<pre><code>function queryPreprocessing() { }
queryPreProcessing.removeStopWords = function (query){ // code here}
queryPreProcessing.stemmer = function (w) { /... | [] | [
{
"body": "<p>Personally, I would make a custom type, <code>Query</code>, and define these as methods on the prototype of <code>Query</code>.</p>\n\n<pre><code>var Query = function(...);\nQuery.prototype.removeStopWords = function (query){ ... }\nQuery.prototype.stemmer = function (query){ ... }\nQuery.prototyp... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-27T15:21:44.480",
"Id": "24428",
"Score": "1",
"Tags": [
"javascript",
"jquery"
],
"Title": "Best design pattern for Javascript"
} | 24428 |
<p><strong>Requirements</strong>:</p>
<blockquote>
<p>Given a long section of text, where the only indication that a paragraph has ended is a shorter line, make a guess about the first paragraph. The lines are hardwrapped, and the wrapping is consistent for the entire text.</p>
</blockquote>
<p>The code below assum... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-27T16:46:31.510",
"Id": "37702",
"Score": "0",
"body": "Can you post example inputs and expected outputs?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-28T08:31:51.227",
"Id": "37739",
"Score": "0... | [
{
"body": "<h2>Stating Your Requirements</h2>\n<p>It's important to have a clear defintion of what you want to achieve before you start writing code, even though there are many different ways of achieving that, for instance by practicing <a href=\"http://en.wikipedia.org/wiki/Test-driven_development\" rel=\"nor... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-27T16:10:13.113",
"Id": "24431",
"Score": "4",
"Tags": [
"python",
"strings"
],
"Title": "Given a random section of text delimited by line breaks, get the first paragraph"
} | 24431 |
<p>I just coded this formatter to format timestamps in javascript (I tied it to underscore for convenience), any remark?</p>
<pre><code>_.toDate = function(epoch, format, locale) {
var date = new Date(epoch),
format = format || 'dd/mm/YY',
locale = locale || 'en'
dow = {};
dow.en =... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-27T22:34:55.150",
"Id": "37724",
"Score": "2",
"body": "I like it (except that I'll stick to some commonly used format, for instance [the one php](http://php.net/date) uses)"
}
] | [
{
"body": "<p>I would use a library that is designed for this, such as <a href=\"http://momentjs.com/\" rel=\"nofollow\">Moment.js</a>. It is a lot more flexible, and has internationalization support also.</p>\n",
"comments": [],
"meta_data": {
"CommentCount": "0",
"ContentLicense": "CC BY-... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-27T17:21:28.460",
"Id": "24435",
"Score": "2",
"Tags": [
"javascript",
"jquery",
"datetime",
"underscore.js"
],
"Title": "Date formatter for javascript"
} | 24435 |
<p>After watching Raymond Hettinger, I decided to go through my code and make it faster and more beautiful. The example given on the slides is, unfortunately, not good enough to get me going (ref. <a href="https://speakerdeck.com/pyconslides/transforming-code-into-beautiful-idiomatic-python-by-raymond-hettinger" rel="n... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-27T17:34:52.260",
"Id": "37704",
"Score": "1",
"body": "Insufficient context: what is s? where did come from?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-27T17:40:47.120",
"Id": "37705",
"Score"... | [
{
"body": "<p>There's not enough context here to review your code. For example, all those calls to <code>int_or_zero</code> and <code>float_or_zero</code> look suspicious (Django ought to be able to do that kind of conversion for you if you set up the model correctly), but without being able to see the model de... | {
"AcceptedAnswerId": "24465",
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-27T17:31:26.097",
"Id": "24437",
"Score": "-1",
"Tags": [
"python",
"django"
],
"Title": "Unpacking sequences for functions"
} | 24437 |
<p>I've just implemented a cache for my server, and I'd like your thoughts about performance improvements and maybe some possible bug fixes:</p>
<pre><code>public class Cache<CacheKey, CacheValue>
{
private Dictionary<CacheKey, KeyValuePair<CacheValue, DateTime>> _map;
/// <summary>
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-01T19:20:43.277",
"Id": "37992",
"Score": "1",
"body": "Don't use `DateTime.Now`. Use `DateTime.UtcNow`. You don't want to introduce caching bugs when daylight savings changes kick in."
}
] | [
{
"body": "<p>If you are using .NET 4 or above drop your code, and use <a href=\"http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache%28v=vs.100%29.aspx\">MemoryCache</a>.</p>\n",
"comments": [],
"meta_data": {
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
... | {
"AcceptedAnswerId": "24447",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-27T22:21:54.710",
"Id": "24444",
"Score": "4",
"Tags": [
"c#",
"performance",
"cache"
],
"Title": "Implementing cache for server"
} | 24444 |
<p>I've recently written a program for Python 3 that I'm using to practice my newly-acquired object-oriented knowledge. It's a simple text game that is really easy to play and can be run straight out of IDLE with no modules (except the <code>Random</code> module). I realize that beginner code can sometimes be sloppy an... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-28T03:56:44.943",
"Id": "37747",
"Score": "0",
"body": "I think toxotes gave a good explanation and I am agree with him. You should do object oriented programming if you are dealing with classes and all. Ok, I will add just one more po... | [
{
"body": "<p>I would use <code>Player.health</code>, <code>.damage_points</code>, and <code>.cash</code> as instance attributes, rather than class attributes:</p>\n\n<pre><code>class Player(object):\n def __init__(self):\n self.health = 20\n self.damage_points = 8\n self.cash = 0\n</cod... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-27T22:28:37.413",
"Id": "24445",
"Score": "7",
"Tags": [
"python",
"beginner",
"game"
],
"Title": "Alien battle code"
} | 24445 |
<p>I was wondering about the best way to return the most common items in a list. So far the best way I could do it was:</p>
<pre><code>//@param string $this->matched_ids_list = 1,11,12,11,12,
$this->matched_ids_list = "," . $this->matched_ids_list;
$this->ids_count = explode(",", $this->matched_ids_lis... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-27T23:46:40.450",
"Id": "96300",
"Score": "1",
"body": "After the explode() call use [array_count_values](http://php.net/manual/en/function.array-count-values.php)."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2... | [
{
"body": "<h2>Skip to the Dramatic Update for the best solution</h2>\n<p>I was curious how to do this myself, because I could clearly tell there had to be an easier way! I started from scratch and I think I've found exactly what this question is asking for.</p>\n<p>I don't normally just rewrite code, but since... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-27T22:50:32.880",
"Id": "24446",
"Score": "4",
"Tags": [
"php",
"performance"
],
"Title": "Return most common items in list"
} | 24446 |
<p>I'm open to any advice to help improve this code for general use. </p>
<pre><code>/**
*
* This code was created by Joshua Getner.
* @license http://opensource.org/licenses/bsd-license.php BSD
*
*
*/
namespace Blacksmith;
class Autoloader
{
// an array that maps classnames to there filepaths
protected... | [] | [
{
"body": "<ol>\n<li>I don't see a reason to use static methods here, consider using plain 'ol methods.</li>\n<li>Why throw an exception? PHP will issue a fatal error when a class is used but not defined.</li>\n<li>Since you are not using <code>spl_autoload_register()</code>, I'm not sure yours will play nicely... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-28T03:19:32.200",
"Id": "24453",
"Score": "1",
"Tags": [
"php",
"object-oriented",
"classes"
],
"Title": "autoloader class fine tune to be usable in every project"
} | 24453 |
<p>I'm trying to merge counts for items (URLs) in the list:</p>
<pre><code>[['foo',1], ['bar',3],['foo',4]]
</code></pre>
<p>I came up with a function, but it's slow when I run it with 50k entries. I'd appreciate if somebody could please review and suggest improvements.</p>
<pre><code>def dedupe(data):
''' Finds... | [] | [
{
"body": "<p>It looks like you could use <a href=\"http://docs.python.org/2/library/collections.html#counter-objects\"><code>collections.Counter</code></a>. Although you may want to use it earlier in your code, when you create the list of pairs you pass to <code>dedupe</code>. As is, you could use the followin... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-28T06:10:21.250",
"Id": "24458",
"Score": "5",
"Tags": [
"python",
"performance",
"array"
],
"Title": "Find and process duplicates in list of lists"
} | 24458 |
<p>I'm (very) new to Scala, and decided I'd try to get a handle on some of the syntax and general language constructs. This currently doesn't implement any backtracking and will only solve things if they can be derived exactly from the values given in the initial board. </p>
<p>That being said, I don't particularly c... | [] | [
{
"body": "<p>There are some things that can improved here. First, try to find the most appropriate data structure that does what you want. Why a <code>List</code> with x and y coordinates instead of a <code>Tuple2</code> or even better a <code>Position</code>?</p>\n\n<hr>\n\n<p>Why does <code>allPossibleValues... | {
"AcceptedAnswerId": "24486",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-28T07:11:00.193",
"Id": "24461",
"Score": "8",
"Tags": [
"scala",
"sudoku"
],
"Title": "Basic (Logical Only) Sudoku Solver"
} | 24461 |
<p>The following piece of code executes 20 million times each time the program is called, so I need a way to make this code as optimized as possible.</p>
<pre><code>int WilcoxinRST::work(GeneSet originalGeneSet, vector<string> randomGenes) {
vector<string> geneIDs;
vector<bool> isOriginal;
vector<... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-28T08:43:55.200",
"Id": "37740",
"Score": "2",
"body": "Your best course of action is to use a profiler. Other classes e.g. GeneSet need looking at too."
}
] | [
{
"body": "<p>It's somewhat difficult without more context, but there are definitely a few things I can see:</p>\n\n<pre><code>int WilcoxinRST::work(GeneSet originalGeneSet, vector<string> randomGenes) \n</code></pre>\n\n<p>This copies <code>randomGenes</code> every time it is called. You should pass by <... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-28T08:03:49.830",
"Id": "24462",
"Score": "8",
"Tags": [
"c++",
"performance",
"statistics",
"bioinformatics"
],
"Title": "Statistical calculations with sets of genes"
} | 24462 |
<p>I have a requirement to implement a generic UniqueIdentifier which can cover identifiers like OrderId, TrackingID, scannableId and other Ids. The ids, as they are now, are either <code>Long</code> or <code>String</code>.<br>
Below is my implementation of the UniqueIdentifier class --<br></p>
<pre><code>public class... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-28T11:28:04.030",
"Id": "37746",
"Score": "0",
"body": "Are you using an ORM, hibernate/JPA or anyhing else?\n\nGive examples of how you would use this class. As it is, it's not very usable.\n\nMost important of all, where and how did ... | [
{
"body": "<p>Although your requirements are not clear, namely, why unwrapped <code>Long</code> and <code>String</code> objects are not enough or if an identifier can be both simultaneously, I'd say that this would be better served by the following:</p>\n\n<pre><code>public abstract class UniqueIdentifier { /* ... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-28T10:59:27.047",
"Id": "24468",
"Score": "1",
"Tags": [
"java"
],
"Title": "Implementation of an UniqueIdentifier class"
} | 24468 |
<p>I am trying to crank up my javascript knowledge by diving into the MVC/MV* design patterns in order to create more reusable and extendable apps.</p>
<p>I am currently sketching out a webapp that involves real time updating of various data feeds.(Facebook and Twitter in the example code) </p>
<p>The app uses jQuery... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-28T14:38:10.273",
"Id": "37754",
"Score": "0",
"body": "The source can be found at this url. http://001-030.nick.mdcdev.nl/app.php right click and view source. Below at the end of the body tag there is a script tag wich holds the confi... | [
{
"body": "<p>I think this code will require more than one review and more than one rewrite.\nPlease consider the following:\n* If you are planning for more feeds, you could consider a helper function to start each one.</p>\n\n<pre><code> for (i = 0; i < nroffeeds; i+=1) {\n\n config = AppApi.config.feed... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-28T13:35:09.770",
"Id": "24475",
"Score": "1",
"Tags": [
"javascript"
],
"Title": "Learning MV* based javascript, would appreciate a review of my current app"
} | 24475 |
<p>I have a class with a method that will be called multiple times over an object's lifetime to perform some processing steps. This method operates on a mixture of immutable (does not change over the lifetime of the process) data and data that is passed as an argument. The immutable data is comparatively expensive to c... | [] | [
{
"body": "<p>I'd go with Option #3: extract <code>$cache</code> and <code>getImmutable</code> to a new class whose sole responsibility is providing the expensive data. Whether it caches it and how should be up to the new class. This new class will look just like Option #2 but be separated from the existing cla... | {
"AcceptedAnswerId": "24482",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-28T15:48:26.940",
"Id": "24480",
"Score": "7",
"Tags": [
"php",
"object-oriented",
"static"
],
"Title": "Static variable in method vs private class property"
} | 24480 |
<p>I'm trying to calculate a total value with an array of six numbers and all the basic operators (+-*/).</p>
<p>Suppose you have: 1, 2, 3, 4, 5, 6 and you want to have a total of 10.</p>
<p>Here is my function call a part of my result:</p>
<blockquote>
<p>10 = 4 - ((2 - (5 + 6)) + (3 * 1))
10 = (((6 - 4) - 1) ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-28T20:29:54.310",
"Id": "37790",
"Score": "0",
"body": "I believe you wrote too much code. Are you using http://en.wikipedia.org/wiki/Reverse_Polish_notation to compute the values or are you doing something more complex? It is hard to ... | [
{
"body": "<p>I'll comment on the C#ness of the code, then we can worry about the logic.</p>\n\n<p>Overall, not bad. I like the casing and white space.</p>\n\n<p>I see a few things to improve though:</p>\n\n<ol>\n<li>In C#, the general standard for class level members is to prefix with a _. i.e. <code>_soluti... | {
"AcceptedAnswerId": null,
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-28T20:06:30.663",
"Id": "24485",
"Score": "2",
"Tags": [
"c#",
"performance"
],
"Title": "Optimization expression evaluation challenge"
} | 24485 |
<p>I need to pull things from lazy source till timeout is reached in this fashion:</p>
<pre><code>var results = lazySource.SelectMany(item =>
{
//processing goes here
}).Pull(timeout: TimeSpan.FromSeconds(5), times: maxNumberOfIterations);
</code></pre>
<p>and I have this solution: </p>
<pre><code>public stat... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T00:17:11.090",
"Id": "37807",
"Score": "0",
"body": "Use Stopwatch (Elapsed property) instead of DateTime especially DateTime.Now which is slow as hell."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29... | [
{
"body": "<ol>\n<li>A better name for <code>times</code> would be <code>count</code>.</li>\n<li><p>Your two <code>Pull</code> methods are inconsistent:</p>\n\n<ul>\n<li>The first one actually returns a copy of the entire input sequence (<code>ToArray()</code>) while the second one doesn't.</li>\n<li>To keep it... | {
"AcceptedAnswerId": null,
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T00:05:56.317",
"Id": "24492",
"Score": "3",
"Tags": [
"c#",
".net",
"system.reactive"
],
"Title": "pull things from lazy source till timeout is reached"
} | 24492 |
<p>I'm trying to get in the habit of writing better looking code and more efficient code blocks. I wrote this Twitch TV Application which allows you to add and edit channels and it lets you know when a channel is live. Is there anything I can work on to make my code work / look better? </p>
<p><strong>HTML</strong></p... | [] | [
{
"body": "<p>From a once over:</p>\n\n<ul>\n<li>You are mixing single double quote and double quote strings, you should stick to 1</li>\n<li>It is considered better style to have a single comma separated <code>var</code> statement</li>\n<li><p>For</p>\n\n<pre><code>$('#addChan input[name=title]').click(functio... | {
"AcceptedAnswerId": "44610",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T04:06:11.323",
"Id": "24493",
"Score": "0",
"Tags": [
"javascript",
"jquery",
"optimization"
],
"Title": "Optimizing Jquery Twitch TV Application"
} | 24493 |
<p>I am wondering what would be an efficient way to simplify this very simple PHP method and I really lack inspiration:</p>
<pre><code>public function formatToFullDate($countryCode, $hideTime = false)
{
$format = '';
if($hideTime === true)
{
switch($countryCode)
{
case 'us':
... | [] | [
{
"body": "<p>There is still some redundant code in there(' - %H:%M' and '%A %e %B %Y'). You could put these in variabels, but i like it so more. Sory for my bad English.</p>\n\n<pre><code>public function formatToFullDate($countryCode, $hideTime = false)\n{\n $format = '';\n $time_format = '';\n switch... | {
"AcceptedAnswerId": "24498",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T09:27:58.567",
"Id": "24495",
"Score": "5",
"Tags": [
"php"
],
"Title": "Avoid redundant code in this simple method"
} | 24495 |
<p>I'm using Raphael.js to generate objects which are unique but do basically the same thing. I'm also using Fancybox to display a unique image after a click on a generated object.</p>
<p>Is there a way to write this more concise?</p>
<pre><code>//OBJECT 101
var p1_101 = p1.path("M361,146.773 366.25,192.94 365.91... | [] | [
{
"body": "<p>Well you could author a jQuery plugin. That would have default settings, with the option of passing some parameters and override settings.</p>\n\n<p>General ideas behind this are :</p>\n\n<ul>\n<li>Making reusable code. Another project with similar stuff? You include the lib and re-use it.</li>\n<... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T10:27:47.973",
"Id": "24496",
"Score": "3",
"Tags": [
"javascript",
"jquery",
"raphael.js",
"fancybox"
],
"Title": "Displaying unique objects generated by Raphael.js in FancyB... | 24496 |
<p>I've got this code that I found in a project and I'm wondering how can it be done better:</p>
<pre><code>- (void) setAlpha:(float)alpha {
if (self.superview.tag == noDisableVerticalScrollTag) {
if (alpha == 0 && self.autoresizingMask == UIViewAutoresizingFlexibleLeftMargin) {
if (sel... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T18:10:47.727",
"Id": "37823",
"Score": "1",
"body": "Woah there. That's a big set of conditions. It would be more helpful to know context: what do these do, why are they here, and what do they apply to? It may be possible to break t... | [
{
"body": "<p>That’s almost unreadable. Or, more precisely, it’s plain what the code does, but it’s almost impossible to guess the purpose. Generally, there are two conditions that prevent the view from taking a new alpha setting, right? Some general remarks:</p>\n\n<ul>\n<li><p>The <code>noDisableHorizontalScr... | {
"AcceptedAnswerId": "24796",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T10:56:24.767",
"Id": "24497",
"Score": "0",
"Tags": [
"objective-c",
"ios"
],
"Title": "iOS/Objective-C \"if\" best practice"
} | 24497 |
<p>What is the best way to tidy up JavaScript code I have written? It seems very long winded, any suggestions to shorten it?</p>
<p>Note: I'm running it on Wordpress which runs jQuery in nonconflict mode, hence the insane amounts of <code>jQuery.()</code>.</p>
<pre><code>jQuery("document").ready(function (jQuery) {
... | [] | [
{
"body": "<p>One simple thing you could do is to use <code>jQuery</code> as <code>$</code> inside your <code>ready</code> function.</p>\n\n<p>Since the <code>ready</code> function takes a parameter to <code>jQuery</code>, you could capture that as <code>$</code> for the duration of the <code>ready</code> funct... | {
"AcceptedAnswerId": "24503",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T13:46:40.100",
"Id": "24499",
"Score": "3",
"Tags": [
"javascript",
"jquery",
"animation",
"wordpress"
],
"Title": "jQuery animation and rotation effects for a WordPress si... | 24499 |
<p>I have a fitness function, where inputs <code>x1</code> to <code>x14</code> have values between 1:4. Is there a way to make this more efficient? Any suggestions are useful. I need to get the computation time down. A smaller scale pseudocode is below:</p>
<pre><code>%Help improve computation time
function y = Red... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-08-26T12:33:50.490",
"Id": "48055",
"Score": "0",
"body": "Please try to [profile](http://www.mathworks.nl/help/matlab/ref/profile.html) your code. That gives you and others a starting point for improvements."
}
] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T15:54:08.903",
"Id": "24505",
"Score": "3",
"Tags": [
"performance",
"matlab"
],
"Title": "Fitness function"
} | 24505 |
<p>How could I possibly shorten / clean this up? I suppose mainly clean up the loop at the start asking whether they would like to scramble the code.</p>
<pre><code>def userAnswer(letters):
print("Can you make a word from these letters? "+str(letters)+" :")
x = input("Would you like to scrample the letters? E... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T18:21:56.353",
"Id": "37824",
"Score": "0",
"body": "What version of python are you using?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2014-05-26T17:58:57.653",
"Id": "89467",
"Score": "0",
"body... | [
{
"body": "<p>For one thing, if <code>checkSubset()</code> and <code>checkWord()</code> return bools, you can leave out the <code>== True</code>. </p>\n",
"comments": [],
"meta_data": {
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T18:22:20.603",
... | {
"AcceptedAnswerId": "24518",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T18:17:16.813",
"Id": "24508",
"Score": "5",
"Tags": [
"python",
"beginner",
"game",
"python-3.x"
],
"Title": "Guessing words from scrambled letters"
} | 24508 |
<p>I am developing a WEB based GPS system and one of its functionalities is to be able to send messages and routes to GARMIN devices.</p>
<p>I have never been using sockets before, and for some (obvious) reason, I don't feel ready to submit my code before being reviewed by more experienced users.</p>
<p>If some of yo... | [] | [
{
"body": "<ol>\n<li><blockquote>\n<pre><code>private ServerSocket mReceiverSocket = null;\n...\npublic MessageReceiver()\n{\n try \n {\n if(mReceiverSocket == null) { mReceiverSocket = new ServerSocket(12346); }\n System.out.println(\"mReceiverSocket initialized\");\n } \n catch (IOE... | {
"AcceptedAnswerId": "43781",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T18:46:58.000",
"Id": "24510",
"Score": "3",
"Tags": [
"java",
"multithreading",
"networking",
"socket",
"location-services"
],
"Title": "Communication with GARMIN throu... | 24510 |
<p>I'm using PDO, but I want to create my own class for more convenient work with the database.</p>
<p>My main idea in example with comments:</p>
<pre><code><?php
/**
* User entity. I want to save it into database.
* @Table(name="my_table")
*/
class User {
/**
* Special phpDoc values indicates that ... | [] | [
{
"body": "<p><strong>Code critique</strong></p>\n\n<p>You indicate that the constructor will be responsible for fetching and hydrating the Entity's values. I know this is tempting. Don't do it. You already have something named <code>DataManager</code>. Shouldn't it handle fetching and hydrating the Entity?</p>... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T19:41:14.740",
"Id": "24512",
"Score": "2",
"Tags": [
"php",
"mysql",
"reflection"
],
"Title": "PHP ORM like system"
} | 24512 |
<p>I've been working on a lightweight cookiemanager and version 1 has been completed and works perfectly as intended. I would love to get a review to see where we can improve it.</p>
<p>Where should I throw logical errors? For now I only log. </p>
<p>Its usage is as follows:</p>
<p>Getting cookies can be done by cal... | [] | [
{
"body": "<p>For a \"lightweight\" cookie manager, there seems to be rather a lot going on in my opinion. And I'd call the object <code>CookieManager</code> since it's two words, but that's not structurally/syntactically important.</p>\n\n<p>I am however a bit suspicious of the <code>get()</code> usage. If I g... | {
"AcceptedAnswerId": "24544",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T21:31:57.357",
"Id": "24513",
"Score": "3",
"Tags": [
"javascript"
],
"Title": "Lightweight Cookiemanager"
} | 24513 |
<p>Basically I have created a function which takes two lists of dicts, for example: </p>
<pre><code>oldList = [{'a':2}, {'v':2}]
newList = [{'a':4},{'c':4},{'e':5}]
</code></pre>
<p>My aim is to check for each dictionary key in the oldList and if it has the same dictionary key as in the newList update the dictionary,... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T22:34:04.337",
"Id": "37835",
"Score": "4",
"body": "Why in the world do you have a list of single element dictionaries?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T23:33:20.897",
"Id": "37844... | [
{
"body": "<p>You don't need the <code>isAdd</code> flag, instead you can structure your for loops like this:</p>\n\n<pre><code>for new, old in newList, oldList:\n if new.keys()[0] == old.keys()[0]:\n old.update(new)\n else:\n oldList.append(new)\nreturn oldList\n</code></pre>\n\n<p>As well,... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T22:08:41.477",
"Id": "24514",
"Score": "1",
"Tags": [
"python",
"hash-map"
],
"Title": "Updating list of Dictionaries from other such list"
} | 24514 |
<p>Here is a problem I am trying to solve: trying to find missing photographs from a sequence of filenames. The problem boils down to: given an unsorted list of integers, return a sorted list of missing numbers. Below is my code.</p>
<p>What I am looking for are:</p>
<ul>
<li>Are there more efficient algorithms?</li>... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T23:56:18.607",
"Id": "37847",
"Score": "0",
"body": "Looks fine to me. `find_missing_items([-20000,0,20000])` returned correctly all 39998 items in less than 2s running on a old dual-core."
}
] | [
{
"body": "<p>For this</p>\n\n<pre><code>full_set = set(xrange(smallest_item, largest_item + 1))\n</code></pre>\n\n<p>I'd suggest inlining the min and max:</p>\n\n<pre><code>full_set = set(xrange(min(original), max(original) + 1))\n</code></pre>\n\n<p>You don't need to convert to a list before using sorted so:<... | {
"AcceptedAnswerId": "24523",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T23:39:45.547",
"Id": "24520",
"Score": "5",
"Tags": [
"python",
"algorithm",
"performance"
],
"Title": "Finding missing items in an int list"
} | 24520 |
<p>Number can be very big.Script is taking number as a "number" list and crossing from 10-decimal based system to n-based system until list equals "5".I don't use letters like in 16-base system(hex).Example if result will be AB5 in hex list will be [10,11,5]. Some ideas how to optimize or/and change this to recursion?A... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T23:48:17.783",
"Id": "37845",
"Score": "2",
"body": "Welcome to Code Review! Please add an explanation of what the purpose of this code is."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T23:49:51.663... | [
{
"body": "<p>I can't do much to help optimize this code as I don't know what its trying to do.</p>\n\n<pre><code>r=-1\nnumber = [9,7,1,3,3,6,4,6,5,8,5,3,1]\n</code></pre>\n\n<p>I suggest presenting this as a function which takes number as a parameter. Code also runs faster in functions.</p>\n\n<pre><code>l=len... | {
"AcceptedAnswerId": null,
"CommentCount": "11",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T23:42:34.243",
"Id": "24521",
"Score": "-3",
"Tags": [
"python",
"optimization"
],
"Title": "Any way to change optimize this or/and change to recursion?"
} | 24521 |
<p>In such an array:</p>
<pre><code>$arr = Array (
[key0] => Array (
[subkey0] => Array (
[0] => "value0"
[1] => "value1"
)
[subkey1] => Array (
[0] => "value0"
[1] => "value1"
)
)
[key1] => Array (
... | [] | [
{
"body": "<p>I don't know if I understand the question right. But this should work.</p>\n\n<pre><code>foreach ($keys as $key) {\n $arr2[$key]['subkey0'][] = $var1;\n $arr2[$key]['subkey1'][] = $var2;\n}\n</code></pre>\n",
"comments": [
{
"ContentLicense": "CC BY-SA 3.0",
"Creati... | {
"AcceptedAnswerId": "24524",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-29T23:43:11.220",
"Id": "24522",
"Score": "1",
"Tags": [
"php",
"array",
"php5"
],
"Title": "Referring to nested arrays and array_merge (php)"
} | 24522 |
<p>I have an http library in Ruby with a Response class, I have broken it down into smaller methods to avoid having a big intialize method, but then I end up with this:</p>
<pre><code> def initialize(res)
@res = res
@headers = Hash.new
if res.empty?
raise InvalidHttpResponse
end
split_data
... | [] | [
{
"body": "<p>Looking through your whole module it's apparent you've only had experience with imperative programming (it's full of side-effects, variable re-bounds, in-place updates, ...). I've already written on the topic (see <a href=\"https://code.google.com/p/tokland/wiki/RubyFunctionalProgramming\" rel=\"n... | {
"AcceptedAnswerId": "24533",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T02:39:38.797",
"Id": "24525",
"Score": "4",
"Tags": [
"ruby"
],
"Title": "Refactor a sequence of functions"
} | 24525 |
<p>I was using <code>foo(bar)</code> as it's adopted for functional programming.</p>
<pre><code>console.log(
join(
map(function(row){ return row.join(" "); },
tablify(
map(function(text){return align(text},
view),
20))),
"\n");
</code></pr... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T06:50:37.970",
"Id": "37868",
"Score": "0",
"body": "This will help you find the right thing... just go to http://jsperf.com/ and paste your code there and compare the speed..."
},
{
"ContentLicense": "CC BY-SA 3.0",
"Cr... | [
{
"body": "<p>I think that the first variant can look nice, too, if you change the way you format the code and you always use the function as the last argument:</p>\n\n<pre><code>log(map(tablify(map(view, function(text) {\n return align(text)\n}), 20), function(row) {\n return row.join(\" \")\n}).join(\"\\n\"... | {
"AcceptedAnswerId": null,
"CommentCount": "11",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T06:29:54.290",
"Id": "24526",
"Score": "3",
"Tags": [
"javascript",
"object-oriented",
"functional-programming"
],
"Title": "The eternal dilemma: bar.foo() or foo(bar)?"
} | 24526 |
<p>I am using PHP to build a REST API. I want to receive requests from the URL in the following format:</p>
<p>http://mydomain.com/api/format/method?app_id=value&call=encrypted_request</p>
<p>So, I have modified my .htaccess as:</p>
<pre><code>Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteCo... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T08:57:56.257",
"Id": "37864",
"Score": "0",
"body": "If you want to fetch the `GET` variables, they are stored in `$_GET` array."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T08:59:48.443",
"Id"... | [
{
"body": "<p>Instead of raw string manipulation, I would use <a href=\"http://php.net/parse_url\" rel=\"nofollow\">parse_url</a> and <a href=\"http://php.net/parse_str\" rel=\"nofollow\">parse_str</a>:</p>\n\n<pre><code><?php\n\n// Note: parse_url is not intended for relative URLs\n// (in other words, bare ... | {
"AcceptedAnswerId": "24555",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T08:44:35.950",
"Id": "24528",
"Score": "2",
"Tags": [
"php",
"api",
".htaccess"
],
"Title": "Is it ok to use PHP's strpos and substr altogether in one statement?"
} | 24528 |
<p>I've recently picked up PHP, for the first assignment in my IT-security class I decided to code it in PHP.
The algorithm is an alternation of the Caesar cipher, instead of using a fixed number to push the letters a keyword is applied to rearrange a couple of letters and then move the rest of the letters accordingly... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T10:10:48.030",
"Id": "37867",
"Score": "3",
"body": "Welcome to CodeReview.SE. Please have a look at the http://codereview.stackexchange.com/faq . Among other things, please make sure you include your code in the question. Enjoy!"
... | [
{
"body": "<p>Give the class a better name... a noun makes more sense than a verb.</p>\n\n<p>Consider simply removing the <code>$uword</code> and <code>$eword</code> properties and applicable setter methods. Instead, your encrypt method signature could be:</p>\n\n<pre><code>/**\n * @param string $uword word to ... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T09:33:47.110",
"Id": "24529",
"Score": "4",
"Tags": [
"php",
"algorithm",
"php5",
"caesar-cipher"
],
"Title": "Alternated Caesar Cipher"
} | 24529 |
<p>Basically I have a 2d list containing movies. Movies are specified by name, version and description. For example</p>
<p><code>['Jaws', 1, 'Movie about sharks']</code> - where <code>Jaws</code> = name, <code>1</code> = version and <code>Movie about sharks</code> = description.</p>
<p>My result is a dictionary whic... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T13:33:52.420",
"Id": "37876",
"Score": "1",
"body": "What if a movie was to be called 'somethingV1' ?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T13:53:57.153",
"Id": "37880",
"Score": "0"... | [
{
"body": "<p>A simple <a href=\"http://www.youtube.com/watch?v=pShL9DCSIUw\">dict comprehension</a> and use of <a href=\"http://docs.python.org/3.3/library/stdtypes.html#str.format\"><code>str.format()</code></a> will do the job here:</p>\n\n<pre><code>>>> {\"{}V{}\".format(name, version): description... | {
"AcceptedAnswerId": "24543",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T12:45:29.473",
"Id": "24534",
"Score": "1",
"Tags": [
"python",
"hash-map"
],
"Title": "update list based on other values"
} | 24534 |
<p>There have been many posts here about enums in python, but none seem to be both type safe and simple. Here is my attempt at it. Please let me know if you see anything obviously wrong with it. </p>
<pre><code>def typeSafeEnum(enumName, *sequential):
topLevel = type(enumName, (object,), dict(zip(sequential, [None... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T11:46:54.223",
"Id": "37877",
"Score": "1",
"body": "What do you mean by \"type safe\"?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T11:51:10.883",
"Id": "37878",
"Score": "0",
"body": ... | [
{
"body": "<pre><code>def typeSafeEnum(enumName, *sequential):\n</code></pre>\n\n<p>Python convention is <code>lowercase_with_underscores</code> for functions and parameters. Although since this is a type factory its not totally clear what convention should appply. I also wonder if passing the enum values as a ... | {
"AcceptedAnswerId": "24541",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T11:38:52.837",
"Id": "24536",
"Score": "3",
"Tags": [
"python",
"enum",
"type-safety"
],
"Title": "An attempt at a simple type safe python enum"
} | 24536 |
<p>Is there any way I can simplify this inefficient piece of JavaScript?</p>
<pre><code>$(function () {
var $carousel = $('#carousel');
var $switch = $('#switch');
var $header = $('#header');
var $submit = $('#submit');
$carousel.bind('slid', function() {
var index = $('#carousel .item').i... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T14:32:11.570",
"Id": "37882",
"Score": "0",
"body": "I'm assuming `$` stands for `jQuery`?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T16:00:02.053",
"Id": "37885",
"Score": "0",
"body... | [
{
"body": "<p>You could do this:</p>\n\n<pre><code>$(function () {\n var $carousel = $('#carousel');\n var $switch = $('#switch');\n var $headerSubmit = $('#header, #submit');\n var $submit = $('#submit');\n var text_strings = [ 'Sign In', 'Sign Up' ];\n var form_strings = [ 'sign_in', 'sign_u... | {
"AcceptedAnswerId": "24539",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T14:18:22.803",
"Id": "24538",
"Score": "8",
"Tags": [
"javascript",
"jquery",
"dom"
],
"Title": "Switching some text labels when the first image of a carousel is active"
} | 24538 |
<p>I'm working on an ASP.NET project using VB.NET that uses <a href="https://github.com/SamSaffron/dapper-dot-net" rel="nofollow">Dapper</a> and the code as implemented so far runs fine with just me testing. In the example below, Dapper calls a stored proc. </p>
<p>But I am wondering primarily right now if the method ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T08:38:58.990",
"Id": "38022",
"Score": "0",
"body": "Just an opinion but if your GetList() method is returning a list then perhaps you should specify an IList instead of an IEnumerable. That way it's explict to the caller that ther... | [
{
"body": "<p>According to MSDN:</p>\n\n<blockquote>\n <p>\"We recommend that you always close the connection when you are finished using it in order for the connection to be returned to the pool. You can do this using either the <strong>Close</strong> or <strong>Dispose</strong> methods of the Connection obje... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T15:52:17.557",
"Id": "24542",
"Score": "4",
"Tags": [
"asp.net",
"database",
"vb.net"
],
"Title": "Handling of open database connection and calling shared function from WebAPI"
} | 24542 |
<p>I have a function that I need to optimize:</p>
<pre><code>def search(graph, node, maxdepth = 10, depth = 0):
nodes = []
for neighbor in graph.neighbors_iter(node):
if graph.node[neighbor].get('station', False):
return neighbor
nodes.append(neighbor)
for i in nodes:
if... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-31T01:17:28.670",
"Id": "37924",
"Score": "0",
"body": "You might have a bug: `if search(graph, i, ...): return i` ==> the search() function might return something other than i, yet you are returning i."
},
{
"ContentLicense": ... | [
{
"body": "<pre><code>def search(graph, node, maxdepth = 10, depth = 0):\n nodes = []\n for neighbor in graph.neighbors_iter(node):\n if graph.node[neighbor].get('station', False):\n return neighbor\n nodes.append(neighbor)\n</code></pre>\n\n<p>Why store the neighbor in the list? ... | {
"AcceptedAnswerId": "24549",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T18:18:15.363",
"Id": "24546",
"Score": "3",
"Tags": [
"python",
"performance",
"recursion",
"graph"
],
"Title": "Finding the closest node"
} | 24546 |
<p>I've implemented selection sort, heap sort, insertion sort, and iterative quicksort using C++11. Am I using the correct STL data structures/algorithms? Am I using move semantics correctly in my code?</p>
<pre><code>#include <algorithm>
#include <vector>
#include <queue>
#include <functional>... | [] | [
{
"body": "<p>Your code looks good, I don't see any major problems.</p>\n<p>Here's what I would consider changing:</p>\n<ul>\n<li>Name your iterator template arguments by the <a href=\"https://en.cppreference.com/w/cpp/named_req\" rel=\"nofollow noreferrer\">iterator concept they are required to meet</a>. For e... | {
"AcceptedAnswerId": "24600",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T18:51:42.670",
"Id": "24548",
"Score": "3",
"Tags": [
"c++",
"c++11",
"sorting",
"stl"
],
"Title": "Am I using C++11 features like STL and move semantics correctly?"
} | 24548 |
<p>I am learning ORM using Hibernate by creating a movie rental application. Currently, I have the following three entities for persistence:</p>
<ul>
<li>User</li>
<li>Movie</li>
<li>Rental (composite element for many-to-many mapping between above two)</li>
</ul>
<p>Now, to handle the cart updates, I have the followi... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T06:47:00.047",
"Id": "38018",
"Score": "1",
"body": "\"I need to keep track of the number of rentals per movie.\" And why is that? How you should store that number depends wholly on why you need it. E.g. if you need it for some mont... | [
{
"body": "<p>You are not correct in assuming the above... If you want to count the number of rentals for one Movie you can do the following (given you are not removing records from your database):</p>\n\n<pre><code>@NamedQueries{\n @NamedQuery(name=\"Movie.getRentalCount\", \n query=\"SELECT COUNT FROM... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-30T20:21:59.753",
"Id": "24552",
"Score": "2",
"Tags": [
"java"
],
"Title": "How do I structure my entities for easier persistence?"
} | 24552 |
<p>The scripts below, I've obtained through tutorials and edited the best I know how. It all works - but I'd like to place them in one, external script. If at all possible, I'd appreciate some help in cleaning up the code too. I've a feeling some of it is redundant.</p>
<p>With the client-side validation script, I hav... | [] | [
{
"body": "<p>basically just cleaned up some redundant brackets, tags, added a comment and modified existing comments, fixed indentations etc. Then i ran it through JSLint(<a href=\"http://www.jslint.com/\" rel=\"nofollow\">http://www.jslint.com/</a>), enjoy :)!</p>\n\n<pre><code>$(document).ready(function() {\... | {
"AcceptedAnswerId": "24563",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-31T05:54:04.693",
"Id": "24562",
"Score": "3",
"Tags": [
"javascript",
"beginner",
"jquery",
"validation"
],
"Title": "Contact information validation"
} | 24562 |
<p>Looking at knockout for the first time, would like to know if I'm approaching it correctly.</p>
<p>Here's a live example:
<a href="http://jsfiddle.net/fergal_doyle/xhAe8/1/" rel="nofollow">http://jsfiddle.net/fergal_doyle/xhAe8/1/</a></p>
<p>I have a "car" select, and an "other" input to allow values not present i... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-31T14:15:25.123",
"Id": "37932",
"Score": "3",
"body": "`if(self.other()) return false; else return true;` is a bit verbose, I'd just write `return !self.other();`"
}
] | [
{
"body": "<p>It looks fine to me. My only real criticism is your <code>checkCar</code> implementation. It ought to be written out as:</p>\n\n<pre><code>self.checkCar = ko.computed(function(){\n return !self.other();\n});\n</code></pre>\n\n<p>On a minor note, you should case your view model name in PascalC... | {
"AcceptedAnswerId": "24569",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-31T13:01:58.353",
"Id": "24565",
"Score": "1",
"Tags": [
"javascript"
],
"Title": "Knockout first-timer"
} | 24565 |
<p>I'm creating a MUD and am running into some design issues regarding Items.</p>
<p>Items are stored in an <code>ItemDatabase</code> after being read from a JSON file. </p>
<pre><code>template <typename ItemType>
static void load(std::string const& filename)
{
PropertyTree propertyTree;
boost::prop... | [] | [
{
"body": "<p>Let me restate the problem, because it wasn't 100% clear to me from what you've written. I personally think you've left out a bit too much code. I'll state my assumptions of what is going on, but ideally this should be clear from what is posted. Firstly, I assume <code>EntityDatabase</code> is a c... | {
"AcceptedAnswerId": "24588",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-31T14:13:24.163",
"Id": "24566",
"Score": "6",
"Tags": [
"c++",
"c++11",
"pointers"
],
"Title": "unique_ptr usage too unwieldy"
} | 24566 |
<p>This is my first real program, though it has gone through a few major revisions. Anyhow, I am sure that there is a lot I could have done better, cleaner or safer.</p>
<p>Can anyone see anything I really need to work on or fix?</p>
<pre><code>#include <stdio.h>
#include <stdlib.h>
#include <unistd.h&... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-31T16:33:08.847",
"Id": "37936",
"Score": "1",
"body": "Hello, and welcome to Code Review. Questions without code in them are considered off topic, so you'll need to inline the link. If you're having trouble indenting so it's formatted... | [
{
"body": "<p>Here are a few things I notice from skimming through your code:</p>\n\n<p>Pros:</p>\n\n<ul>\n<li>Nice and clean style, lines are short enough to ease reading</li>\n<li>Picked good names for functions</li>\n</ul>\n\n<p>Cons:</p>\n\n<ul>\n<li>The use of one-letter variable. They are not descriptive.... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-31T15:55:53.750",
"Id": "24568",
"Score": "8",
"Tags": [
"c",
"beginner",
"game",
"playing-cards"
],
"Title": "First Blackjack game in C"
} | 24568 |
<p>I have a weighted BFS search in python for networkx that I want to optimize:</p>
<pre><code>def bfs(graph, node, attribute, max = 10000):
# cProfile shows this taking up the most time. Maybe I could use heapq directly? Or maybe I would get more of a benefit making it multithreaded?
queue = Queue.PriorityQue... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-31T17:20:41.260",
"Id": "37942",
"Score": "0",
"body": "Definitely use `heapq` directly when you don't need the thread synchronization that `Queue.PriorityQueue` provides."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDa... | [
{
"body": "<p>Some things that might improve your code:</p>\n\n<ol>\n<li><p>Use <code>heapq</code> rather than <code>Queue.PriorityQueue</code>. The latter does synchronization stuff that you don't need, so <code>heapq</code> will be faster.</p></li>\n<li><p>Don't add <code>neighbor</code> to <code>done</code> ... | {
"AcceptedAnswerId": "24574",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-31T17:06:50.900",
"Id": "24572",
"Score": "2",
"Tags": [
"python",
"queue"
],
"Title": "Optimize BFS weighted search in python"
} | 24572 |
<p>I want to use some constant in both server side and client side.</p>
<p>Currently, I use PHP reflection in order to synchronize PHP constants and Javascript constants. But for the maintainable reason, it's not a best practise.</p>
<p>For example:</p>
<pre><code>interface IType{
//...
const ID_TYPE_TEXT = 100... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-01T11:14:32.463",
"Id": "37966",
"Score": "0",
"body": "Either place your constants somewhere in the DOM, or retrieve the variables from the server using AJAX"
}
] | [
{
"body": "<p>One way is to keep the constants in a database table and have a script that automatically generates both JS and PHP code by looking through it. The advantages of this are that you can store other bits of information if you want, and the code is all generated beforehand so there is no extra overhe... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-31T20:28:52.913",
"Id": "24576",
"Score": "3",
"Tags": [
"php",
"reflection"
],
"Title": "How to avoid echoing JavaScript from PHP?"
} | 24576 |
<p>I'm implementing an <code>is_dir()</code> function to determine if a given item is a directory or a file for FTP/FTPS connections. Currently, I have two methods: one using PHP's FTP wrapper for the <code>is_dir()</code> function and another recommended in the help page for the <code>ftp_chdir()</code> function.</p>
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-01T11:17:44.900",
"Id": "37968",
"Score": "0",
"body": "Why do you iterate through all entries of the directory?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-01T16:31:43.260",
"Id": "37982",
"Sco... | [
{
"body": "<p>In case someone is interested, here's a follow up to MarcDefiant's comment about using <code>ftp_rawlist()</code>. It's fast if you're already using it for getting the file list as well. Here's a snippet from the <code>filebrowser.php</code> in my <a href=\"https://github.com/mircerlancerous/FileB... | {
"AcceptedAnswerId": null,
"CommentCount": "8",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-31T22:42:29.873",
"Id": "24578",
"Score": "6",
"Tags": [
"php",
"file-system",
"ftp"
],
"Title": "is_dir function for FTP/FTPS connections"
} | 24578 |
<p>In Rails 3, I have two models: <code>Nutrient</code> and <code>Recommendation</code>:</p>
<ul>
<li><code>Nutrient</code> has_many <code>Recommendation</code></li>
<li><code>Recommendation</code> belongs_to <code>Nutrient</code></li>
</ul>
<p>I am trying to implement the create method of the recommendation in the c... | [] | [
{
"body": "<p>Your approach is fundamentally flawed, I'm afraid. You're not using Rails' abilities to your advantage at all.</p>\n\n<p>For a simple create operation, your controller method rarely needs to be more than 10 lines or so. There's way too much going on in yours.</p>\n\n<p>Start by nesting your routes... | {
"AcceptedAnswerId": "24609",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-31T23:02:03.323",
"Id": "24579",
"Score": "1",
"Tags": [
"ruby",
"ruby-on-rails"
],
"Title": "Nutrient and Recommendation models"
} | 24579 |
<p>I have a number of functions inside class, which serve my scripts to provide functionality such as a login, which is the function I am going to be using as my examples. </p>
<pre><code> public static function GetCredentials ($Argument_1, $Argument_2){
$GetUserInformation = self::$Database->prepare("S... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-01T09:58:39.477",
"Id": "37965",
"Score": "0",
"body": "I'd suggest you not to store password as text. Use `PASSWORD()` function in MySQL."
}
] | [
{
"body": "<h2>Multiple problems</h2>\n\n<p>Why is static?\nWhy is the database hardcoded in the class?\nEcho or return? Neither\nExit?\nDirect $_SESSION write (super global)\nHTTP stuff in an Auth class?</p>\n\n<h2>Why is static?</h2>\n\n<p>You are using stuffs in static where you should not have. Handling dat... | {
"AcceptedAnswerId": "24597",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-31T23:37:30.320",
"Id": "24580",
"Score": "3",
"Tags": [
"php"
],
"Title": "Return over echo?"
} | 24580 |
<p>I have to write methods for a binary tree that has positive integers as values. These are not full methods. They are supposed to be written in pseudocode.</p>
<p>a) Count number of nodes in a Tree</p>
<pre><code>countNodes(TreeNode root){
if(root == null)
return 0;
else{
TreeNode left = roo... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T01:50:38.550",
"Id": "38013",
"Score": "1",
"body": "I'm afraid that psuedocode is off-topic as per the FAQ."
}
] | [
{
"body": "<p>Even if you have to use pseudocode, you should do some checks. Either in pseudcode (calculcate by hand) or implement it in any language.</p>\n\n<p>Most of them look ok, some comments:</p>\n\n<pre><code> return search(i, root.getLeftChild);\n return search(i, root.getRightChild);\n</code></pr... | {
"AcceptedAnswerId": "24617",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-01T01:56:36.003",
"Id": "24581",
"Score": "-2",
"Tags": [
"java"
],
"Title": "Review of the following methods of a binary tree which contains positive integers as it's elements"
} | 24581 |
<pre><code>private eFileStatus CheckConfigSettings()
{
mFileStatus = mXMLHelper.CheckElement(eElementName.StartLanguage.ToString(), out mContent);
if (eFileStatus.OK != mFileStatus)
{
return mFileStatus;
}
else if (eFileStatus.OK == mFileStatus... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-01T14:48:44.650",
"Id": "37976",
"Score": "2",
"body": "This code cannot compile - you don't have a \"fall-through\" return value."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-03T15:14:22.223",
"Id":... | [
{
"body": "<p>It would be straightforward to extract the repeated code into a function. However, this would still leave a lot of room for improvement:</p>\n\n<p><strong>Separation of concerns</strong>: Your function does several unrelated things: reading a configuration file, some input validation, and user inp... | {
"AcceptedAnswerId": null,
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-01T06:59:56.740",
"Id": "24587",
"Score": "-2",
"Tags": [
"c#"
],
"Title": "How can I refactor this code in order to not repeat code?"
} | 24587 |
<p>I know that Swing <a href="http://www.oracle.com/technetwork/articles/javase/index-142890.html#3" rel="nofollow">isn't true MVC</a>:</p>
<blockquote>
<p>Using this modified MVC helps to more completely decouple the model
from the view.</p>
</blockquote>
<p>Leaving aside the veracity of the above claim, the pro... | [] | [
{
"body": "<p>Extract the busy-work code of transforming the <code>List<Message></code> to <code>DefaultListModel</code> into another method.</p>\n\n<p>From what I'm seeing I would think about the concepts you want to express in your code vis-a-vis some technical adherence to \"encapsulation\". If you wan... | {
"AcceptedAnswerId": "24620",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-01T10:04:32.223",
"Id": "24591",
"Score": "1",
"Tags": [
"java",
"design-patterns",
"mvc",
"swing"
],
"Title": "Encapsulating this List<Message> properly"
} | 24591 |
<p>My implemented regex pattern contains two repeating symbols: <code>\d{2}\.</code> and <code><p>(.*)</p></code>. I want to get rid of this repetition and asked myself if there is a way to loop in Python's regular expression implementation.</p>
<p><strong>Note</strong>: I do <strong>not</strong> ask for h... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-01T14:30:35.247",
"Id": "37974",
"Score": "1",
"body": "Parsing XML with regex is usually wrong, what are you really trying to accomplish?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-01T14:37:01.643",
... | [
{
"body": "<p>Firstly, just for anyone who might read this: DO NOT take this as an excuse to parse your XML with regular expressions. It generally a really really bad idea! In this case the XML is malformed, so its the best we can do.</p>\n\n<p>The regular expressions looping constructs are <code>*</code> and <... | {
"AcceptedAnswerId": "24599",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-01T13:38:29.410",
"Id": "24594",
"Score": "1",
"Tags": [
"python",
"regex"
],
"Title": "Improvment of and looping in a regular expression pattern"
} | 24594 |
<p>Teachers can raise 'dramas' which are when students forget equipment, don't do homework or are flagged for concern. Each drama for each student is stored as a separate record.</p>
<p>I seek a list of all unresolved dramas, <strong>grouped by student & date</strong>. So if on one day a student forgets their home... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-01T16:06:23.010",
"Id": "37980",
"Score": "0",
"body": "What LINQ provider are you using? Can't you just retrieve the whole `Drama`, `Student` and `Teacher` objects?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "... | [
{
"body": "<p>It seems like you could do this:</p>\n\n<pre><code>var results = \n (from d in ctx.Dramas\n where !d.Resolved\n group d by new { d.Student, DateHappened = d.DateHappened.ToString(\"ddMMyy\") } into g\n select new\n {\n Student = g.Key.Student,\n DateHappened = g.... | {
"AcceptedAnswerId": "24614",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-01T15:03:53.043",
"Id": "24595",
"Score": "3",
"Tags": [
"c#",
"linq",
"linq-to-sql"
],
"Title": "LINQ statement to aggregate data"
} | 24595 |
<p>This started simple, as most things do. I just wanted a little structure to hold polar coordinates for a totally minor class. But starting with <a href="https://stackoverflow.com/a/9485264">this answer</a> and borrowing from existing Objective-C concepts like <code>CGPoint</code> and <code>CGSize</code>, the simple ... | [] | [
{
"body": "<ul>\n<li><p>Why the <code>#ifdef</code> guard? It’s not needed in Objective-C since we have the <code>#import</code> directive instead of <code>#include</code>.</p></li>\n<li><p>Why have the whole thing in the header? The usual solution is to put the function declarations into the header and the imp... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-01T16:19:42.053",
"Id": "24596",
"Score": "2",
"Tags": [
"objective-c",
"ios",
"coordinate-system"
],
"Title": "A structure and helper functions for working with polar coordinates"
} | 24596 |
<p>In the snippet below the three exceptions I declare in the <code>throws</code> clause are all thrown by BeanUtils.setProperty(), which is a third party library.</p>
<p>Is letting these three exceptions "bubble upwards" with a <code>throws</code> declaration bad style? Is it better to wrap them in my own RoleBeanEx... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-01T17:48:42.157",
"Id": "37984",
"Score": "0",
"body": "Are these errors anything that the calling code would actually want/need to handle?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-01T17:51:29.403",
... | [
{
"body": "<p>Programming errors should, as much as possible, produce unchecked rather than checked exceptions. Checked exceptions are for circumstances that will occur during the normal operation of the program and thus should be handled. For example, you should handle the IOException raised by attempting to o... | {
"AcceptedAnswerId": "24601",
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-01T17:37:37.467",
"Id": "24598",
"Score": "1",
"Tags": [
"java",
"exception-handling"
],
"Title": "When to catch/wrap an exception vs declaring that method throws exception"
} | 24598 |
<p>As an interesting exercise in Python 3.3, I implemented a <a href="http://en.wikipedia.org/wiki/Trie" rel="noreferrer"><strong>Trie</strong></a> (also known as a Prefix Tree).</p>
<h2>Example usages</h2>
<h3>Create from list of key-value-tuples</h3>
<pre><code>mapping = (('she', 1), ('sells', 5), ('sea', 10), ('shel... | [] | [
{
"body": "<p>A note on terminology: a <em>mapping</em> is, according to <a href=\"http://docs.python.org/3/glossary.html#term-mapping\" rel=\"nofollow\">Python glossary</a>,</p>\n\n<blockquote>\n <p>A container object that supports arbitrary key lookups and implements\n the methods specified in the Mapping o... | {
"AcceptedAnswerId": "24611",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-01T18:43:37.063",
"Id": "24602",
"Score": "9",
"Tags": [
"python",
"algorithm",
"trie"
],
"Title": "Readability and Performance of my Trie implementation"
} | 24602 |
<p>Here is my first try at Google's Go language, trying to solve Project Euler Problem 10:</p>
<blockquote>
<p>The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.</p>
<p>Find the sum of all the primes below two million.</p>
</blockquote>
<p>Any suggestions on the style and usage (best practice) of Go?</p>
<... | [] | [
{
"body": "<p><strong>Algorithm : really important</strong></p>\n\n<p>What you want to to is to use the <a href=\"http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes\" rel=\"nofollow\">Sieve of Eratosthenes</a> because you already know an upper bound for the biggest number you're going to consider. Getting all t... | {
"AcceptedAnswerId": "24613",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-01T21:23:46.527",
"Id": "24612",
"Score": "3",
"Tags": [
"programming-challenge",
"primes",
"go"
],
"Title": "Project Euler 10: Summation of primes in Go"
} | 24612 |
<p>I'm working on a simple superfeedr powered rails app here.</p>
<p>Based on the <a href="https://github.com/superfeedr/rack-superfeedr" rel="nofollow">superfeedr-rack gem documentation</a>, I'm doing this to initialize the middleware (snippet from application.rb config block):</p>
<pre><code>Configuration = YAML.lo... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T20:00:44.993",
"Id": "38227",
"Score": "0",
"body": "Try Rails.configuration.middleware"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-05T11:34:50.717",
"Id": "38228",
"Score": "0",
"body": ... | [
{
"body": "<p>Based on tapajos comment, I was able to to refactor the code using initializers. First I created one to load the configuration file:</p>\n\n<pre><code>APP_CONFIG = YAML.load_file(File.expand_path('../../config.yml', __FILE__))\n</code></pre>\n\n<p>Then I created another initializer, like this:</p>... | {
"AcceptedAnswerId": "24935",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T01:10:03.693",
"Id": "24618",
"Score": "2",
"Tags": [
"ruby",
"ruby-on-rails"
],
"Title": "Rails middleware initialization block"
} | 24618 |
<p>I have the below program </p>
<pre><code>class Program
{
static void Main(string[] args)
{
List<Assembly> failedAssemblies = LoadAssembly();
string batchFile = "D:\\1" + ".bat";
FileStream fs = File.Create(batchFile);
fs.Close();
... | [] | [
{
"body": "<p>First off, you are right, this is much more complicated than it needs to be. I do like most of the naming conventions, casing and white space in your code.</p>\n\n<p>I would start off by making <code>maxLimit</code>, <code>command</code>, and <code>testContainer</code> class level constants.</p>\... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T01:59:39.590",
"Id": "24619",
"Score": "2",
"Tags": [
"c#"
],
"Title": "A complex string splitter- How do I make this working code better?"
} | 24619 |
<p>I get great FPS with this as is, but I'm a performance freak and I don't know <code>AffineTransform</code> or the Java graphics libraries very well. I'm sure there is something I could be doing differently to make this faster, but I can't think of anything, other than it looks like the scale transformation gives me ... | [] | [
{
"body": "<p>Sometimes, the correct answer really is \"Nope; it's all good.\" It makes for a boring answer, but there it is. It doesn't look like you can make this much faster, based on what you've provided.</p>\n\n<p>One thing you might consider is checking the variables to see if the image manipulations ar... | {
"AcceptedAnswerId": "42460",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T03:59:05.720",
"Id": "24621",
"Score": "6",
"Tags": [
"java",
"algorithm",
"performance"
],
"Title": "Can I make my render method more efficient?"
} | 24621 |
<p><a href="http://www.wikiupload.com/SJ1VLZZZBZU6TVR" rel="nofollow">Link to a full functional solution.</a></p>
<p>I'm working on a physics based algorithm, and I find myself working a lot with functions of style </p>
<pre><code>double GetSpeed(double acceleration, double angle, double resistance)
</code></pre>
<p... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T16:09:22.730",
"Id": "38050",
"Score": "3",
"body": "Have you looked at F#? It deals with things like units as well as values, and is [ideal for physics type applications](http://stackoverflow.com/questions/167909/is-f-suitable-for... | [
{
"body": "<p>The main issue in your code is that you don't control proper combination of units in operations, e.g. you allow summing up speeds with kilograms. </p>\n\n<p>I would rather create a class (or maybe better a struct?) that is aware of unit types as well, and create a number of static/extension method... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T08:27:35.390",
"Id": "24623",
"Score": "4",
"Tags": [
"c#",
".net",
"design-patterns"
],
"Title": "Encapsulated double for type safety"
} | 24623 |
<p>It doesn't happen often, but I sometimes come across cases where I miss Java's checked exceptions, especially in medium sized methods of about 30 odd lines that call outward. Often, the following pattern emerges:</p>
<pre><code>bool Foo(){
LibType l1 = new LibType("bla");
LibType2 smt = l1.bar("context");
...... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T18:01:44.690",
"Id": "38057",
"Score": "3",
"body": "“this doesn't even compile, as not all code paths return a value” That's not true, the code you posted should compile just fine."
}
] | [
{
"body": "<p>I really don't see any advantage in your approach.</p>\n\n<p>If you want to catch specific exception from a specific part of the code, do that:</p>\n\n<pre><code>bool Foo(){\n try {\n LibType l1 = new LibType(\"bla\");\n LibType2 smt = l1.bar(\"context\");\n } catch (LibException le){\n ... | {
"AcceptedAnswerId": "24647",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T08:48:30.970",
"Id": "24624",
"Score": "4",
"Tags": [
"c#",
"exception-handling"
],
"Title": "Wrapping Exceptions"
} | 24624 |
<p>The code below is for testing Primes.</p>
<pre><code>testPrime(100000);
testPrime(1000000);
testPrime(10000000);
testPrime(100000000);
</code></pre>
<p>Now my goal is to make the code super fast at finding prime number, min-max, close prime.
Are there any improvements that can be done to the code below to improve ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T13:16:16.163",
"Id": "38039",
"Score": "0",
"body": "Profile it and look where you spent most of the time. I would expect the modpow function., which is not that easy to further optimize."
},
{
"ContentLicense": "CC BY-SA 3.... | [
{
"body": "<p>You could use the <a href=\"http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes\" rel=\"nofollow\">Sieve of Eratosthenes</a> for numbers less than 10 million (you are only going up to 100 million).</p>\n\n<p>I think that checking the low bit <code>(num & 1) == 0</code> is quicker than checking... | {
"AcceptedAnswerId": null,
"CommentCount": "6",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T09:15:43.543",
"Id": "24625",
"Score": "3",
"Tags": [
"java",
"performance",
"primes"
],
"Title": "Miller-Rabin Prime test (Speed is the main goal)"
} | 24625 |
<p>I want to create a file consisting <code>take</code> rows of distinct numbers in ascending order. They are randomly taken from the first <code>total</code> integer. The file will be used to make an excerpt as discussed <a href="https://tex.stackexchange.com/q/106456/19356">here</a>.</p>
<p>I realize that my naming ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T11:18:53.153",
"Id": "38030",
"Score": "0",
"body": "In other words: How to make an array (of type `int` with length `m`) containing distinct, ascending ordered numbers in a set of the first `n` **positive** integers (`n>m`)? The di... | [
{
"body": "<p>With out commenting on the business logic (the class assignment) I would say this about the coding standard as a whole.</p>\n\n<p>My opionion is:\nYou are writing very c style. \nInstead think of C# as oo</p>\n\n<p>So\nSmaller functions, this make it very obvious to anyone reading it whats going o... | {
"AcceptedAnswerId": "24698",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T10:00:33.353",
"Id": "24627",
"Score": "0",
"Tags": [
"c#",
"optimization",
"algorithm",
"random"
],
"Title": "How to take the some elements of a list of random numbers and... | 24627 |
<p>I have to write a program that changes a string's vowels, consonants and other symbols into C, V respectively 0. I've done this but I wonder if there is a more efficient and elegant way to do it. Would appreciate input.</p>
<pre><code>(defun string-to-list (string)
(loop for char across string collect char))
(defu... | [] | [
{
"body": "<p>First, <a href=\"http://dept-info.labri.u-bordeaux.fr/~idurand/enseignement/PFS/Common/Strandh-Tutorial/indentation.html\" rel=\"nofollow\">proper indentation</a> helps a lot. I do appreciate you not putting the closing parens on separate lines though. Thank you.</p>\n\n<pre><code>(defun string-to... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T13:24:08.427",
"Id": "24634",
"Score": "2",
"Tags": [
"strings",
"lisp",
"common-lisp"
],
"Title": "LISP - Modify string"
} | 24634 |
<p>I have a dashboard like interface, that can have many tabs. There can be as many or as few tabs as the user likes, with the user being allowed to re order them as they wish. Exactly like browsers do.</p>
<p>My problem is moving the tabs position, for example, look at these tabs: </p>
<pre><code>[Sales] [Admin] [Fi... | [] | [
{
"body": "<p>Fundamentally, you should probably use the array's own ordering. It'd remove the need for a <code>sequence</code> number that you manually have to track and update. Instead, you can simply pluck index 3 out of the array, and splice it in at index 1, and not worry about sequence numbers at all.</p>... | {
"AcceptedAnswerId": "24649",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T16:35:36.093",
"Id": "24640",
"Score": "2",
"Tags": [
"javascript",
"array",
"json"
],
"Title": "Most efficient way to insert into ordered sequence"
} | 24640 |
<p>I wrote a simple program to remove duplicates from a String without using additional buffer.</p>
<p>The program should filter the duplicates and return just the unique string.</p>
<p>Example:</p>
<blockquote>
<p><strong>Input:</strong><br>
FOOOOOOOOLLLLLOWWWWWWWWWW UUUUP</p>
<p><strong>Output:</strong><b... | [] | [
{
"body": "<p>Thats not working. Try the input: <code>abaa</code> (result: <code>aa</code>)</p>\n\n<p>I do not know what you mean with \"without using additional buffer\", because finalValue is at least an additional buffer (which is kind of necessary. You could create a view, but this will be a bit complex).</... | {
"AcceptedAnswerId": "24644",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T17:05:03.213",
"Id": "24641",
"Score": "4",
"Tags": [
"java",
"strings"
],
"Title": "Remove duplicates from string without using additional buffer"
} | 24641 |
<p>I've constructed a simple class whose entire purpose is to hash passwords securely and simply.</p>
<p>The catch is the PHP version is probably going to be 5.2.x. This means:</p>
<ul>
<li>No <code>CRYPT_BLOWFISH</code></li>
<li>Obviously no <code>password_hash()</code>.</li>
</ul>
<p>The question is, is the follow... | [] | [
{
"body": "<p>Just use PHPass when your PHP version is too low for the native password API instead of trying to roll your own implementation.</p>\n\n<p><a href=\"http://www.openwall.com/phpass/\" rel=\"nofollow\">http://www.openwall.com/phpass/</a></p>\n\n<p><sub><sub>Although you should <strong>really</strong>... | {
"AcceptedAnswerId": "24646",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T17:34:17.270",
"Id": "24643",
"Score": "3",
"Tags": [
"php",
"security"
],
"Title": "Hasher class for PHP<5.3"
} | 24643 |
<p>I have made a class to import into my projects to more easily handle the state of the keyboard so I can use stuff like:</p>
<pre><code>if(Keys.returnKey(37)){
</code></pre>
<p>As opposed to setting up key listeners.</p>
<p>Please give this code a general review:</p>
<pre><code>/*
ALWAYS DO THIS FIRST:
Ke... | [] | [
{
"body": "<p>Looking at it from a general perspective...</p>\n\n<p>Put your comments that describe functions in javadoc-style outside your functions. Comment blocks before functions are a common format when it comes to describing a function. Because it's at the same indentation level as the function, it can't ... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T22:35:17.403",
"Id": "24655",
"Score": "10",
"Tags": [
"performance",
"classes",
"api",
"actionscript-3"
],
"Title": "Key Press Handler"
} | 24655 |
<p>I'm implementing a login system on app engine (I have to, so please don't tell me to use the User service, or an other way to delegate authentication), and I'm wondering whether this setup is secure.</p>
<pre><code>from pbkdf2 import PBKDF2
import os
salt = os.urandom(8)
password = PBKDF2(passphrase, salt).rea... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T09:17:18.293",
"Id": "38163",
"Score": "2",
"body": "If you're building something to last, it's also worth asking: what do I do if this ceases to be secure? PBKDF2, like bcrypt etc., has a difficulty parameter; it would be advisable... | [
{
"body": "<p>Pretty much, yes. PBKDF2 is a well-established algorithm, and <code>os.urandom</code> is a suitable <a href=\"http://en.wikipedia.org/wiki/CSPRNG\" rel=\"nofollow\"><code>CSPRNG</code></a> that can be used in salt generation on all major platforms (patched, of course).</p>\n\n<p>Your implementatio... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-03T07:57:31.510",
"Id": "24657",
"Score": "4",
"Tags": [
"python",
"security",
"cryptography"
],
"Title": "Is this a secure way to hash a password?"
} | 24657 |
<p>I've never done Java GUI's before. After much trouble I've gotten my GUI to look how I want, however it feels as if my code is very inefficient, as if I'm going about this the wrong way?</p>
<pre><code> //main panels
private JPanel westPanel = new JPanel(new BorderLayout());
private JPanel eastPanel = new JPane... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T21:34:29.320",
"Id": "38088",
"Score": "4",
"body": "What's wrong with your code? It seems OK to me."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T21:39:47.930",
"Id": "38089",
"Score": "2",... | [
{
"body": "<p>Your code seems perfectly fine!</p>\n\n<p>However...</p>\n\n<p>If all of this code is in the same block (e.g., a constructor or a layout builder method), I would recommend that you break up the layout code into smaller functions. That will improve readability and code structure a bit.</p>\n\n<p>Al... | {
"AcceptedAnswerId": null,
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-02T21:30:00.607",
"Id": "24660",
"Score": "4",
"Tags": [
"java",
"swing",
"user-interface"
],
"Title": "Java/Swing GUI code/layout, am I doing this wrong?"
} | 24660 |
<p>I am new to C++ and am looking to expand my knowledge. Below is a simple program that I've made. I would like to know how it could be improved, in any way. The introduction of new ways to do things is what I am looking for. These could be anything from improving efficiency to validating input - just whatever you thi... | [] | [
{
"body": "<ul>\n<li><p><strong>Headers</strong>. Did you leave out the includes and function prototype at the top for simplicity or something? If not, you need to put them here otherwise your program will not compile.</p></li>\n<li><p><strong>Function-returning and displaying</strong>. The last statement in... | {
"AcceptedAnswerId": "24668",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-03T13:47:29.833",
"Id": "24663",
"Score": "6",
"Tags": [
"c++",
"beginner",
"calculator"
],
"Title": "Four-function calculator design"
} | 24663 |
<p>I am bashing my head against how to solve this puzzle. Is there any super SQL expert out there who can lend some help</p>
<p>I have a database with the following structure.</p>
<pre><code>adjlevel | scheme | holder | client | companyID | Rate
</code></pre>
<p>A rate can be held in this table based on either of th... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-03T14:51:46.193",
"Id": "38099",
"Score": "0",
"body": "Define: \"super slow\"."
}
] | [
{
"body": "<p>I would pull all the possible rates and just sort by the most specific.</p>\n\n<pre><code>SELECT @retval = bhrlyrate FROM [MYDATABASE].dbo.inv_hrrate WHERE \nadjlevel = @nadjlevel \nand cmode = @cmode\nand companyid = @ncompanyid \nand (client = @client or client = '')\nand (holder = @holder OR (h... | {
"AcceptedAnswerId": "24674",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-03T14:26:46.077",
"Id": "24667",
"Score": "2",
"Tags": [
"sql"
],
"Title": "SQL Query based on results of preceding query"
} | 24667 |
<p>Which is best practice for creating an enum-like lookup for templates? I enjoy being able to bring up my templates with VS IntelliSense so either works for me.</p>
<p>Option 1 (<code>enum</code>/<code>Dictionary</code> combo):</p>
<pre><code>public enum Template
{
AccountInformation = 1,
Registration = 2,
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-03T16:29:15.733",
"Id": "38107",
"Score": "2",
"body": "Option #1, but make the declaration `static readonly IDictionary<>`"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-03T16:46:01.053",
"Id": "38110... | [
{
"body": "<p>I think this depends on the logical meaning of the string.</p>\n\n<p>If there is a fixed number of valid values, then you should use your first option, use the enum almost everywhere, and convert to <code>string</code> using the dictionaly at the last possible place. (By “fixed” I mean that the st... | {
"AcceptedAnswerId": "25784",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-03T16:00:44.173",
"Id": "24669",
"Score": "3",
"Tags": [
"c#",
"comparative-review"
],
"Title": "Email template referencer"
} | 24669 |
<p>I have written a program that, given an amount, returns the interest you would receive on that figure. There are 3 interest rate bands as follows:</p>
<ul>
<li>1% - £0 to £1000</li>
<li>2% - £1000 to £5000</li>
<li>3% - £5000+</li>
</ul>
<p>Can anyone tell me if the following is a good solution or are the <code>if... | [] | [
{
"body": "<blockquote>\n <p>are the 'if' statements too restrictive?</p>\n</blockquote>\n\n<p>I am not sure if the statements correspond the the given specification.\nYou have to clarify the corner cases. I would say, 1000.01 belongs to 2%.</p>\n\n<p>Rest is probably mostly fine, with some small problems:</p>... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-03T16:05:59.623",
"Id": "24670",
"Score": "1",
"Tags": [
"java",
"finance"
],
"Title": "Interest calculator"
} | 24670 |
<p>I'm working on a project where I need to solve for one of the roots of a quartic polymonial many, many times. Is there a better, i.e., faster way to do this? Should I write my own C-library? The example code is below.</p>
<pre><code># this code calculates the pH of a solution as it is
# titrated with base and th... | [] | [
{
"body": "<p>NumPy computes the roots of a polynomial by first constructing the companion matrix in Python and then solving the eigenvalues with LAPACK. The companion matrix case looks like this using your variables (as <code>a</code>==1):</p>\n\n<pre><code>[0 0 0 -e\n 1 0 0 -d\n 0 1 0 -c\n 0 0 1 -b]\n</code><... | {
"AcceptedAnswerId": "24676",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-03T16:26:46.280",
"Id": "24671",
"Score": "2",
"Tags": [
"python",
"numpy"
],
"Title": "Using numpy polynomial module - is there a better way?"
} | 24671 |
<p>I have created the following stored procedure which duplicates a record in a table and also all its related records in other tables however since I am a newbie to SQL I would appreciate it if someone could review my code and show me how it could be improved and whether there are any hazards.</p>
<pre><code> USE [... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-03T20:58:30.167",
"Id": "38147",
"Score": "0",
"body": "Your comment for updating the `numInBatch` doesn't match (probably most) people's expectation for 'proceeding' (that is, that lower numbers come 'first'). Aside from that, you ma... | [
{
"body": "<p>Here are some comments in no particular order:</p>\n\n<ul>\n<li>Your <code>CATCH</code> block - including comments - is a copy and paste from the <a href=\"http://msdn.microsoft.com/en-us/library/ms189797.aspx\" rel=\"nofollow noreferrer\">documentation</a>. While it's always good to read the docu... | {
"AcceptedAnswerId": "24720",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-03T18:34:29.977",
"Id": "24677",
"Score": "2",
"Tags": [
"sql",
"sql-server",
"t-sql"
],
"Title": "How can I improve the following stored procedure?"
} | 24677 |
<p>This code seems to work, but I'd like someone with shell-fu to review it.</p>
<ul>
<li>Is the temp file a good idea? How would I do without?</li>
<li>Is sed the right tool?</li>
<li>Any other general advice for improving my shell script</li>
</ul>
<h1>Script/Code to Review:</h1>
<pre><code># Grab max field lengt... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-06T16:12:33.723",
"Id": "38318",
"Score": "2",
"body": "I'd suggest using something which actually understood XML, as opposed to a sed script which will be fooled by totally valid XML. You might want to look at xslt?"
}
] | [
{
"body": "<h3>1. Comments on your code</h3>\n\n<ol>\n<li><p>Your example input file is not a <a href=\"http://en.wikipedia.org/wiki/Well-formed_document\" rel=\"nofollow noreferrer\">well-formed XML document</a>. An XML document must have a <em>single</em> root element, but your example has two. What tool did ... | {
"AcceptedAnswerId": "24851",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-03T20:58:27.293",
"Id": "24684",
"Score": "4",
"Tags": [
"bash",
"shell",
"sed"
],
"Title": "Bash Shell Script uses Sed to create and insert multiple lines after a particular line... | 24684 |
<p>Please check if my tables specs agree with business rules. Also, please explain the differences between <code>CONSTRAINT</code>, <code>FOREIGN KEY</code> and <code>CONSTRAINT FOREIGN KEY</code>.</p>
<p>I have four MySQL tables (<code>USER</code>, <code>NAME</code>, <code>PATIENT</code> and <code>DOCTOR</code>) with... | [] | [
{
"body": "<p>The <code>CONSTRAINT</code> word used within a <code>CREATE TABLE</code> or <code>ALTER TABLE</code> tells MySQL engine that this value is not a separate column, but rather it is applied on a column within the table. </p>\n\n<p>The <code>FOREIGN KEY</code> word tells MySQL that whatever is in that... | {
"AcceptedAnswerId": "51343",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-03T21:16:15.423",
"Id": "24685",
"Score": "3",
"Tags": [
"sql",
"mysql"
],
"Title": "Tables for doctor and patients"
} | 24685 |
<p>I'm self-studying Java and have a question about one of the end-of-chapter exercises. The chapter focus is on inheritance and the book hasn't officially introduced polymorphism so I'm trying to stay within those bounds. </p>
<p>The exercise is:</p>
<blockquote>
<p>Write an inheritance hierarchy for classes Qua... | [] | [
{
"body": "<p>While the problem description isn't very clear, I expect the intention was for you to use <code>Point</code> to encapsulate a single (x, y) pair and use it in <code>Quadrilateral</code> et al. However, your version contains <em>two</em> points and thus should be called <code>Line</code> or <code>S... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-03T22:42:37.643",
"Id": "24687",
"Score": "3",
"Tags": [
"java",
"inheritance"
],
"Title": "My self-study inheritance and sub-class exercise"
} | 24687 |
<p>I have completed my homework with directions as follows:</p>
<blockquote>
<p>Declare and implement a class named Binary. This class will have a
method named printB(int n) that prints all binary strings of length n.
For n = 3, it will print</p>
<pre><code>000
001
010
011
100
101
110
111
</code></pre>
<p>... | [] | [
{
"body": "<blockquote>\n <p>Is there any way to make this shorter or more efficient?</p>\n</blockquote>\n\n<p>Yes, we can do some things.</p>\n\n<pre><code>for(int i = 0; i < Math.pow(2,n); i++)\n</code></pre>\n\n<p>You calculate the pow for every iteration. The pow call takes some time (compared to basic ... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2013-04-04T01:35:33.927",
"Id": "24690",
"Score": "9",
"Tags": [
"java"
],
"Title": "Print all binary strings of length n"
} | 24690 |
<p>The code below implements an intrusive lock-free queue that supports multiple concurrent producers and consumers.</p>
<p>Some features:</p>
<ul>
<li>Producers and consumers work on separate ends of the queue most of the time.</li>
<li>The fast path for producers and consumers has 4 atomic ops.</li>
<li>Version num... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-06-27T15:06:45.073",
"Id": "43419",
"Score": "0",
"body": "Any Benchmarks on this?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-06-30T21:58:21.103",
"Id": "43619",
"Score": "1",
"body": "Filipe, no... | [
{
"body": "<p>I just have some stylistic things for a start:</p>\n\n<ul>\n<li><p>Since your custom types are in PascalCase, your function names should instead be in camelCase or in snake_case. This isn't a requirement, but it makes it easier to distinguish between them.</p></li>\n<li><p>The naming convention u... | {
"AcceptedAnswerId": null,
"CommentCount": "9",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T07:09:45.097",
"Id": "24696",
"Score": "16",
"Tags": [
"c++",
"thread-safety",
"lock-free",
"atomic",
"producer-consumer"
],
"Title": "Lock-free multiple-consumer multiple... | 24696 |
<p>I've created the following string manipulation function for randomizing my passed string in Lua:</p>
<pre><code>require "string"
require "math"
math.randomseed( os.time() )
function string.random( self )
local tTemporary, tNew = {}, {}
if not self or self:len() < 5 then
return nil
end
s... | [] | [
{
"body": "<p>A couple of points to consider:</p>\n\n<ul>\n<li>Calling your function <code>shuffle</code> instead of <code>random</code> would be a better name since that's really what it's doing.</li>\n<li>The extra intermediate tables aren't really necessary to perform the shuffle. A single table is enough an... | {
"AcceptedAnswerId": "25794",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T10:10:15.953",
"Id": "24700",
"Score": "4",
"Tags": [
"strings",
"random",
"lua"
],
"Title": "Generating random strings"
} | 24700 |
<p>I've implemented a simple program that finds all prime numbers between two integer inputs using Sieve of Eratosthenes, but the online judge is still complaining that the time limit is exceeding. Maybe I'm doing something wrong, but how can I optimize (or debug if necessary) my code?</p>
<pre><code>#include <iost... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T12:02:12.497",
"Id": "38173",
"Score": "0",
"body": "I don't know enough about C++ to offer any feedback on your code, but take a look through the primes tag -- there are several questions that discuss how to implement the Sieve of ... | [
{
"body": "<h3>General Comments:</h3>\n<p>In C++ prefer not to use <code>using namespace std;</code><br />\nSee: <a href=\"https://stackoverflow.com/q/1452721/14065\">https://stackoverflow.com/q/1452721/14065</a> and nearly every other C++ question on code review.</p>\n<p>Prefer one line per variable.</p>\n<pre... | {
"AcceptedAnswerId": "24721",
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T10:37:00.110",
"Id": "24701",
"Score": "1",
"Tags": [
"c++",
"optimization",
"algorithm",
"primes"
],
"Title": "Prime Number Generator algorithm optimization"
} | 24701 |
<p>Think of the numpad number layout.</p>
<p><code>hvd = horizontal, vertical or diagonal</code></p>
<p><code>coord = 1,2 or 3</code> if horizontal or vertical and 1 or 2 if diagonal (1 is up, 2 is down)...</p>
<p><code>hello('v', 2)</code> - Asking for the second vertical column etc</p>
<pre><code>def hello(hvd, c... | [] | [
{
"body": "<p>A nicer way to do the same would be to use a dictionary:</p>\n\n<pre><code>def hello(hvd, coord): \n d = {'h': [\"789\", \"456\", \"123\"],\n 'v': [\"741\", \"852\", \"963\"], \n 'd': [\"159\", \"753\"]}\n try:\n print d[hvd][coord-1]\n except IndexError:\n... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T11:51:31.947",
"Id": "24703",
"Score": "3",
"Tags": [
"python"
],
"Title": "Numpad number layout"
} | 24703 |
<p>Is this the most efficient way to find if a number is prime? I can't think of a better way and it seems to run pretty quickly.</p>
<pre><code> public boolean primes(int num){
for(int i = 2; i < num; i++){
if(num % i == 0){
System.out.println(num + " is not prime");
return f... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T13:16:08.667",
"Id": "38181",
"Score": "1",
"body": "Probably even the deterministic variant of Miller-Rabin primality testing is much faster."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-05T12:18:45.... | [
{
"body": "<p>A faster method would be to skip all even numbers and only try up to the square root of the number.</p>\n\n<pre><code>public static boolean isPrime(int num){\n if ( num > 2 && num%2 == 0 ) {\n System.out.println(num + \" is not prime\");\n return false;\n }\n int ... | {
"AcceptedAnswerId": "24708",
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T12:07:46.340",
"Id": "24704",
"Score": "7",
"Tags": [
"java",
"primes"
],
"Title": "Efficiently determining if a number is prime"
} | 24704 |
<p><a href="http://angularjs.org/" rel="nofollow">AngularJS</a> is an open-source JavaScript framework for building <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete" rel="nofollow">CRUD</a> centric <a href="http://en.wikipedia.org/wiki/Ajax_%28programming%29" rel="nofollow">AJAX</a> style web appli... | [] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T12:14:43.797",
"Id": "24705",
"Score": "0",
"Tags": null,
"Title": null
} | 24705 |
Code written using version 1 of the AngularJS open-source JavaScript framework. Use 'angular-2+' for code using later versions. | [] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2013-04-04T12:14:43.797",
"Id": "24706",
"Score": "0",
"Tags": null,
"Title": null
} | 24706 |
<p>Is there anything wrong with doing this:</p>
<pre><code>Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For value As Integer = 0 To 1000000
Dim p As New Person
p.ID = value
p.DoSomething()
p = ... | [] | [
{
"body": "<p>In sample1, as you call it, you have an instance field which you change. Therefore, the <code>Person</code> class you are using is <strong>mutable</strong>. Mutable classes are prominent to concurrency issues if a single instance is manipulated by multiple threads, <strong>unless explicit synchron... | {
"AcceptedAnswerId": "24714",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T14:37:11.067",
"Id": "24713",
"Score": "2",
"Tags": [
"design-patterns",
"vb.net"
],
"Title": "Objects and instance variables in loops"
} | 24713 |
<p>I've leaped into SASS and am loving it. I'm asking for comments here on whether my SASS is well-formatted. </p>
<p>Here's the original CSS based on Zurb's Foundation Pagination styles, but adapted for use with Laravel's built-in pagination: </p>
<pre><code>div.pagination ul { display: block; height: 24px; margin-l... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-09-05T20:33:46.930",
"Id": "49069",
"Score": "0",
"body": "Doesn't Foundation already use Sass?"
}
] | [
{
"body": "<p>Some comments on your style of selecting:</p>\n\n<ul>\n<li><code>div.pagination</code> is overqualified. A container is almost always going to be a div, so you just can write <code>.pagination</code></li>\n<li>In many cases, there is no use for an extra container. You might as well apply the <code... | {
"AcceptedAnswerId": "40036",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T16:44:45.660",
"Id": "24716",
"Score": "12",
"Tags": [
"beginner",
"css",
"sass"
],
"Title": "Comments on SASS from CSS for a SASS beginner"
} | 24716 |
<p>I have written a small snippet to validate a substring in a string in O(n). I have tested the code using some combinations. I require your help to let me know if there are any bugs in my code with respect to any other cases that I might have missed out:-</p>
<pre><code>class StringClass( object ):
def __init__(... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T18:20:19.363",
"Id": "38191",
"Score": "0",
"body": "this is O(n), but incorrect (as @Poik pointed out). There are no O(n) algorithms for string matching, maybe O(n+m), using Rabin-Karp or Knuth-Morris-Pratt (n=length of string, m=l... | [
{
"body": "<p>Your implementation seems ok, although I would test some corner cases before starting the iteration, ask yourself:</p>\n\n<p>What should be done in the case <code>s is None</code>? or <code>s == ''</code>? (Both can be handled in python simply by <code>if s:</code>.</p>\n\n<p>Besides that, your im... | {
"AcceptedAnswerId": "24719",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T16:50:08.520",
"Id": "24717",
"Score": "0",
"Tags": [
"python"
],
"Title": "Possible Bugs in substring program"
} | 24717 |
<p>I'm working on Project Euler's <a href="http://projecteuler.net/problem=23" rel="nofollow">problem #23</a>, which is</p>
<blockquote>
<p>Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers</p>
</blockquote>
<p>I came up with this algorithm. Find all abundant numbe... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-05T11:39:56.470",
"Id": "38230",
"Score": "0",
"body": "Why to optimize the PE solution, which finishes in the slowest language in far less, than a minute?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-05... | [
{
"body": "<p>Your idea is perfectly fine (subtracting all sum of pairs from the candidates range), but I would write it differently:</p>\n\n<pre><code>xs = (1..28123)\nabundants = xs.select(&:abundant?)\nsolution = (xs.to_set - abundants.repeated_combination(2).to_set { |x, y| x + y }).sum\n</code></pre>\n... | {
"AcceptedAnswerId": "24752",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T18:23:58.067",
"Id": "24723",
"Score": "1",
"Tags": [
"ruby",
"programming-challenge",
"primes"
],
"Title": "Optimizing code for Project-Euler Problem #23"
} | 24723 |
<p>This plugin is meant to filter through a table based on column header text that matches options given to it.
check it out here: <a href="http://jsfiddle.net/CEZx7/1/" rel="nofollow" title="Filter Table Plugin">Filter Table Plugin</a></p>
<p>Plugin code below for reference:</p>
<pre><code>//framework from http://j... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T19:26:03.350",
"Id": "38195",
"Score": "0",
"body": "Forgot to mention one thing I might add is matching against whole words only - instead of 'ter' matching filter and terminal, it would only match terminal."
},
{
"Content... | [
{
"body": "<ol>\n<li><p>You have: </p>\n\n<pre><code>this._defaults = defaults;\n</code></pre>\n\n<p>This is redundant as the declaration :</p>\n\n<pre><code>var pluginName = \"filterTable\",\n defaults = {\n };\n</code></pre>\n\n<p>Can be accessed anywhere in your plugin.</p></li>\n<li><p>Declare all v... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T19:24:24.107",
"Id": "24728",
"Score": "4",
"Tags": [
"javascript",
"jquery",
"html"
],
"Title": "What improvements can I make to this table filtering jQuery Plugin?"
} | 24728 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.