-
StringsData TypesFundamentalsBasic Language FeaturesAlgorithmsLogic
Description Obligatory Python golf solution. Don't really like the final version I ended up on. I don't know APL, but this problem almost felt designed for it. The shortest solution I could think of was
{s r c←⍵⋄k←+/q←c⍷s⋄(({⍵'0'[' '=⍵]}¨(-k)↑⍕r)@(k↑⍒q))s}
, I'm sure it can be greatly improved by somebody who knows what they're doing though. (index origin is 0 btwCode # exponential time - fast enough when first testcase is commented out f=lambda s,r,c:s and(f(z:=s[:-1],r,c)+s[-1],f(z,r//10,c)+str(r%10))[c==s[-1]] # linear recursive f=lambda s,r,c:s and(c!=s[-1]and f(s[:-1],r,c)+s[-1]or f(s[:-1],r//10,c)+str(r%10)) f=lambda s,r,c:"".join(x==c and[str(r%10),r:=r//10][0]or x for x in s[::-1])[::-1]
Test Cases import codewars_test as test # TODO Write tests import solution # or from solution import example # test.assert_equals(actual, expected, [optional] message) .describe("Example") def test_group(): .it("test case") def test_case(): test.assert_equals(f("0000##81#059671####=1811", 3, '#'), "0000008100596710003=1811") test.assert_equals(f("9182 7391238 91$$$$", 3, '$'), "9182 7391238 910003") test.assert_equals(f("The counter is ---", 11, '-'), "The counter is 011")
Output:
-
Code using System;using System.Linq;using System.Collections.Generic;- # exponential time - fast enough when first testcase is commented out
- f=lambda s,r,c:s and(f(z:=s[:-1],r,c)+s[-1],f(z,r//10,c)+str(r%10))[c==s[-1]]
- # linear recursive
- f=lambda s,r,c:s and(c!=s[-1]and f(s[:-1],r,c)+s[-1]or f(s[:-1],r//10,c)+str(r%10))
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){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;}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;}}- f=lambda s,r,c:"".join(x==c and[str(r%10),r:=r//10][0]or x for x in s[::-1])[::-1]
Test Cases - import codewars_test as test
- # TODO Write tests
- import solution # or from solution import example
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
- test.assert_equals(f("0000##81#059671####=1811", 3, '#'), "0000008100596710003=1811")
- test.assert_equals(f("9182 7391238 91$$$$", 3, '$'), "9182 7391238 910003")
- test.assert_equals(f("The counter is ---", 11, '-'), "The counter is 011")
- 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 }}