logo
down
shadow

prolog,using the right algorithm but getting the wrong answer


prolog,using the right algorithm but getting the wrong answer

By : user2950378
Date : November 17 2020, 11:55 AM
should help you out i am trying to find the maximum number out of a list and even used write() for every recursion to verify my steps.here is my code , The problem is in this clause:
code :
maxx([H|T],B,C):-
    (   H > B
    ->  C is H
    ;   C is B
    ),
    write(' maximmum of'),
    write(C),
    maxx(T,C,R1).
maxx([H|T], B, R):-
    (   H > B
    ->  C is H
    ;   C is B
    ),
    write(' maximmum of'),
    write(C),
    maxx(T, C, R).   % R is the final result that you want, not C
maxx([H|T], B, R):-
    (   H > B
    ->  C = H
    ;   C = B
    ),
    write(' maximmum of'),
    write(C),
    maxx(T, C, R).   % R is the final result that you want, not C


Share : facebook icon twitter icon
wrong answer in kadanes algorithm

wrong answer in kadanes algorithm


By : SalsaPants
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Change currmax= max(currmax, currmax+arr[i]) to currmax= max(arr[i], currmax+arr[i]) as to compare current value with array value.
In you case -1, 4, 5
code :
#include <iostream>
#include <cmath>
using namespace std;

int Max( int *arr, int n){

    int currmax=arr[0];
    int globalmax=arr[0];

    for(int i=1;i<n;i++) {
             currmax= max(arr[i], currmax+arr[i]); // your code line was wrong  here  check and try
             if(currmax>globalmax) 
                    globalmax=currmax;
    }
    return globalmax;

}

int main() {
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int arr[n];
        for(int i=0;i<n;i++) cin>>arr[i];
        cout<<Max(arr,n)<<" ";
    }
}
Prolog, getting right answer in terminal, but wrong answer when running a program

Prolog, getting right answer in terminal, but wrong answer when running a program


By : roshani gulhane
Date : March 29 2020, 07:55 AM
hop of those help? When you type the query member(2, [1,2,3]), GNU Prolog prompts you for a possible additional solution (hence the true ? prompt) as only by backtracking (and looking to the last element in the list, 3) it could check for it. When you press enter, you're telling the top-level interpreter that you are satisfied with the current solution (the element 2 in the list second position). The second query, member(4, [1,2,3]), have no solutions so you get a no.
To execute a query when a file is loaded, the standard and portable way of doing it, is to use the standard initialization/1 directive. In this case, you would write:
Can anyone correct my prolog answer for the given statement if it is wrong?

Can anyone correct my prolog answer for the given statement if it is wrong?


By : Gerald
Date : March 29 2020, 07:55 AM
wish of those help It really depends how you want to organize your database. You could, for example, also say:
Wrong answer in task - mistake in algorithm

Wrong answer in task - mistake in algorithm


By : Vatsal Shah
Date : March 29 2020, 07:55 AM
may help you . please pay attention to the "one of the coins must be reversed"
it means that you MUST flip and only ONE coin!!!
code :
public static int solution(int[] A) 
{
            int n = A.length;
            int result = 0; 

            for (int i = 0; i < n - 1; ) 
            {
                if (A[i] == A[i + 1]) 
                    result = result + 1;
                i = i+1;
            }      
            if (result == n-1)   return result-1;   // to cover {1,1}, {1,1,1}

            int max = 0;
            for (int i = 0; i <n; i++) 
            {
                int count = 0;
                if (i > 0)   // starting up from 1 and  covering the last
                {
                    if (A[i-1] != A[i])
                        count = count + 1;    
                    else
                       count = count - 1;
                }
                if (i < n - 1) 
                {
                    if (A[i] != A[i + 1])   // starting with 0
                        count = count + 1;
                    else
                        count = count - 1;
                }               
                max = Math.max(max,count);              
            }     
            return result + max; // 
        }
Wrong answer from spigot algorithm

Wrong answer from spigot algorithm


By : frank Dongo
Date : March 29 2020, 07:55 AM
Hope that helps I think you are translating this answer.
You need to be more careful of your indices and your loop ranges; for example, you’ve translated
code :
for(int i = len; i > 0; --i) {
  int x  = 10 * A[i-1] + q*i;
  A[i-1] = x % (2*i - 1);
  q = x / (2*i - 1);
}
for i in reverse 1..length loop
    x:=10*a(i) + q*i;
    a(i):= x mod (2*i-1);
    q:= x/(2*i-1);
end loop;
  for (int k = 0; k < nines; ++k) {
    printf("%d", 0);
  }
  for k in 0..nines loop
      put("0");
  end loop;
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org