6
\$\begingroup\$

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:

  1. Its digits can only be 3's and/or 5's.
  2. The number of 3's it contains is divisible by 5.
  3. The number of 5's it contains is divisible by 3.
  4. 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"
\$\endgroup\$
5
\$\begingroup\$
  1. 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.

  2. 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 }
    }
    
  3. 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.

\$\endgroup\$
1
  • \$\begingroup\$ I have literally done 0 of these types of things before. There is only so many PPCG posts I can read before I had to start trying. Thanks for the information. Just trying to soak in your other answer now. \$\endgroup\$ – Matt Feb 8 '16 at 1:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.