correctness

Correctness Of Minimum Number Of Swaps To Sort An Array

情到浓时终转凉″ 提交于 2019-12-04 21:06:27
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 of the swaps required as I compare both the sorted array and the original array. This should be

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

浪尽此生 提交于 2019-12-04 08:37:14
This question already has an answer here: Is it bad practice to have more than one assertion in a unit test? [closed] 8 answers 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 -> setExpectedException('InvalidArgumentException'); $this -> keyword -> getKeywordCount($KeywordID,'Active'); $KeywordID = 1

Open Type Level Proofs in Haskell/Idris

人走茶凉 提交于 2019-12-03 08:08:08
问题 In Idris/Haskell, one can prove properties of data by annotating the types and using GADT constructors, such as with Vect, however, this requires hardcoding the property into the type (e.g. a Vect has to be a separate type from a List). Is it possible to have types with an open set of properties (such as list carrying both a length and running average), for example by overloading constructors or using something in the vein of Effect? 回答1: I believe that McBride has answered that question (for

Open Type Level Proofs in Haskell/Idris

僤鯓⒐⒋嵵緔 提交于 2019-12-02 20:45:15
In Idris/Haskell, one can prove properties of data by annotating the types and using GADT constructors, such as with Vect, however, this requires hardcoding the property into the type (e.g. a Vect has to be a separate type from a List). Is it possible to have types with an open set of properties (such as list carrying both a length and running average), for example by overloading constructors or using something in the vein of Effect? I believe that McBride has answered that question (for Type Theory) in his ornament paper (pdf) . The concept you are looking for is the one of an algebraic

How do I use System.Net.ConnectStream?

别等时光非礼了梦想. 提交于 2019-12-02 06:45:23
问题 I am trying to get my head around some of my predecessors code who, helpfully, has used 'var' to declare everything. I have a using statement which is below: using (var postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } When I put a breakpoint here, postStream shows up in the Autos window as System.Net.ConnectStream. Instead of 'var' I want to use 'ConnectStream' but the compiler doesn't like this. What am I missing, why can't I write my code like

How do I use System.Net.ConnectStream?

走远了吗. 提交于 2019-12-02 05:20:57
I am trying to get my head around some of my predecessors code who, helpfully, has used 'var' to declare everything. I have a using statement which is below: using (var postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } When I put a breakpoint here, postStream shows up in the Autos window as System.Net.ConnectStream. Instead of 'var' I want to use 'ConnectStream' but the compiler doesn't like this. What am I missing, why can't I write my code like this: using (ConnectStream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0,

What is the appropriate way to intercept WSGI start_response?

女生的网名这么多〃 提交于 2019-12-01 05:50:06
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(int(stat_str.split(' ')[0])) return start_response(stat_str, headers) try: result = self.application

When is the use of std::ref necessary?

自古美人都是妖i 提交于 2019-11-30 06:02:47
Consider: std::tuple<int , const A&> func (const A& a) { return std::make_tuple( 0 , std::ref(a) ); } Is the std::ref required for writing correct and portable code? (It compiles fine without it) Background: If I remove std::ref my code builds fine without any warnings ( g++-4.6 -Wall ), but doesn't run correctly. In case of interest the definition of A : struct A { std::array<int,2> vec; typedef int type_t; template<typename... OPs,typename... VALs> A& operator=(const std::pair< std::tuple<VALs...> , std::tuple<OPs...> >& e) { for( int i = 0 ; i < vec.size() ; ++i ) { vec[i] = eval( extract(i

Select n records at random from a set of N

前提是你 提交于 2019-11-29 15:27:09
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 number generator, is this algorithm correct? NOTE There're many questions of this kind on stackoverflow

When is the use of std::ref necessary?

拈花ヽ惹草 提交于 2019-11-29 05:28:09
问题 Consider: std::tuple<int , const A&> func (const A& a) { return std::make_tuple( 0 , std::ref(a) ); } Is the std::ref required for writing correct and portable code? (It compiles fine without it) Background: If I remove std::ref my code builds fine without any warnings ( g++-4.6 -Wall ), but doesn't run correctly. In case of interest the definition of A : struct A { std::array<int,2> vec; typedef int type_t; template<typename... OPs,typename... VALs> A& operator=(const std::pair< std::tuple