Here is a brain exercise using powershell. Actually, use any form of language you want, I just like powershell.

The purpose of these brain exercises isn’t to really perform a task you would do in production. While the outcome is ridiculous, the skills used along the way will absolutely come in handy in your travels.

Task

You’re to grab the list of names from www.nlymbery.com.au/names.txt, grab the current day date (if date is 01, then pick a random int. between 10-20), and that’s how many lists you will use. So for e.g. 14/11/2016, you will make 14 lists. RANDOMLY and evenly (MUST be random AND even), split all names across your lists. Finally, output how many lists you create, what each list contains and how many names in each list.

 

Click ‘Keep Reading’ for the solution I reached

  
\## PSBE - Powershell Brain Excercise 1

  
\## You're to grab the list of names from www.nlymbery.com.au/names.txt, grab the current

  
\## day date (if date is 01, then pick a random int. between 10-20), and that's how many

  
\## lists you will use. So for e.g. 14/11/2016, you will make 14 lists. RANDOMLY and evenly

  
\## (MUST be random AND even), split all names across your lists. Finally, output how many

  
\## lists you create, what each list contains and how many names in each list.


#=============#


\# Get the list

  
$URI = "http://test.nlymbery.com.au/names.txt"
  
$html = Invoke-WebRequest -Uri $URI

\# Something we can use

  
$html = ($html.Content).ToString()
  
$html = $Html.Split("\`n")

\# for misc purposes

  
$backuphtml = $html

#master array and day date

  
$master = @()
  
$daydate = (get-date).Day
  
if ($daydate -eq 1)
  
{
  
$script:daydate = (get-random -Minimum 10 -Maximum 20)
  
}

\# Count and split

  
$namecount = $html.length
  
$evensplit = [math]::Round($namecount/$daydate)
  
$range = 1..$evensplit
  
\# Do the magic

  
foreach($x in $range)
  
{
  
\# Setup dynamic array name

  
$y = @()

\# Drag in name list, modify and set global/script variable

  
$temphtml = $script:html

\# Select the pre-determined amount of names from list

  
$temphtml = $temphtml | get-random -count $evensplit -ErrorAction SilentlyContinue

\# Set the new contents of list

  
$remove = ($script:html | select-string -NotMatch $temphtml -ErrorAction SilentlyContinue)
  
$script:html = $remove

\# Add temp names to temp array, add array to master array and repeat.

  
$y += $temphtml
  
$script:master += ,$y
  
}

write-host -foregroundcolor green "Array has " $master.Count " sub arrays"
  
foreach($z in ($num = 1..($master.count)))
  
{
  
Write-Host -ForegroundColor Yellow "Up to array number $z"

\# We start at 1 but gizmos start at 0

  
$z = $z - 1

\# Blank lines getting added, perhaps from uneven division

  
$master[$z] = ($master[$z] | select-string -NotMatch " ")
  
$master[$z]
  
write-host -ForegroundColor cyan "There are" ($master[$z] | measure-object).Count "names in sub-array"
  
write-host "\`n"
  
}