Skip to content

Instantly share code, notes, and snippets.

@AlexRasch
Created October 17, 2023 13:53
Show Gist options
  • Save AlexRasch/e15b547cd7c39e39518c30101679c3ce to your computer and use it in GitHub Desktop.
Save AlexRasch/e15b547cd7c39e39518c30101679c3ce to your computer and use it in GitHub Desktop.
Kod test
using System.Runtime;
namespace CodeTest
{
internal class Program
{
/*
* Complete the 'countSubstrings' function below.
* The function is expected to return an INTEGER.
* The function accepts STRING input_str as parameter.
*/
public static int countSubstrings(string input_str)
{
int iSum, iSubstringLength, iReturnValue = 0;
// Get Substring
List<string> listStrings = fnSubString(input_str, input_str.Length);
//Parallel.ForEach(listStrings, str =>
//{
// iSum = fnCharMappedToNumber(str);
// iSubstringLength = str.Length;
// if (iSum % iSubstringLength == 0)
// iReturnValue++;
//
//});
// Get sum
foreach (string str in listStrings) {
// Sum
iSum = fnCharMappedToNumber(str);
// Length
iSubstringLength = str.Length;
// Is divisible
if (iSum % iSubstringLength == 0)
{
iReturnValue++;
}
//Console.WriteLine($"str: {str} Sum: {iSum} Legnth: {iSubstringLength} Mod:{iSum % iSubstringLength}");
}
return iReturnValue;
}
static List<string> fnSubString(string str, int n)
{
List<string> listStrings = new List<string>();
string strTemp = "";
// Pick starting point
for (int len = 1; len <= n;len++)
{
// Pick ending point
for (int i = 0;i <= n - len; i++)
{
int j = i + len - 1;
for (int k = i; k <= j; k++)
{
//Console.Write(str[k]);
strTemp += str[k];
}
listStrings.Add(strTemp);
strTemp = "";
//Console.WriteLine();
}
}
return listStrings;
}
public static int fnCharMappedToNumber(string strInput)
{
int iReturnValue = 0;
foreach (char cChar in strInput)
{
//if (!char.IsLetter(cChar))
//break;
char cChar2 = char.ToLower(cChar);
switch (cChar2)
{
case 'a':
case 'b':
iReturnValue += 1;
break;
case 'c':
case 'd':
case 'e':
iReturnValue += 2;
break;
case 'f':
case 'g':
case 'h':
iReturnValue += 3;
break;
case 'i':
case 'j':
case 'k':
iReturnValue += 4;
break;
case 'l':
case 'm':
case 'n':
iReturnValue += 5;
break;
case 'o':
case 'p':
case 'q':
iReturnValue += 6;
break;
case 'r':
case 's':
case 't':
iReturnValue += 7;
break;
case 'u':
case 'v':
case 'w':
iReturnValue += 8;
break;
case 'x':
case 'y':
case 'z':
iReturnValue += 9;
break;
default:
break;
}
}
return iReturnValue;
}
static void Main(string[] args)
{
Console.WriteLine($"{countSubstrings("asdf")}");
//Console.WriteLine($"{countSubstrings("a")}");
//Console.WriteLine($"{countSubstrings("aux")}");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment