Skip to content

Instantly share code, notes, and snippets.

@lucas-zimerman
Created May 2, 2021 18:29
Show Gist options
  • Save lucas-zimerman/a9b8663f1ea958487c335f8e92c6a30e to your computer and use it in GitHub Desktop.
Save lucas-zimerman/a9b8663f1ea958487c335f8e92c6a30e to your computer and use it in GitHub Desktop.
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;
}
}
}
@lucas-zimerman
Copy link
Author

The goal from this gist is to shift the position of the character by walking towards the text.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment