Last active
January 13, 2024 11:33
-
-
Save cvpfus/b8500365633f67a0bf39f675905394b2 to your computer and use it in GitHub Desktop.
This file contains 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.Text; | |
namespace FoobarPattern; | |
public class Foobar | |
{ | |
private int _patternLength; | |
private Dictionary<int, string> _patternDict = new(); | |
private StringBuilder _sb = new(); | |
public Foobar(int patternLength) | |
{ | |
CheckNegativeLength(patternLength); | |
_patternDict = new Dictionary<int, string>(){{3, "foo"}, {5, "bar"}}; | |
_patternLength = patternLength; | |
} | |
public Foobar(Dictionary<int, string> patternDict, int patternLength) | |
{ | |
CheckNegativeKey(patternDict); | |
CheckNegativeLength(patternLength); | |
_patternDict = patternDict; | |
_patternLength = patternLength; | |
} | |
public void SetPatternLength(int patternLength) | |
{ | |
CheckNegativeLength(patternLength); | |
_patternLength = patternLength; | |
} | |
public void Add(int key, string value) | |
{ | |
CheckNegativeKey(key); | |
_patternDict.Add(key, value); | |
} | |
public void Add(Dictionary<int, string> patternDict) | |
{ | |
CheckNegativeKey(patternDict); | |
foreach (var kvp in patternDict) | |
{ | |
_patternDict.Add(kvp.Key, kvp.Value); | |
} | |
} | |
public void Update(int key, string newValue) | |
{ | |
if (_patternDict.ContainsKey(key)) _patternDict[key] = newValue; | |
else throw new KeyNotFoundException(); | |
} | |
public void Delete(int key) | |
{ | |
if (_patternDict.ContainsKey(key)) _patternDict.Remove(key); | |
else throw new KeyNotFoundException(); | |
} | |
private void CheckNegativeKey(Dictionary<int, string> patternDict) | |
{ | |
foreach (var kvp in patternDict) | |
{ | |
if (kvp.Key < 0) throw new NegativeNumberException(); | |
} | |
} | |
private void CheckNegativeKey(int key) | |
{ | |
if (key < 0) throw new NegativeNumberException(); | |
} | |
private void CheckNegativeLength(int patternLength) | |
{ | |
if (patternLength < 0) throw new NegativeNumberException(); | |
} | |
public string GetPattern() | |
{ | |
_sb.Clear(); | |
for (int i = 0; i <= _patternLength; i++) | |
{ | |
string pattern = ""; | |
bool hasPattern = false; | |
if (i == 0) _sb.Append("0, "); | |
else | |
{ | |
foreach (var kvp in _patternDict) | |
{ | |
if (i % kvp.Key == 0) | |
{ | |
pattern += kvp.Value; | |
hasPattern = true; | |
} | |
} | |
if (!hasPattern) | |
{ | |
pattern = i.ToString(); | |
} | |
pattern += ", "; | |
_sb.Append(pattern); | |
} | |
} | |
return _sb.Replace(", ", ".", _sb.Length - 2, 2).ToString(); | |
} | |
} |
This file contains 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
namespace FoobarPattern; | |
public class NegativeNumberException : Exception | |
{ | |
public NegativeNumberException(string message = "Number can't be negative.") : base(message) | |
{ | |
} | |
} |
This file contains 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 FoobarPattern; | |
public class Program | |
{ | |
static void Main() | |
{ | |
Foobar foobar = new Foobar(new Dictionary<int, string>(){{3, "foo"}, {5, "bar"}}, 15); | |
Console.WriteLine(foobar.GetPattern()); | |
foobar.Add(7, "fizz"); | |
Console.WriteLine(foobar.GetPattern()); | |
foobar.Update(5, "buzz"); | |
Console.WriteLine(foobar.GetPattern()); | |
foobar.Delete(3); | |
Console.WriteLine(foobar.GetPattern()); | |
/* | |
This code produces the following output: | |
0, 1, 2, foo, 4, bar, foo, 7, 8, foo, bar, 11, foo, 13, 14, foobar. | |
0, 1, 2, foo, 4, bar, foo, fizz, 8, foo, bar, 11, foo, 13, fizz, foobar. | |
0, 1, 2, foo, 4, buzz, foo, fizz, 8, foo, buzz, 11, foo, 13, fizz, foobuzz. | |
0, 1, 2, 3, 4, buzz, 6, fizz, 8, 9, buzz, 11, 12, 13, fizz, buzz. | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sudah saya tambahkan mas, bisa dicek misal perlu saran lagi 😁