language-agnostic

Detecting repetition with infinite input

What is the most optimal way to find repetition in a infinite sequence of integers? i.e. if in the infinite sequence the number '5' appears twice then we will return 'false' the first time and 'true' the second time. In the end what we need is a function that returns 'true' if the integer appeared before and 'false' if the function rec...

Use case for try-catch-finally with both catch and finally

I understand how try-catch works and how try-finally works, but I find myself using those (usually) in two completely different scenarios: try-finally (or using in C# and VB) is mostly used around some medium-sized code block that uses some resource that needs to be disposed properly. try-catch is mostly used either around a single s...

Random number generator that fills an interval

How would you implement a random number generator that, given an interval, (randomly) generates all numbers in that interval, without any repetition? It should consume as little time and memory as possible. Example in a just-invented C#-ruby-ish pseudocode: interval = new Interval(0,9) rg = new RandomGenerator(interval); count = inter...

How to deal with images that have white spots because of lighting while making snapshot?

Does anyone know how to remove the effect of the bright white spots on the image? That is, these spots occur because of the lighting when photographing. As a result, these spots affect the further processing of the image. If you cut a white spot of light from the image, then the results will be very good. This happens if you make a pictu...

Is there a way to implement this very simple boolean logic using only math operands (such as mod)?

I want to decrease a value by one and if it reaches zero, set it to the maximum value. Is there a way to do this via math without resorting to if (n-1 == 0) { n = max; } The opposite scenario of increasing a value by one and then setting it to zero when it is greater than max can easily be achieved using n = (n + 1) % (max + 1);. Furthe...

How should I populate city/state fields based on the zip?

I'm aware there are databases for zip codes, but how would I grab the city/state fields based on that? Do these databases contain the city/states or do I have to do some sort of lookup to a webservice? ...

directed graph with minimum number of chains

Hello. I have a problem but I can't figure out a solution. It goes like this: I have a directed graph with N nodes and M links and without cycles. I need to find out the minimum numbers of chains so every node belongs to only one chain. Example: 7 11 7 nodes; 11 links 1 2 1 5 2 3 2 5 2 7 3 4 // link exists between 3 and...

When searching for a tutorial site, where should I AVOID?

Note that this is the opposite of most questions. Which sites tend to be the worst for promoting poor practices? Edit: I guess I wasn't clear enough, although one person made an answer into a comment that might otherwise have been promoted. I'm looking for places that SAY they teach you X programing knowledge, but instead teach you thin...

Find the first un-repeated character in a string

What is the quickest way to find the first character which only appears once in a string? ...

Recommended library for scraping html data.

I need to process quite a bit of [fairly] arbitrary html data. The data thankfully can be broken into about twelve different templates. My current plan is to build a filter for each of the templates that allows me to extract the required data sans irrelevant content. Problem is I'm not sure what the ideal tool for the job is. I was h...

Why is it recommended to have empty line in the end of file?

Some code style tools recommend this and I remember seeing some unix command line tools warning about missing empty line. What is the reasoning for having an extra empty line? ...

How feasible/difficult is it to run an application that runs on a router?

In my example, I want to build an application that sends users who join a network some kind of interface and manage this at a central station (possibly the router, or a central server). The new user's input to this interface will be sent back to the central station and controlled. How plausible is this? Is sending something to a newl...

What is the difference between object and instance?

I'd like to know the difference between object and instance of class. I feel both are same, but why do we call with two names. Can anybody explain with real life example? ...

zig zag scan algorithm

I need a pseudocode or real code on how to do a zig zag scanning on a 2-d array using the following pattern: I have tried to use google to find this, but can't find any answers... This pattern is usually used for image decoding ...

Any research on maintainability of "guard statement" vs. "single function exit point" paradigm available?

I'm wondering if there has been any research (both casual and robust) on the maintainability of projects that use the "guard statement" paradigm vs. the "single function exit point" paradigm? Guard statement example (in C#): string GetSomeString() { if(necessaryConditionFails) { return null; } if(!FunctionWithBoolReturn(someAt...

Should a class use its private members directly or its publicly exposed member functions?

Well, I'm not sure about this one. I have a class like class Object { int internalValue ; struct SomeStructType { int superInternalValue ; } someStruct ; public: int getInternalValue() { return internalValue ; } int getSuperInternalVa...

Algorithm: Is there a good way of solving a comparison?

I have a set of numbers to compare. Let's say that I have to get this comparison from a user. I have a choice of either asking him a question consisting of 2 numbers or 3 numbers of 4 numbers. For instance, I can ask any of the following questions: Which of the numbers is greater? 2 OR 4 Which of the numbers is greater? 2 OR 3 OR 4 ...

Convert rounded decimal to (approximate) radical value?

Hello, I've made a lot of random math programs to help me with my homework (synthetic division being the most fun) and now I'm wanting to reverse a radical expression. For instance, in my handy TI calculator I get .2360679775 Well, I want to convert that number to it's equivalent irrational expression, which is sqrt(5)-2 I realiz...

What algorithm gives suggestions in a spell checker?

What algorithm is typically used when implementing a spell checker that is accompanied with word suggestions? At first I thought it might make sense to check each new word typed (if not found in the dictionary) against it's Levenshtein distance from every other word in the dictionary and returning the top results. However, this seems l...

How to determine an elite proxy...?

How does one programmatically determine if a given proxy is elite? What is the general method/headers checked for? Google isn't coming up with much for me on this inquiry... ...