language-agnostic

Best way to find a point on a circle closest to a given point

Hi, Given a point (pX, pY) and a circle with a known center (cX,cY) and radius (r), what is the shortest amount of code you can come up with to find the point on the circle closest to (pX, pY) ? I've got some code kind of working but it involves converting the circle to an equation of the form (x - cX)^2 + (y - cY)^2 = r^2 (where r is ...

Sort numbers by sum algorithm

Hello all, I have a language-agnostic question about an algorithm. This comes from a (probably simple) programming challenge I read. The problem is, I'm too stupid to figure it out, and curious enough that it is bugging me. The goal is to sort a list of integers to ascending order by swapping the positions of numbers in the list. Each ...

Tips for writing security classes for user authentication and authorisation

I have a bunch of objects in my application (Organisations, Individuals, Orders, etC) and I need a nice clean way to decide which users can and can't view/edit these objects. User have a range of permissions such as 'Can edit own contacts' and 'Can view team's contacts' and can also be members of groups such as 'Account Manager' so vario...

What is a programming idiom?

I see the phrase "programming idiom" thrown around as if it is commonly understood. Yet, in search results and stackoverflow I see everything... From micro: Incrementing a variable Representing an infinite loop Swapping variable values To medium: PIMPL RAII Format, comments, style... To macro: Programming paradigm or common li...

Refactoring for non OO languages

Can anyone recommend a website, book, or simply a list of refactoring strategies for procedural languages as opposed to object oriented languages? Everything I was able to find contained some strategies that could apply, most were useful only if working in an OO language. ...

Your personal, successful coding practices.

I've been thinking lately about a few practices that I have kind of adopted. Not things you see listed all the times, but patterns that you've looked back and said "I'm glad I did that", then adopted for yourself. I'm not really thinking of the ones you hear all the time, like "Refactoring" or any design patterns listed in common books...

Should I capitalize my constants?

What do you guys think? Should constants be capitalized? Or is that an archaic practice? So... const int MY_CONSTANT = 5; vs. const int myConstant = 5; ...

Memorable pedagogic programming experiments ?

"Tell me, and I'll forget. Show me, and I'll remember. Involve me, and I'll learn." We all have participated to some experiments with a teacher, a pro or a friend who wanted to make his point and involved you in the demonstration. For example: My communication teacher asked me to sit back to back with a friend. He gave him a s...

How to test error-conditions with code that interacts with remote web-servers?

I have a small project that basically a Python wrapper to a websites API. It's fairly well tested, but there are some conditions I cannot work out how to easily test: when the remote API is unreachable, or otherwise broken. More specifically, I would quite like a test for each of the following: When the site is unreachable (connectio...

how to create a site map/list

I need to create a site map/list,but I need the link-name to show up as well. What I mean by that is given , say , www.google.com , I need the following list to be created. Google - www.google.com Images - http://images.google.com/imghp?hl=en&tab=wi ... My Account - http://images.google.com/imghp?hl=en&tab=wi Personal Infor...

Accessing parent elements in an object model efficiently

I have a following object model: - Book -- Chapter 1 --- Page 1 ---- Image 1 ---- Image 2 ---- Text 1 --- Page 2 ... Resources are way down at the page level. But, I need to know the full path to resources, from the resources' point of view. One way, is to have resources be aware of their parents. So my Image object could have...

Tips on making good documentation for developers

I'm preparing documentation for an open source project. My target audience are developers. Right now, I'm working on the API, porting and code design sections. Anyone has any tips on making documentation for developers? ...

How many of you have contributed/gave feedback to your standards committees?

Ok as programmers we all follow programming standards (as in W3 standards, or C++ standard, etc...), and many of us consider these standards "sacred" and also many seasoned programmers can recite their choice of standards by heart when asked about a question in their field of expertise. You can see this last case a lot here on SO by the ...

Story generation

Upon reading a blog post about a minimalist story-generating python program, I was asking myself - and you - which are the most successful attempts at such programs. I remember seeing something using generating grammars, for instance. And which are the best attempts that, like this one, are extremely compact, either self-contained or abl...

How do you handle/react to user input concurrency on the GUI layer?

What are good ways to handle user input concurrency? As the answers to this question already rule out database locking, how do you handle concurrent user inputs in general? Is locking always a bad idea, even if it is not implemented by row locking? Are there best practices which are not use case dependant? What were your experiences wi...

Angular Momentum Transfer equations

Does anyone have any good references for equations which can be implemented relatively easily for how to compute the transfer of angular momentum between two rigid bodies? I've been searching for this sort of thing for a while, and I haven't found any particularly comprehensible explanations of the problem. To be precise, the question ...

How to design a class that has only one heavy duty work method and data returning other methods?

I want to design a class that will parse a string into tokens that are meaningful to my application. How do I design it? Provide a ctor that accepts a string, provide a Parse method and provide methods (let's call them "minor") that return individual tokens, count of tokens etc. OR Provide a ctor that accepts nothing, provide a Parse ...

Code Golf: Number to Words

The code golf series seem to be fairly popular. I ran across some code that converts a number to its word representation. Some examples would be (powers of 2 for programming fun): 2 -> Two 1024 -> One Thousand Twenty Four 1048576 -> One Million Forty Eight Thousand Five Hundred Seventy Six The algorithm my co-worker came up was alm...

Minimize function in adjacent items of an array

I have an array (arr) of elements, and a function (f) that takes 2 elements and returns a number. I need a permutation of the array, such that f(arr[i], arr[i+1]) is as little as possible for each i in arr. (and it should loop, ie. it should also minimize f(arr[arr.length - 1], arr[0])) Also, f works sort of like a distance, so f(a,b) ...

Offset vs page number when doing pagination

This is pretty trivial, but I noticed on SO that instead of an offset they are using page numbers. I know the difference is minor (multiply the page number by rows on a page or divide offset by rows on a page), but I'm wondering if one is recommended over the other. Some sites, like Google, of course use a more complicated system becaus...