+ 3
Code coach "camel and snake"
I can't for the life of me find a solution for this one. I tried both in python and c#, but it never manages to pass the final test which is locked, so I have no idea what I'm doing wrong!!
12 Réponses
+ 6
here's an alternative solution using more advanced methods.
Also, C# code can now be written without the boilerplates.
using System;
string camel = Console.ReadLine();
string snake =
camel.Substring(0,1).ToLower();
foreach(char c in camel.Substring(1)){
if(char.IsUpper(c))
snake += quot;_{char.ToLower(c)}";
else snake += c;
}
Console.WriteLine(snake);
+ 3
Remove the unnecessary conditions from the inner if statement.
if (i>0 && (char.IsLower(camel[i-1])||(i+1 < camel.Length && char.IsLower(camel[i+1]))))
Becomes
if (i>0)
Then it will pass.
+ 2
And we have no idea what code you tried. Show your code.
+ 2
OMG it worked!! Thank you so much đ„°đ„°
+ 1
Thank you! Didn't know about that, and it works great and it's simpler too. Thanks again đ
+ 1
camel = input()
snake = ""
for i in range(len(camel)):
if i == 0:
snake += camel[i].lower()
elif 'A' <= camel[i] <= 'Z':
snake += '_' + camel[i].lower()
else:
snake += camel[i]
print(snake)
+ 1
Oh this one is in python! It works better than mine, thank you so much đ
+ 1
Hiba
here is a simpler method: just replace all upper cases with _char.ToLower() then trim the leading '_' if the first letter happened to be uppercase.
using System;
string snake = string.Empty;
foreach(char c in Console.ReadLine()){
if(char.IsUpper(c))
snake += quot;_{char.ToLower(c)}";
else snake += c;
}
Console.WriteLine(snake.TrimStart('_'));
I adapted it from my Python solution:
print(''.join([f'_{c.lower()}' if c.isupper() else c for c in input()]).lstrip('_'))
0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sololearn
{
class Program
{
static void Main(string[] args)
{
string camel = Console.ReadLine();
string snake = "";
for (int i = 0; i< camel.Length; i++)
{
char c = camel[i];
if(char.IsUpper(c))
{
if(i>0 && (char.IsLower(camel[i-1])||(i+1 < camel.Length && char.IsLower(camel[i+1]))))
{
snake += "_";
}
snake += char.ToLower(c);
}
else
{
snake += c;
}
}
Console.WriteLine(snake);
}
}
}
0
Here, it's correct tho. It just fails the final test
0
Bob_Li
It works great! Thank you so much for your help đ



