Doppleganger challenge

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

55 Replies - 13520 Views - Last Post: 03 September 2012 - 02:25 PM Rate Topic: -----

#1 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Doppleganger challenge

Post icon  Posted 27 July 2011 - 12:15 PM

*
POPULAR

Here's a challenge that's short and sweet.

Write a function that takes two lists of ints as input and outputs a single list that contains only the values that appear in both lists. Shoot for efficiency!
Is This A Good Question/Topic? 7
  • +

Replies To: Doppleganger challenge

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: Doppleganger challenge

Posted 27 July 2011 - 12:19 PM

Does it have to be in Python?
Was This Post Helpful? 0
  • +
  • -

#3 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Doppleganger challenge

Posted 27 July 2011 - 12:19 PM

def doppleganger(listone, listtwo):
    return set(listone).intersection(set(listtwo))


Thats my answer :)
Was This Post Helpful? 4
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Doppleganger challenge

Posted 27 July 2011 - 12:26 PM

Haha don't be lame brewer! Create an actual algorithm for solving the problem

Quote

Does it have to be in Python?

Any high level language will do fine... but no functional languages :)

Put it in any language you damn well please.

This post has been edited by atraub: 28 July 2011 - 09:42 PM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5936
  • View blog
  • Posts: 12,862
  • Joined: 16-October 07

Re: Doppleganger challenge

Posted 27 July 2011 - 12:29 PM

Spoiler


Edit: Hmmm, got it backwards. brb. :P

This post has been edited by baavgai: 27 July 2011 - 12:31 PM

Was This Post Helpful? 0
  • +
  • -

#6 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Doppleganger challenge

Posted 27 July 2011 - 12:31 PM

This sounds like fun, I'll give it a shot when I get off work.
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5936
  • View blog
  • Posts: 12,862
  • Joined: 16-October 07

Re: Doppleganger challenge

Posted 27 July 2011 - 12:33 PM

Right, sorry about that.

Spoiler


Actually an easier question. :P
Was This Post Helpful? 2
  • +
  • -

#8 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Doppleganger challenge

Posted 27 July 2011 - 12:36 PM

haha these are some creative answers for sure. Not exactly what I was hoping for though :sweatdrop:
Was This Post Helpful? 0
  • +
  • -

#9 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 797
  • Joined: 08-June 10

Re: Doppleganger challenge

Posted 27 July 2011 - 12:46 PM

How should it behave when there are multiples of an integer in a list? What's the output for: ([1,2,3,3,3,4,4], [3,1,1,6])?

Here's a less-traditional way of doing this; not sure if dict's hashing of the key will give a performance gain or loss:
Spoiler

Was This Post Helpful? 0
  • +
  • -

#10 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,605
  • Joined: 20-January 09

Re: Doppleganger challenge

Posted 27 July 2011 - 12:47 PM

PHP

function dg($listone, $listtwo)
{
    return array_merge($listone, $listtwo);
}


Was This Post Helpful? 0
  • +
  • -

#11 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Doppleganger challenge

Posted 27 July 2011 - 12:51 PM

View Postatraub, on 27 July 2011 - 08:26 PM, said:

Haha don't be lame brewer! Create an actual algorithm for solving the problem

Quote

Does it have to be in Python?

Any high level language will do fine... but no functional languages :)


Why no functional languages? If you don't use core functions, you still have to write an algorithm for this. It isn't cheating by any means.
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5936
  • View blog
  • Posts: 12,862
  • Joined: 16-October 07

Re: Doppleganger challenge

Posted 27 July 2011 - 12:53 PM

Hmm... what if the lists look like [ 2, 5, 5, 5, 8, 3, 3, 12 ], [ 12, 5, 5, 1, 3, 3, 3, 4 ].

The answer I'd want ( order doesn't matter): [3, 3, 12, 5, 5]

That's far more fun:
Spoiler


I'm sure someone can do better than that.
Was This Post Helpful? 1
  • +
  • -

#13 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Doppleganger challenge

Posted 27 July 2011 - 12:58 PM

Fine, functional languages too
Was This Post Helpful? 0
  • +
  • -

#14 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: Doppleganger challenge

Posted 27 July 2011 - 12:59 PM

Another O(len(seq1) + len(seq2)) solution:
Spoiler


Without using a hash table, i.e. a dictionary or set, it would be troublesome.
Was This Post Helpful? 0
  • +
  • -

#15 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Doppleganger challenge

Posted 27 July 2011 - 01:06 PM

am I the only one who did not use a set or a dictionary?
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »