6
\$\begingroup\$

A set of whole numbers (0-) will be given separated by a space. Some numbers will be missing between certain numbers in the set. Your job is to find the missing odd numbers between them.

Conditions

  1. The input will be a one line string.
  2. Your code must left out a variable for inserting the input case string.
  3. The output should be a string containing all the missing odd numbers in the set of numbers given as input.
  4. The numbers of the output must be separated by a comma - ,.
  5. No extra characters should be included in the output.
  6. If there are no odd numbers missing, the code should print out null or a blank line.

Here are some sample inputs and outputs according to its order :

Input

4 6 8 10
5 15 18 21 26
2 15 6
1 20
1 3 5 7 9

Output

5,7,9
7,9,11,13,17,19,23,25
3,5,7,9,11,13
3,5,7,9,11,13,15,17,19
null

The winner will be decided by the Code length.

The calculation of code length won't include the variable that is left for the test case.

\$\endgroup\$
  • 7
    \$\begingroup\$ Seems awfully unfair to state that you'll be awarding the win to PHP, even if it's longer than the shortest not-PHP solution. \$\endgroup\$ – Kyle Kanos May 30 '14 at 14:31
  • 3
    \$\begingroup\$ Removed arbitrary language requirement; challenges here are generally language-agnostic unless a very good reason exists to limit or otherwise suppress other languages. \$\endgroup\$ – Doorknob May 30 '14 at 14:35
  • 7
    \$\begingroup\$ Again, why must your answer be in PHP? This is beginning to sound like a homework problem.... \$\endgroup\$ – Kyle Kanos May 30 '14 at 14:37
  • 3
    \$\begingroup\$ "Your code must left out a variable for inserting the input case string." — I don't understand what this means. \$\endgroup\$ – squeamish ossifrage May 30 '14 at 14:45
  • 3
    \$\begingroup\$ @James Even if the numbers in the set are out of order like 5 9 3, the code should find the missing odd numbers. The conditions never said that the numbers given will be in order. \$\endgroup\$ – Subin May 31 '14 at 4:32

33 Answers 33

0
\$\begingroup\$

05AB1E, 20 13 bytes

#DZÅÉsWL«K',ý

Matching the specs exactly.. Input as space-delimited string, output as comma-separated string, or a blank line if no odd integers are found.

Try it online or verify all test cases.

With flexible/reasonable I/O rules this would be 8 bytes:

ZÅÉsWL«K

Input as list, output [] if no odd integers are found.

Try it online or verify all test cases.

Explanation:

#               # Split the (implicit) input-string on spaces
 D              # Duplicate this list
  Z             # Get the maximum (without popping the list itself)
   ÅÉ           # Create a list of all positive odd integers in the range [1, max]
     s          # Swap so the input-list is at the top of the stack again
      W         # Get the minimum (without popping the list itself)
       L        # Create a list in the range [1, min]
        «       # Merge it with the input-list
         K      # Remove all values in this list from the odd list we created earlier
          ',ý  '# Join the result by ","
                # (and output the top of the stack implicitly as result)
\$\endgroup\$
0
\$\begingroup\$

PHP, 71 bytes

<?=join(array_diff(range(min($a=explode(' ',$i))|1,max($a),2),$a),',');

Input as predefined variable $i, per spec.

Try it online!

Ungolfed / explanation:

// input is from predefined variable $i
$i = '4 6 8 10';

// split space-delimited string from input into array
$a = explode( ' ', $i );

// create an array of odd numbers starting with the lowest odd number from the input
// ex: [4, 6, 8, 10] => [5, 7, 9]
//     [3, 4, 6, 7]  => [3, 5, 7]
$o = range( min( $a ) | 1, max( $a ), 2 );

// remove any numbers in the input array from the array of odds
// ex: remove [3, 4, 6, 7] from [3, 5, 7] => [ 5 ]
$b = array_diff( $a, $o );

// output array as comma-delimited string
echo join( ',', $b );       

Or 74 bytes with input from STDIN, 63 bytes with flexible I/O.

\$\endgroup\$
-1
\$\begingroup\$

Javascript

function a(e){e=e.split(" ");var t=[];for(i=1;i<e.length*2;i+=2){if(e.join(",").indexOf(i)>-1){}else{t.push(i)}}return t.join(",")}

Ungolfed

function missingOdd(str) {
    str = str.split(' ');
    var out = [];
    for (i=1; i < str.length * 2; i+=2) {
        if (str.join(',').indexOf(i) > -1) {}else{
            out.push(i);
        }
    }
    return out.join(',');
}
\$\endgroup\$
  • \$\begingroup\$ If I give the input 4 6 8 10, it outputs 3,5,7. It's wrong output since between 4 & 10, there is no 3. \$\endgroup\$ – Subin Oct 9 '14 at 5:42

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.