Skip to content

Instantly share code, notes, and snippets.

@unilecs
Created October 15, 2021 01:28
Show Gist options
  • Save unilecs/587aedf4cb17a00fb0c12d1227c371ee to your computer and use it in GitHub Desktop.
Save unilecs/587aedf4cb17a00fb0c12d1227c371ee to your computer and use it in GitHub Desktop.
Задача: Преобразование лицензионного ключа
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static string UpdateLicenseKeyFormat(string s, int k)
{
int len = s.Length;
int letterCount = s.Count(c => c != '-');
var result = new List<char>();
int curK = 0;
for (int i = len - 1; i >= 0; i--)
{
// время поставить '-'
if (curK == k && letterCount > 0)
{
result.Add('-');
curK = 0;
}
// иначе считаем текущий символ
if (s[i] != '-')
{
result.Add(char.ToUpper(s[i]));
curK++;
letterCount--;
}
}
result.Reverse();
return new string(result.ToArray());
}
public static void Main()
{
Console.WriteLine("UniLecs");
// tests
Console.WriteLine(UpdateLicenseKeyFormat("5F3Z-2e-9-w", 4)); // 5F3Z-2E9W
Console.WriteLine(UpdateLicenseKeyFormat("2-5g-3-J", 2)); // 2-5G-3J
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment