[Community Puzzle] Magic String

Coding Games and Programming Challenges to Code Better

Send your feedback or ask for help here!

Created by @java_coffee_cup,validated by @David_Augusto_Villa,@pin72 and @Westicles.
If you have any issues, feel free to ping them.

Hello @java_coffee_cup , @David_Augusto_Villa , @pin72 and @Mrs.GloriaZindlebock .
I am having an issue with validator 4 (all the IDE tests and other validators are all right). Would you be kind to give me this validator’s names’ list or something similar in order to find out what is wrong with my code ? Thanks in advance.

The validator is very alike the related test case. Your approach might be not generalized enough, trying to “hardcodedly” cover every scenario but missed out a few possibilities.
Try this shorter one

4
PR
PZ
PA
PP

1 Like

thanks a lot, I was indeed missing one case :+1: :hugs:

2 Likes

I pass all test cases but after submitting validator 8 fails. Can some please provide me with a custom test case to find where my code is not OK?

Hi,

This is similar to the validator 8:

Input
2
AA
AAA

Output
AA

Thanks! But my code produces AA, so apparently the validator is some other edge case.

The only difference with Quick Test 4 Validator is:

Summary

The validator uses the letter Z

Ah that explains, trying with ZZ I get an error.

import sys

class Sorter:

    def __init__(self):
        # n - number of participants
        # s - participants names
        n = int(input())
        self.names = []
        for i in range(n):
            s = input()
            self.names.append(s)
    
    def validate_input(self, names):
        # Validate the number of participants
        if not 2 <= len(names) <= 100:
            raise ValueError("Number of participants should be between 2 and 100.")

        # Validate the length of each name
        if not all(1 <= len(name) <= 30 for name in names):
            raise ValueError("Length of each name should be between 1 and 30 characters.")

    def sort_names(self):
        self.names.sort()
        print(self.names, file=sys.stderr)

    def split_names(self, a_list):
        half = len(a_list)//2
        return a_list[:half], a_list[half:]

    
    def find_magic_string(self):
        
        team_a, team_b = self.split_names(self.names)

        test_strings = team_a[-1] < team_b[0]
        if test_strings:
            # print(team_a[-1])
            return team_a[-1]
        else:
            return team_b[0]
            # print(team_b[0])

    def solve_sort(self):

        # Check if strings are ok
        self.validate_input(self.names)
        sorted_names = self.find_magic_string()

        magic_string = ''.join(sorted_names)
        print(magic_string, file=sys.stderr)
        print(magic_string)


sorter = Sorter()
sorter.sort_names()
sorter.solve_sort()

This code works for all but test 3, test 6 and test 9. What am I missing? I can’t see how, for example for test 3, I am supposed to :

Standard Output Stream:

[‘YZ’, ‘ZZ’]

YZ

YZ

Failure

Found:

YZ

Expected:

Z