TCS codevita 2020 [ Counting Palindromes ]


What i did is just brute force and find palidromes but it is wrong on test case itself ,

def ispali(s):
    r = s[::-1]
    return (r==s)

cnt=0

st , en = map(int,input().split(' '))
for i in range(st,en+1):
    s1 = str(i)
    for hr in range(0,24):
      s2 = (2-len(str(hr)))*'0' + str(hr)
      for mini in range(0,60):
          s3 = (2-len(str(mini)))*'0' + str(mini)
          for sec in range(0,60):
              s4 = (2-len(str(sec)))*'0' + str(sec)
              if(ispali(s1+s2+s3+s4)):
                  cnt+=1

print(cnt)

#approach 2

cnt=0
for i in range(st,en+1):
    for j in range(0,86400):
        x = j
        hr = x//3600
        x = x%3600
        mini = x//60
        x = x%60
        sec = x%60
        
        s2 = (2-len(str(hr)))*'0' + str(hr)
        s3 = (2-len(str(mini)))*'0' + str(mini)
        s4 = (2-len(str(sec)))*'0' + str(sec)
        
        if(ispali(str(i) + s2+s3+s4)):
            cnt+=1
print(cnt)


#both give same answer for
# 1 2 
# 288

Help anyone @everule1 @spaanse @galencolin @aryan12 @ay2306

I think, given test cases were incorrect , i got WA on private tcs

MY CODE 1:

bool pal(int n)
{
int t = n, r = 0;
while (t != 0)
{
r = r * 10;
r = r + t % 10;
t = t / 10;
}
if (n == r)
return true;
else
return false;
}
int digit(int n)
{ int cnt = 0;
while (n != 0)
{
cnt++;
n = n / 10;
}
return cnt;
}
bool isPalindrome(string str)
{
int l = 0;
int h = str.size() - 1;
while (h > l)
{
if (str[l++] != str[h–])
{

		return false;
	}
}
return true;

}
int main() {

int n1, n2, ans = 0;
cin >> n1 >> n2;

for (int i = n1; i <= n2; i++)
{
	for (int j = 0; j <= 235959; j++)
	{

		if (i == 0)
		{	string s = "";
			int g;
			for (g = 0; g < 7 - digit(j); g++)
			{
				s += "0";
			}
			s += to_string(j);
			if (isPalindrome(s))
				ans++;
		}
		if ((i != 0) && pal(i * 1000000 + j))
			ans++;
	}
}
cout << ans;

}

CODE 2:
bool isPal(string str)
{
ll l = 0;
ll h = str.size() - 1;
while (h > l)
{
if (str[l++] != str[h–])
{

		return false;
	}
}
return true;

}
int main() {
#ifndef ONLINE_JUDGE
freopen(“input.txt”, “r”, stdin);
freopen(“output.txt”, “w”, stdout);
#endif
ll n1, n2, ans = 0;
cin >> n1 >> n2;

for (ll i = n1; i <= n2; i++)
{
	for (ll h = 0; h <= 23; h++)
	{
		for (ll m = 0; m <= 59; m++)
		{
			for (ll s = 0; s <= 59; s++)
			{
				string check = to_string(i);
				if (h <= 9)
					check += "0" + to_string(h);
				else
					check += to_string(h);
				if (m <= 9)
					check += "0" + to_string(m);
				else
					check += to_string(m);
				if (s <= 9)
					check += "0" + to_string(s);
				else
					check += to_string(s);
				if (isPal(check))
					ans++;
			}
		}
	}
}
cout << ans << endl;

}

well acc. to me my code 2 is correct , but it passes none of the tcs
PLEASE HELP…
i have implemented it in two ways one passes public and none passes private tcs
‘’’

Give me full code.

LOL , Ur code give 288 on test case 1 and u said it is correct .

now have a look bro

Can you please share your logic

My code also gave 288 and 432 as answers. Any info about this from tcs?

1 Like

n1,n2=[int(x) for x in input().split()]
a=1000000

c=0
while(a//1000000<=n2-n1+1):
a+=1
if(a%100>29):
a+=100
a-=30

    if((a//100)%100>59):
        a+=10000
        a-=6000
        if((a//10000)%100>23):
            a+=1000000
            a-=240000
s=str(a)
if (s[::-1]==s):
    c+=1
    print(s)

getting 288 leave the case for first nno as zero
how 432

They had wrong test cases for this problem even the sample outputs were wrong, 288 and 432 were correct answers. There’s something wrong from their side itself.

3 Likes

Read this Tough TCS Codevita: Zone 2 - #42 by mk1969 bro

1 Like

Have you mailed them regarding this?

the ques is wrong ,same case with me

So, the test cases are incorrect right?

According to me, test cases were wrong
some people were saying that their solution passes tcs for min and sec for loop till 97.
But i think on earth , time in min and sec can go upto 59.

Yes , I tried this question in 2 ways both have same answer and they failed on given test case itself .

Bro Test-Case is wrong
I also got this question and that too as my 1st question in TCS CodeVita :worried:
I wasted a lot of time in it.
TCS should take care of such things

They don’t , instead of clearifying , one user mailed them about this issue and they replied that question and sample test cases are correct many users solved this please check your code . :man_shrugging:

@goswamiji
Will You please share their mail id ?
I want to mail for wrong test cases.

@ashish_kaur please give their mail ID.

Yup Test cases were wrong.
Any idea when Results for Zone 2 will be out ?