language-agnostic

Interpolating missing contour lines between existing contour lines

不问归期 提交于 2021-02-19 05:41:47
问题 Contour lines (aka isolines) are curves that trace constant values across a 2D scalar field. For example, in a geographical map you might have contour lines to illustrate the elevation of the terrain by showing where the elevation is constant. In this case, let's store contour lines as lists of points on the map. Suppose you have map that has several contour lines at known elevations, and otherwise you know nothing about the elevations of the map. What algorithm would you use to fill in

Test if number is inside circular interval

余生长醉 提交于 2021-02-19 01:15:53
问题 Let us suppose we have a number circle, that ranges from -180 to 180, looking something like this: 180/-180 *** *** *** 90 *** *** -90 *** *** *** 0 A section of the circle is always swept in a clockwise direction. How can you tell if a number is inside or outside of the interval swept? In the following sample I/O, the first two numbers represent the interval and the third number is the number being checked. Output is true if the point is (inclusively) inside the interval, false otherwise. 2

On complexity of recursive descent parsers

浪子不回头ぞ 提交于 2021-02-18 20:32:58
问题 It's known that recursive descent parsers may require exponential time in some cases; could anyone point me to the samples, where this happens? Especially interested in cases for PEG (i.e. with prioritized choices). 回答1: It's because you can end up parsing the same things (check the same rule at the same position) many times in different recursion branches. It's kind of like calculating the n-th Fibonacci number using recursion. Grammar: A -> xA | xB | x B -> yA | xA | y | A S -> A Input:

Why is compare-and-swap (CAS) algorithm a good choice for lock-free synchronization?

筅森魡賤 提交于 2021-02-18 11:22:21
问题 CAS belongs to the read-modify-write (RMW) family, a set of algorithms that allow you to perform complex transactions atomically. Specifically, Wikipedia says that CAS is used to implement synchronization primitives like semaphores and mutexes, as well as more sophisticated lock-free and wait-free algorithms. [...] CAS can implement more of these algorithms than atomic read, write, or fetch-and-add, and assuming a fairly large amount of memory, [...] it can implement all of them. https://en

Find the length of the longest valid parenthesis sequence in a string, in O(n) time

不问归期 提交于 2021-02-17 10:30:42
问题 My friend ran into a question in an interview and he was told that there is an O(n) solution. However, neither of us can think it up. Here is the question: There is a string which contains just ( and ) , find the length of the longest valid parentheses substring, which should be well formed. For example ")()())" , the longest valid parentheses is ()() and the length is 4. I figured it out with dynamic programming, but it is not O(n). Any ideas? public int getLongestLen(String s) { if (s ==

Two egg dropping puzzle variation: unknown/infinite floors

白昼怎懂夜的黑 提交于 2021-02-17 05:46:12
问题 Preface This problem was inspired by a similar question last week on SO, that got deleted before it was clear what the real question was. I think this variation makes a nice problem that I wanted to share. Two Egg Problem A detailed definition and solution can be found here, but I will add a quick summary: Definition You are given two eggs, and access to a k -storey building. Both eggs are identical. The aim is to find out the highest floor f* from which an egg will not break when dropped out

Permutations unrank

匆匆过客 提交于 2021-02-10 04:24:03
问题 I know of an algorithm (it can be found online) to rank a permutation, i.e. given a permutation return the integer index into the list of lexicographically-sorted permutations, but I don't know any unrank algorithm that does the opposite: given an index i, return the i-th permutation in that lexicographic order. Since I couldn't find any, can somebody please shed some light? 回答1: Let's say you are permutating the letters (a, b, c). There are 3×2×1=6 permutations. Out of these, a third starts

Permutations unrank

蹲街弑〆低调 提交于 2021-02-10 04:21:50
问题 I know of an algorithm (it can be found online) to rank a permutation, i.e. given a permutation return the integer index into the list of lexicographically-sorted permutations, but I don't know any unrank algorithm that does the opposite: given an index i, return the i-th permutation in that lexicographic order. Since I couldn't find any, can somebody please shed some light? 回答1: Let's say you are permutating the letters (a, b, c). There are 3×2×1=6 permutations. Out of these, a third starts

Permutations unrank

房东的猫 提交于 2021-02-10 04:18:31
问题 I know of an algorithm (it can be found online) to rank a permutation, i.e. given a permutation return the integer index into the list of lexicographically-sorted permutations, but I don't know any unrank algorithm that does the opposite: given an index i, return the i-th permutation in that lexicographic order. Since I couldn't find any, can somebody please shed some light? 回答1: Let's say you are permutating the letters (a, b, c). There are 3×2×1=6 permutations. Out of these, a third starts

How do I write a function to compare and rank many sets of boolean (true/false) answers?

心已入冬 提交于 2021-02-08 17:12:06
问题 I've embarked on a project that is proving considerably more complicated than I'd first imagined. I'm trying to plan a system that is based around boolean (true/false) questions and answers. Users on the system can answer any questions from a large set of boolean (true/false) questions and be presented with a list showing the most similar users (in order of similarity) based on their answers. I've Googled far and wide but still not come up with much, so I was hoping somebody could point me in