correctness

Add integers safely, and prove the safety

安稳与你 提交于 2019-12-23 16:24:17
问题 Programming course assignment asks to write a (safe) function that adds two integers, and show that the function is safe. The following code represents my solution. I am not an expert on the C standard (or on formal verification methods). So I would like to ask: Are there better (or different) solutions? Thank you #include <limits.h> /* Try to add integers op1 and op2. Return 0 (success) or 1 (overflow prevented). In case of success, write the sum to res. */ int safe_int_add(int * res, int

Correctness Of Minimum Number Of Swaps To Sort An Array

痞子三分冷 提交于 2019-12-22 00:57:12
问题 The question is from here: https://www.geeksforgeeks.org/minimum-number-swaps-required-sort-array/ I will repeat it below: Given an array of n distinct elements, find the minimum number of swaps required to sort the array. Examples: Input : {4, 3, 2, 1} Output : 2 Explanation : Swap index 0 with 3 and 1 with 2 to form the sorted array {1, 2, 3, 4}. Input : {1, 5, 4, 3, 2} Output : 2 I have solved the problem by doing the following. Sorting the array (n log(n)) time Making a hash to keep track

What is the appropriate way to intercept WSGI start_response?

早过忘川 提交于 2019-12-19 08:01:43
问题 I have WSGI middleware that needs to capture the HTTP status (e.g. 200 OK ) that inner layers of middleware return by calling start_response . Currently I'm doing the following, but abusing a list doesn't seem to be the “right” solution to me: class TransactionalMiddlewareInterface(object): def __init__(self, application, **config): self.application = application self.config = config def __call__(self, environ, start_response): status = [] def local_start(stat_str, headers=[]): status.append

Select n records at random from a set of N

主宰稳场 提交于 2019-12-18 06:57:49
问题 I need to select n records at random from a set of N (where 0 < n < N ). A possible algorithm is: Iterate through the list and for each element, make the probability of selection = (number needed) / (number left) So if you had 40 items, the first would have a 5/40 chance of being selected. If it is, the next has a 4/39 chance, otherwise it has a 5/39 chance. By the time you get to the end you will have your 5 items, and often you'll have all of them before that. Assuming a good pseudo-random

Try / Try-with-resources and Connection, Statement and ResultSet closing

半城伤御伤魂 提交于 2019-12-18 03:58:17
问题 I have recently having some discussions with my professor about how to handle the basic jdbc connection scheme. Suppose we want to execute two queries, this is what he proposes public void doQueries() throws MyException{ Connection con = null; try { con = DriverManager.getConnection(dataSource); PreparedStatement s1 = con.prepareStatement(updateSqlQuery); PreparedStatement s2 = con.prepareStatement(selectSqlQuery); // Set the parameters of the PreparedStatements and maybe do other things s1

Correctness and Logic of algorithm: minimum steps to one

南笙酒味 提交于 2019-12-07 01:58:38
问题 Problem Statement: On a positive integer, you can perform any one of the following 3 steps. Subtract 1 from it. ( n = n - 1 ) If its divisible by 2, divide by 2. ( if n % 2 == 0 , then n = n / 2 ) If its divisible by 3, divide by 3. ( if n % 3 == 0 , then n = n / 3 ) Given a positive integer n and you task is find the minimum number of steps that takes n to one . My Recursive Solution (in C++) compares all the 3 cases when N is divisible by 3, while the general solution compares only 2, but

Proving correctness of multithread algorithms

这一生的挚爱 提交于 2019-12-06 19:15:23
问题 Multithread algorithms are notably hard to design/debug/prove. Dekker's algorithm is a prime example of how hard it can be to design a correct synchronized algorithm. Tanenbaum's Modern operating systems is filled with examples in its IPC section. Does anyone have a good reference (books, articles) for this? Thanks! 回答1: It is impossible to prove anything without building upon guarentees, so the first thing you want to do is to get familiar with the memory model of your target platform; Java

Assert multiple conditions in a single test, or split into multiple tests? [duplicate]

我只是一个虾纸丫 提交于 2019-12-06 03:58:29
问题 This question already has answers here : Is it bad practice to have more than one assertion in a unit test? [closed] (8 answers) Closed 2 years ago . If you were testing a count function like the one below, is it considered to be 'right' or 'wrong' to test multiple things for the function in one function vs having a test function for each of the tests? function testGetKeywordCount() { $tester = $this -> getDatabaseTester($this -> CompleteDataFile); $tester -> onSetUp(); $KeywordID = 0; $this

Correctness and Logic of algorithm: minimum steps to one

泄露秘密 提交于 2019-12-05 09:07:59
Problem Statement: On a positive integer, you can perform any one of the following 3 steps. Subtract 1 from it. ( n = n - 1 ) If its divisible by 2, divide by 2. ( if n % 2 == 0 , then n = n / 2 ) If its divisible by 3, divide by 3. ( if n % 3 == 0 , then n = n / 3 ) Given a positive integer n and you task is find the minimum number of steps that takes n to one . My Recursive Solution (in C++) compares all the 3 cases when N is divisible by 3, while the general solution compares only 2, but still gives the correct solution. int min_steps(int N){ if(N==1) return 0; else{ if(N%3==0){ if(N%2==0)

Proving correctness of multithread algorithms

↘锁芯ラ 提交于 2019-12-05 00:44:39
Multithread algorithms are notably hard to design/debug/prove. Dekker's algorithm is a prime example of how hard it can be to design a correct synchronized algorithm. Tanenbaum's Modern operating systems is filled with examples in its IPC section. Does anyone have a good reference (books, articles) for this? Thanks! It is impossible to prove anything without building upon guarentees, so the first thing you want to do is to get familiar with the memory model of your target platform; Java and x86 both have solid and standardized memory models - I'm not so sure about CLR, but if all else fails,