Last active
April 24, 2018 18:16
-
-
Save maacpiash/50d493f9d273095f31b821a424face35 to your computer and use it in GitHub Desktop.
A program to shuffle the lines of a text file
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
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp2.0</TargetFramework> | |
</PropertyGroup> | |
</Project> |
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.IO; | |
using static System.Console; | |
using static System.IO.File; | |
namespace ListShuffler | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string fileName; | |
if(args.Length < 1) | |
{ | |
WriteLine("Please enter a filename: "); | |
fileName = ReadLine(); | |
} | |
else | |
fileName = args[0]; | |
string[] inputLines, outputLines; | |
try | |
{ | |
inputLines = ReadAllLines(fileName); | |
} | |
catch (Exception x) | |
{ | |
WriteLine(x.ToString()); | |
return; | |
} | |
int max = inputLines.Length; | |
outputLines = new string[max]; | |
bool[] check = new bool[max]; | |
// This is for keeping track of lines already included in the new array | |
Random r = new Random(); | |
int count = 0, token; | |
while(count < max) | |
{ | |
token = r.Next(0, max); | |
if(!check[token]) // Means this line has not been used | |
{ | |
outputLines[count++] = inputLines[token]; | |
check[token] = true; | |
} | |
} | |
WriteAllLines(fileName, outputLines); | |
} | |
} | |
} |
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
7 | |
1 | |
9 | |
3 | |
6 | |
8 | |
5 | |
4 | |
2 | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment