├── .gitattributes ├── .gitignore ├── .vscode ├── launch.json └── tasks.json ├── AbsoluteDifference ├── AbsoluteDifference.csproj ├── Program.cs └── README.md ├── AnagramDetection ├── AnagramDetection.csproj ├── Program.cs └── README.md ├── ArrayComparison ├── ArrayComparison.csproj ├── Program.cs └── README.md ├── FizzBuzzInTDD ├── FizzBuzzInTDD.Test │ ├── FizzBuzzInTDD.Test.csproj │ └── FizzBuzzTest.cs ├── FizzBuzzInTDD │ ├── FizzBuzzHelper.cs │ ├── FizzBuzzInTDD.csproj │ └── Program.cs └── README.md ├── LeftRotate ├── LeftRotate.csproj ├── Program.cs └── README.md ├── PalindromeDetection ├── PalindromeDetection.csproj ├── Program.cs └── README.md ├── PrimeNumberChecker ├── PrimeNumberChecker.csproj ├── Program.cs └── README.md ├── README.md ├── ReverseString ├── Program.cs ├── README.md └── ReverseString.csproj ├── StringCompression ├── Program.cs ├── README.md └── StringCompression.csproj ├── WinnerOfAnElection ├── Program.cs ├── README.md └── WinnerOfAnElection.csproj └── algorithm-playground.sln /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | [Xx]64/ 19 | [Xx]86/ 20 | [Bb]uild/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opendb 80 | *.opensdf 81 | *.sdf 82 | *.cachefile 83 | *.VC.db 84 | 85 | # Visual Studio profiler 86 | *.psess 87 | *.vsp 88 | *.vspx 89 | *.sap 90 | 91 | # TFS 2012 Local Workspace 92 | $tf/ 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | *.DotSettings.user 101 | 102 | # JustCode is a .NET coding add-in 103 | .JustCode 104 | 105 | # TeamCity is a build add-in 106 | _TeamCity* 107 | 108 | # DotCover is a Code Coverage Tool 109 | *.dotCover 110 | 111 | # NCrunch 112 | _NCrunch_* 113 | .*crunch*.local.xml 114 | nCrunchTemp_* 115 | 116 | # MightyMoose 117 | *.mm.* 118 | AutoTest.Net/ 119 | 120 | # Web workbench (sass) 121 | .sass-cache/ 122 | 123 | # Installshield output folder 124 | [Ee]xpress/ 125 | 126 | # DocProject is a documentation generator add-in 127 | DocProject/buildhelp/ 128 | DocProject/Help/*.HxT 129 | DocProject/Help/*.HxC 130 | DocProject/Help/*.hhc 131 | DocProject/Help/*.hhk 132 | DocProject/Help/*.hhp 133 | DocProject/Help/Html2 134 | DocProject/Help/html 135 | 136 | # Click-Once directory 137 | publish/ 138 | 139 | # Publish Web Output 140 | *.[Pp]ublish.xml 141 | *.azurePubxml 142 | 143 | # TODO: Un-comment the next line if you do not want to checkin 144 | # your web deploy settings because they may include unencrypted 145 | # passwords 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # NuGet Packages 150 | *.nupkg 151 | # The packages folder can be ignored because of Package Restore 152 | **/packages/* 153 | # except build/, which is used as an MSBuild target. 154 | !**/packages/build/ 155 | # Uncomment if necessary however generally it will be regenerated when needed 156 | #!**/packages/repositories.config 157 | # NuGet v3's project.json files produces more ignoreable files 158 | *.nuget.props 159 | *.nuget.targets 160 | 161 | # Microsoft Azure Build Output 162 | csx/ 163 | *.build.csdef 164 | 165 | # Microsoft Azure Emulator 166 | ecf/ 167 | rcf/ 168 | 169 | # Windows Store app package directory 170 | AppPackages/ 171 | BundleArtifacts/ 172 | 173 | # Visual Studio cache files 174 | # files ending in .cache can be ignored 175 | *.[Cc]ache 176 | # but keep track of directories ending in .cache 177 | !*.[Cc]ache/ 178 | 179 | # Others 180 | ClientBin/ 181 | [Ss]tyle[Cc]op.* 182 | ~$* 183 | *~ 184 | *.dbmdl 185 | *.dbproj.schemaview 186 | *.pfx 187 | *.publishsettings 188 | node_modules/ 189 | orleans.codegen.cs 190 | 191 | # RIA/Silverlight projects 192 | Generated_Code/ 193 | 194 | # Backup & report files from converting an old project file 195 | # to a newer Visual Studio version. Backup files are not needed, 196 | # because we have git ;-) 197 | _UpgradeReport_Files/ 198 | Backup*/ 199 | UpgradeLog*.XML 200 | UpgradeLog*.htm 201 | 202 | # SQL Server files 203 | *.mdf 204 | *.ldf 205 | 206 | # Business Intelligence projects 207 | *.rdl.data 208 | *.bim.layout 209 | *.bim_*.settings 210 | 211 | # Microsoft Fakes 212 | FakesAssemblies/ 213 | 214 | # GhostDoc plugin setting file 215 | *.GhostDoc.xml 216 | 217 | # Node.js Tools for Visual Studio 218 | .ntvs_analysis.dat 219 | 220 | # Visual Studio 6 build log 221 | *.plg 222 | 223 | # Visual Studio 6 workspace options file 224 | *.opt 225 | 226 | # Visual Studio LightSwitch build output 227 | **/*.HTMLClient/GeneratedArtifacts 228 | **/*.DesktopClient/GeneratedArtifacts 229 | **/*.DesktopClient/ModelManifest.xml 230 | **/*.Server/GeneratedArtifacts 231 | **/*.Server/ModelManifest.xml 232 | _Pvt_Extensions 233 | 234 | # LightSwitch generated files 235 | GeneratedArtifacts/ 236 | ModelManifest.xml 237 | 238 | # Paket dependency manager 239 | .paket/paket.exe 240 | 241 | # FAKE - F# Make 242 | .fake/ 243 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to find out which attributes exist for C# debugging 3 | // Use hover for the description of the existing attributes 4 | // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": ".NET Core Launch (console)", 9 | "type": "coreclr", 10 | "request": "launch", 11 | "preLaunchTask": "build", 12 | // If you have changed target frameworks, make sure to update the program path. 13 | "program": "${workspaceFolder}/PalindromeDetection/bin/Debug/netcoreapp2.2/PalindromeDetection.dll", 14 | "args": [], 15 | "cwd": "${workspaceFolder}/PalindromeDetection", 16 | // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window 17 | "console": "internalConsole", 18 | "stopAtEntry": false, 19 | "internalConsoleOptions": "openOnSessionStart" 20 | }, 21 | { 22 | "name": ".NET Core Attach", 23 | "type": "coreclr", 24 | "request": "attach", 25 | "processId": "${command:pickProcess}" 26 | } 27 | ,] 28 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "label": "build", 6 | "command": "dotnet", 7 | "type": "process", 8 | "args": [ 9 | "build", 10 | "${workspaceFolder}/PalindromeDetection/PalindromeDetection.csproj" 11 | ], 12 | "problemMatcher": "$msCompile" 13 | } 14 | ] 15 | } -------------------------------------------------------------------------------- /AbsoluteDifference/AbsoluteDifference.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /AbsoluteDifference/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AbsoluteDifference 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | int n = int.Parse(Console.ReadLine()); 10 | int differences = GetAbsoluteDifference(n); 11 | 12 | Console.WriteLine($"Absolute differences is: {differences}"); 13 | } 14 | 15 | static int GetAbsoluteDifference(int n) 16 | { 17 | const int x = 43; 18 | 19 | if (n > x) 20 | { 21 | return (n - x) * 2; 22 | } 23 | 24 | return x - n; 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /AbsoluteDifference/README.md: -------------------------------------------------------------------------------- 1 | #### Problem: 2 | 3 | Write a short program to get the absolute difference between n and 43. If n is greater than 43 return double the absolute difference. 4 | 5 | E.g: n is 15 = 43-15 = 28 6 | -------------------------------------------------------------------------------- /AnagramDetection/AnagramDetection.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /AnagramDetection/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace AnagramDetection 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | string input1 = "abcd"; 11 | string input2 = "dabc"; 12 | 13 | bool result = AreAnagram(input1, input2); 14 | 15 | Console.WriteLine($"{input1} and {input2} are anagram: {result}"); 16 | } 17 | 18 | static bool AreAnagram(string input1, string input2) 19 | { 20 | if (input1.Length != input2.Length) 21 | { 22 | return false; 23 | } 24 | 25 | input1 = string.Concat(input1.OrderBy(x => x)); // O(N log N) 26 | input2 = string.Concat(input2.OrderBy(x => x)); 27 | 28 | // O(n) 29 | for (int i = 0; i < input1.Length; i++) 30 | { 31 | if (input1[i] != input2[i]) 32 | { 33 | return false; 34 | } 35 | } 36 | 37 | return true; 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /AnagramDetection/README.md: -------------------------------------------------------------------------------- 1 | #### Problem: 2 | 3 | Write a short program to check whether two given strings are anagram of each other or not. 4 | 5 | E.g: "abcd" and "dabc" are anagram of each other. -------------------------------------------------------------------------------- /ArrayComparison/ArrayComparison.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /ArrayComparison/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace ArrayComparison 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | int[] a = new int[] { 3, 1, 2, 4 }; 11 | int[] b = new int[] { 1, 2, 5 }; 12 | 13 | int[] output = GetCommonValues(a, b); 14 | 15 | Console.WriteLine(string.Join(',', Array.ConvertAll(output, c => c.ToString()))); 16 | } 17 | 18 | static int[] GetCommonValues(int[] a, int[] b) 19 | { 20 | // Time complexity is O(n^2) 21 | int[] output = new int[a.Length]; // It can be List 22 | int counter = 0; 23 | 24 | for (int i = 0; i < a.Length; i++) 25 | { 26 | for (int y = 0; y < b.Length; y++) 27 | { 28 | if (b[y] == a[i]) 29 | { 30 | output[counter] = b[y]; 31 | counter++; 32 | } 33 | } 34 | } 35 | 36 | Array.Resize(ref output, counter); 37 | 38 | return output; 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /ArrayComparison/README.md: -------------------------------------------------------------------------------- 1 | #### Problem: 2 | 3 | Write a short program that compares two arrays and creates another array which holds the common values between two array. 4 | 5 | E.g: A={3,1,2,4} B={1,2,5} and C={1,2} -------------------------------------------------------------------------------- /FizzBuzzInTDD/FizzBuzzInTDD.Test/FizzBuzzInTDD.Test.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.2 5 | 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /FizzBuzzInTDD/FizzBuzzInTDD.Test/FizzBuzzTest.cs: -------------------------------------------------------------------------------- 1 | using FizzBuzzInTDD; 2 | using NUnit.Framework; 3 | 4 | namespace Tests 5 | { 6 | [TestFixture] 7 | public class Tests 8 | { 9 | [Test] 10 | public void IsDivisableThree_WhenEnteredANumberThatDivisableByThree_ShouldReturnTrue() 11 | { 12 | //Arrange 13 | FizzBuzzHelper fizzBuzzHelper = new FizzBuzzHelper(); 14 | int number = 3; 15 | 16 | //Act 17 | bool result = fizzBuzzHelper.IsDivisableThree(number); 18 | 19 | //Assert 20 | Assert.True(result); 21 | } 22 | 23 | [Test] 24 | public void IsDivisableThree_WhenEnteredANumberThatNotDivisableByThree_ShouldReturnFalse() 25 | { 26 | //Arrange 27 | FizzBuzzHelper fizzBuzzHelper = new FizzBuzzHelper(); 28 | int number = 5; 29 | 30 | //Act 31 | bool result = fizzBuzzHelper.IsDivisableThree(number); 32 | 33 | //Assert 34 | Assert.False(result); 35 | } 36 | 37 | [Test] 38 | public void IsDivisableFive_WhenEnteredANumberThatDivisableByFive_ShouldReturnTrue() 39 | { 40 | //Arrange 41 | FizzBuzzHelper fizzBuzzHelper = new FizzBuzzHelper(); 42 | int number = 5; 43 | 44 | //Act 45 | bool result = fizzBuzzHelper.IsDivisableFive(number); 46 | 47 | //Assert 48 | Assert.True(result); 49 | } 50 | 51 | [Test] 52 | public void IsDivisableFive_WhenEnteredANotNumberThatDivisableByFive_ShouldReturnFalse() 53 | { 54 | //Arrange 55 | FizzBuzzHelper fizzBuzzHelper = new FizzBuzzHelper(); 56 | int number = 3; 57 | 58 | //Act 59 | bool result = fizzBuzzHelper.IsDivisableFive(number); 60 | 61 | //Assert 62 | Assert.False(result); 63 | } 64 | 65 | [Test] 66 | public void GetFizzBuzzByNumber_WhenEnteredANumberThatDivisableByThree_ShouldReturnFizz() 67 | { 68 | //Arrange 69 | FizzBuzzHelper fizzBuzzHelper = new FizzBuzzHelper(); 70 | int number = 3; 71 | string expected = "Fizz"; 72 | 73 | //Act 74 | string result = fizzBuzzHelper.GetFizzBuzzByNumber(number); 75 | 76 | //Assert 77 | Assert.AreEqual(expected, result); 78 | } 79 | 80 | [Test] 81 | public void GetFizzBuzzByNumber_WhenEnteredANumberThatDivisableByFive_ShouldReturnBuzz() 82 | { 83 | //Arrange 84 | FizzBuzzHelper fizzBuzzHelper = new FizzBuzzHelper(); 85 | int number = 5; 86 | string expected = "Buzz"; 87 | 88 | //Act 89 | string result = fizzBuzzHelper.GetFizzBuzzByNumber(number); 90 | 91 | //Assert 92 | Assert.AreEqual(expected, result); 93 | } 94 | 95 | [Test] 96 | public void GetFizzBuzzByNumber_WhenEnteredANumberThatDivisableByThreeAndFive_ShouldReturnFizzBuzz() 97 | { 98 | //Arrange 99 | FizzBuzzHelper fizzBuzzHelper = new FizzBuzzHelper(); 100 | int number = 15; 101 | string expected = "FizzBuzz"; 102 | 103 | //Act 104 | string result = fizzBuzzHelper.GetFizzBuzzByNumber(number); 105 | 106 | //Assert 107 | Assert.AreEqual(expected, result); 108 | } 109 | 110 | [Test] 111 | public void GetFizzBuzzByNumber_WhenEnteredANumberThatNotDivisableByThreeOrFive_ShouldReturnGivenNumber() 112 | { 113 | //Arrange 114 | FizzBuzzHelper fizzBuzzHelper = new FizzBuzzHelper(); 115 | int number = 1; 116 | string expected = "1"; 117 | 118 | //Act 119 | string result = fizzBuzzHelper.GetFizzBuzzByNumber(number); 120 | 121 | //Assert 122 | Assert.AreEqual(expected, result); 123 | } 124 | } 125 | } -------------------------------------------------------------------------------- /FizzBuzzInTDD/FizzBuzzInTDD/FizzBuzzHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FizzBuzzInTDD 4 | { 5 | public class FizzBuzzHelper 6 | { 7 | public bool IsDivisableThree(int number) 8 | { 9 | return (number % 3) == 0; 10 | } 11 | 12 | public bool IsDivisableFive(int number) 13 | { 14 | return (number % 5) == 0; 15 | } 16 | 17 | public string GetFizzBuzzByNumber(int number) 18 | { 19 | if (IsDivisableThree(number) && IsDivisableFive(number)) 20 | { 21 | return "FizzBuzz"; 22 | } 23 | else if (IsDivisableThree(number)) 24 | { 25 | return "Fizz"; 26 | } 27 | else if (IsDivisableFive(number)) 28 | { 29 | return "Buzz"; 30 | } 31 | else 32 | { 33 | return number.ToString(); 34 | } 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /FizzBuzzInTDD/FizzBuzzInTDD/FizzBuzzInTDD.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /FizzBuzzInTDD/FizzBuzzInTDD/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FizzBuzzInTDD 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | FizzBuzzHelper fizzBuzzHelper = new FizzBuzzHelper(); 10 | 11 | for(int i = 1; i <= 100; i++) 12 | { 13 | Console.WriteLine(fizzBuzzHelper.GetFizzBuzzByNumber(i)); 14 | } 15 | 16 | Console.ReadLine(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /FizzBuzzInTDD/README.md: -------------------------------------------------------------------------------- 1 | #### Problem: 2 | 3 | Write a short program that prints each number from 1 to 100 on a new line. 4 | For each multiple of 3, print "Fizz" instead of the number. 5 | For each multiple of 5, print "Buzz" instead of the number. 6 | For numbers which are multiples of both 3 and 5, print "FizzBuzz" instead of the number. -------------------------------------------------------------------------------- /LeftRotate/LeftRotate.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /LeftRotate/Program.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using System; 3 | 4 | namespace LeftRotate 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | int[] input = new int[] { 1, 2, 3, 4, 5 }; 11 | 12 | int[] output = RotLeft(input, 2); 13 | 14 | Console.WriteLine(string.Join(',', Array.ConvertAll(output, c => c.ToString()))); 15 | } 16 | 17 | static int[] RotLeft(int[] a, int d) 18 | { 19 | int[] output = new int[a.Length]; 20 | 21 | for (int i = 0; i < a.Length; i++) 22 | { 23 | output[i] = a[(i + d) % a.Length]; 24 | } 25 | 26 | return output; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /LeftRotate/README.md: -------------------------------------------------------------------------------- 1 | #### Problem: 2 | 3 | A left rotation operation on an array shifts each of the array's elements 1 unit to the left. For example, if 2 left rotations are performed on array [1,2,3,4,5], then the array would become [3,4,5,1,2]. 4 | 5 | Write a short program that rotates an array's elements. -------------------------------------------------------------------------------- /PalindromeDetection/PalindromeDetection.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /PalindromeDetection/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PalindromeDetection 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | string input = "madam"; 10 | 11 | bool result = IsPalindrome(input); 12 | 13 | Console.WriteLine($"{input} is palindrome: {result}"); 14 | } 15 | 16 | static bool IsPalindrome(string input) 17 | { 18 | if(string.IsNullOrEmpty(input)) 19 | { 20 | return false; 21 | } 22 | 23 | bool result = true; 24 | 25 | // O(N) 26 | for (int i = 0; i < input.Length; i++) 27 | { 28 | if (input[i] != input[(input.Length - 1) - i]) 29 | { 30 | result = false; 31 | } 32 | } 33 | 34 | return result; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /PalindromeDetection/README.md: -------------------------------------------------------------------------------- 1 | #### Problem: 2 | 3 | Write a short program to check the given string is palindrome or not. 4 | 5 | E.g: "madam". -------------------------------------------------------------------------------- /PrimeNumberChecker/PrimeNumberChecker.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /PrimeNumberChecker/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PrimeNumberChecker 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | Console.WriteLine("Hello World!"); 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /PrimeNumberChecker/README.md: -------------------------------------------------------------------------------- 1 | #### Problem: 2 | 3 | Create a function that checks whether a number is a prime number. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Algorithm Playground 2 | This repository contains my solutions and personal notes about algorithms. 3 | 4 | #### Algorithms 5 | - [Winner of an election] 6 | - [FizzBuzz in TDD] 7 | - [Reverse String] 8 | - [String Compression] 9 | - [Left Rotate] 10 | - [Anagram Detection] 11 | - [Palindrome Detection] 12 | - [Array Comparison] 13 | - [Prime Number Checker] 14 | - [Absolute Difference] 15 | 16 | [Winner of an election]: https://github.com/GokGokalp/algorithm-playground/blob/master/WinnerOfAnElection 17 | [FizzBuzz in TDD]: https://github.com/GokGokalp/algorithm-playground/tree/master/FizzBuzzInTDD 18 | [Reverse String]: https://github.com/GokGokalp/algorithm-playground/tree/master/ReverseString 19 | [String Compression]: https://github.com/GokGokalp/algorithm-playground/tree/master/StringCompression 20 | [Left Rotate]: https://github.com/GokGokalp/algorithm-playground/tree/master/LeftRotate 21 | [Anagram Detection]: https://github.com/GokGokalp/algorithm-playground/tree/master/AnagramDetection 22 | [Palindrome Detection]: https://github.com/GokGokalp/algorithm-playground/tree/master/PalindromeDetection 23 | [Array Comparison]: https://github.com/GokGokalp/algorithm-playground/tree/master/ArrayComparison 24 | [Prime Number Checker]: https://github.com/GokGokalp/algorithm-playground/tree/master/PrimeNumberChecker 25 | [Absolute Difference]: https://github.com/GokGokalp/algorithm-playground/tree/master/AbsoluteDifference -------------------------------------------------------------------------------- /ReverseString/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ReverseString 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | string input = "Hello World"; 10 | 11 | string reversedInput = Reverse(input); 12 | 13 | Console.WriteLine(reversedInput); 14 | Console.ReadKey(); 15 | } 16 | 17 | static string Reverse(string input) 18 | { 19 | // Time complexity O(N) 20 | string[] inputArr = input.Split(" "); 21 | string reversedInput = string.Empty; 22 | 23 | for (int i = inputArr.Length -1; i >= 0; i--) 24 | { 25 | reversedInput += inputArr[i] + " "; 26 | } 27 | 28 | reversedInput = reversedInput.Substring(0, reversedInput.Length -1); 29 | return reversedInput; 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /ReverseString/README.md: -------------------------------------------------------------------------------- 1 | #### Problem: 2 | 3 | Write a short program that prints words of a string in reverse order. -------------------------------------------------------------------------------- /ReverseString/ReverseString.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /StringCompression/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text; 3 | 4 | namespace StringCompression 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | string input = "AAABBZZDDD"; 11 | 12 | string output = CompressString(input); 13 | 14 | Console.WriteLine(output); 15 | Console.ReadLine(); 16 | } 17 | 18 | static string CompressString(string input) 19 | { 20 | char[] charArr = input.ToCharArray(); 21 | char prev = charArr[0]; 22 | int count = 1; 23 | StringBuilder sBuilder = new StringBuilder(); 24 | 25 | //Time complexity is O(N) 26 | for (int i = 1; i < charArr.Length; i++) 27 | { 28 | if (charArr[i] == prev) 29 | { 30 | count += 1; 31 | } 32 | else 33 | { 34 | sBuilder.Append(prev); 35 | sBuilder.Append(count); 36 | 37 | prev = charArr[i]; 38 | count = 1; 39 | } 40 | } 41 | 42 | sBuilder.Append(prev); 43 | sBuilder.Append(count); 44 | 45 | return sBuilder.ToString(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /StringCompression/README.md: -------------------------------------------------------------------------------- 1 | #### Problem: 2 | 3 | Given a string input, create a function that will output a compressed version of the string.  4 | 5 | For example input: "AAABBZZDDD" and Output = "A3B2Z2D3" -------------------------------------------------------------------------------- /StringCompression/StringCompression.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /WinnerOfAnElection/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace WinnerOfAnElection 7 | { 8 | class Program 9 | { 10 | static void Main (string[] args) 11 | { 12 | string[] votes = { "chris", "anna", "anna", "chris", "chris", "jackie", "jamie", "jamie", "anna" }; 13 | 14 | string winningCandidate = FindWinner (votes); 15 | 16 | Console.WriteLine (winningCandidate); 17 | } 18 | 19 | static string FindWinner (string[] votes) 20 | { 21 | Dictionary candidatesWithVotes = new Dictionary (); 22 | 23 | for (int i = 0; i < votes.Length; i++) 24 | { 25 | if (candidatesWithVotes.ContainsKey (votes[i])) 26 | { 27 | candidatesWithVotes[votes[i]] = candidatesWithVotes[votes[i]] + 1; 28 | } 29 | else 30 | { 31 | candidatesWithVotes.Add (votes[i], 1); 32 | } 33 | } 34 | 35 | // one liner - the time complexity of the sort operation stays the typical for QuickSort O(N*logN) average / O(N2) worst case. 36 | var winner = candidatesWithVotes.OrderBy(c => c.Key).Aggregate((x, y) => x.Value > y.Value ? x : y).Key; 37 | 38 | return winner; 39 | 40 | // classic approach 41 | /* List winnerNames = new List (); 42 | //int tmpMaxVote = 0; 43 | 44 | foreach (KeyValuePair candidate in candidatesWithVotes) 45 | { 46 | if (candidate.Value >= tmpMaxVote) 47 | { 48 | winnerNames.Add (candidate.Key); 49 | 50 | tmpMaxVote = candidate.Value; 51 | } 52 | } 53 | 54 | string winner; 55 | if(winnerNames.Count > 1) 56 | { 57 | winner = winnerNames.OrderBy(c => c).Last(); 58 | } 59 | else 60 | { 61 | winner = winnerNames.First(); 62 | } 63 | 64 | return winner; */ 65 | } 66 | } 67 | } -------------------------------------------------------------------------------- /WinnerOfAnElection/README.md: -------------------------------------------------------------------------------- 1 | #### Problem: 2 | 3 | In election that use the ballot box system for voting, each voter writes the name of a candidate on a ballot and places it in the ballat box. The candidate with the highest number of votes wins the election. If two or more candidates have the same number of votes, then the tied candidates' names are ordered alphabetically and the last name in the alphabetical order wins. 4 | 5 | For example votes are in the names ['Chris', 'Anna', 'Anne', 'Chris']. Each candidate received two votes. Mary is alphabetically later than Joe, so she wins. 6 | 7 | #### Function Description 8 | 9 | The function must return a string denoting th ename of the winning candidate. -------------------------------------------------------------------------------- /WinnerOfAnElection/WinnerOfAnElection.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /algorithm-playground.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26124.0 5 | MinimumVisualStudioVersion = 15.0.26124.0 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArrayComparison", "ArrayComparison\ArrayComparison.csproj", "{04FC55D0-6430-4211-B7D4-721C400F9D88}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AnagramDetection", "AnagramDetection\AnagramDetection.csproj", "{0D635355-2D3B-43BF-908F-9761960F2D32}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbsoluteDifference", "AbsoluteDifference\AbsoluteDifference.csproj", "{3352C3DC-E940-4E62-921B-5FC1DE9AF04D}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | Debug|x64 = Debug|x64 16 | Debug|x86 = Debug|x86 17 | Release|Any CPU = Release|Any CPU 18 | Release|x64 = Release|x64 19 | Release|x86 = Release|x86 20 | EndGlobalSection 21 | GlobalSection(SolutionProperties) = preSolution 22 | HideSolutionNode = FALSE 23 | EndGlobalSection 24 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 25 | {04FC55D0-6430-4211-B7D4-721C400F9D88}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 26 | {04FC55D0-6430-4211-B7D4-721C400F9D88}.Debug|Any CPU.Build.0 = Debug|Any CPU 27 | {04FC55D0-6430-4211-B7D4-721C400F9D88}.Debug|x64.ActiveCfg = Debug|Any CPU 28 | {04FC55D0-6430-4211-B7D4-721C400F9D88}.Debug|x64.Build.0 = Debug|Any CPU 29 | {04FC55D0-6430-4211-B7D4-721C400F9D88}.Debug|x86.ActiveCfg = Debug|Any CPU 30 | {04FC55D0-6430-4211-B7D4-721C400F9D88}.Debug|x86.Build.0 = Debug|Any CPU 31 | {04FC55D0-6430-4211-B7D4-721C400F9D88}.Release|Any CPU.ActiveCfg = Release|Any CPU 32 | {04FC55D0-6430-4211-B7D4-721C400F9D88}.Release|Any CPU.Build.0 = Release|Any CPU 33 | {04FC55D0-6430-4211-B7D4-721C400F9D88}.Release|x64.ActiveCfg = Release|Any CPU 34 | {04FC55D0-6430-4211-B7D4-721C400F9D88}.Release|x64.Build.0 = Release|Any CPU 35 | {04FC55D0-6430-4211-B7D4-721C400F9D88}.Release|x86.ActiveCfg = Release|Any CPU 36 | {04FC55D0-6430-4211-B7D4-721C400F9D88}.Release|x86.Build.0 = Release|Any CPU 37 | {0D635355-2D3B-43BF-908F-9761960F2D32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 38 | {0D635355-2D3B-43BF-908F-9761960F2D32}.Debug|Any CPU.Build.0 = Debug|Any CPU 39 | {0D635355-2D3B-43BF-908F-9761960F2D32}.Debug|x64.ActiveCfg = Debug|Any CPU 40 | {0D635355-2D3B-43BF-908F-9761960F2D32}.Debug|x64.Build.0 = Debug|Any CPU 41 | {0D635355-2D3B-43BF-908F-9761960F2D32}.Debug|x86.ActiveCfg = Debug|Any CPU 42 | {0D635355-2D3B-43BF-908F-9761960F2D32}.Debug|x86.Build.0 = Debug|Any CPU 43 | {0D635355-2D3B-43BF-908F-9761960F2D32}.Release|Any CPU.ActiveCfg = Release|Any CPU 44 | {0D635355-2D3B-43BF-908F-9761960F2D32}.Release|Any CPU.Build.0 = Release|Any CPU 45 | {0D635355-2D3B-43BF-908F-9761960F2D32}.Release|x64.ActiveCfg = Release|Any CPU 46 | {0D635355-2D3B-43BF-908F-9761960F2D32}.Release|x64.Build.0 = Release|Any CPU 47 | {0D635355-2D3B-43BF-908F-9761960F2D32}.Release|x86.ActiveCfg = Release|Any CPU 48 | {0D635355-2D3B-43BF-908F-9761960F2D32}.Release|x86.Build.0 = Release|Any CPU 49 | {3352C3DC-E940-4E62-921B-5FC1DE9AF04D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 50 | {3352C3DC-E940-4E62-921B-5FC1DE9AF04D}.Debug|Any CPU.Build.0 = Debug|Any CPU 51 | {3352C3DC-E940-4E62-921B-5FC1DE9AF04D}.Debug|x64.ActiveCfg = Debug|Any CPU 52 | {3352C3DC-E940-4E62-921B-5FC1DE9AF04D}.Debug|x64.Build.0 = Debug|Any CPU 53 | {3352C3DC-E940-4E62-921B-5FC1DE9AF04D}.Debug|x86.ActiveCfg = Debug|Any CPU 54 | {3352C3DC-E940-4E62-921B-5FC1DE9AF04D}.Debug|x86.Build.0 = Debug|Any CPU 55 | {3352C3DC-E940-4E62-921B-5FC1DE9AF04D}.Release|Any CPU.ActiveCfg = Release|Any CPU 56 | {3352C3DC-E940-4E62-921B-5FC1DE9AF04D}.Release|Any CPU.Build.0 = Release|Any CPU 57 | {3352C3DC-E940-4E62-921B-5FC1DE9AF04D}.Release|x64.ActiveCfg = Release|Any CPU 58 | {3352C3DC-E940-4E62-921B-5FC1DE9AF04D}.Release|x64.Build.0 = Release|Any CPU 59 | {3352C3DC-E940-4E62-921B-5FC1DE9AF04D}.Release|x86.ActiveCfg = Release|Any CPU 60 | {3352C3DC-E940-4E62-921B-5FC1DE9AF04D}.Release|x86.Build.0 = Release|Any CPU 61 | EndGlobalSection 62 | EndGlobal 63 | --------------------------------------------------------------------------------