Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
31
Task/Number-reversal-game/C-sharp/number-reversal-game-1.cs
Normal file
31
Task/Number-reversal-game/C-sharp/number-reversal-game-1.cs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var r = new Random();
|
||||
|
||||
var tries = 1;
|
||||
var sorted = Enumerable.Range(1, 9).ToList();
|
||||
var values = sorted.OrderBy(x => r.Next(-1, 1)).ToList();
|
||||
|
||||
while (Enumerable.SequenceEqual(sorted, values)) {
|
||||
values = sorted.OrderBy(x => r.Next(-1, 1)).ToList();
|
||||
}
|
||||
|
||||
//values = "1 3 9 2 7 5 4 8 6".Split().Select(x => int.Parse(x)).ToList();
|
||||
|
||||
while (!Enumerable.SequenceEqual(sorted, values))
|
||||
{
|
||||
Console.Write("# {0}: LIST: {1} - Flip how many? ", tries, String.Join(" ", values));
|
||||
|
||||
values.Reverse(0, int.Parse(Console.ReadLine()));
|
||||
tries += 1;
|
||||
}
|
||||
|
||||
Console.WriteLine("\nYou took {0} attempts to put the digits in order!", tries - 1);
|
||||
Console.ReadLine();
|
||||
}
|
||||
}
|
||||
86
Task/Number-reversal-game/C-sharp/number-reversal-game-2.cs
Normal file
86
Task/Number-reversal-game/C-sharp/number-reversal-game-2.cs
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
int[] values = new int[9];
|
||||
Random tRandom = new Random();
|
||||
int tries = 0;
|
||||
|
||||
for (int x = 0; x < values.Length; x++)
|
||||
{
|
||||
values[x] = x + 1;
|
||||
}
|
||||
|
||||
values = RandomPermutation<int>(values);
|
||||
|
||||
do
|
||||
{
|
||||
Console.Write("Numbers: ");
|
||||
for (int x = 0; x < values.Length; x++)
|
||||
{
|
||||
Console.Write(" ");
|
||||
Console.Write(values[x]);
|
||||
}
|
||||
Console.WriteLine(". Enter number of numbers from the left to reverse: ");
|
||||
|
||||
string tIn = "";
|
||||
do
|
||||
{
|
||||
tIn = Console.ReadLine();
|
||||
} while (tIn.Length != 1);
|
||||
|
||||
int nums = Convert.ToInt32(tIn.ToString());
|
||||
|
||||
int[] newValues = new int[9];
|
||||
for (int x = nums; x < newValues.Length; x++)
|
||||
{
|
||||
// Move those not reversing
|
||||
newValues[x] = values[x];
|
||||
}
|
||||
for (int x = 0; x < nums; x++)
|
||||
{
|
||||
// Reverse the rest
|
||||
newValues[x] = values[nums - 1 - x];
|
||||
}
|
||||
values = newValues;
|
||||
tries++;
|
||||
} while (!check(values));
|
||||
|
||||
Console.WriteLine("Success!");
|
||||
Console.WriteLine("Attempts: " + tries);
|
||||
|
||||
Console.Read();
|
||||
}
|
||||
|
||||
public static bool check(int[] p)
|
||||
{
|
||||
// Check all items
|
||||
for (int x = 0; x < p.Length - 1; x++)
|
||||
{
|
||||
if (p[x + 1] <= p[x])
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static T[] RandomPermutation<T>(T[] array)
|
||||
{
|
||||
T[] retArray = new T[array.Length];
|
||||
array.CopyTo(retArray, 0);
|
||||
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < array.Length; i += 1)
|
||||
{
|
||||
int swapIndex = random.Next(i, array.Length);
|
||||
if (swapIndex != i)
|
||||
{
|
||||
T temp = retArray[i];
|
||||
retArray[i] = retArray[swapIndex];
|
||||
retArray[swapIndex] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
return retArray;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue