I ran into this challenge a couple of times today and thought I could tackle it with PowerShell. I refused to look at other peoples approaches to this hoping to complete it on my own. I'm sure the general idea is the same in either case. I have included more comments than I would normally to help people not familiar with the language/challenge.
A Decent Number has the following properties:
- Its digits can only be 3's and/or 5's.
- The number of 3's it contains is divisible by 5.
- The number of 5's it contains is divisible by 3.
- If there are more than one such number, we pick the largest one.
Constraints
1≤T≤20
1≤N≤100000
Input Format
The first line is an integer, T, denoting the number of test cases.
The T subsequent lines each contain an integer, N, detailing the number of digits in the number.
Output Format
Print the largest Decent Number having N digits; if no such number exists, tell Sherlock by printing -1.
# Split the lines in the string passed as string array. Cast each value as an integer
$inputNumbers = $args[0] -split "`r`n" | ForEach-Object{[int]$_}
# Cycle the numbers. Amount determined by the first line.
for($inputIndex=1;$inputIndex -le $inputNumbers[0];$inputIndex++){
# Create a loop that runs for the number of passes in the current line of input.
# Count down from the number until 0
$highestNumber = $inputNumbers[$inputIndex]..0 | ForEach-Object{
# Check to see if this digit combination is a decent number.
# If it is then we pass it thru the pipeline and stop the loop.
If(($_ % 3) -eq 0 -and (($inputNumbers[$inputIndex] - $_) % 5) -eq 0){
("5" * $_).PadRight($inputNumbers[$inputIndex],"3")
# If we get a result then this is the highest. Exit the loop.
continue
}
}
# Check the value of $highestNumber. It is possible that for this pass there
# are no decent numbers. Nulls evaluate to false.
if($highestNumber){$highestNumber}else{-1}
}
Usage
Save the above code as a ps1 file. Run it passing a multi-line string parameter.
.\Get-DecentNumbers.ps1 "4
1
3
5
11"