language-agnostic

Is it safe to replace “a/(b*c)” with “a/b/c” when using integer-division?

核能气质少年 提交于 2019-12-21 09:36:28
问题 Is it safe to replace a/(b*c) with a/b/c when using integer-division on positive integers a,b,c , or am I at risk losing information? I did some random tests and couldn't find an example of a/(b*c) != a/b/c , so I'm pretty sure it's safe but not quite sure how to prove it. Thank you. 回答1: Mathematics As mathematical expressions, ⌊a/(bc)⌋ and ⌊⌊a/b⌋/c⌋ are equivalent whenever b is nonzero and c is a positive integer (and in particular for positive integers a , b , c ). The standard reference

Is a For Loop always executed at least once?

为君一笑 提交于 2019-12-21 07:35:44
问题 According to my teacher, a for-loop always executes at least once, even if the condition is not met. Example (like I know it from C++): for (int i=6; i <=5; i++) { //irrelevant for this question } According to her, this loop would execute at least once, yet it does not, or am I missing something? Is there any case, no matter what language, where this would execute once? To eliminate the thought in advance: yes, it was about for loops, not do-while-loops. Edit : Thanks for all those quick

Elegantly check if a given date is yesterday

一个人想着一个人 提交于 2019-12-21 07:26:10
问题 Assuming you have a Unix timestamp, what would be an easy and/or elegant way to check if that timestamp was some time yesterday? I am mostly looking for solutions in Javascript, PHP or C#, but pseudo code and language agnostic solutions (if any) are welcome as well. 回答1: PHP: $isYesterday = date('Ymd', $timestamp) == date('Ymd', strtotime('yesterday')); 回答2: In C# you could use this: bool isYesterday = DateTime.Today - time.Date == TimeSpan.FromDays(1); 回答3: You can use this in C#: bool

Does a garbage collector collect stack memory, heap memory, or both?

随声附和 提交于 2019-12-21 07:13:38
问题 I read lot of articles about garbage collection and almost all article tells about heap memory. so my question is "garbage collection collects stack memory or heap memory or both". 回答1: It collects heap memory. Usually, stack memory is collected automatically when the execution path reaches the end of the scope. e.g.: void fun() { int n; // reservation on the stack as part of the activation record ... } // returning the stack pointer to where it was before entering the scope In fact, in a

Efficiently get sorted sums of a sorted list

邮差的信 提交于 2019-12-21 06:58:33
问题 You have an ascending list of numbers, what is the most efficient algorithm you can think of to get the ascending list of sums of every two numbers in that list. Duplicates in the resulting list are irrelevant, you can remove them or avoid them if you like. To be clear, I'm interested in the algorithm. Feel free to post code in any language and paradigm that you like. 回答1: Edit as of 2018: You should probably stop reading this. (But I can't delete it as it is accepted.) If you write out the

Scrape HTML tables from a given URL into CSV

六眼飞鱼酱① 提交于 2019-12-21 05:18:09
问题 I seek a tool that can be run on the command line like so: tablescrape 'http://someURL.foo.com' [n] If n is not specified and there's more than one HTML table on the page, it should summarize them (header row, total number of rows) in a numbered list. If n is specified or if there's only one table, it should parse the table and spit it to stdout as CSV or TSV. Potential additional features: To be really fancy you could parse a table within a table, but for my purposes -- fetching data from

Efficient algorithm to find all “character-equal” strings?

╄→尐↘猪︶ㄣ 提交于 2019-12-21 05:11:18
问题 How can we write an efficient function that outputs "homoglyph equivalents" of an input string? Example 1 (pseudo-code): homoglyphs_list = [ ["o", "0"], // "o" and "0" are homoglyphs ["i", "l", "1"] // "i" and "l" and "1" are homoglyphs ] input_string = "someinput" output = [ "someinput", "s0meinput", "somelnput", "s0melnput", "some1nput", "s0me1nput" ] Example 2 : homoglyphs_list = [ ["rn", "m", "nn"], ] input_string = "rnn" output = ["rnn", "rm", "mn", "rrn", "nnn", "nm", "nrn"] Example 3 :

Is there a specialized algorithm, faster than quicksort, to reorder data ACEGBDFH?

落花浮王杯 提交于 2019-12-21 05:03:10
问题 I have some data coming from the hardware. Data comes in blocks of 32 bytes, and there are potentially millions of blocks. Data blocks are scattered in two halves the following way (a letter is one block): A C E G I K M O B D F H J L N P or if numbered 0 2 4 6 8 10 12 14 1 3 5 7 9 11 13 15 First all blocks with even indexes, then the odd blocks. Is there a specialized algorithm to reorder the data correctly (alphabetical order)? The constraints are mainly on space. I don't want to allocate

Explaining nested arrays to a programmer [closed]

六眼飞鱼酱① 提交于 2019-12-21 04:58:40
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . How have you explained nested arrays to a programmer. I'm thinking someone that has an entry level understanding of programming, but is trying to do more complicated coding. The array with array works, but they can't quite get their mind around the idea. Edit: example of a nested array: array( 'array1' => array(

What language features are required in a programming language to make a compiler?

橙三吉。 提交于 2019-12-21 04:13:36
问题 Programming languages seem to go through several stages. Firstly, someone dreams up a new language, Foo Language. The compiler/interpreter is written in another language, usually C or some other low level language. At some point, FooL matures and grows, and eventually someone, somewhere will write a compiler and/or interpreter for FooL in FooL itself. My question is this: What is the minimal subset of language features such that someone could implement that language in itself? 回答1: Compiler