Discussion About Sort the Strings

This is Discussion thread about Sort the Strings

Can some1 tell me what is wrong in this code: https://ideone.com/O6xgQ9

n = int(input())
num = 0
if n >= 1 and n <= (10 ** 5):
	arr = []
	for i in range(n):
		t = input()
		if len(t) >= 1 and len(t) <= 50:
			str = t.strip().split(" ")
			num = len(str)
			arr.append(str)
		else:
			print("Try Again")
			break

	str = input().strip().split(" ")
	key = int(str[0])

	def get_key(e):
		return e[key -1]
	
	def get_int_key(e):
		return int(e[key - 1])

	if key >= 1 and key <= num:

		reversed = str[1]
		comparison_type = str[2]

		if comparison_type == 'lexicographical':

			if reversed == 'true':
				arr.sort(key=get_key, reverse=True)

			else:
				arr.sort(key=get_key)

		if comparison_type == 'numeric':

			if reversed == 'true':
				arr.sort(key=get_int_key, reverse=True)

			else:
				arr.sort(key=get_int_key)

		for i in range(len(arr)):
			for j in range(len(arr[0])):
				if j == len(arr[0]) - 1:
					print(arr[i][j])
				else:
					print(arr[i][j], end=" ")
	else:
		print("Try Again")
else:
	print("Try Again")

Please tell me what’s wrong with this code. I am able to get the correct output but it’s not passing any of the 3 test cases.

n = int(input())
num = 0
if n >= 1 and n <= (10 ** 5):
	arr = []
	for i in range(n):
		t = input()
		if len(t) >= 1 and len(t) <= 50:
			str = t.strip().split(" ")
			num = len(str)
			arr.append(str)
		else:
			print("Try Again")
			break

	str = input().strip().split(" ")
	key = int(str[0])

	def get_key(e):
		return e[key -1]
	
	def get_int_key(e):
		return int(e[key - 1])

	if key >= 1 and key <= num:

		reversed = str[1]
		comparison_type = str[2]

		if comparison_type == 'lexicographical':

			if reversed == 'true':
				arr.sort(key=get_key, reverse=True)

			else:
				arr.sort(key=get_key)

			for i in range(len(arr)):
				for j in range(len(arr[0])):
					if j == len(arr[0]) - 1:
						print(arr[i][j])
					else:
						print(arr[i][j], end=" ")

		elif comparison_type == 'numeric':

			if reversed == 'true':
				arr.sort(key=get_int_key, reverse=True)

			else:
				arr.sort(key=get_int_key)

			for i in range(len(arr)):
				for j in range(len(arr[0])):
					if j == len(arr[0]) - 1:
						print(arr[i][j])
					else:
						print(arr[i][j], end=" ")

		else:
			print("Try Again")

		
	else:
		print("Try Again")
else:
	print("Try Again")

You have printed Try again so many times see the correct form of output. You are only supposed to give the desired output format to the Online Judge

[https://ide.codingblocks.com/s/451725]
What’s wrong in it It is not providing the correct output

Can somebody tell me what could be the possible issue in this?
#include<bits/stdc++.h>

#define pb push_back

using namespace std;

bool cmp_int(pair<int,string> &a, pair<int,string> &b)

{

stringstream ss;

ss<<a.second;

int n1;

ss>>n1;

ss<<b.second;

int n2;

ss>>n2;

// int n1=stoi(a.second);

// int n2=stoi(b.second);

return n1<n2;

}

bool cmp_str(pair<int,string> &a, pair<int,string> &b)

{

return a.second<b.second;

}

string token(string s, int key)

{

char * str=strtok((char*)s.c_str()," ");

int count=1;

while(count<=key)

{

str=strtok(NULL," ");

count++;

}

return (string)str;

}

int main() {

int n;

cin>>n;

vector v(n);

cin.get();

for(int i=0;i<n;++i)

{

getline(cin,v[i]);

}

int key;

string rev,type;

cin>>key>>rev>>type;

key–;

vector<pair<int,string> > check(n);

for(int i=0;i<n;++i)

{

check[i].first=i;

check[i].second=token(v[i],key);

}

if(type==“lexicographical”)

{

sort(check.begin(),check.end(),cmp_str);

}

else

{

sort(check.begin(),check.end(),cmp_int);

}

if(rev==“true”)

{

reverse(check.begin(),check.end());

}

for(int i=0;i<n;++i)

{

cout<<v[check[i].first]<<endl;

}

return 0;

}