-2
\$\begingroup\$

The task

Your program will be given the following inputs: a number n, and an array of n positive integers. Your program only has to work when n=1000.

The program must do the following:

  • If the input array is a permutation of the range [1, 2, …, n], output Huh!?
  • Otherwise, return a three-element array [i, j, n], where i is a number from 1 to n inclusive that does not appear in the input array, and j is a number that appears more than once in the input array.

Clarifications

  • Because n will always have the same value, your program may get at it three different ways: hardcoding it as 1000, checking the length of the input array, or taking it from input.
  • Per PPCG rules, you don't have to read an input at all if you don't need it to solve the question.
  • The input will always be chosen so that there's a unique answer (in other words, the input array will contain no more than one duplicated element).

Test case generator

The following test case generator, in the statistical programming language R, will generate an appropriate test case for this challenge. It takes the following inputs:

  1. The value of n. In order to create an appropriate test case, this should be 1000.
  2. The value of i.
  3. The value of j.

Here's the generator itself:

#!/usr/bin/env Rscript 
#Random_number_generator_1.R
getwd()

printf <- function(...) cat(sprintf(...))
print("Please set the range of random numbers from 1 to n \n")

this_range = scan(file = 'stdin', what = integer(), nmax = 1)

numbers = 1:this_range

#print("How many random numbers in this range")
# random_numbers = scan(file = "", what = integer(), n = 1)

#if(random_numbers > this_range)
#{
#  remove(random_numbers)
#  print("Sorry. When Replace = FALSE. It's not possible to have more unique random numbers than the given range")
#}

random_numbers = sample(numbers)
random_numbers = sample(random_numbers)

print("Now remove any random number in the available range")
number_taken = scan(file = 'stdin' , what = integer(), nmax = 1)
random_numbers = random_numbers[random_numbers != number_taken]

print("Now insert any random number within the range")
number_inserted = scan(file = 'stdin', what = integer(), nmax = 1)

random_numbers = append(random_numbers, number_inserted)

print(random_numbers)

write(random_numbers, file = "my_random_numbers.txt", sep = " ")

Victory condition

This is , so the shortest code in bytes wins!

\$\endgroup\$
  • \$\begingroup\$ I almost solved this problem completely with 580 bytes of an R-script. \$\endgroup\$ – xyz123 Jun 17 '17 at 18:59
  • \$\begingroup\$ Please explain the dislike? I provided all details that you need to solve this problem including the desired output, and that the problem is truly solvable. \$\endgroup\$ – xyz123 Jun 17 '17 at 19:00
  • \$\begingroup\$ The question is too long. Delete the unrelated part, and move the most important part to the top, while keeping the rest as appendix, and outsource long programs to pastebin. \$\endgroup\$ – Leaky Nun Jun 17 '17 at 19:11
  • \$\begingroup\$ So it's a good question. You just don't like how it's formatted? What do you consider unrelated exactly? The last time my question was put on hold for being too vague, and now it's too exact? How does that work? \$\endgroup\$ – xyz123 Jun 17 '17 at 19:16
  • 1
    \$\begingroup\$ @xyz123 the balance between concision and precision is not always an easy one. \$\endgroup\$ – Leaky Nun Jun 17 '17 at 19:26
1
\$\begingroup\$

Jelly,  27 26  21 18 bytes

-8 bytes by applying a suggestion from Leaky Nun (use multi-set difference to J and a dequeue to replace a length-1 test)

ḲµVœ^J;LµḊȧȯ“Huh!?

A full program.

Try it online!

How?

ḲµVœ^J;LµḊȧȯ“Huh!? - Main link: list of characters
Ḳ                  - split at spaces, call this x
 µ                 - start a new monadic chain with the result of that on the left
  V                - evaluate as Jelly code (vectorises) -> list of integers
     J             - range(length(x)) -> [1,2,3,...,n]
   œ^              - multi-set difference -> one of: []; [i,j]; or [j,i]
       L           - length of x -> n
      ;            - concatenate -> one of: [n]; or [i, j, n]; or [j, i, n]
        µ          - new monadic chain, call that r
         Ḋ         - dequeue r -> one of: []; [j, n]; or [i, n]
          ȧ        - logical and with r -> []; [i, j, n]; or [j, i, n]
            “Huh!? - list of characters "Huh!?" (end of a program so no close quote needed)
           ȯ       - logical or -> one of "Huh!?"; [i, j, n]; or [j, i, n]
                   - implicit print
\$\endgroup\$
0
\$\begingroup\$

Mathematica, 70 bytes

{(a=(Range@1000~Complement~#)[[1]])+Tr@#-500500,a,1000}~Check~"Huh!?"&

Anonymous function. Takes a list of numbers as input and returns a list of numbers or a string as output. Ignore any messages generated. Works by finding j and then computing i.

\$\endgroup\$
  • \$\begingroup\$ That's pretty neat. \$\endgroup\$ – xyz123 Jun 17 '17 at 19:50

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.