Problem: Take a string as an input (combination of alphanumeric and special characters) from the user.
If the number of special character in the given string is even, then we have to print the first even digit and next odd digits alternatively in the same order as they are present in the string.
If the number of special character in the given string is odd, then we have to print the first odd digit and next even digits alternatively in the same order as they are present in the string.
Input format: The first line contains an integer denoting the number of testcases T. Next T lines contain a the string as input.
(NOTE: The actual problem didn’t have the number of testcases as input. But for your better convenience we have worked with testcases. Omit the testcase input line to get the actual format.)
Sample Input:
2
A5w8@k7!l23n69
#bn7856!@kn2n65jbnj482375
Sample Output:
8527639
7856523674582
Explanation: For the first string, the number of special character in the string is two (@, ! ), which is an even number, so the output will start from the even numeric character present in the string.
Here, ‘8’ is the first even number in the string, then the first odd number present is ‘5’. Then the second even number is ‘2’, the second odd number is ‘7’, and so on
Solution: We strongly recommend you to try the problem first before moving to the solution.
Python
# Code to understand Odd Even Alternating problem group in Python
# www.codewindow.in
def solve(string):
even=[]
odd=[]
special_char=0
for ch in string:
if(ch.isalnum() == False):
special_char+=1
elif(ch.isdigit()):
if int(ch)%2==0:
even.append(ch)
else:
odd.append(ch)
if(special_char%2!=0):
odd, even=even, odd
even_len=len(even)
odd_len=len(odd)
m=max(even_len, odd_len)
ev=0
ox=0
for i in range(m):
if(ev!=even_len):
print(even[ev], end='')
ev+=1
if(ox!=odd_len):
print(odd[ox], end='')
ox+=1
print()
# Driver code
t=int(input())
while(t):
s=input()
solve(s)
t-=1
Input:
2
A5w8@k7!l23n69
#bn7856!@kn2n65jbnj482375
Output:
8527639
7856523674582
Follow Us
You Missed
- Unlocking Innovation and Diversity: Accenture HackDiva Empowers Women in Tech with Cutting-Edge Solutions – codewindow.in
- QA Engineer Opportunities at Siemens Company: Apply Now – codewindow.in
- QA Engineer Opportunities at Siemens Company: Apply Now – codewindow.in
- Software Engineer Positions at Siemens Company: Apply Now – codewindow.in
- Cloud Engineer II Opportunities at Insight Company: Apply Now – codewindow.in
- Shape Your Career: Assistant Engineer Opportunities at Jindal Company – codewindow.in
- Shape Your Future: Executive Opportunities at Jindal Company – cdewindow.in
- Associate Engineer, Software Development at Ingram: Apply Now – codewindow.in
- Jade Company’s UI/UX Development Engineer Opportunities – Apply Now – codewindow.in
- Transform Your Career with S&P Global: Apply for the Software Development Engineer Role and Lead the Future of Financial Technology Innovation – codewindow.in
- Unlock Your Potential at Accenture as an Associate Software Engineer – Elevate Your Career with Innovation and Excellence – codewindow.in
- Accelerate Your Career: Join NVIDIA’s Elite Software Engineering Internship Program and Shape the Future of Technology – codewindow.in
- C Programming Interview Questions – codewindow.in
- Lead the Way in Analytics: Specialist Position at Razorpay – codewindow.in
- Innovate with Cyient: Junior Software Engineer Wanted – codewindow.in
- Innovate with Volvo: Associate Software Engineer Wanted – codewindow.in
- Lead the Tech Revolution: Full Stack Developer at Unisys – codewindow.in
- Software Engineer at ABB: Unlock Innovation and Shape the Future – codewindow.in
- IBM Associate Systems Engineer Job: Boost Your Career with a Leading Technology Giant – codewindow.in
- Make Your Mark in Android Development: Join Concentrix – codewindow.in
- Infosys is Growing: Field Services Developer Role Now Open – codewindow.in
- Start Your IT Career Journey with Amazon: IT Services Support Associate I Opportunity – codewindow.in
- Shape the Future of Web: Front-End Software Engineer Opportunity at Google Cloud – codewindow.in
- Barclays QA Team Expands: QA Analyst Role Now Open- codewindow.in
- Eurofins QA Team Grows: Test Engineer Role Now Open – codewindow.in
- Exciting Opportunity: Java Spring Boot Senior Developer Role at Infosys – codewindow.in
- Unlock Your Potential at Nokia: Software Engineer Opportunities Await – codewindow.in
- Join Microsoft’s World-Class Team as a Software Engineer and Shape the Future of Technology – codewindow.in
- Virtusa is Seeking Talented React JS Developers to Drive Digital Excellence – codewindow.in
- Join IBM Dynamic Team as a Full Stack Developer and Shape the Future – codewindow.in
- EY Welcomes Aspiring AI/ML Interns to Unlock the Future of -codewindow.in
- Exciting Opportunity: Project Engineer at Rockwell Automation- codewindow.in