Created
April 13, 2022 11:02
-
-
Save ebresafegaga/611dbc52fb3f128adf28dcc379d5faa7 to your computer and use it in GitHub Desktop.
MS Codility Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace interview { | |
class Program { | |
// check if the last 2 chars with c are the same | |
public static bool predicate (string s, char c) { | |
if (s.Length < 2) { | |
return true; | |
} else { | |
var l = new List<char> {s[s.Length - 1],s[s.Length -2]}; | |
return !l.TrueForAll (elem => elem == c); | |
} | |
} | |
public static string append (string s, char c) { | |
var sl = s.ToList (); | |
sl.Add (c); | |
return new String (sl.ToArray ()); | |
} | |
public static List<string> helper (string prev, int A, int B, int C) { | |
// 1. we can generate all possible solutions | |
// 2. or we can backtrack | |
// three cases: pick A, B or C (iff not zero, obviously) | |
var table = new Dictionary<char, int> { | |
{'A', A}, | |
{'B', B}, | |
{'C', C} | |
}; | |
var al = new List<string> (); | |
var bl = new List<string> (); | |
var cl = new List<string> (); | |
// This is just a fold | |
foreach (var pair in table) { | |
var c = pair.Key; | |
var n = pair.Value; | |
if (n > 0) { | |
if (c == 'A') { | |
if (predicate (prev, char.ToLower (c))) { | |
var a = append (prev, 'a'); | |
al = helper (a, A-1,B,C); | |
// sol.Concat (s); | |
} | |
} else if (c == 'B') { | |
if (predicate (prev, char.ToLower (c))) { | |
var a = append (prev, 'b'); | |
bl = helper (a, A,B-1,C); | |
// sol.Concat (s); | |
} | |
} else if (c == 'C') { | |
if (predicate (prev, char.ToLower (c))) { | |
var a = append (prev, 'c'); | |
cl = helper (a, A,B,C-1); | |
// sol.Concat (s); | |
} | |
} | |
} | |
} | |
var sol = al.Concat (bl).Concat (cl).ToList (); | |
if (sol.Count == 0) { | |
sol.Add (prev); | |
} | |
return sol; | |
} | |
public static string solution (int A, int B, int C) { | |
// Generate list of all possible solutions | |
var l = helper ("", A, B, C); | |
if (l.Count == 0) { | |
return ""; | |
} else { | |
return l[0]; | |
} | |
} | |
static void Main(string[] args) { | |
Console.WriteLine (solution (7, 3, 3)); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
namespace interview { | |
class Program { | |
public static char Flip (char c) { | |
if (char.IsUpper (c)) { | |
return char.ToLower (c); | |
} else { | |
return char.ToUpper (c); | |
} | |
} | |
public static string solution(string S) { | |
// create a maping form letters to numbers | |
var seen = new List<char> (); | |
// create a list of letters | |
var ds = new List<char> (); | |
// once we find second put in a list | |
foreach (var c in S) { | |
if (seen.Contains (Flip (c)) ) { | |
ds.Add (char.ToUpper (c)); | |
// update seen | |
seen.Add (c); | |
} else { | |
seen.Add (c); | |
} | |
} | |
// find the minimun from the list | |
if (ds.Count == 0) { | |
return "NO"; | |
} else { | |
return ds.Max ().ToString (); | |
} | |
} | |
static void Main(string[] args) { | |
Console.WriteLine(""); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace interview { | |
class Program { | |
public int[] solution(int N) { | |
// two cases | |
// n is even | |
// n is odd | |
var sol = new List<int> (); | |
if (N % 2 == 0) { | |
// even | |
var count = 1; | |
while (N > 0) { | |
sol.Add (count); | |
sol.Add (-count); | |
count++; | |
N = N-2; | |
} | |
} else { | |
// odd | |
var count = 1; | |
sol.Add (0); | |
N = N - 1; | |
while (N > 0) { | |
sol.Add (count); | |
sol.Add (-count); | |
count++; | |
N = N-2; | |
} | |
} | |
return sol.ToArray (); | |
} | |
static void Main(string[] args) { | |
Console.WriteLine(""); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment