C# Fundamentals - Text Processing Ex. 1 - Valid Usernames
Hello everyone.
I'm trying to finish the exercise Valid Usernames here: https://judge.softuni.bg/Contests/Compete/Index/1217#0
However, the test only give me 40 for this exercise.
Both Zero tests go through successfully, so good luck trying to figure out what the issue is.
Problem description
Write a program that reads user names on a single line (joined by ", ") and prints all valid usernames.
A valid username is:
-
Has length between 3 and 16 characters
-
Contains only letters, numbers, hyphens and underscores
-
Has no redundant symbols before, after or in between
Examples
Input |
Output |
sh, too_long_username, !lleg@l ch@rs, jeffbutt |
jeffbutt |
Jeff, john45, ab, cd, peter-ivanov, @smith |
Jeff John45 peter-ivanov |
This is my code.
The issue is most likely with redundant symbols, but what does that mean here in the end?
using System;
public class Program
{
public static void Main()
{
string [] userNames = Console.ReadLine().Split(',');
string userName;
foreach (string un in userNames)
{
//Remove the whitespaces
userName = un.Replace(" ", string.Empty);
CheckIfValid(userName);
}
}
public static void CheckIfValid(string userName)
{
bool isValid = true;
bool containsHyphen = false;
bool containsUnderscore = false;
if (userName.Length >= 3 && userName.Length <= 16)
{
isValid = true;
} else
{
isValid = false;
}
if (isValid)
{
foreach (char ch in userName)
{
if (Char.IsLetterOrDigit(ch))
{
//Passed
}
else if (ch == '-')
{
//Passed
if (containsHyphen)
{
isValid = false;
break;
}
containsHyphen = true;
} else if (ch == '_') {
if (containsUnderscore)
{
isValid = false;
break;
}
containsUnderscore = true;
}
else
{
//Failed
isValid = false;
break;
}
}
}
if (isValid)
{
Console.WriteLine(userName);
}
}
}
On a totally unrelated note, why do we even have Judge system in place?
It's the absolute worst of the 3 environments I've had to use.
When I was studying in Finland I was using Test My Code which told you directly "Oh, try with the input x = 10 and y = -5, it seems you´re getting the wrong answer here" and this was super convenient because it told you what to watch out for.
It performed a lot of checks for each situation you might get wrong with your code.
Here the Judge system just tells you "Oh you have a mistake. You won´t learn anything here because I won´t tell you what the mistake is".
It´s ridiculous to me how you keep using such a primitive system, not to mention it´s very counterproductive for learning.
We are students, we cannot be expected to do this perfectly.
Sure it's a good lesson on life and how we cannot expect to always be guided, but this is a school.
You might us well just tell us to write the code on a piece of paper and tear it up if we get it wrong, that's how useful the Judge system is.
Either way, thank you for your time!