Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

95% of /prog/ can't write this simple program

Name: Anonymous 2008-05-12 15:10

You are given an array of 1001 integers. The integers are in random order, but you know each of the integers is between 1 and 1000 (inclusive). Each number appears only once in the array, except for one number, which occurs twice. Assume that you can access each element of the array only once.

Task: In your language of choice, write an function to find the repeated number without using auxiliary storage.

Name: Anonymous 2008-05-12 15:12

I just wanted to use a double for loop ;_;

Name: Anonymous 2008-05-12 15:12

I WOULD USE A BUBBLE SORT

Name: Anonymous 2008-05-12 15:13

Sorting and scanning for two identical adjacent nodes?

Name: Anonymous 2008-05-12 15:14

Easy - sum them, then subtract 500500 (the sum of the integers from 1 to 1000) from that sum. This will be the missing number.

Name: 4 2008-05-12 15:14

Oh, fuck you, >>3, you always have to be faster.

Name: Anonymous 2008-05-12 15:14

Fucking morons

I would add them all together and use the sum of the numbers 1-1000 (easily found) and compare.

GTFO THIS IS /PROG/

Name: Anonymous 2008-05-12 15:15

I choose the language FindDuplicateInArrayOf1001:

(this language does not have any syntax)

Name: Anonymous 2008-05-12 15:15

>>5
I dislike you.

Name: Anonymous 2008-05-12 15:16

>>9

Name: Anonymous 2008-05-12 15:17

>>5,7

YHBT

Name: Anonymous 2008-05-12 15:17

How the hell can you write a function without using "auxiliary storage", anyway? That's poorly defined.

Name: Anonymous 2008-05-12 15:20

>>1

(define (sum l)
  (define (sum-iter l n)
    (if (null? l) n
    (sum-iter (cdr l) (+ n (car l))))
(sum-iter l 0)

(define (solution l)
  (* (car (reverse l)) (+ (car (reverse l)) 1) .5))

Something like this?

Name: Anonymous 2008-05-12 15:21

>>12
Since the function itself is data, you have to transcend.

Name: Anonymous 2008-05-12 15:23

>>13
Bob saget! I've been trolled by the parenthesiae

Name: Anonymous 2008-05-12 15:31

>>12
def foo(list): return 500500 - reduce(lambda x,y: x + y, list)

Name: Anonymous 2008-05-12 15:31

By the way, to sum any integers from 1 to n, simply multiply n by (n + 1) and divide the result by 2.

Name: Anonymous 2008-05-12 15:32

How can we solve this when you haven't specified which number occurs twice?!
And should the function find the first or the last occurence of the repeated number?

Name: Anonymous 2008-05-12 15:32

>>17
You mean to sum all integers from 1 to n inclusive.

Name: Anonymous 2008-05-12 15:33

>>18
You thick cunt

Name: Anonymous 2008-05-12 15:35

findDup list = foldl1 xor list - (length list - 1)

Name: Anonymous 2008-05-12 15:57

findDup list = foldl1 xor list `xor` 1000

fix'd

Name: Anonymous 2011-02-03 5:01

Name: Anonymous 2011-02-04 18:42

Name: Sgt.Kabukiman遴 2012-05-23 5:37

All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List