Goats are interesting creatures. They are incredibly durable and can fall from great heights, be str…
Click here to read the complete problem statement.
If you need help solving this problem, mention your approach and ask specific questions. Please avoid sharing your code and asking the Community to figure out “what’s wrong”.
#include<iostream>
using namespace std;
int main()
{
int n;
string s[20];
cin >> n;
if (n < 20)
{
for (int i = 0; i < n; i++)
{
string q;
cin >> q;
int f = q.length();
if (f <= 20)
{
if (q.at(0) == 'b')
{
int z = 0;
int k = 0;
for (int i = 1; i < f; i++)
{
if (q[i] == 'a')
{
z++;
}
else if (q[i] != 'a')
{
k++;
}
}
if (z >= 1 && k == 0)
{
if (z == 1)
{
for (int i = 0; i <= f; i++)
{
if ((i+1) == f)
{
q.resize(f+1);
q.at(i+1) = 'a';
}
}
s[i] = q;
}
else if (z > 1)
{
if(z%2==1)
{
f - 1;
for (int i = 0; i < f; i++)
{
if (i == (f - 1))
{
q[i] = NULL;
}
}
s[i] = q;
}
else
{
s[i]=q;
}
}
}
}
}
}
int xx = 0;
int maxlen = 0;
for (int i = 0; i < n; i++)
{
if (s[i].empty()==false)
{
xx = s[i].length();
if (xx > maxlen)
{
maxlen = xx;
}
}
}
int maxdev = 20 - maxlen;
if (maxdev >= 0)
{
maxdev = maxdev / 2;
for (int i = 0; i < n; i++)
{
int size = 0;
size = s[i].length();
int div = 20 - size;
if (div >= 0)
{
div = div / 2;
div = div - maxdev;
for (int i = 0; i < div; i++)
{
cout << " ";
}
cout << s[i] << endl;
}
else
{
cout<<s[i];
}
}
}
}
}
times = int(input())
lis = list()
for i in range(1,times +1):
string = input()
index = string.rfind("a")
if index%2 !=0:
nstring = string[0:index]
nindex = nstring.rfind("a")
if(nindex == -1):
lis.append("ba")
else:
lis.append(nstring)
else:
lis.append(string)
maxl = len(max(lis,key=len))
for x in lis:
print(x.center(maxl))
my code gives right answer in my editor but does not pass the tests. can anyone explain where am I moving wrong?
You’re using the center() method to align the string.
But, the Python center() method adds characters to both ends of a string to align it to the center. So no matter which character you use, the final string will always have trailing fill characters (change the character parameter to something else to visualize easily).
So, you have to do the aligning part manually, or somehow remove the trailing spaces.
n=int(input())
if 0<n<20:
listOfBaa= []
maxLen=0
for i in range(n):
x =input()
if(maxLen<len(x)):
maxLen=len(x)
listOfBaa.append(x)
elif(len(x)<=20):
listOfBaa.append(x)
for i in range(len(listOfBaa)):
baa=list(listOfBaa[i])
if len(baa)==2:
baa.append('a')
elif(len(baa)%2==0):
baa.pop(len(baa)-1)
outputResult=''.join(baa)
print(' '*(int(maxLen/2)-int(len(outputResult)/2))+outputResult)
Looks like you have solved the problem. Nice work!
When a problem statement gives you a constraint around the input, it is there to help you understand how you should be solving the problem and how the test cases have been prepared. You do not need to validate it yourself in your code. You can assume that the constraints given will be maintained in the test case.
In the unfortunate circumstance where the constraints are incorrect, it is upon the author of the problem to correct it.