body stringlengths 25 86.7k | comments list | answers list | meta_data dict | question_id stringlengths 1 6 |
|---|---|---|---|---|
<p>I often find myself writing code as follows...</p>
<pre><code>var startX, startY, dragging = false, element = document.getElementById("whatever");
var start = function(e){
dragging = true;
startX = e.pageX;
startY = e.pageY;
element.style.opacity = 0.5;
}
var move = function(e){
if (!dragging) return;
... | [] | [
{
"body": "<p>You could simply declare the functions inline, like:</p>\n\n<pre><code>element.addEventListener(\"mousedown\", function (e){\n dragging = true;\n startX = e.pageX;\n startY = e.pageY;\n element.style.opacity = 0.5;\n}, false);\n</code></pre>\n\n<p>And likewise for the other 2 functions.</p>\n\... | {
"AcceptedAnswerId": "17524",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-14T11:22:05.620",
"Id": "17521",
"Score": "1",
"Tags": [
"javascript"
],
"Title": "How to reorganize event listeners in javascript"
} | 17521 |
<p>Problem:</p>
<blockquote>
<p>Given a randomly ordered array of <em>n</em>-elements, partition the elements
into two subsets such that elements <= x are in one subset and elements</p>
<blockquote>
<p>x are in the other subset.</p>
</blockquote>
<p>One way to do this is to sort the array in asc... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-15T03:45:36.607",
"Id": "27907",
"Score": "0",
"body": "Technically it's still `O(n)`. A second pass is `O(2n) == O(n)`."
}
] | [
{
"body": "<p>Unless it is a homework for sort algorithm study :</p>\n\n<ul>\n<li>Manage <code>ArrayList</code> and load your list of number,</li>\n<li>put all <code>number<=x</code> in a new ArrayList,</li>\n<li>delete them from the original ArrayList,</li>\n<li>finish with <code>.addAll(..),</code> to conc... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-14T13:51:24.730",
"Id": "17523",
"Score": "4",
"Tags": [
"java",
"algorithm",
"performance",
"array"
],
"Title": "Partitioning array elements into two subsets"
} | 17523 |
<p>This function checks if an attribute has been passed and if not, asks user for input.</p>
<p>I'm new in bash scripting and would like to get some feedback. Is it okay or should I refactor this code somehow to make it more concise?</p>
<pre><code>#!/usr/bin/env bash
fn() {
if [ -z "$1" ]; then
read -p ... | [] | [
{
"body": "<p>What you're doing is perfectly fine.</p>\n\n<p>If you absolutely want to shorten it (at a slight loss of readability) for whatever reason you could do it this way (at least in bash):</p>\n\n<pre><code>fn() {\n [ -z \"$1\" ] && read -p \"username:\" username || username=$1\n echo \"us... | {
"AcceptedAnswerId": "17552",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-14T16:53:00.803",
"Id": "17526",
"Score": "5",
"Tags": [
"bash"
],
"Title": "A little bash function, assigning attributes"
} | 17526 |
<p>I've created <a href="http://jsfiddle.net/9SXLJ/" rel="nofollow noreferrer">this fiddle</a> to demonstrate something I've been working on. I've been trying to mimic the behaviour of Twitter where if a tweet is expanded, the border-radius and margins of the previous and next tweets are altered.</p>
<p>I've used togg... | [] | [
{
"body": "<p>at first glance here's a few things that immediately come to mind (not a criticism, just an observation):</p>\n\n<ol>\n<li>You're right--there's a lot of extra markup to accomplish what you're trying to do, but that's something you can work on easily</li>\n<li>Your writing a more jQuery-handlers t... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-14T17:21:32.457",
"Id": "17527",
"Score": "2",
"Tags": [
"javascript",
"jquery",
"html",
"css"
],
"Title": "Toggleclass for mimicking certain Twitter tweet behavior"
} | 17527 |
<p>This is what the code should do:</p>
<blockquote>
<p>Check if the first dimensions and the second dimensions of each 2-dimensionalarray are the same. If they are not the same, then return a 0x0 2-dimensional array. array, otherwise do the following;</p>
<ul>
<li><p>Allocate memory for a local 2-dim. array ... | [] | [
{
"body": "<p>You probably should reread the specifications. “Check if the first dimensions and the second dimensions of each 2-dim. array array are the same” means that you should check if first and second matrix lengths match; it does <em>not</em> mean to test <code>doubMatrix1[i][j] == doubMatrix2[i][j]</c... | {
"AcceptedAnswerId": "17532",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-14T19:47:19.827",
"Id": "17531",
"Score": "2",
"Tags": [
"java",
"matrix",
"mathematics"
],
"Title": "Adding matrices"
} | 17531 |
<p>Below is my attempt at the beginnings of a reusable ado.net data access class. Please let me know of any improvements that I could make.</p>
<p>Thanks!</p>
<pre><code>public class DataAccess : IDisposable
{
public DbProviderFactory Factory { get; set; }
public string Connection { get; set; }
public... | [] | [
{
"body": "<p>You do not need IDisposable in your current implementation as you do not use any unmanaged resources or have any fields or properties which implement IDisposable which your class owns.</p>\n\n<p>Setting the properties to null in the dispose method is unnecessary.</p>\n\n<p>Method names should be P... | {
"AcceptedAnswerId": "17535",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-14T21:09:58.213",
"Id": "17533",
"Score": "2",
"Tags": [
"c#",
".net",
"database"
],
"Title": "reusable data access class in C#"
} | 17533 |
<p>I have been programming in C++ for around a month now, just long enough, I figure, to develop bad habits. Can anyone point out where I'm making mistakes and offer topics I should investigate to understand why?</p>
<pre><code>#include <iostream>
#include <cassert>
#include <sstream>
#include <st... | [] | [
{
"body": "<p>I presume your code's brace format and function heading format is dictated by instructor and I won't comment on it. The code is presented clearly enough. One comment says problem 8, a couple of others say 10.</p>\n\n<p>I deem it inappropriate to have a <code>get_line</code> routine; for code tha... | {
"AcceptedAnswerId": "17559",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-14T22:34:46.767",
"Id": "17536",
"Score": "4",
"Tags": [
"c++",
"beginner"
],
"Title": "Determine number of even, odd, and zero digits of a given integer"
} | 17536 |
<p>I would really like to hear how I can improve my form validator. Next, I want to refactor it into OO and learn a testing framework like Jasmine.</p>
<p>Some things I know I could improve:</p>
<ul>
<li>Don't make it tied to the DOM as much (using <code>parent().parent()</code>, for ex.).</li>
<li>Combined validatio... | [] | [
{
"body": "<p>You could break out your validation functions into a separate object, like so:</p>\n\n<pre><code>validators =\n isinteger: (input) ->\n # check that value is an int\n\n notblank: (input) ->\n # check that value is present\n</code></pre>\n\n<p>These functions could return a simple boo... | {
"AcceptedAnswerId": "17541",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-15T00:03:30.807",
"Id": "17538",
"Score": "2",
"Tags": [
"validation",
"coffeescript"
],
"Title": "Simple form validation"
} | 17538 |
<p>I updated my code according to a few suggestions from <a href="https://codereview.stackexchange.com/questions/16386/howd-i-do-on-this-card-matching-game-link-and-code-inside">my last post</a>, so I thought I'd repost it to see what other feedback I could get. I also commented it heavily and fixed some of the functio... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-15T01:10:32.283",
"Id": "27896",
"Score": "5",
"body": "You can look at this post to see the [difference between a function expression vs declaration in Javascript][1]\r\n\r\n\r\n [1]: http://stackoverflow.com/questions/1013385/what-i... | [
{
"body": "<p>I like your code</p>\n\n<ul>\n<li>\"Use Strict\" within a IFFE</li>\n<li>Good level of comments</li>\n<li>Correct casing and decent naming</li>\n<li>Nice size of methods</li>\n</ul>\n\n<p>Some very minor stuff</p>\n\n<ul>\n<li>JsHint says you never use <code>that</code> ( line 12 ) nor <code>i</co... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-15T00:57:52.540",
"Id": "17542",
"Score": "5",
"Tags": [
"javascript",
"html",
"css",
"game",
"playing-cards"
],
"Title": "Card-matching game"
} | 17542 |
<p>I have created iterator and generator versions of Python's <a href="http://docs.python.org/library/functions.html#range" rel="nofollow"><code>range()</code></a>:</p>
<h3>Generator version</h3>
<pre><code>def irange(*args):
if len(args) > 3:
raise TypeError('irange() expected at most 3 arguments, got %s' %... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-11-14T18:10:01.257",
"Id": "29647",
"Score": "1",
"body": "how is this different than `xrange`? Or are you just testing your ability to write generators/iterators?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012... | [
{
"body": "<p>First of all, to validate that the function is working,\nit's good to use <code>assert</code> statements:</p>\n\n<pre><code>assert [0, 1, 2, 3, 4] == [x for x in irange(5)]\nassert [2, 3, 4] == [x for x in irange(2, 5)]\nassert [2, 1, 0, -1, -2] == [x for x in irange(2, -3, -1)]\n</code></pre>\n\n... | {
"AcceptedAnswerId": null,
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-15T03:15:57.977",
"Id": "17543",
"Score": "4",
"Tags": [
"python",
"reinventing-the-wheel",
"iterator",
"generator"
],
"Title": "Iterator and Generator versions of Python's range... | 17543 |
<p>I have a use case where I want to use both <code>HttpRequestInterceptor</code> and <code>HttpResponseInterceptor</code> in one <code>class</code>.</p>
<p><br/>
I'm asking if the following would be a good solution and what are the potential problems?</p>
<pre><code>public interface HttpRequestResponseInterceptor ex... | [] | [
{
"body": "<p>You don't necessarily need to have another interface that extends the two you want to combine, you just have to say that he class implements both of them. One java class can implement as many interfaces as you desire. </p>\n\n<p>Problems may arise if you have any shared data problems, but by say... | {
"AcceptedAnswerId": "17573",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-15T11:33:24.670",
"Id": "17551",
"Score": "5",
"Tags": [
"java",
"android"
],
"Title": "Is this the best approch to combine 2 Interfaces?"
} | 17551 |
<p>This is a Java method that takes as input a 2 dimensional array called <code>grid</code>.</p>
<p>It uses the <code>i1</code> and <code>j1</code> values from when the corresponding <code>grid[i1][j1]</code> position is <code>true</code> for the first time, and then <code>i2</code> and <code>j2</code> from when the c... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-12T00:55:20.120",
"Id": "27921",
"Score": "0",
"body": "Watever language the above code is, Its a small piece of code so performance wont matter much unless you are providing really large data. However, declaring a integer inside two f... | [
{
"body": "<p>I would create a support method that knows to look for the next true value in the grid</p>\n\n<pre><code>void FindTrue(int &x, int &y, bool[][] grid)\n{\n for(; x < rows; x++, y = 0)\n for(; y < columns; y++)\n if (grid[x][y]) return;\n}\n</code></pre>\n\n<p>now ... | {
"AcceptedAnswerId": "17556",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-12T00:50:27.720",
"Id": "17553",
"Score": "10",
"Tags": [
"java"
],
"Title": "Better way to write nested loop, if condition and breaking it"
} | 17553 |
<p>I'm trying to make a Node.js server as minimal as possible to show a landing page, but i'm not sure this is the greatest and fastest way to do it.</p>
<p>I'm using dirty to store my html data.</p>
<p><strong>My app.js file</strong></p>
<pre><code>var http = require('http')
, db = require('dirty')('pages.db')
, ur... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-12T13:00:16.237",
"Id": "27932",
"Score": "0",
"body": "You question is a little vague. You code looks acceptable... you may want to add a response.end() after .pipe(response), I'm not sure if it's really necessary though. Also it seem... | [
{
"body": "<p>Good question,\nmade me learn some more about <code>dirty</code>.</p>\n\n<p>Some observations:</p>\n\n<ul>\n<li>1 <code>var</code> block is good, having the commas not at the end of each line looks odd</li>\n<li>I wonder if it is faster to get the extension of the requested resource and compare to... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-12T12:46:23.870",
"Id": "17560",
"Score": "2",
"Tags": [
"javascript",
"node.js"
],
"Title": "Node.js Is this a great way to show pages in a minimal way as possible?"
} | 17560 |
<p>I had an exercise as below for an interview. I used two design patterns: factory and decorator. I put my code at the end of it. </p>
<p>How could I improve the solution?</p>
<hr>
<h2>Basic OOD & Solution Design Requirements</h2>
<p>Develop a simple Visual Studio CONSOLE Application which simulates a vector-... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-04T14:57:24.007",
"Id": "27936",
"Score": "1",
"body": "This isn't really on topic for SO. Programmers...maybe. (If you re-work the actual question you're asking.)"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "... | [
{
"body": "<p>Off the bat I see a couple of things with regards to MS published coding standards, although I'm not sure what their standards are.</p>\n\n<p>I'm not sure why the X/Y locations are private. Seems to me that you'd want those properties exposed as public members with regular fields backing them and... | {
"AcceptedAnswerId": "17562",
"CommentCount": "7",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-04T14:54:52.327",
"Id": "17561",
"Score": "5",
"Tags": [
"c#",
"object-oriented",
"design-patterns"
],
"Title": "Basic OOP & Solution Design"
} | 17561 |
<pre><code>selected_hour=12
dates=[]
date1 = datetime.datetime(2012, 9, 10, 11, 46, 45)
date2 = datetime.datetime(2012, 9, 10, 12, 46, 45)
date3 = datetime.datetime(2012, 9, 10, 13, 47, 45)
date4 = datetime.datetime(2012, 9, 10, 13, 06, 45)
dates.append(date1)
dates.append(date2)
dates.append(date3)
dates.append(date4)... | [] | [
{
"body": "<p>Try something like this:</p>\n\n<pre><code>for i in dates:\n if i.hour == selected_hour:\n print i\n elif i.hour == (selected_hour - 1) % 24 and i.minute > 45:\n print i\n elif i.hour == (selected_hour + 1) % 24 and i.minute <= 15:\n print i\n</code></pre>\n\n<p... | {
"AcceptedAnswerId": "17565",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-15T01:37:45.080",
"Id": "17564",
"Score": "3",
"Tags": [
"python"
],
"Title": "filter date for a specific hour and minute"
} | 17564 |
<p>I'm making a converter app, and after realising I would have to type about 200 lines of code to get it working with more than 5 units converted, I should have a better conversion calculation.</p>
<p>What I have currently is a ifelse that find out what units i selected from the wheel, a sting to find out what it sho... | [] | [
{
"body": "<p><em><strong>Copied from SO with a few edits and additions.</em></strong></p>\n\n<hr>\n\n<p>Dude use C arrays to create an nxn matrix of conversions. Then just have one array with unit names in it. Done and done. If you don't know what a matrix is, then you have more theory to learn.</p>\n\n<p>It g... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-15T18:28:45.243",
"Id": "17575",
"Score": "3",
"Tags": [
"objective-c",
"ios"
],
"Title": "Improving calculation code"
} | 17575 |
<blockquote>
<p>Compute the number of connected component in a matrix, saying M. Given
two items with coordinates [x1, y1] and [x2, y2] in M, if</p>
<ul>
<li>M(x1, y1) == -1 <em>and</em></li>
<li>M(x2, y2) == -1 <em>and</em></li>
<li>|x1-x2| + |y1-y2| == 1</li>
</ul>
<p>then they are connected.</p... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T08:48:57.627",
"Id": "27991",
"Score": "0",
"body": "Why is the isolated -1 (item 2 in the output) a component? It isn't connected to anything, assuming that off-the-edge shouldn't count."
},
{
"ContentLicense": "CC BY-SA 3.... | [
{
"body": "<p><em>Style</em>: Depending on coding guidelines and preferences you may want to consider moving the declaration of <code>r</code> and <code>c</code> into the for-loops (<code>for (int r = 0...</code>), as these variables are not supposed to be used outside of the loop, and hence, an outside declara... | {
"AcceptedAnswerId": "17631",
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T01:52:06.600",
"Id": "17577",
"Score": "4",
"Tags": [
"c++",
"c",
"algorithm",
"interview-questions",
"matrix"
],
"Title": "The number of connected component"
} | 17577 |
<p>Which of the following way of checking whether a function returns <code>TRUE</code> or <code>FALSE</code> is best, in terms of efficiency and code readability?</p>
<p>I was told by a friend that Method B is a good practice, but I believe Method A is better since it checks whether function returns <code>TRUE</code> ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T06:34:34.920",
"Id": "27983",
"Score": "0",
"body": "I agree with your friend, method B"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T06:35:06.643",
"Id": "27984",
"Score": "0",
"body": ... | [
{
"body": "<p>The performance difference is irrelevant; it is really minor (if not zero) and if such minor differences mattered not using PHP at all would be the only proper solution.</p>\n\n<p>Since you only need the variable in the true-branch of the if statement and it's likely to be something other than jus... | {
"AcceptedAnswerId": "17581",
"CommentCount": "7",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T06:32:41.043",
"Id": "17578",
"Score": "8",
"Tags": [
"php",
"performance",
"optimization"
],
"Title": "PHP ideal way to check function returns true"
} | 17578 |
<p>Its working but I don't think its the best way of doing it. I also changed variable names just to let you know,</p>
<pre><code> string [] columnN = AnotherString.Split(','); //Another String is something like this = "HEhehehehehE heeh, aSKdjhkaaksjd, asldkhja slkdlk, asdajsdlka, asdljkasd, asdkasjdasd, asdas... | [] | [
{
"body": "<p>Ok a few things:</p>\n\n<ol>\n<li>Do this once and cache the result <code>Res.ResourceManager.GetString(cN.ToLower().Trim())</code>, the call <code>.ToLower()</code> creates a new string and then the <code>.Trim()</code> call creates yet another so you save the creation of 2 extra strings for each... | {
"AcceptedAnswerId": "17590",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T09:28:03.260",
"Id": "17586",
"Score": "7",
"Tags": [
"c#",
"optimization"
],
"Title": "Can someone improve this C# code"
} | 17586 |
<p>I'm writing an ORPG. The code below is client-side.</p>
<p>I have a main thread looping in <code>Game::Run</code>:</p>
<pre><code>Game::~Game()
{
PopAllStates();
}
void Game::Run()
{
sf::Event Event;
while(Window->isOpen())
{
if(LoadQueueMutex.try_lock())
{
while(!L... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-11-14T15:20:57.553",
"Id": "29627",
"Score": "0",
"body": "what's the point of the pushState/popState mechanism? Is is possible to temporarily load/start a game, play it, and close it to return to the previously opened one?"
}
] | [
{
"body": "<ul>\n<li><p>Your variables and functions should either be in camelCase or another naming convention that differentiates them from user-defined types (such as <code>Game</code> here), which are capitalized.</p></li>\n<li><p>If a parameter is not supposed to be modified, and it's not a primitive type ... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T09:48:25.127",
"Id": "17588",
"Score": "13",
"Tags": [
"c++",
"multithreading",
"game",
"boost",
"state"
],
"Title": "Handling game states for an online RPG game"
} | 17588 |
<p>I need to check if a map is subset of another map. For this, I have written the following code:</p>
<pre><code>#include <string>
#include <iostream>
#include <map>
using namespace std;
/**
* Checks if rhs is a subset of lhs
*/
template<class Map>
bool map_compare(Map &lhs, Map &r... | [] | [
{
"body": "<p>I believe you will find the standard library has the appropriate functions:</p>\n<p><a href=\"http://www.sgi.com/tech/stl/includes.html\" rel=\"noreferrer\">Includes</a></p>\n<blockquote>\n<p>Includes tests whether one sorted range includes another sorted range. That is, it returns true if and onl... | {
"AcceptedAnswerId": "17608",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T10:36:44.733",
"Id": "17591",
"Score": "1",
"Tags": [
"c++"
],
"Title": "Checking if the entries of a map exist in another map"
} | 17591 |
<p>I have a relatively simple project to parse some HTTP server logs using Python and SQLite. I wrote up the code but I'm always looking for tips on being a better Python scripter. Though this is a simple task and my code works as written, I was hoping for some pointers to improve my coding ability.</p>
<pre><code>imp... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2014-05-12T13:08:32.560",
"Id": "87102",
"Score": "0",
"body": "Read [http://www.dabeaz.com/generators/]. There's a wonderful amount of information in there about how to deal with logs in a scalable manner."
}
] | [
{
"body": "<p>It would probably be better if you didn't read the whole log file at once. You could try something like this instead</p>\n\n<pre><code>with open('logs','r') as f:\n for line in f:\n #print line\n # Parse data from each line\n date = re.findall(\"\\[.*?\\]\", line)\n ... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T11:35:13.870",
"Id": "17592",
"Score": "3",
"Tags": [
"python",
"parsing",
"regex"
],
"Title": "Parsing HTTP server logs"
} | 17592 |
<p>The following code is a simple random password generator that spits out a randomized password based on some input flags.</p>
<pre><code>import string
import random
import argparse
def gen_char(lower, upper, digit, special):
_lower_letters = string.ascii_lowercase
_upper_letters = string.ascii_uppercase
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T17:44:35.063",
"Id": "28035",
"Score": "0",
"body": "I don't think `_case` is a particularly good name here. Consider `alphabet` instead."
}
] | [
{
"body": "<p>Re-assembling the _case character list for each character is rather wasteful. I would get rid of the gen_char function.</p>\n\n<pre><code>import string\nimport random\nimport argparse\n\ndef main():\n\n \"\"\"\n\n The following lines of code setup the command line flags that the\n program... | {
"AcceptedAnswerId": "17607",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T12:43:36.570",
"Id": "17594",
"Score": "5",
"Tags": [
"python"
],
"Title": "Code review for python password generator"
} | 17594 |
<p>The Monty Hall Problem is a simple puzzle involving probability that even stumps professionals in careers dealing with some heavy-duty math. Here's the basic problem:</p>
<p><strong>Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. Y... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-15T21:53:01.020",
"Id": "28006",
"Score": "2",
"body": "parallel computing is a solution and it's been done in R with this very problem. See this post: http://librestats.com/2012/03/15/a-no-bs-guide-to-the-basics-of-parallelization-in... | [
{
"body": "<p>R has vectorized operators and using those is much more efficient in this case:</p>\n\n<pre><code>Monty_Hall2 <- function(repetitions){\n doors <- c('A','B','C')\n winning_door <- sample(doors, repetitions, replace=TRUE)\n contestant_choose <- sample(doors, repetitions, replac... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-15T21:33:57.840",
"Id": "17596",
"Score": "2",
"Tags": [
"r"
],
"Title": "Increase efficiency for an R simulator of the Monty Hall Puzzle"
} | 17596 |
<p>I'm a Clojure n00b (but am experienced with other languages) and am learning the language as I find time - enjoying it so far, though the strange dreams and accidental use of parenthesis elsewhere (e.g. Google searches) are a bit disconcerting!</p>
<p>Usually when learning a new language the first thing I do is imp... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T03:37:58.643",
"Id": "28014",
"Score": "0",
"body": "Take a look at this neat little implementation of life in Clojure: http://clj-me.cgrand.net/2011/08/19/conways-game-of-life/"
},
{
"ContentLicense": "CC BY-SA 3.0",
"C... | [
{
"body": "<p>Some quick points / ideas:</p>\n\n<ul>\n<li>The code looks pretty decent overall for a first attempt at Clojure, and you have made good use of the \"functional\" style. Nice work!</li>\n<li>You have used a <code>defrecord</code> for the cells data structure. This isn't really buying you anything, ... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T00:23:27.380",
"Id": "17603",
"Score": "5",
"Tags": [
"clojure",
"game-of-life"
],
"Title": "Critique my Clojure \"Game of Life\" code"
} | 17603 |
<p>Previously I asked a <a href="https://stackoverflow.com/questions/9791778/grouping-python-dictionaries-in-hierachical-form" title="question">question</a> on how to group dictionaries in a list in a hierarchical structure.</p>
<p>Initially I wanted to group a list of dictionaries that looks like the following, using... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T20:36:36.470",
"Id": "28046",
"Score": "1",
"body": "Welcome to Code Review. If you read the FAQ, you will find that you need to post your code into your question or we'll have to close it. Please edit your post and paste in your co... | [
{
"body": "<p>Try doing a recursive sequence that iterates through each group, by passing a group index to each call, and find and build the tree as it receives inputs:</p>\n\n<pre><code>def generate_hierarchical_hash(entries, groups):\n \"\"\"Returns hierarchical hash of entries, grouped at each level by the ... | {
"AcceptedAnswerId": null,
"CommentCount": "16",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T15:21:18.533",
"Id": "17611",
"Score": "3",
"Tags": [
"python",
"optimization",
"algorithm",
"performance"
],
"Title": "Need an algorithm to optimize grouping python dictiona... | 17611 |
<p>What is the simplest way of hiding labels and inputs in a way that they <strong>do not</strong> affect the layout. See image and the code below. The label <em>text3</em> is hidden and there is no extra gap between <em>text2</em> and <em>text4</em>. In this implementation I put both in <code>div</code> container and ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T20:03:08.183",
"Id": "28041",
"Score": "1",
"body": "So you _want_ to have a gap between 2 and 4? Just trying to make sure I understand"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T20:50:30.037",
... | [
{
"body": "<p>I would use a combination of CSS and JavaScript for this use-case. Essentially what you want to achieve is a simple solution to showing and hiding form elements.</p>\n\n<p>This is what I would suggest for markup:</p>\n\n<pre><code><form id=\"myform\" method=\"POST\" action=\"/some/processing/sc... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T19:40:46.817",
"Id": "17620",
"Score": "1",
"Tags": [
"javascript",
"html"
],
"Title": "What is the simplest way to hide labels and inputs with javascript?"
} | 17620 |
<p>I would like to get some feedback on the following implementation of disjoint sets, based on disjoint-sets forests (<em>Cormen, et.al., Introduction to Algorithms, 2nd ed., p.505ff</em>). It should have the following properties:</p>
<ul>
<li>Abstracts away all internal data structures and can be used to create disj... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-17T18:29:54.763",
"Id": "31523",
"Score": "0",
"body": "FWIW, Sylvain Conchon and J.C. Filliatre have an axiomatized (and proven correct) implementation in \"A Persistent Union-Find Data Structure\" which reads in Ocaml... I was trying... | [
{
"body": "<p>I have a feeling that your <code>find</code> is suboptimal. I'd say you should compress the whole path up to the root, not just the parent of the single node. So you should find the root node first and then traverse the path again and set <code>parent</code> to <code>Some(rootNode)</code> for all ... | {
"AcceptedAnswerId": "28003",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T20:51:47.330",
"Id": "17621",
"Score": "11",
"Tags": [
"algorithm",
"scala"
],
"Title": "Disjoint sets implementation"
} | 17621 |
<p>I am trying to write a merge sort algorithm. I can't tell if this is actually a canonical merge sort. If I knew how to calculate the runtime I would give that a go. Does anyone have any pointers?</p>
<pre><code>public static void main(String[] argsv) {
int[] A = {2, 4, 5, 7, 1, 2, 3, 6};
int[] L, R;
L... | [] | [
{
"body": "<p>When a prospective employer asks you a question like this one, they most likely want to see something original. They want you to show them that you can create code and not just copy and paste bits and pieces of code.</p>\n\n<p>If what you wrote works and runs correctly, then you have accomplished... | {
"AcceptedAnswerId": "17623",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T20:47:10.337",
"Id": "17622",
"Score": "7",
"Tags": [
"java",
"algorithm",
"sorting",
"mergesort"
],
"Title": "Is this attempt at a merge sort correct?"
} | 17622 |
<p>Below is a module that executes a sequence of actions in any possible order. <code>LANGUAGE_DataKinds</code> and <code>LANGUAGE_DefaultSignatures</code> are predefined cpp symbols.</p>
<p>Is this permutation applicative/monad implementation clear? I am particularly concerned about the use of DataKinds and GADTs, ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-17T16:14:09.117",
"Id": "28099",
"Score": "1",
"body": "Can you add a usage example?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-17T17:23:40.403",
"Id": "28100",
"Score": "0",
"body": "Yes, ... | [
{
"body": "<p>Below is what I have now. For modifications to this solution, please add a new answer that I can choose (rather than comment on it). Disadvantages compared to the original implementation include an additional <code>Monad n</code> constraint on <code>hoistPerm</code>. Advantages include a simple... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T23:52:50.533",
"Id": "17626",
"Score": "2",
"Tags": [
"haskell",
"monads"
],
"Title": "Module that executes a sequence of actions in any possible order"
} | 17626 |
<p>I have the following code:</p>
<pre><code>var sessionsWithError = SessionsFilteredByDate
.Where(i => i.TrackerId > 0 && i.StatusId == 0)
.Select(i => i.SessionId);
var sessionsWithErrorFixed = BusinessClient.Instance.Tracker
.GetAllTrackerAttempts()
.Select(i => i.SessionId);
var... | [] | [
{
"body": "<p>Put the values into a <code>HashSet<T></code> (see <a href=\"http://msdn.microsoft.com/en-us/library/bb359438.aspx\" rel=\"nofollow\">here</a>) as the lookup for that is constant O(1).</p>\n\n<pre><code>var sessionsWithErrorSet = new HashSet<int>(\n SessionsFilteredByDate.Where(i =&... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-17T11:08:01.927",
"Id": "17636",
"Score": "4",
"Tags": [
"c#",
"performance",
".net",
"linq",
"asp.net"
],
"Title": "Optimization code for checking if a list contains any ele... | 17636 |
<p>I have just started learning some Scheme this weekend. I recently solved a problem that goes something like:</p>
<pre><code>Count the number of ways possible to give out a certain amount of change using
1 5 10 25 and 50 cent pieces.
--SICP 1.16
</code></pre>
<p>So my code to this question looks like this:</p>
<pr... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2016-01-14T17:47:50.063",
"Id": "217081",
"Score": "0",
"body": "many scheme implementations offer a 2-D table, which would provide a simpler interface."
}
] | [
{
"body": "<p>Well, I could try to rewrite it to not use mutation, but it looks like the mutation is just there as a memoization optimization. You could either pass the vector as an argument into calculate, or you could abstract the memoization via something like define/memo provided in <a href=\"http://planet... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-17T11:33:51.883",
"Id": "17637",
"Score": "2",
"Tags": [
"lisp",
"scheme",
"functional-programming"
],
"Title": "Counting Ways to Make Change -- Is this good functional/Lisp style?"
... | 17637 |
<p>The following code works but I am wondering if it can be improved both readability and performance.<br>
I am using jquery and underscore.<br>
Any suggestions, comments or feedbacks will be appreciate.
thanks.</p>
<pre><code>var getThemeName = function (theme_name, transparency) {
if (theme_name === 'light') {
... | [] | [
{
"body": "<p>If you keep the naming consistent, you could probably do something like:</p>\n\n<pre><code>function getThemeName(themeName, transparency) {\n var name = \"messages:theme.info.\" + themeName;\n if( transparency ) {\n name += \"_with_transparency\";\n return translator.get(name, { percentage... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-17T11:44:28.573",
"Id": "17640",
"Score": "3",
"Tags": [
"javascript",
"jquery"
],
"Title": "function to generate values of a javascript object"
} | 17640 |
<p>I'm trying to write a Python module to handle matrices. (I know about numpy, this is just for fun) </p>
<p>So far I have written a few classes, <code>Matrix</code>, <code>Dim</code>, and <code>Vec</code>. <code>Matrix</code> and <code>Vec</code> are both subclasses of <code>Dim</code>. When creating a matrix, one w... | [] | [
{
"body": "<p>First, the thing that was most obvious in your code was that you never used a space after a comma.</p>\n\n<p>Second, you have the Matrix class where you override <code>__new__</code> and <code>__init__</code>, however it would be work just fine if you'd use <code>class Matrix(Dim): pass</code>.</p... | {
"AcceptedAnswerId": "17724",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-17T14:09:18.527",
"Id": "17643",
"Score": "11",
"Tags": [
"python"
],
"Title": "Python __new__ and __init__ usage"
} | 17643 |
<p>I have an instructor who is very stringent on makefiles only containing rules and dependencies that need to be included.</p>
<p>The structure for the program is very basic. The requirement is to produce two executable files: a3b and a3m. There was some code duplication, so I included that in a source code file c... | [] | [
{
"body": "<p><code>clean</code> and <code>all</code> are not files should therefore be declared as <a href=\"http://linuxdevcenter.com/pub/a/linux/2002/01/31/make_intro.html?page=2\" rel=\"noreferrer\">phony</a>. </p>\n\n<pre><code>.PHONY: clean all\nall : $(PROG1) $(PROG2) $(PROG3)\n...\nclean :\n /bin/rm ... | {
"AcceptedAnswerId": "17653",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-17T14:31:54.607",
"Id": "17645",
"Score": "4",
"Tags": [
"c",
"makefile"
],
"Title": "A clean and efficient makefile for a simple c program"
} | 17645 |
<p>I'm in the process of building a small application and obviously I need some sort of way to authenticate users. I'm not sure if I am way off or if this is close to what I should be thinking. Please let me know what you think...</p>
<pre><code><?php
class Authentication_controller {
private __constructor() {
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-17T20:24:44.097",
"Id": "28103",
"Score": "1",
"body": "Includes should go at the top of the PHP file, before the class declaration, not inside it. Also, use `require_once` rather than `include` - for one `require_once` performs better... | [
{
"body": "<p>I took the liberty of going through your code - see the comments preceded with MOD (modification), STAT (statement), QUES (question), SOL (solution) and REMOVED (removed)</p>\n\n<pre><code># MOD: Moved all include() to here\n# MOD: Changed from include() to require_once()\n# MOD: Added \"\" around... | {
"AcceptedAnswerId": "17652",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-17T19:42:20.803",
"Id": "17650",
"Score": "4",
"Tags": [
"php"
],
"Title": "Extremely basic authentication class"
} | 17650 |
<p>I am writing a simple script to automate my regression testing. I am new to bash scripting and my goal is achieve the most efficient and clean code. I have written a simple function to allow me to select which subject I would like to test. This will loop until I select the exit option. Again, this is quite simple; h... | [] | [
{
"body": "<ol>\n<li><code>while :</code> is a standard idiom for \"loop forever\" in bash. The colon (<code>:</code>) is a synonym for <code>true</code>.</li>\n<li>Prefer <code>$(command)</code> to <code>`command`</code> (\"backticks\"). The former allows you to nest; the latter does not.</li>\n<li>Though in y... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-17T22:51:47.273",
"Id": "17656",
"Score": "5",
"Tags": [
"optimization",
"bash",
"beginner"
],
"Title": "Script for automating regression testing"
} | 17656 |
<p>I am trying to generate LCM by prime factorization. I have already done the other way around to generate LCM using GCD but I am trying to achieve it using prime factorization. </p>
<p>I am getting right prime factors but the problem is that to generate LCM we need to multiply each factor the greatest number of time... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-18T14:17:33.283",
"Id": "28125",
"Score": "0",
"body": "If you search for factors in increasing order in `IsPrime` and you reach a point where your `i*i > n`, you can safely stop your search (do you understand why that is the case?) Us... | [
{
"body": "<h3>IsPrime</h3>\n\n<pre><code>public static bool IsPrime(int n)\n{\n for (int i = 2; i <= n; i++)\n {\n if (n % i == 0)\n {\n if (i == n)\n return true;\n else\n return false;\n }\n }\n return false;\n}\n</code><... | {
"AcceptedAnswerId": "17668",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-18T06:16:40.700",
"Id": "17662",
"Score": "3",
"Tags": [
"c#",
"algorithm",
"primes"
],
"Title": "Prime numbers, Prime Factors and LCM"
} | 17662 |
<p>I have the following class inheritances hierarchy:</p>
<p>First the abstract parent class:</p>
<pre><code><?php
abstract class SegmentParserAbstract{
/** @var ParserResult */
protected $_result;
protected $_invalidSegmentMessage;
protected $_segmentRegex;
protected $_segment;
abstract protected function create... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-18T09:40:13.400",
"Id": "73062",
"Score": "0",
"body": "See http://stackoverflow.com/a/12907998/1015656 I just had this discussion at SO. It's a simple example, but I hope it helps."
}
] | [
{
"body": "<p><strong>Edit:</strong> As David pointed out in the comments, I am completely wrong about constructors in the abstract classes. I hid the (ir)relevant section to avoid confusion and attempted to edit the rest of the post so it fit the new context. In case I missed something, Abstract classes CAN ha... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-18T09:15:19.760",
"Id": "17664",
"Score": "3",
"Tags": [
"php",
"object-oriented",
"design-patterns"
],
"Title": "Refactoring a class hierarchy to remove subclasses"
} | 17664 |
<p>I tried to implement an <a href="http://en.wikipedia.org/wiki/Unrolled_linked_list" rel="nofollow" title="Wikipedia">unrolled linked list</a> in C#. I only needed to add things and clear the whole list so I didn't implement <code>IList<T></code> (I tried but it was getting too complex, so I postponed it).</p>
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-18T23:16:21.797",
"Id": "28182",
"Score": "0",
"body": "I think you shouldn't use `for (;;)`, because it can be confusing to programmers that didn't encounter it before. The idiomatic way to write an infinite loop in C# is `while (true... | [
{
"body": "<p>I would change <code>GetEnumerator</code> as follows:</p>\n\n<pre><code>public IEnumerator<T> GetEnumerator()\n{\n for (var current = _FirstNode ; current != null ; )\n {\n var last = current.Next == null ? _NodeSize : _LastNodeCount;\n for (var i = 0 ; i != last ; i++) {... | {
"AcceptedAnswerId": "17674",
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-18T13:53:57.137",
"Id": "17670",
"Score": "7",
"Tags": [
"c#",
".net",
"collections",
"linked-list"
],
"Title": "A simple unrolled linked list implementation"
} | 17670 |
<p>I have to select all the rows from a database table containing a defined <code>(long)sessionId</code> where the <code>sessionId</code> row is indexed.
But it is slow, and since the code to access it is really simple, I'm wondering where the problem is. Here is the code of the three layers:</p>
<pre><code>var localP... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-20T00:47:41.423",
"Id": "28264",
"Score": "0",
"body": "Just to repeat a now deleted comment by @GeneS: Please run the SQL Profiler and post the SQL Statement that is being executed."
}
] | [
{
"body": "<p>I think the bottleneck is in <code>IQueryable<Model.Tracker.MilestonesInSession> GetAll()</code> because it looks like you are executing that query into memory by creating a <code>new Model.Tracker.MilestonesInSession</code>\nTry this:</p>\n\n<pre><code>public IQueryable<MilestonesInSessi... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-18T13:56:39.247",
"Id": "17672",
"Score": "2",
"Tags": [
"c#",
"performance",
".net",
"linq",
"asp.net"
],
"Title": "Simple retrieving sessionId rows from indexed SQL column ... | 17672 |
<p>This presumably simple task resulted in much more scripting logic than I thought would be necessary.</p>
<p><strong>Goal:</strong> when the user clicks a button inside one of the divs, it affects said div.</p>
<p><strong>Issues:</strong> I did not wish to have my function target specific elements in my DOM. (ex. t... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T21:18:35.357",
"Id": "28128",
"Score": "4",
"body": "You could simplify it some by doing: `$(this).siblings(\".innerBox\")...`"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T21:20:16.413",
"Id": ... | [
{
"body": "<p>My only alteration would be</p>\n\n<pre><code>$(this).parent().children(\".innerBox\") \n</code></pre>\n\n<p>to instead </p>\n\n<pre><code>$(this).siblings(\".innerBox\")\n</code></pre>\n\n<p>see here: <a href=\"http://jsbin.com/azamot/1/edit\" rel=\"nofollow\">http://jsbin.com/azamot/1/edit</a></... | {
"AcceptedAnswerId": "17677",
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-16T21:15:22.800",
"Id": "17675",
"Score": "2",
"Tags": [
"javascript",
"jquery",
"dom"
],
"Title": "Selector targets overly obtuse - recommendations?"
} | 17675 |
<p>I need to upload some images to a server. This is done with a json object. The json object has 2 values, a timestamp and a Base64 encoded string of the image. I am using the jackson library and was wondering if this is a good way to upload the images. I am worried about memory consumption if I happen to upload image... | [] | [
{
"body": "<p>I'm not familiar with Android at all, so just some generic Java notes:</p>\n\n<ol>\n<li><p>I'd rename <code>bm</code> to <code>bitmap</code>. A little bit longer variable names are usually easier to read and maintain.</p></li>\n<li><p>The constructor should validate its input parameters. Does it m... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-18T15:04:11.823",
"Id": "17680",
"Score": "3",
"Tags": [
"java",
"android"
],
"Title": "File to String Pattern for uploading to a server"
} | 17680 |
<p>I haven't studied a lot about positioning of controls in ASP.NET using .Net framework 3.5 but still I know following control positioning technique isn't the best one, just to mention its a user control,</p>
<pre><code> <asp:Label ID="Label2" runat="server" Text="Number of Days"></asp:Label>
&nbsp... | [] | [
{
"body": "<p>Yes, use CSS and some HTML to put things where they belong. Theres is nothing special about ASP.NET controls just check the generated HTML markup on the client side.</p>\n",
"comments": [],
"meta_data": {
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDat... | {
"AcceptedAnswerId": "17682",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-18T16:04:00.230",
"Id": "17681",
"Score": "3",
"Tags": [
"html",
"asp.net"
],
"Title": "ASP.NET controls positioning"
} | 17681 |
<p>This is an assignment question from school:</p>
<blockquote>
<p>Write a method called <code>licencePlate</code> that takes an array of objectionable words and returns a random licence plate that does not have any of those words in it. Your plate should consist of four letters, a space, then three numbers.</p>
</b... | [] | [
{
"body": "<p>A few suggestions, and then I'll get to how to test it:</p>\n\n<ul>\n<li>Don't be afraid of whitespace -- your code is a little hard to read due to the lack of new lines. Also, it's fairly standard to indent method names inside of classes.</li>\n<li>Always use descriptive names: <code>String[] a<... | {
"AcceptedAnswerId": "17690",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-18T19:49:38.393",
"Id": "17689",
"Score": "11",
"Tags": [
"java",
"homework",
"random"
],
"Title": "Turning an array of words into a random license plate"
} | 17689 |
<p>Is there anything what could be improved on this code?</p>
<pre><code>def factorial(n: Int, offset: Int = 1): Int = {
if(n == 0) offset else factorial(n - 1, (offset * n))
}
</code></pre>
<p>The idea is to have tail-recursive version of factorial in Scala, without need to define internal function or alias.</p>... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-18T21:55:28.967",
"Id": "28170",
"Score": "0",
"body": "Not familiar with Scala, but for what it's worth, the slowest part of recursive factorial is the repeated calculations. Make a tree of factorial calls, and you'll see that calls ... | [
{
"body": "<p>Looks ok in its basic form, but here are some points to consider:</p>\n\n<ol>\n<li><p>You may want to consider using at least <code>Long</code>, as factorials tend to get large quickly.</p></li>\n<li><p>Whenever you write a function that you believe to be tail-recursive, then do add <code>@tailrec... | {
"AcceptedAnswerId": "17706",
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-18T21:47:43.057",
"Id": "17691",
"Score": "5",
"Tags": [
"recursion",
"functional-programming",
"scala"
],
"Title": "Tail-recursive factorial"
} | 17691 |
<p>I've set up some PHP to delete a directory, it's contents, and any subdirectory and its contents... I'm new to PHP so I'm most definitely doing something WRONG or am doing something in the most inefficient way.</p>
<p>Looking for some references or suggestion on how to do this better...</p>
<p>Using PHP 5.3.8.</p>... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-19T02:03:51.347",
"Id": "28185",
"Score": "0",
"body": "What version of PHP do you use/can you use? You should tag it if possible as there is a lot of differences between 5.4 and 5.0 in regards to cleanliness of code."
},
{
"Co... | [
{
"body": "<p>Well one way would be to use the Recursive functionality...</p>\n\n<pre><code>chmod($main_dir, 0755) // Not sure why you keep chmodding stuff\n// Recursively loop through directory, starting with the children\n$dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($main_dir),Recursive... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-19T01:56:08.973",
"Id": "17696",
"Score": "1",
"Tags": [
"php"
],
"Title": "PHP - Deleting Directory Contents & SubDirectory Contents"
} | 17696 |
<p>The below logic is working fine. But is there any way to optimize this logic ?</p>
<p>I have a <code>string1, string2,...,stringn</code>
and values of each strings are</p>
<p><code>string1 = "s1k1-s1k2-s1k3-....-s1kn | s1v1-s1v2-s1v3-....-s1vn";</code></p>
<p><code>string2 = "s2k1-s2k2-s2k3-....-s2kn | s2v1-s2v2-... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-19T03:55:51.533",
"Id": "28192",
"Score": "2",
"body": "Optimize for what? Speed? Memory consumption? Readability?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-19T09:21:34.223",
"Id": "28211",
"S... | [
{
"body": "<p>If you want to optimize for speed and possibly memory, just use regular loops to build up your dictionary, though it may hurt readability.</p>\n\n<p>The idea is simple, scan the string for keys and values simultaneously. Don't extract substrings until you know what you need. Scan each token unti... | {
"AcceptedAnswerId": "17704",
"CommentCount": "6",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-19T02:13:20.087",
"Id": "17697",
"Score": "0",
"Tags": [
"c#"
],
"Title": "How to optimize this C# code"
} | 17697 |
<p>Here is some code I wrote in Python / Numpy that I pretty much directly translated from MATLAB code. When I run the code in MATLAB on my machine, it takes roughly 17 seconds. When I run the code in Python / Numpy on my machine, it takes roughly 233 seconds. Am I not using Numpy effectively? Please look over my Pytho... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-31T13:52:34.893",
"Id": "33900",
"Score": "0",
"body": "Not sure how this works in python, but could you run a kind of profile on the code to see which lines use most computation time?"
},
{
"ContentLicense": "CC BY-SA 3.0",
... | [
{
"body": "<p>The first obvious thing that jumped out was using nested \"for\" loops to work with array elements. Replace with whole-array arithmetic, and use offset slicing to shift the array (minus one end or the other) </p>\n\n<p>The first and last elements will require special attention, taking a few min... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-19T04:40:44.927",
"Id": "17702",
"Score": "6",
"Tags": [
"python",
"matlab",
"numpy"
],
"Title": "Python / Numpy running 15x slower than MATLAB - am I using Numpy effeciently?"
} | 17702 |
<p>I'm learning python and I want to model a <a href="http://en.wikipedia.org/wiki/Single-elimination_tournament" rel="noreferrer">single elimination tournament</a> like they use in athletic events like tennis, basketball, etc. The idea would be to insert some meaningful metrics to determine the winner. I've already ... | [] | [
{
"body": "<p>You have two lines marked as \"doesn't scale\".</p>\n\n<p>The initial team list can be obtained from your database table of available teams (select of team details).</p>\n\n<p>The other \"problem line\", is</p>\n\n<pre><code> if gameid in [8,12,14]: ##this is a manual decision tree, doesn't scale... | {
"AcceptedAnswerId": "17713",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-19T04:42:51.203",
"Id": "17703",
"Score": "7",
"Tags": [
"python"
],
"Title": "Using Python to model a single elimination tournament"
} | 17703 |
<p>In my project, they use Hibernate's session the below mentioned way and then save entity objects with in a transaction.</p>
<pre><code>Session session = HibernateUtil.getCurrentSession();
session.beginTransaction();
Employee employee = new Employee() ;
employee.setName("someName") ;
employee.setEmailId ("someId")... | [] | [
{
"body": "<p>Since <a href=\"http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html\" rel=\"nofollow\">Hibernate session provides APIs to run native SQL queries</a>, I would avoid extracting the connection from the session and leave the life cycle management of the Statement, ResultSet used fo... | {
"AcceptedAnswerId": "17726",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-19T07:34:37.913",
"Id": "17708",
"Score": "3",
"Tags": [
"java",
"hibernate"
],
"Title": "Any problems in using hibernate's session.connection() to run native sql"
} | 17708 |
<p>I'm trying to improve my coding practices and I was just wondering if (I'm definitely sure there are) there are better ways of doing the following task.</p>
<p>I'm rewriting the rent system of the building I live in, which currently (still) uses an Access '97 database. This is <a href="http://resunay.com/rent/schem... | [] | [
{
"body": "<p><strong>Functions!</strong></p>\n\n<p>Functions are what this code needs. You've already started separating your code logically, which is the first step in creating functions. Just look at your comments. You can start by making each commented section its own function. Of course, this isn't the end... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-19T12:15:39.973",
"Id": "17717",
"Score": "3",
"Tags": [
"php",
"pdo"
],
"Title": "Rewriting the rent system of a building"
} | 17717 |
<p>I have a form. When I click the <kbd>save</kbd> button, I want to create an object from the data and pass that info to HTML elements on the same page (div.form_content). </p>
<p>I decided, instead of creating a variable for each HTML element to create an array of HTML elements. I know the first HTML element is goi... | [] | [
{
"body": "<p>I'd say give your \"output\" elements a <code>data-*</code> attribute that matches the name of the form value it should contain. For instance:</p>\n\n<pre><code><div class=\"form_content\">\n <div data-key=\"fname\"></div>\n <div data-key=\"lname\"></div>\n ... | {
"AcceptedAnswerId": "17722",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-19T13:31:23.220",
"Id": "17720",
"Score": "1",
"Tags": [
"javascript",
"jquery"
],
"Title": "Form data to object to HTML"
} | 17720 |
<p>Since I'm learning F# along with functional programming, I managed to implement the rules for Conway's Game of Life. I'm not sure if I can improve some of its parts, though. For example, the <code>neighbours</code> function does an ugly range testing, but I can't think of anything simpler.</p>
<pre><code>module Gam... | [] | [
{
"body": "<p>I thought it would make sense if you created a function <code>alive</code>, which would take care of bounds checking for you. And also simplify the the final expression by using a single sequence expression instead of two like you do.</p>\n\n<p>But I'm not sure it's actually much better than your ... | {
"AcceptedAnswerId": "17749",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-19T15:57:11.433",
"Id": "17723",
"Score": "4",
"Tags": [
"f#",
"game-of-life"
],
"Title": "Conway's Game of Life in F#"
} | 17723 |
<p>Basically I have some methods that access the file system and to avoid that in the unit test I have broken those sections out into a protected virtual method. In the Unit Test I then use Moq to setup those protected methods the way I want. I am just curious if I am going about this correctly or is there a better way... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-19T17:26:18.717",
"Id": "28242",
"Score": "0",
"body": "Look at http://systemwrapper.codeplex.com for a different approach."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-19T17:57:09.280",
"Id": "28245... | [
{
"body": "<p>Another approach is to use a wrapper for the DirectoryInfo, FileInfo and FileStream. Then your methods can use the wrappers and inject your mocks in the unit tests. Several people have already thought of this and have written the wrappers for you. </p>\n\n<p>The one I like is <a href=\"http://sys... | {
"AcceptedAnswerId": "17729",
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-19T16:18:48.680",
"Id": "17725",
"Score": "10",
"Tags": [
"c#",
"unit-testing",
"file-system",
"moq"
],
"Title": "Unit tests for methods that access the file system"
} | 17725 |
<p>Please review the following code. Methods <code>getFirsts</code> and <code>getSeconds</code>, both of which are <code>private</code>, return a list of objects which implement <code>CommonInterface</code>. Is this a good or bad design?</p>
<pre><code>@Override
public final List<? extends CommonInterface> getOb... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-19T22:41:41.850",
"Id": "28260",
"Score": "3",
"body": "If this list returned through getFirsts or getSeconds are mutable, you have to realize that you are exposing the lists to mutation to any caller of \"getObjects\" (which is public... | [
{
"body": "<p>It's hard to say anything since it seems rather a pseudo-code. Anyway, two notes which you might find useful:</p>\n\n<ol>\n<li><p>I guess you could replace the switch-case structure with polymorphism. Two useful reading:</p>\n\n<ul>\n<li><em>Refactoring: Improving the Design of Existing Code</em> ... | {
"AcceptedAnswerId": "17744",
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-19T22:32:43.457",
"Id": "17734",
"Score": "7",
"Tags": [
"java",
"design-patterns"
],
"Title": "Returning a list of objects"
} | 17734 |
<p>The following code should be mostly OK, but I'm trying to avoid any stylistic problems or find anything that I overlooked.</p>
<p>The code is an implementation of asynchronous leadership election on a one way ring.</p>
<p>Parts of the implementation are a bit unnatural, because it has some forced features.</p>
<p... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-06-14T06:37:05.863",
"Id": "42585",
"Score": "0",
"body": "I'm really late here, but I thought I'd chime in and say it looks pretty good in general. I'd consider using a `std::unique_lock` though instead of manually calling `lock()` and `... | [
{
"body": "<p>First, a general observation: you have a fair number of typos in your comments. Although the compiler doesn't check such things, I prefer to think of the comments as an integral part of the code, so even minor typos should be fixed.</p>\n\n<ul>\n<li>Random numbers</li>\n</ul>\n\n<p>Right now, you'... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-20T00:08:58.503",
"Id": "17736",
"Score": "7",
"Tags": [
"c++",
"c++11",
"asynchronous"
],
"Title": "Asynchronous leadership election"
} | 17736 |
<p>I implemented reversing a linked list in iterative way, just by knowing we need 3 pointers and I have never seen a code before, when I coded. I ran this code and tested it, it works fine. Even for null element and one element. But all the implementations on net have slightly different code. I was just wondering, how... | [] | [
{
"body": "<p>Try this code, it's a bit easier to follow. Also, you define the return type as int, but you're not returning anything meaningful, so you should change the function to void.</p>\n\n<pre><code>void reverse(Node* & node)\n{\n if(!node)\n {\n cerr << \"The node doesn't exist \\... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-20T05:57:10.987",
"Id": "17741",
"Score": "13",
"Tags": [
"c",
"algorithm",
"interview-questions",
"linked-list"
],
"Title": "Reversing a linked list"
} | 17741 |
<p>So this is an app I'm working on, and I'd like to get some feedback on it. I'm leaving some key parts out, as I don't want everyone everywhere to have access to the complete code. The main pieces that I would like you guys to look at are still there though.</p>
<pre><code>// Does a remote AJAX request that scrapes ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-20T15:55:17.227",
"Id": "28281",
"Score": "5",
"body": "It'd probably be beneficial if you wrote a few sentences about what the code is supposed to do and/or what you're particularly interested in having reviewed. Also, try running you... | [
{
"body": "<p>This is not a complete review but one concerning organization aspects only. </p>\n\n<p>The first thing I suggest you do is organize your javascript into objects and classes such. Having a bunch of functions lying around usually is not the best idea as they are really hard to test and mantain after... | {
"AcceptedAnswerId": "17845",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-20T14:05:57.880",
"Id": "17750",
"Score": "3",
"Tags": [
"javascript",
"jquery"
],
"Title": "Javascript app review"
} | 17750 |
<p>Sparse is probably the wrong word - this is for encoding arrays of booleans where contiguous values tend to be the same. It'd be great to know a proper name for this data structure so I could read about it or use an existing implementation. I already realize I probably ought not to use doctest, and would welcome fee... | [] | [
{
"body": "<ol>\n<li>Your <code>__repr__</code> function tells the outside world about the implementation of your class. The fact that you are using a particular internal representation of a string shouldn't be exposed even here. I suggest this function should return something of the form <code>SparseBitArray('... | {
"AcceptedAnswerId": "17819",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-20T14:39:15.427",
"Id": "17753",
"Score": "5",
"Tags": [
"python",
"array"
],
"Title": "Sparse Bitarray Class in Python (or rather frequently having contiguous elements with the same ... | 17753 |
Servlet is a Java application programming interface (API) running on the server machine which can intercept on the requests made by the client and can generate/send a response accordingly. | [] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-20T15:56:20.810",
"Id": "17755",
"Score": "0",
"Tags": null,
"Title": null
} | 17755 |
JSP (Java Server Pages) is a server side technology used for presentation layer for the web applications. JSP are available on the server machine which allows you to write template text in (the client side languages like HTML, CSS, JavaScript and so on) and interact with backend Java code. | [] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-20T15:57:18.237",
"Id": "17757",
"Score": "0",
"Tags": null,
"Title": null
} | 17757 |
<p>I'd love some feedback about this code that I'm editing and shortening now.</p>
<pre><code><?php
namespace bbn\cls;
class mvc
{
use \bbn\traits\info;
// From \bbn\traits\info.
protected static $info=array();
protected static $cli=false;
// Is set to null while not routed, then false if routi... | [] | [
{
"body": "<p>First rewrite you code without the following things:</p>\n\n<ul>\n<li>global</li>\n<li>static</li>\n<li>@</li>\n</ul>\n\n<p>Your code isn't looking an object oriented approach right now. If you can forget the usage of the two mentioned keywords (global is really bad and the static can be a hard qu... | {
"AcceptedAnswerId": "17821",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-20T17:36:39.990",
"Id": "17758",
"Score": "2",
"Tags": [
"php",
"mvc",
"template",
"url-routing",
"mustache"
],
"Title": "A PHP MVC working with Mustache (and now nested t... | 17758 |
<p>My app has a feature that when you click the <kbd>new data</kbd> button, HTML is loaded to the page via ajax. Because I am using AJAX, all events I want to add has to be bound using the <code>on()</code> method.</p>
<p>There are 4 <code>on('click')</code> functions that are bound to 4 different HTML elements:</p>
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-20T19:41:47.720",
"Id": "28289",
"Score": "2",
"body": "See [this question/answer](http://codereview.stackexchange.com/questions/16483/jquery-multi-functional-event-delegate/16494#16494) (/shameless self-promotion)"
},
{
"Conte... | [
{
"body": "<p>Perhaps something like this? </p>\n\n<pre><code>function manipulateData($action){\n $('#container').on( {\n click: function() {\n switch(action){\n case \"save\" : saveData();\n break;\n case \"delete\" : deleteData();\n ... | {
"AcceptedAnswerId": "17778",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-20T19:20:49.200",
"Id": "17759",
"Score": "2",
"Tags": [
"javascript",
"jquery"
],
"Title": "Multiple .on() events"
} | 17759 |
<p>I want to get my first CSS layout reviewed.
First of all, the related HTML code is as follows -</p>
<pre><code><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title> Preferences </title>
<script src="options.js"></script>... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-04T19:28:38.593",
"Id": "32225",
"Score": "0",
"body": "Get rid of the `<br>`"
}
] | [
{
"body": "<p>I'm no CSS expert, but two things I would change are:</p>\n\n<ul>\n<li>remove unnecessary spaces (indenting and in ' : ').</li>\n<li><p>condense explicit margin-top, margin-left etc (and padding):</p>\n\n<pre><code>h1{\nwidth:40%;\nbackground:#eee;\nbox-shadow:0 0 5px 5px #888;\nmargin:10% 30% 0 3... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-20T21:17:25.793",
"Id": "17764",
"Score": "6",
"Tags": [
"optimization",
"css",
"html5"
],
"Title": "Review for CSS layout code"
} | 17764 |
<p>In the very simple code below I'm illustrating having a master 'App' object with 'Chat' & 'Posts' modules inside.</p>
<p>Now, what I'm asking is, in my conscious effort to keep my components loosely coupled and modular, how I can use other functionality in the App object following the modular, decoupled pattern... | [] | [
{
"body": "<p>Well, your code wouldn't break as long as <code>App.Utils.add()</code> is still around to be called - even if <code>App.Chat.init()</code> becomes <code>SomethingElse.Chat.init()</code>. Of course, if you completely remove <code>App.Utils.add()</code> then, yeah, it'll break.</p>\n\n<p>However, th... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-20T22:11:52.317",
"Id": "17765",
"Score": "-2",
"Tags": [
"javascript"
],
"Title": "Keeping my JS decoupled but still use functionality"
} | 17765 |
<p>I want to defer the reading of a bitmap to another thread. I'm mainly concerned about concurrency issues since I'm kind of green on that subject, so I would like to know if this code has any potential flaws.</p>
<pre><code>public class SomeActivity extends Activity {
Bitmap threadBitmap;
@Override
pro... | [] | [
{
"body": "<p>I would implement a progress bar to inform the user of the image loading status to eliminate confusion to what it's going on. </p>\n\n<p>Also, you could deactivate the button for as long as the thread is running. And reactivate it in <code>onPostExecute()</code>.</p>\n",
"comments": [],
"m... | {
"AcceptedAnswerId": "17779",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-20T22:44:00.587",
"Id": "17766",
"Score": "1",
"Tags": [
"java",
"android"
],
"Title": "Reading a Bitmap from disk on a separate thread on Android"
} | 17766 |
<p>I have a string containing around 1 million sets of float/int coords, i.e.:</p>
<pre><code> '5.06433685685, 0.32574574576, 2.3467345584, 1,,,'
</code></pre>
<p>They are all in one large string and only the first 3 values are used. The are separated by 3 commas. The code i'm using right now to read through them ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-21T02:48:05.637",
"Id": "28294",
"Score": "0",
"body": "Could a map perform faster then a for loop? What about multiprocessing.pool.map? Could anyone provide an example that I could apply the above too?"
}
] | [
{
"body": "<p>How about this?</p>\n\n<pre><code>import random\n\ndef build_string(n):\n s = []\n for i in range(n):\n for j in range(3):\n s.append(random.random())\n for j in range(3):\n s.append(random.randint(0, 10))\n s = ','.join(map(str, s))+','\n return s\n... | {
"AcceptedAnswerId": "17774",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-20T22:54:22.760",
"Id": "17768",
"Score": "3",
"Tags": [
"python",
"optimization"
],
"Title": "Python Optimizing/Speeding up the retrieval of values in a large string"
} | 17768 |
<p>I was interested in how this code can be improved apart from basic caching. This object creates 'tokens' much like that the iOS mail app and the tag section below.</p>
<pre><code>var TokenTime = {
assets : {
tokenArray : []
},
init : function (){
var self = this;
//add event handler
$('#friend').on... | [] | [
{
"body": "<p>First of all, there are jQuery plugins out there, that do this for you. <a href=\"http://loopj.com/jquery-tokeninput/\" rel=\"nofollow\">Tokeninput</a>, for example, comes to mind. But if you want to roll your own, by all means do so. I'll continue under that assumption.</p>\n\n<p>So here, in no p... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-20T23:32:46.973",
"Id": "17771",
"Score": "4",
"Tags": [
"javascript",
"jquery"
],
"Title": "Tokenizer object"
} | 17771 |
<p>Does the code below look correct and efficient for removing duplicates from an unsorted linked list without using extra memory?</p>
<p>An object of type Node denotes a LL node. </p>
<pre><code>void removeDuplicates()
{
Node current = head;
while(current!=null)
{
Node prev = current;
Node next = curr... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-21T01:43:53.627",
"Id": "28291",
"Score": "1",
"body": "It seems your method only remove the adjacent duplicate elements. is that the case?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-21T02:42:21.557",
... | [
{
"body": "<p>This review is two-fold: once, in case you have to uphold your restriction and then what changes if you allow extra space.</p>\n\n<h2>Review</h2>\n\n<ul>\n<li><p><em>Comments</em>: they are simply missing. You need at least some javadoc, and for non-trivial methods like this some more never hurts.... | {
"AcceptedAnswerId": "17801",
"CommentCount": "6",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-21T00:17:06.870",
"Id": "17772",
"Score": "6",
"Tags": [
"java",
"algorithm",
"linked-list"
],
"Title": "Removing duplicates from an unsorted linked list"
} | 17772 |
<p>I'm new to Java and have been through some tutorials and I'm in the next step of checking how I'm doing now. Is there a better way of doing this?</p>
<pre><code>import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
i... | [] | [
{
"body": "<p>Welcome to Code Review.</p>\n\n<p>Before reviewing your code, it needs to be fixed:</p>\n\n<h2>First, make it work...</h2>\n\n<ol>\n<li><p><strong>Usage is never printed</strong> </p>\n\n<p>Your program is supposed to print the usage instructions if the wrong number of parameters ist supplied. Ch... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-21T16:13:03.820",
"Id": "17781",
"Score": "4",
"Tags": [
"java",
"sorting",
"file"
],
"Title": "Sorting file lines in natural order"
} | 17781 |
<p>I have an app that has a number ajax requests and instead of writing them all out, I created a plugin that takes options based on the request I am making.</p>
<p>I would like the ajax requests to occur when page loads and when the user clicks, mouses over certain buttons.</p>
<p>I decided to use the jquery.bind() ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-23T19:14:57.610",
"Id": "28422",
"Score": "0",
"body": "To, hopefully, prevent further close votes: This topic **IS** on topic. The code works, he simply had an issue getting it to that point. Read all the way through the post please."... | [
{
"body": "<p>Edit: The part below was, I think, me not seeing the forest for all the trees. The issue is quite simply that you're passing the <em>string</em> <code>'window'</code> to jQuery, rather than the <em>object</em> <code>window</code>. So jQuery tries to bind an event listener to a <code><window>... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-21T18:25:14.780",
"Id": "17784",
"Score": "1",
"Tags": [
"javascript",
"jquery",
"plugin"
],
"Title": "Ajax plugin that binds to click and page load"
} | 17784 |
<p>In my Java program, I need to operate on the sublists a of <code>ArrayList</code> or <code>LinkedList</code> often, like removing a sublist, comparing two sublists.</p>
<p>For <code>ArrayList</code> or <code>LinkedList</code>, I didn't find any good APIs to do these. My current implementation is as below. Basically... | [] | [
{
"body": "<p>Yes, this doesn't look pretty. Consider converting the list to sets or using RegEx.</p>\n\n<pre><code>ArrayList<String> A = new ArrayList<String>();\nA.add(\"Z\");\nA.add(\"Z\");\nA.add(\"C\");\nA.add(\"X\");\nA.add(\"Z\");\n\nSet<String> set = new HashSet<String>(A);\n\nfo... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-21T18:39:30.977",
"Id": "17788",
"Score": "6",
"Tags": [
"java",
"performance",
"linked-list"
],
"Title": "Operating sublists efficiently"
} | 17788 |
<p>I've written this simple script to force download some files (jpg, mp3, usually those which are loaded in browser by default).
I was wondering whether there's any way this could be improved upon, which means:</p>
<ul>
<li>making it more secure</li>
<li>making it use less cpu (filesize(), fopen(), fpassthru() are th... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-21T15:35:16.017",
"Id": "28326",
"Score": "1",
"body": "Before you start worrying about the microscopic amount of cpu time that filesize() will consume, you should start panicking about how this script lets a malicious user download AN... | [
{
"body": "<p>what about just puting a link to that file?</p>\n\n<p>for example:</p>\n\n<pre><code><a href=\"/file.mp3\">download file</a>\n</code></pre>\n\n<p>as far as i know it depends of the user configs if it will download it or use it in the browser</p>\n",
"comments": [],
"meta_data":... | {
"AcceptedAnswerId": null,
"CommentCount": "6",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-21T15:31:36.710",
"Id": "17790",
"Score": "5",
"Tags": [
"php"
],
"Title": "Is there a better way how to force file download than using this simple PHP script?"
} | 17790 |
<p>I am wondering if the code beneath considered as the proper way to achieve the topics stated in the title of the post.</p>
<p>I wonder is the best practice for achieving it?</p>
<pre><code>var Obj = function() {
if (arguments.length) {
//do something
for (var i in arguments) //iterate over the ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-20T09:30:41.720",
"Id": "28334",
"Score": "0",
"body": "Why not just use `arguments` from within the function?"
}
] | [
{
"body": "<p>Answer: Yes, yes, and yes.</p>\n\n<p>Honestly, I'm wondering how else you'd achieve it. The <code>arguments</code> object exists to let you access whatever was passed; <code>return this</code> is really the only way to allow chaining; and the \"static\" method is technically just an object propert... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-19T15:46:52.567",
"Id": "17796",
"Score": "2",
"Tags": [
"javascript"
],
"Title": "Javascript - varargs constructors, method chaining, and static methods"
} | 17796 |
<h2>Q.</h2>
<p>Basically this piece of code appears in my class constructor. It's working, but it seems very confusing. </p>
<p>It basically checks if the jQuery library is present locally and if it's not it will try to download it.</p>
<p>Is there any way I can improve this? Namely avoiding re-setting vars all the ... | [] | [
{
"body": "<p>Try this, just removed a couple of the repeated code (moved it to a function) and sets minified version or regular version based on a bool variable (getminified), thus you only have filePathFull (no longer have filePathMin):</p>\n\n<pre><code># Set `true` if minified version is preferred.\n// let'... | {
"AcceptedAnswerId": "17814",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-22T06:50:17.307",
"Id": "17803",
"Score": "4",
"Tags": [
"php"
],
"Title": "Check if the jQuery library is present locally"
} | 17803 |
<p>Since asynchronous operations (like <code>Socket</code>'s <code>Begin*</code>-<code>End*</code> pairs and <code>*Async</code> methods) that use <a href="http://msdn.microsoft.com/en-us/library/aa365198%28VS.85%29.aspx" rel="nofollow" title="MSDN | I/O Completion Ports">IOCP</a> under the hood cause the byte array th... | [] | [
{
"body": "<ol>\n<li><pre><code>var mod = blockSize % bufferSize;\nif (mod != 0)\n blockSize = (blockSize - mod) + bufferSize;\n</code></pre>\n\n<p>Wouldn't it be better if the constructor actually accepted <code>bufferCount</code> instead of <code>blockSize</code>, so that you wouldn't have to have this non... | {
"AcceptedAnswerId": "17817",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-22T07:47:01.597",
"Id": "17804",
"Score": "8",
"Tags": [
"c#",
".net",
"array",
"memory-management",
"asynchronous"
],
"Title": "A blocking buffer manager to provide segme... | 17804 |
<p><a href="https://www.interviewstreet.com/challenges/dashboard/#problem/4f2c2e3780aeb" rel="nofollow">This is a problem from Interview Street in Dynamic Programming section</a>.</p>
<blockquote>
<p><strong>Billboards (20 points)</strong></p>
<p>ADZEN is a very popular advertising firm in your city. In every r... | [] | [
{
"body": "<p>The inputs have to be limited with constraints you have listed, then</p>\n\n<ul>\n<li>use a TreeSet to store the N objects in it </li>\n<li>remove the K first </li>\n<li>print the Set</li>\n</ul>\n\n<p>few lines in Java (no malloc problem, memory managed by JVM): </p>\n\n<ul>\n<li>read the first ... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-22T13:04:58.990",
"Id": "17808",
"Score": "8",
"Tags": [
"algorithm",
"c",
"interview-questions"
],
"Title": "Billboard challenge algorithm"
} | 17808 |
<p>I have a list of dictionaries, with keys 'a', 'n', 'o', 'u'.
Is there a way to speed up this calculation, for instance with <a href="http://numpy.scipy.org/" rel="nofollow">NumPy</a>? There are tens of thousands of items in the list.</p>
<p>The data is drawn from a database, so I must live with that it's in the for... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-22T15:40:02.320",
"Id": "28349",
"Score": "0",
"body": "what are you trying to calculate?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-22T15:44:34.283",
"Id": "28350",
"Score": "0",
"body": "... | [
{
"body": "<p>I'm not sure if there really is a better way to do this. The best I could come up with is:</p>\n\n<pre><code>import itertools\nfrom collections import Counter\n\ndef convDict(inDict):\n inDict['a'] = inDict['a'] * inDict['n']\n return Counter(inDict)\n\naverage = sum(itertools.imap(convDict,... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-22T15:13:17.540",
"Id": "17810",
"Score": "3",
"Tags": [
"python",
"optimization"
],
"Title": "Optimization of average calculation"
} | 17810 |
<p>I'm learning programming and would appreciate any feedback on my code. I've come up with the below code. The logic works fine but the code itself seems rather confusing to me. Are there some patterns I could use to improve this code?</p>
<pre><code>// This code snippet handles click events on the Ingredients list (... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-22T19:59:04.747",
"Id": "28362",
"Score": "2",
"body": "You're right, it's confusing. It'd be great if you could write a few lines about what the code's meant to accomplish. Obviously, there are 3 arrays (buy, have, and need) but you'r... | [
{
"body": "<p>You have 4 actions, two of which are repeated twice... </p>\n\n<p><strong>Action #1</strong> -- repeated twice</p>\n\n<pre><code>weNeed.push(ingr);\nwhIndex = weHave.indexOf(ingr);\nweHave.splice(whIndex, 1);\n</code></pre>\n\n<p>This action requires that either <code>buy.indexOf(ingr) != -1 &... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-22T19:48:46.740",
"Id": "17816",
"Score": "10",
"Tags": [
"javascript",
"jquery"
],
"Title": "Handling click events on ingredients list"
} | 17816 |
<p>Here are some snippets I coded and I would like some feedback on my way of handling this:</p>
<p>I have a utility class, as a singleton, that provides me with a method named <code>randomColor</code> which returns a <code>(UIColor*)</code>:</p>
<pre><code>-(UIColor*)randomColor
{
float l_fRandomRedColor = [[Mat... | [] | [
{
"body": "<p>Alright, there's a number of simplifications to the Utility Class I'd like to make before I continue:</p>\n\n<p>Your abstraction from <code>arc4random</code> (at least that's what I hope it is), is too expensive to not just use the corresponding C-code directly. You could even make your own metho... | {
"AcceptedAnswerId": "18181",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-22T20:47:20.820",
"Id": "17818",
"Score": "3",
"Tags": [
"objective-c",
"memory-management",
"singleton"
],
"Title": "Objective-C retain / release snippet"
} | 17818 |
<p>I want to implement the flood-fill algorithm for <a href="https://stackoverflow.com/questions/12995378/is-there-a-proper-algorithm-for-detecting-the-background-color-of-a-figure">https://stackoverflow.com/questions/12995378/is-there-a-proper-algorithm-for-detecting-the-background-color-of-a-figure</a>. I did it recu... | [] | [
{
"body": "<p>A note on terminology: What you have implemented is usually called a <em>stack</em> rather than a queue, but it can also be called a LIFO (last in, first out) queue, so it's not wrong to call it a queue, only uncommon.</p>\n\n<p>The <code>nextPixel()</code> method is rather inefficient. You keep t... | {
"AcceptedAnswerId": "17827",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-22T23:11:13.917",
"Id": "17823",
"Score": "6",
"Tags": [
"java"
],
"Title": "Vector-based flood-fill algorithm queue class"
} | 17823 |
<p>I am trying to learn some Java on my own, and I am tackling some "programming challenge" problems to practice the little I have learnt so far.</p>
<p>Could anybody constructively critique this beginner's effort <a href="https://bitbucket.org/robottinosino/topcoder_java/src/2bf27cffa902/src/com/topcoder/srm144/div1/... | [] | [
{
"body": "<p>These critiques are mostly style and syntax related, and do not address your algorithm (you could probably find the simplest possible algorithm with Google anyway).</p>\n\n<ul>\n<li>make <code>decode</code> and <code>decodeMessage</code> static</li>\n<li><p>explicitly hide the constructor for <cod... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-22T23:54:08.763",
"Id": "17824",
"Score": "3",
"Tags": [
"java",
"beginner",
"strings",
"programming-challenge"
],
"Title": "Decoding binary string message"
} | 17824 |
<blockquote>
<p>Given a string of length n, print all permutation of the given string.
Repetition of characters is allowed. Print these permutations in
lexicographically sorted order </p>
</blockquote>
<p>Examples:</p>
<p>Input: AB</p>
<p>Ouput: All permutations of AB with repetition are:</p>
<pre><code> AA... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-23T17:52:50.960",
"Id": "28417",
"Score": "0",
"body": "Doesn't seem to work. Firstly, the strings are not in lexicographically sorted order, and secondly there are repetitions - eg if `string` = \"AAA\", it prints \"AAA\" 27 times whe... | [
{
"body": "<ul>\n<li><p>In case you ever want to just display the string (if you have something like \"AAA\"):</p>\n\n<pre><code>// if a different character from the first is not found\n// std::string::npos corresponds to \"not found\" (or -1)\n\nif (s.find_first_not_of(s.front()) == std::string::npos)\n{\n ... | {
"AcceptedAnswerId": null,
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-23T01:37:25.807",
"Id": "17828",
"Score": "11",
"Tags": [
"c++",
"strings",
"recursion",
"combinatorics"
],
"Title": "Print all permutations with repetition of characters"
} | 17828 |
<p>Recently I had the need to make some ajax calls within a MVC 3.0 with Razor and jQuery application. After a bit of trial and error and refactoring it was discovered that a number of different needs were required. These included:</p>
<ol>
<li>The serverside code would be responsible for creating any redirect urls ... | [] | [
{
"body": "<p>It seems to me that you're duplicating some of what <code>$.ajax()</code> will do for you (such as calling <code>beforeSend</code>) and you're also leaving the door open for using the deferred promise methods (<code>then</code>, <code>done</code>, <code>fail</code>, etc.) that jQuery's XHR object ... | {
"AcceptedAnswerId": "17889",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-23T04:30:02.253",
"Id": "17830",
"Score": "5",
"Tags": [
"javascript",
"jquery",
"asp.net-mvc-3"
],
"Title": "Wrapper for jquery ajax to ensure redirects occur on clientside"
} | 17830 |
<p>I'd like a code review on my very simple server application that validates whether the serial number retrieved from the client is a valid one or not. </p>
<ol>
<li><p>Is there a better way to handle the start/stop on the service? If you look at my Start and Service methods, I'm basically looking at <code>isServerRu... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-11-02T10:24:54.870",
"Id": "28950",
"Score": "0",
"body": "For serial number validation, is it not more appropriate to use WCF? have you looked at is as an option?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-... | [
{
"body": "<p>Without reviewing other aspects, I think you should separate the code so that there's the server itself in its class (or, maybe even better, its own project), and there's the UI.</p>\n\n<p>For instance, you should not show a MessageBox inside a server's code. There are so many reasons for that so ... | {
"AcceptedAnswerId": "17840",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-23T05:13:23.073",
"Id": "17832",
"Score": "26",
"Tags": [
"c#",
".net",
"networking"
],
"Title": "Implementing a good TCP Socket Server"
} | 17832 |
<p>I am trying to write 2 functions, one to read the matrix (2D array) and other one to print it out. So far I have:</p>
<pre><code>/* Read a matrix: allocate space, read elements, return pointer. The
number of rows and columns are given by the two arguments. */
double **read_matrix(int rows, int cols){
double... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-23T08:21:39.823",
"Id": "28395",
"Score": "2",
"body": "Please note that as per the [faq], any code should be working, and actual code from a project rather than pseudo-code or example code."
},
{
"ContentLicense": "CC BY-SA 3.... | [
{
"body": "<p>It seems OK to me as far as it goes. A couple of suggestions though:</p>\n\n<ol>\n<li><code>read_matrix</code> may be better split up into two functions, one to create it and the other to read the contents from the source, even if the <code>create_matrix</code> function is called from <code>read_m... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-23T05:15:49.613",
"Id": "17833",
"Score": "4",
"Tags": [
"c"
],
"Title": "Creating an 2D array using pointer/malloc, and then print it out"
} | 17833 |
<p>I have a menu, where a user selects one out of 4, and another 2 or 4 options will appear. I used big buttons; a picturebox + label to create the buttons.</p>
<p>The 4 main buttons are fixed, but the 4 other buttons depend on the first choice. Text and image will change. I programmed this, but I kinda feel this must... | [] | [
{
"body": "<p>A couple of points that may help you.</p>\n\n<ol>\n<li><p>When you see yourself copy and pasting or writing duplicated code, consider refactoring. I could only see one difference in your code in the case statements. You could move all that to a method and pass in the necessary struct. Even furt... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-23T07:13:32.617",
"Id": "17834",
"Score": "10",
"Tags": [
"c#",
"strings"
],
"Title": "Creating submenus with buttons"
} | 17834 |
<p>I have this class whose code I pasted in full. The <code>CredentialsManager</code> class is not shown here as all it does is return the DB connection variables.</p>
<p>Can be improved or is this is OK as I've written it? Namely, I'm interested if it's OK to have a private variable <code>mysqli</code> populate insid... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-23T16:09:03.473",
"Id": "28412",
"Score": "0",
"body": "That's not how you spell credentials? Anyways, I'm looking at the code now, will post an answer after I go through it."
}
] | [
{
"body": "<p>\"I'm pretty confident it would be a bad thing to have the same code for opening a connection in each function of the class, that's why I've put the connection opening code in a function itself.\"</p>\n\n<ul>\n<li>That would be correct when possible, practice DRY: Don't Repeat Yourself</li>\n</ul>... | {
"AcceptedAnswerId": "17851",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-23T07:58:26.350",
"Id": "17842",
"Score": "3",
"Tags": [
"php",
"object-oriented",
"mysqli",
"logging"
],
"Title": "Logger class for logging connections"
} | 17842 |
<p>I'd like to know whether having different variables for the src (source) and dst (destination) of an OpenCV function will have an effect on the processing time. I have two functions below
that does the same thing.</p>
<pre><code>public static Mat getY(Mat m){
Mat mMattemp = new Mat();
Imgproc.cvtColor(m,mM... | [] | [
{
"body": "<p>Just looking over the two functions, I would probably say the first function is a better practice. It's shorter (meaning it's more maintainable), it uses fewer variables (which can cause less confusion), and does the same thing as the second function. Overall the advantage is that the first funct... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-23T15:32:02.840",
"Id": "17846",
"Score": "10",
"Tags": [
"java",
"performance",
"android",
"opencv"
],
"Title": "OpenCV Mat processing time"
} | 17846 |
<p><strong>Preamble</strong></p>
<p>I am trying to learn JavaScript by writing code and looking up documentation, one problem at a time. I am already familiar with several "dynamic" languages, so I'm hoping to be productive quickly. This may not be the perfect way to learn the language, but I don't have the time to pi... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-24T04:14:29.183",
"Id": "28445",
"Score": "2",
"body": "Haven't gone over your code in detail, but I've noticed quite a few implied globals in your functions; i.e. variables you haven't declared with a `var` keyword, like `l` in `repea... | [
{
"body": "<p>Probably the biggest problem is that you forget <code>var</code> keywords every now and then. In JavaScript, these are <strong>required</strong> or the variable will implicitly be global. Yes, your program will execute, but you'll end up with a bunch of global variables you didn't expect. Bad.</p>... | {
"AcceptedAnswerId": "17862",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-24T00:03:30.577",
"Id": "17858",
"Score": "2",
"Tags": [
"javascript",
"algorithm",
"programming-challenge",
"combinatorics"
],
"Title": "Counting the ways to pick increasing ... | 17858 |
<p>Title:
Identifying equivalent lists</p>
<p>I am using lists as keys, and therefore want to identify equivalent ones.</p>
<p>For example,</p>
<pre><code>[0, 2, 2, 1, 1, 2]
[4, 0, 0, 1, 1, 0]
</code></pre>
<p>are 'equivalent' in my book. I 'equivalify' the list by checking the list value with the highest count, an... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-24T14:51:48.873",
"Id": "28469",
"Score": "0",
"body": "Could it be the case that there are multiple values with the same count? E.g. `[0, 1, 1, 2, 2, 3, 3, 3]`?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "201... | [
{
"body": "<p>You can do things slightly differently - instead of manually sorting, you can use the standard library sort() function. Below I've modified things slightly for convenience, but using it, <code>timeit</code> originally gave 16.3 seconds for 10^6 iterations and now gives 10.8 seconds.</p>\n\n<pre><c... | {
"AcceptedAnswerId": "17880",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-24T02:28:51.793",
"Id": "17859",
"Score": "2",
"Tags": [
"python"
],
"Title": "Identifying equivalent lists"
} | 17859 |
<p>The Model is simple: a <code>Player</code> class with three attributes: <code>first_name</code>, <code>last_name</code>, and <code>team_id</code>.</p>
<p>I'm just trying to get a handle on TDD for what will expand into a much more robust API. Here is my first stab at the integration/controller specs for REST action... | [] | [
{
"body": "<p>This looks pretty okay to me. I don't see why you have multiple header data tests though, I would assume they are all generated through the same piece of code, so you should either remove redundant tests to DRY up your specs, or if you are not using the same bit of code to generate the headers ref... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-24T02:43:09.963",
"Id": "17860",
"Score": "3",
"Tags": [
"ruby",
"ruby-on-rails",
"api",
"rspec"
],
"Title": "RSpec integration tests for a simple Rails API"
} | 17860 |
<p>I know there are other Python wiki API classes out there. I'm writing this one because I don't need all the bells and whistles, no edits, no talks, etc. I just need to be able to search for titles and get the wiki markup.</p>
<p>Any advice or suggestions or comments or a review or anything really.</p>
<pre><code>#... | [] | [
{
"body": "<p>An obvious one that jumps out at me is this:</p>\n\n<pre><code>class Wiki:\n def __init__(self, api=None):\n if api == None:\n self.api = \"http://en.wikipedia.org/w/api.php\"\n else:\n self.api = api\n return\n</code></pre>\n\n<p>Can be simplified to ... | {
"AcceptedAnswerId": "17961",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-24T04:15:46.467",
"Id": "17861",
"Score": "2",
"Tags": [
"python",
"beginner",
"classes"
],
"Title": "Wiki API getter"
} | 17861 |
<p>for now I'm using:</p>
<pre><code>int connect(const String& address, int port) {
struct sockaddr_in servAddr;
struct hostent* host; /* Structure containing host information */
/* open socket */
if((handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
return ERROR;
/... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-24T17:15:16.030",
"Id": "28484",
"Score": "0",
"body": "Not a code review question. Code posted her is expected to be working, but needing improvements."
}
] | [
{
"body": "<p>This works for me:</p>\n\n<pre><code>int connect2(const CStringA& address, int port) {\n struct sockaddr_in servAddr;\n /* open socket */\n SOCKET handle;\n if((handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)\n return ERROR;\n //gethostbyname by getaddrinfo replacement\n A... | {
"AcceptedAnswerId": "17866",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-24T06:39:46.530",
"Id": "17863",
"Score": "1",
"Tags": [
"c++"
],
"Title": "Socket connect realization: gethostbyname or getnameinfo"
} | 17863 |
<p>I've written a BST implementation and would like a code review and any suggestions on how to make it better.</p>
<pre><code>#include<iostream.h>
#include<conio.h>
#include<stack>
#include<queue>
#ifndef BINSTREE
#define BINSTREE
using namespace std;
typedef int valuetype;
/*struct Node{
v... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-24T07:35:43.823",
"Id": "28453",
"Score": "1",
"body": "replace `void custom_print(){` with `void custom_print();` in your class declaration"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-24T10:15:05.950",... | [
{
"body": "<p>This is an old deprecated header don't use it:</p>\n\n<pre><code>#include<iostream.h>\n\n// modern C++ standard headers have dropped the '.h' part\n// It is also more visually pleasing to add a space after include\n#include <iostream>\n</code></pre>\n\n<p>This is non standard (MS-DOS s... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-24T07:13:57.780",
"Id": "17864",
"Score": "8",
"Tags": [
"c++",
"tree",
"binary-search"
],
"Title": "Binary search tree implementation in C++"
} | 17864 |
<p>I have a timer that is only supposed to tick between x and x on weekdays.</p>
<p>I have used the following implementation.</p>
<pre><code> void tmrMain_Tick(object sender, EventArgs e)
{
if (!(DateTime.Now.DayOfWeek == DayOfWeek.Saturday || DateTime.Now.DayOfWeek == DayOfWeek.Sunday))
{
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-24T13:31:47.533",
"Id": "28465",
"Score": "4",
"body": "Ditch the `== true` bits. It's redundant. For example, `if (pnlAssembly.Visisble) {` is more idiomatic as it's already a Boolean expression and reads more naturally."
}
] | [
{
"body": "<p>I would use immediate returns to reduce the nesting of the first function.</p>\n\n<p>I would extract the constants out.</p>\n\n<p>I would also consider extracting to a method the logic of whether or not to do the actions.</p>\n\n<pre><code>private readonly TimeSpan StartTime = new TimeSpan(8, 0, 0... | {
"AcceptedAnswerId": "17883",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-24T09:11:22.077",
"Id": "17869",
"Score": "5",
"Tags": [
"c#",
"winforms",
"timer"
],
"Title": "Timer Tick Event only to execute at specific time"
} | 17869 |
<p>I've written a thread safe, persistent FIFO for <code>Serializable</code> items. The reason for reinventing the wheel is that we simply can't afford any third party dependencies in this project and want to keep this really simple. </p>
<p>The problem is it isn't fast enough. Most of it is undoubtedly due to reading... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-24T08:32:31.753",
"Id": "28458",
"Score": "0",
"body": "So why can't you afford 3rd party dependencies? It's actually simpler to use them in a case like this. Performance tuning and error handling for I/O is... hard."
},
{
"C... | [
{
"body": "<p>The problem is that you are always seeking. This slows you down very much on a conventional hard disk. The solution is to only append to a file and do compaction later. Such data structures are called journals. See <a href=\"https://github.com/sbtourist/Journal.IO\" rel=\"nofollow\">Journal.IO</a>... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-24T07:58:54.403",
"Id": "17870",
"Score": "5",
"Tags": [
"java",
"multithreading",
"recursion",
"queue",
"reinventing-the-wheel"
],
"Title": "Optimizing a thread safe Java NI... | 17870 |
<p>I was trying to solve the Median challenge at Interviewstreet.com.</p>
<p>I wasn't able to pass most of the test cases. But according to my understanding my code should work fine.</p>
<p>Here is the question:
<a href="https://www.interviewstreet.com/challenges/dashboard/#problem/4fcf919f11817" rel="nofollow">https... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-25T03:23:42.177",
"Id": "28506",
"Score": "0",
"body": "I see that you were redirected here by someone on Stackoverflow. This question actually belongs there though. CR is intended for code that the poster believes to be correct. (A... | [
{
"body": "<ol>\n<li><p>The following is duplicated, you could extract it out a method:</p>\n\n<pre><code>int itemindex = items.size();\nCollections.sort(items);\nif (itemindex %2 == 0) {\n result[i]=(float)((items.get((itemindex/2))+items.get(((itemindex)/2)-1))/2.0);\n} else {\n result[i]=items.get((ite... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-24T10:31:46.337",
"Id": "17874",
"Score": "1",
"Tags": [
"java"
],
"Title": "Interview street Median challenge"
} | 17874 |
<p>I've been using the following user agent Regular Expression to detect mobile devices, but I recently came across a few resources that listed a whole host of mobile user agents that I had not heard of before.</p>
<p>Whilst my agent check is only the first step in the detection process - I also have some JavaScript a... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-11-11T01:07:36.983",
"Id": "29423",
"Score": "0",
"body": "Any reason not to use a case-insensitive flag to avoid all the `[Aa]` character classes? Additionally, it would seem to me that you could simply surround the branching in `\\b` wo... | [
{
"body": "<p>First of all I guess your first step of the detection process is processed <strong>server side</strong> because you said there is a fallback which is processed by JavaScript.\nTherefore I guess you only perform a <strong>HTTP User-Agent</strong> check so far.</p>\n<h2>Suggestion BlackBerry</h2>\n<... | {
"AcceptedAnswerId": null,
"CommentCount": "6",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-10-24T12:14:31.667",
"Id": "17879",
"Score": "2",
"Tags": [
"regex",
"mobile"
],
"Title": "Mobile user agent check"
} | 17879 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.