Created
May 2, 2021 18:29
-
-
Save lucas-zimerman/a9b8663f1ea958487c335f8e92c6a30e 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; | |
using System.Diagnostics; | |
namespace ShiftString | |
{ | |
class Program | |
{ | |
struct Data | |
{ | |
public int leftShift { get; set; } | |
public int rightShift { get; set; } | |
public string value { get; set; } | |
}; | |
static Data GetTestSample() | |
=> new Data() | |
{ | |
leftShift = 1, | |
rightShift = 2, | |
value = "abcd" | |
}; | |
static Data GetTestSample2() | |
=> new Data() | |
{ | |
leftShift = 99999998, | |
rightShift = 1000000000, | |
value = "gscelpbobqbzzvzjckhmsimayykyylrmdyoumdqvobcnorruyhsgbtjfyrxnlohmkbpghoqqkfucyldioakhiqobsukeuhznppxtqxozywgvsierwaxdsfxqgzkaampiuhyoqfjfeycemstyxskrxijfreoztsxoialrihwbqldfztalcdzfhcfskmmskjmothkgcmhzfoxovlhxggcpymumbshygftuhhcmkqroaalxyrywhijujkezgodwisgcvyckndevbnhqlzwjcqkletilwwegrquotfxslxgggxtixbeuedcnaiwwpgzherfwblyhxnogejhwellluwrpoqagpwvgvbghtshwxixhhbujganqhphoxvqqeeyenxfjfinephwcdeoumoxtdygpxvyxsczmfmmrckonjtryedvatewrniwkoewjymxquudueohoycfauzpcswkczexdtnbplgovlemibeppilgklljqtfyvmedlxdpgbdmpjkfnypzfqpaiewhghqaatjqjimwohfbfficamlskijuiaskemwmowmwlpppchaiikzgtesocmgielosaefijndgooqvbqkwthbimlceqdvdnvrtrhuzzpaxdtzjmlavvyhixkxkniagllxkxudcfqkgxlxciebfejlmwejfcppxnbuqcsfwuufrrqemovhydxxreqtjzodtjkhnmljkuobjahlxwufngokfhsemjdwrduujioyffrtibxkaauumpyvciwwjxlnjnflbjniowwjxzxbukrhozntmtxyrmilqjyhtgakqgloiyhjnpcriqkzkjlrqhxgjffianyhnzybvnsqdkutbaxhyvuumgkqbwatvayczvqmkvuiuasqgxqpwbpabfeehvxkbvqgjfuqyooivrodxiyrrtzwzlmoowmshiyjbdquplitqpaqlzdvafzyjbjzymaplsbtaylqpymyluiezesitvpzmgkyki" | |
}; | |
static Data GetTestSample3() | |
=> new Data() | |
{ | |
rightShift = 99999998, | |
leftShift= 1000000000, | |
value = "gscelpbobqbzzvzjckhmsimayykyylrmdyoumdqvobcnorruyhsgbtjfyrxnlohmkbpghoqqkfucyldioakhiqobsukeuhznppxtqxozywgvsierwaxdsfxqgzkaampiuhyoqfjfeycemstyxskrxijfreoztsxoialrihwbqldfztalcdzfhcfskmmskjmothkgcmhzfoxovlhxggcpymumbshygftuhhcmkqroaalxyrywhijujkezgodwisgcvyckndevbnhqlzwjcqkletilwwegrquotfxslxgggxtixbeuedcnaiwwpgzherfwblyhxnogejhwellluwrpoqagpwvgvbghtshwxixhhbujganqhphoxvqqeeyenxfjfinephwcdeoumoxtdygpxvyxsczmfmmrckonjtryedvatewrniwkoewjymxquudueohoycfauzpcswkczexdtnbplgovlemibeppilgklljqtfyvmedlxdpgbdmpjkfnypzfqpaiewhghqaatjqjimwohfbfficamlskijuiaskemwmowmwlpppchaiikzgtesocmgielosaefijndgooqvbqkwthbimlceqdvdnvrtrhuzzpaxdtzjmlavvyhixkxkniagllxkxudcfqkgxlxciebfejlmwejfcppxnbuqcsfwuufrrqemovhydxxreqtjzodtjkhnmljkuobjahlxwufngokfhsemjdwrduujioyffrtibxkaauumpyvciwwjxlnjnflbjniowwjxzxbukrhozntmtxyrmilqjyhtgakqgloiyhjnpcriqkzkjlrqhxgjffianyhnzybvnsqdkutbaxhyvuumgkqbwatvayczvqmkvuiuasqgxqpwbpabfeehvxkbvqgjfuqyooivrodxiyrrtzwzlmoowmshiyjbdquplitqpaqlzdvafzyjbjzymaplsbtaylqpymyluiezesitvpzmgkyki" | |
}; | |
static void Main(string[] _) | |
{ | |
var input = GetTestSample3(); | |
int leftShift = input.leftShift; | |
int rightShift = input.rightShift; | |
string value = input.value; | |
/* Expected input : abcd | |
* Expected output: dabc | |
* -> _ _ _ _ | |
* _ a _ _ | |
* _ a b _ | |
* _ a b c | |
* d a b c | |
*/ | |
var stopWatch = new Stopwatch(); | |
stopWatch.Start(); | |
var output = ShiftString(value, leftShift, rightShift); | |
stopWatch.Stop(); | |
Console.Write("Input: "); | |
Console.WriteLine(value); | |
Console.Write("Output: "); | |
Console.WriteLine(output); | |
Console.WriteLine($"Took {stopWatch.ElapsedMilliseconds}ms OR {stopWatch.ElapsedTicks} ticks"); | |
Console.ReadKey(); | |
} | |
public static char[] ShiftString(string text, int leftShift, int rightShift) | |
{ | |
var length = text.Length; | |
int offset = rightShift - leftShift; | |
if (offset < 0 ) | |
{ | |
offset = length + (offset % length); | |
} | |
else if(offset > length) | |
{ | |
offset = offset % length; | |
} | |
var shiftedText = new char[length]; | |
int newOffset; | |
for (int i = 0; | |
i < length; | |
i++) | |
{ | |
newOffset = i + offset; | |
if (newOffset >= length) | |
{ | |
newOffset -= length; | |
} | |
shiftedText[newOffset] = text[i]; | |
} | |
return shiftedText; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The goal from this gist is to shift the position of the character by walking towards the text.