Question from the interview - how to get through an array with a different starting point?

Say you have the line "This is a test"

I pass it to the zee method, for example ("This is a test", 1) and want to "test This is a";

I pass it to the zee method, for example ("This is a test", 2) and want to "test It",

the number may exceed common words in a variable. If so, he should go in cycles.

I started with ....

public static string zee(string origString, int i)
{
    StringBuilder sb = new StringBuilder();

    ArrayList list = new ArrayList();
    list.AddRange(origString.Split(' '));

    // not sure here - 
    for (int c = i; c < (list.Count + i); c++)
    {
        sb.AppendFormat("{0} ", list[c]);
    }

    return sb.ToString();
}
+3
source share
8 answers
for(int j=0; j < list.length; j++){
    int idx = (j + i) % list.length;
    sb.AppendFormat("{0} " , list[idx]);
}

Mostly like Brent Arias solution, but I think the for loop is more readable, less likely to be infinite.

    public static string zee(string origString, int i)
    {
        StringBuilder sb = new StringBuilder();

        List<string> list = new List<string>();
        list.AddRange(origString.Split(' '));

        for (int j = 0; j < list.Count; j++)
        {
            int idx = (j + i) % list.Count;
            sb.AppendFormat("{0} ", list[idx]);
        }
        return sb.ToString();
    }
+5
source

This is how I solve it.

    private static string f(string s, int start)
    {
        var arr=s.Split(' ');
        start %= arr.Length;

        var res=arr.Skip(arr.Length - start).ToList();
        res.AddRange(arr.Take(arr.Length - start));
        return string.Join(" ", res);
    }

liner linq, , 2 . Union Join - , .

+1

I have not tried this, but I think it would do this:

i %= list.Length;
int index = i;
do {
  index %= list.Length;
  sb.AppendFormat("{0} ", list[index]);
while (++index != i);
0
source

This is how I solve this using strings.

    public static string zee(string origString, int i)
    {
        string[] splitStr = origString.Split(' ');
        string newStr = "";

        // Not sure what you meant by wrap around but this should
        // do the trick.
        i %= splitStr.Length;

        for (int j = (splitStr.Length - i); j < splitStr.Length; j++)
            newStr += splitStr[j] + " "; // Add spaces taken by split :(

        for (int j = 0; j < (splitStr.Length - i); j++)
            newStr += splitStr[j] + " ";

        return
            newStr;
    }
0
source

Sounds like a homework question for me, but here is the efficient use of the .Net framework:

    private static string [] SplitWords(string s, int startWord)
    {
        string[] words = s.Split(' ');
        List<string> output = new List<string>();
        output.AddRange(words.Skip(startWord).ToArray());
        output.AddRange(words.Take(startWord).ToArray());
        return output.ToArray();
    }

There is no error checking in this function, so you have to change it for the production code, but you get this idea.

0
source

Here's an abomination trying to fit into one line as much as possible:

static string zee(string sentence, int wordCount)
{
  var words = sentence.Split(' ');
  return string.Join(" ", new[] { words.Skip(words.Count() - wordCount), words.Take(words.Count() - wordCount) }.SelectMany(w => w).ToArray());
}
0
source
static string rearrange(string phase,int index)
{
    string[] words = phase.Split(' ');
    string[] newwords = new string[words.Length];

    int pointer = index;
    for (int i = 0; i < words.Length;i++ )
    {
        if(pointer>=words.Length)
        {
            pointer = 0;
        }
        newwords[i] = words[pointer];
        pointer++;
    }

    return string.Join(" ", newwords);
}
0
source
public string SetStart(int startAt)
{
    const string sentence = "this is a test so it is";

    var words = sentence.Split(' ');

    var x = (startAt > words.Count()) ? startAt%words.Count() : startAt;

    return string.Join(" ", words.Skip(x).Concat(words.Take(x)));            
}
-1
source

Source: https://habr.com/ru/post/1776332/


All Articles