How to test my solution (contest)? [closed] - testing

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I made a code for contest, I am getting 'WA' but I am sure that my code is correct . So before I complain admin, how could I be sure that my code is actually correct and not that problem test data is incorrect? Here's the question and this is my solution in c++ , so can anyone suggest methods of testing one's own solution or can anybody supply test input where my solution produces wrong answer?

Oh , god you should check properly , i found another input which gives wrong output ( i told you usually test inputs are correct and we are usually wrong :D) . For input "1 2 1 3 4 2" your code outputs "1 4 2" which is certainly wrong , it should be "2 4 2" .

You have several examples inside the question explanation you can use to test your code:
For instance, the alternating depth of (([]))[[[()]]] is 2, the
maximum number of symbols between a matched pair () is 6 and the
maximum number of symbols between a matched pair [] is 8.
And
Sample Input
14
1 1 3 4 2 2 3 3 3 1 2 4 4 4
Sample Output
2 6 8
You can also invent some yourself, count the depth and maximum number of symbols and use that as input.
Moreover, it is said in the question that
The time limit for this task is 1 second. The memory limit is 32MB.
Are you sure your code didn't take more than a second on one of the very big inputs, with N is close to 10^5?
If the tests don't pass, its highly probable that the problem is in your code and not in the automated checker.

Related

Answer Set Programming - make a FACT invalid

I have a question regarding Answer Set Programming on how to make an existing fact invalid, when there is already (also) a default statement present in the Knowledge Base.
For example, there are two persons seby and andy, one of them is able to drive at once. The scenario can be that seby can drive as seen in Line 3 but let's say, after his license is cancelled he cannot drive anymore, hence we now have Lines 4 to 7 and meanwhile andy learnt driving, as seen in Line 7. Line 6 shows only one person can drive at a time, besides showing seby and andy are not the same.
1 person(seby).
2 person(andy).
3 drives(seby).
4 drives(seby) :- person(seby), not ab(d(drives(seby))), not -drives(seby).
5 ab(d(drives(seby))).
6 -drives(P) :- drives(P0), person(P), P0 != P.
7 drives(andy).
In the above program, Lines 3 and 7 contradict with Line 6, and the Clingo solver (which I use) obviously outputs UNSATISFIABLE.
Having said all this, please don't say to delete Line 3 and the problem is solved. The intention behind asking this question is to know whether it is possible now to make Line 3 somehow invalid to let Line 4 do its duty.
However, Line 4 can also be written as:
4 drives(P) :- person(P), not ab(d(drives(P))), not -drives(P).
Thanks a lot in advance.
I do not fully understand the problem. Line 3 and line 4 are separate rules, even if line 4's antecedent is false line 3 would still be true. In other words, line 4 seems redundant.
It seems like you want a choice. I assume ab(d(drives(seby))) denotes that seby has lost their license. So, below line four is your constraint on only people with a license driving. Line five is the choice, so by default andy or seby can drive but not both. Notice in the ground program how line five is equivalent to drives(seby) :- not drives(andy). and drives(andy) :- not drives(seby). You can also have seby as being the preferred driver using optimization statements (the choice rule below is like an optimization statement).
person(seby).
person(andy).
ab(d(drives(seby))).
:- person(P), ab(d(drives(P))), drives(P).
1{drives(P) : person(P)}1.
If something is true, it must always be true. Therefore the line:
drives(seby).
will always be true.
However, we can get around this by putting the fact into a choice rule.
0{drives(seby)}1.
This line says that an answer will have 0 to 1 drives(seby).. This means we can have rules that contradict drives(seby). and the answer will still be satisfiable, but we can also have drives(seby). be true.
Both this program:
0{drives(seby)}1.
drives(seby).
And this program:
0{drives(seby)}1.
:- drives(seby).
Are satisfiable.
Most likely you will want drives(seby). to be true if it can be, and false if it can't be. To accomplish this, we need to force Clingo into making drives(seby). true if it can. We can do this by using an optimization.
A naive way to do this is to count how many drives(seby). exist (either 0 or 1) and maximize the count.
We can count the number of drives(seby). with this line:
sebyCount(N) :- N = #count {drives(seby) : drives(seby)}.
N is equal to the number of drives(seby). in the domain drives(seby).. This will either be 0 or 1.
And then we can maximize the value of N with this statement:
#maximize {N#1 : sebyCount(N)}.
This maximizes the value of N with the priority 1 (the lower the number, the lower the priority) in the domain of sebyCount(N).

Algorithm for finding a sum within an array? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Say you have some array with elements that you know are all positive, and smaller than a number N.
Can someone give me a general, high-level description of an algorithm to find out whether there is some subset within the array in which all the elements add up exactly to N?
It doesn't need to be particularly efficient; the set I'm working with is very small.
If efficiency is not important, an algorithm at a high-level is:
input: array A[1..K] of positive numbers, positive N
for each subset S of { 1..K }
sum = 0
for each i in S
sum = sum + A[i]
end
if (sum equals N) return true
end
return false
Pseudocode. Extremely inefficient.
if empty(numbers) return false
return hasSumSubset(numbers, N)
boolean hasSumSubset(numbers[], N):
p = pop(numbers)
if empty(numbers) return N == p
return hasSumSubset(numbers, N-p) or hasSumSubset(numbers, N)
If you actually implement this make sure numbers is copied (not passed in by ref) for the recursive calls; otherwise it won't work correctly.

Sieve Of Sundaram [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm need someone to walk me through the Sieve of Sundaram (wiki). Lets say I have a list of Integers, (41,43,47,49,51,53,59) in an array.
Now the above mentioned wikipedia explanation mentions: *
An odd integer is excluded from the final list if and only if it is of
the form 2(i+j+2ij)+1
*. I am trying to understand how to achieve this in a program (preferably VBA). Such that the output will be (41,43,47,53,59).
Explanations sincerely appreciated.
Regards,
I discuss the Sieve of Sundaram at my blog. The algorithm is given by the following pseudocode:
function sundaram(n)
m := n // 2
sieve := makeArray(m+1, True)
for i from 1 to m // 4
for j from i to (m-i) // (2*i+1)
sieve[i+j+2*i*j] := False
ps := [2]
for i from 1 to m
if sieve[i]
append 2*i+1 to ps
return ps
As per the WP article, after removing some numbers from a range k=1...n, we get the results as m=2k+1. That means that all ms are odd.
Now, an odd composite must be a product of two odd numbers: 2k+1 = (2i+1)(2j+1) = 4ij+2i+2j+1 (if one of them were even, the product would be even too). But that means that k = 2ij + i + j. Hence the sieve's operation.
As nicely explained here.

Hybrid Functional/Object Oriented languages like F#/Scala: Use types with methods or methods and types [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
So to add onto what I learned from this question I asked:
F# Records: Dangerous, only for limited use, or well used functionality?
This got me thinking about whether I should be using Types with methods or Records(?) AND methods. For example, this is a typical type I might make:
(* User Interaction Start *)
type UserInteraction public() =
member public x.CreatePassword(originalPassword) =
if String.IsNullOrEmpty originalPassword then
raise(new ArgumentException "CreatePassword: password is null or empty")
let hashUtility = new HashUtility();
hashUtility.CreateHash originalPassword
The problem I could see with this is there's a possibility that something within that UserInteraction type is mutable due that being allowed. Now with record types, as far as I understand, they are guaranteed to be immutable.
Would it be better design a module with methods that take in or return record types only or even go one step further with method chaining?
You may want to read When to Use Classes, Unions, Records, and Structures [MSDN] (bottom of the page). You appear to have some incorrect assumptions about records. For instance, they can have mutable fields just like classes. The important differences are inheritance, pattern matching, constructors, hidden members, equality, and copy-and-update expressions.
Just noticed one small error on that page: records can, in fact, implement interfaces.
Another possibly helpful section: Differences Between Records and Classes [MSDN] (bottom of page)
Note that you can create a member on an immutable type returning a new modified instance of the type which is a perfectly reasonable thing to do:
type Person =
{ Name : string; Age : int }
member x.GrowOlder n =
{ x with Age = x.Age + n }
let q = { Name = "David"; Age = 29 };;
q.GrowOlder 1
It is interesting to note that in the following C# code:
var x = 0;
var y = 1;
var z = x + y;
You wouldn't expect neither x nor y to be mutated by the use of the operator +, that is + returns a new value and does not mutate x nor y. You can generally consider designing everything with that in mind.

Objective-c random point which lies in given cgrect [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
my requirement is generating a random point in a given area, i.e i have an CGRect of some space and i need to generate a rendom point in this rect ..
how can i proceed in this scenario ??
You should generate 2 random values that lie in range of your rect.
Eg. a rect like this (100,50,300,200) will require you to get an x-value between 0 and 300 (your width) and then add 100 to it (your origin). Same will be required for y-value, get a random value between 0 and 200 (height) and add 50 (origin) to it.