using System;
namespace MoveFirst
{
class Program
{
static void Main(string[] args)
{
int[] values = ReadValuesList();
int[] positionsToMove = ReadPositions();
for (int i = 0; i < positionsToMove.Length; i++)
MoveFirst(values, positionsToMove[i]);
PrintValuesList(values);
Console.WriteLine(CheckIfSortedAscending(values));
Console.Read();
}
static bool CheckIfSortedAscending(int[] values)
{
for (int i = 1; i < values.Length; i++)
if (values[i - 1] > values[i])
return false;
return true;
}
public static void MoveFirst(int[] values, int index)
{
var temp = values[0];
values[0] = values[index];
values[index] = temp;
}
static int[] ReadPositions()
{
int positionsNumber = Convert.ToInt32(Console.ReadLine());
int[] positions = new int[positionsNumber];
for (int i = 0; i < positionsNumber; i++)
positions[i] = Convert.ToInt32(Console.ReadLine());
return positions;
}
static int[] ReadValuesList()
{
string[] inputValues = Console.ReadLine().Split(' ');
int[] values = new int[inputValues.Length];
for (int i = 0; i < values.Length; i++)
values[i] = Convert.ToInt32(inputValues[i]);
return values;
}
static void PrintValuesList(int[] valuesList)
{
for (int i = 0; i < valuesList.Length; i++)
Console.Write(valuesList[i] + " ");
Console.Write('\n');
}
}
}
This is my whole code, but I have a problem with the result..Can I get some suggestions how to correct the code within the MoveFirst method ?
There are given a series of numbers on a single line, separated by a space. There are N operations to move a string element to the first position.. Each move is specified on one line (the index of the element which has to be moved to the first position), but all other elements of the string remain in the same order.
For example if the user inputs : 1 2 3 4 5 6
1
5
the result should be : 6 2 1 3 4 5
my result is : 6 1 3 4 5 2
Here is working example, using LinkedList<int>
Input: 1 2 3 4 5 6
2
1
5
Result: 6 2 1 3 4 5
using System;
using System.Collections.Generic;
using System.Linq;
namespace MoveFirst
{
class Program
{
static void Main(string[] args)
{
LinkedList<int> values = ReadValuesList();
int[] positionsToMove = ReadPositions();
for (int i = 0; i < positionsToMove.Length; i++)
MoveFirst(values, positionsToMove[i]);
PrintValuesList(values);
Console.WriteLine(CheckIfSortedAscending(values));
Console.Read();
}
static bool CheckIfSortedAscending(IEnumerable<int> valuesList)
{
var prevValue = int.MinValue;
foreach (int value in valuesList)
{
if (prevValue > value)
return false;
prevValue = value;
}
return true;
}
public static void MoveFirst(LinkedList<int> values, int index)
{
if (index == 0 || index >= values.Count)
return;
var node = values.First;
for (var i = 0; i < index; i++)
node = node.Next;
values.Remove(node);
values.AddFirst(node);
}
static int[] ReadPositions()
{
int positionsNumber = Convert.ToInt32(Console.ReadLine());
int[] positions = new int[positionsNumber];
for (int i = 0; i < positionsNumber; i++)
positions[i] = Convert.ToInt32(Console.ReadLine());
return positions;
}
static LinkedList<int> ReadValuesList()
{
return new LinkedList<int>(Console.ReadLine().Split(' ').Select(Int32.Parse));
}
static void PrintValuesList(IEnumerable<int> valuesList)
{
foreach (int value in valuesList)
Console.Write(value + " ");
Console.Write('\n');
}
}
}
Update:
MoveFirst
method for situation when you forced to use array.
public static void MoveFirst(int[] values, int index)
{
if (index == 0 || index >= values.Length)
return;
var temp = values[index];
for (int i = index - 1; i >=0; i--)
values[i + 1] = values[i];
values[0] = temp;
}