DIRECTI : Need Help

can anyone tell whats wrong with this python code?

t=input()
a=[]
for i in range(t):
    n=input()
    a.append([])
    for j in range(n):
        a[i].append(raw_input().split(" on "))
        
for i in a:
    for j in range(len(i)-1):
        if i[j+1][0]=="Right":
            i[j][0]="Left"
        if i[j+1][0]=="Left":
            i[j][0]="Right"
    i[len(i)-1][0]="Begin"
    for k in range(len(i)-1,0,-1):
        print i[k][0],"on",i[k][1]
    
    if i!=a[len(a)-1]:
        print i[0][0],"on",i[0][1],"\n"
    else:
        print i[0][0],"on",i[0][1]

Consider this test case:

1
3
Begin on Sion Road
Left on Matunga Road
Right on Dadar Road

Please help finding mistake with this c++ code :


#include<iostream>
#include<cstring>
#include<stdio.h>
#include<string>
#include<vector>
using namespace std;

int main()
{
	int tc, n, i;
	vector<string> dr;
	vector<string> sn;
	char d[50], t[50], rn[50];
	cin>>tc;
	while(tc)
	{
		cin>>n;
		for(i=0; i<n; i++)
		{
			cin>>d>>t;
			gets(rn);
		dr.push_back(d);
		sn.push_back(rn);
		}
		cout<<"Begin on "<<sn[n-1]<<endl;
		for(i=n-1; i>0; i--)
		{
			if(dr[i]=="Left")
				cout<<"Right on ";
			if(dr[i]=="Right")
				cout<<"Left on ";
			if(dr[i]=="left")
				cout<<"right on ";
			if(dr[i]=="right")
				cout<<"left on ";
			cout<<sn[i-1]<<endl;
		}
		dr.clear();
		sn.clear();
		cout<<endl;
		tc--;

	}
	return 0;
}

kapildd wrote :

Consider this test case:

1  
3  
Begin on Sion Road  
Left on Matunga Road  
Right on Dadar Road  

the output according to my program is :
Begin on Dadar Road
Left on Matunga Road
Right on Sion Road
which if i am not wrong is correct…

@kapildd:
tried it … did not work… still getting “wrong answer”

my mistake … got it!! thanks!!

One thing is that you have separate lower case and upper case at different places. “left” in some places “Left” in other places. They should all be “Left”, and “Right” correspondingly.

I think your program is printing extra spaces. Use “on” while printing, rather than " on "!

I tried but it still saying wrong answer.

You must be kidding! See this http://www.codechef.com/viewsolution/1651033

1 Like