patternshellMinor
Sherlock is going against the Beast with PowerShell
Viewed 0 times
thegoingwithpowershellsherlockagainstbeast
Problem
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:
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.
# 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 fa
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 fa
Solution
-
I'm no powershell expert but if spaces sprinkled around here and there would make the code more readable. So instead of this:
it would read like this:
Less crammed and easier to read I'd say.
-
Your outer
-
As for the algorithm: You kind of brute force it which is usually not the intend of these programming challenges. I have outlined a slightly better algorithm in my answer to another question trying to solve the same problem.
I'm no powershell expert but if spaces sprinkled around here and there would make the code more readable. So instead of this:
for($inputIndex=1;$inputIndex -le $inputNumbers[0];$inputIndex++){it would read like this:
for (inputIndex = 1; $inputIndex -le $inputNumbers[0]; $inputIndex++) {Less crammed and easier to read I'd say.
-
Your outer
for loop could be expresses as a ForEach which would yield cleaner code:ForEach ($N in $inputNumbers) {
# Create a loop that runs for the number of passes in the current line of input.
# Count down from the number until 0
$highestNumber = $N..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 (($N - $_) % 5) -eq 0) {
("5" * $_).PadRight($N, "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 }
}-
As for the algorithm: You kind of brute force it which is usually not the intend of these programming challenges. I have outlined a slightly better algorithm in my answer to another question trying to solve the same problem.
Code Snippets
for($inputIndex=1;$inputIndex -le $inputNumbers[0];$inputIndex++){for (inputIndex = 1; $inputIndex -le $inputNumbers[0]; $inputIndex++) {ForEach ($N in $inputNumbers) {
# Create a loop that runs for the number of passes in the current line of input.
# Count down from the number until 0
$highestNumber = $N..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 (($N - $_) % 5) -eq 0) {
("5" * $_).PadRight($N, "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 }
}Context
StackExchange Code Review Q#119152, answer score: 5
Revisions (0)
No revisions yet.