-
StringsData TypesFundamentalsBasic Language FeaturesAlgorithmsLogic
Description Line 9 is maybe an anti-pattern, but it's the most sensible way to do it I could figure out.
Code using System.Linq; public class Replacer { public static string sValueReplacer(string s, int z, char x) { var q = Enumerable.Range(0, s.Length).Where((i) => (s[i] == x)); char[] r = s.ToCharArray(); q.Zip(z.ToString().PadLeft(q.Count(), '0'), (i, e) => r[i] = e).Count(); return new string(r); } }
Test Cases namespace Solution { using NUnit.Framework; using System; // TODO: Replace examples and use TDD by writing your own tests [TestFixture] public class SolutionTest { [Test] public void MyTest() { Assert.AreEqual("0000008100596710003=1811", Replacer.sValueReplacer("0000##81#059671####=1811", 3, '#')); Assert.AreEqual("9182 7391238 910003", Replacer.sValueReplacer("9182 7391238 91$$$$", 3, '$')); Assert.AreEqual("The counter is 011", Replacer.sValueReplacer("The counter is ---", 11, '-')); } } }
Output:
-
Code using System;- using System.Linq;
using System.Collections.Generic;public class Test{static void Main(){Console.WriteLine(Replacer.sValueReplacer("0000##81#059671####=1811", 3, '#'));}}public class Replacer{public static string sValueReplacer(string source, int index, char char_to_replace)- public class Replacer
- {
- public static string sValueReplacer(string s, int z, char x)
- {
string res = source;try{if (!string.IsNullOrEmpty(char_to_replace.ToString())){if (res.Contains(char_to_replace.ToString())){// Get ALL Indexes position of charactervar Indexes = GetIndexes(res, char_to_replace.ToString());int max = GetMaxValue(Indexes.Count);while (index >= max){index -= max;}var new_value = index.ToString().PadLeft(Indexes.Count, '0');for (int i = 0; i < Indexes.Count; i++){res = res.Remove(Indexes[i], 1).Insert(Indexes[i], new_value[i].ToString());}}}}catch (Exception){res = source;}return res;- var q = Enumerable.Range(0, s.Length).Where((i) => (s[i] == x));
- char[] r = s.ToCharArray();
- q.Zip(z.ToString().PadLeft(q.Count(), '0'), (i, e) => r[i] = e).Count();
- return new string(r);
- }
private static List<int> GetIndexes(string mainString, string toFind){var Indexes = new List<int>();for (int i = mainString.IndexOf(toFind); i > -1; i = mainString.IndexOf(toFind, i + 1)){// for loop end when i=-1 (line.counter not found)Indexes.Add(i);}return Indexes;}private static int GetMaxValue(int numIndexes){int max = 0;for (int i = 0; i < numIndexes; i++){if (i == 0)max = 9;elsemax = max * 10 + 9;}return max;}- }
Test Cases - namespace Solution {
- using NUnit.Framework;
- using System;
- // TODO: Replace examples and use TDD by writing your own tests
- [TestFixture]
- public class SolutionTest
- {
- [Test]
- public void MyTest()
- {
- Assert.AreEqual("0000008100596710003=1811", Replacer.sValueReplacer("0000##81#059671####=1811", 3, '#'));
- Assert.AreEqual("9182 7391238 910003", Replacer.sValueReplacer("9182 7391238 91$$$$", 3, '$'));
- Assert.AreEqual("The counter is 011", Replacer.sValueReplacer("The counter is ---", 11, '-'));
- }
- }
- }
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}