Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Python

Python Discussion forum for Python, a high-level language with simple syntax, but yet powerful.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-18-2007, 01:09 PM
sovixi sovixi is offline
Newbie
 
Join Date: Apr 2007
Posts: 1
Rep Power: 0
sovixi is on a distinguished road
Default efficient algorithm

Hey I'm looking for a solution to the below question. Any recommendations on how to write this algorithm???
Thanks:


You are given a sequence s of numbers, in increasing order, for example:

s = 2, 5, 7, 9, 13, 15.

You are also given a number n, say n = 16. You have to try to find two numbers x and y from s, such that x+y = n, if such numbers exist. In this example, you could take x = 7 and y = 9.

However, you must do so as efficiently as possible: you must not perform more additions than the number of elements in s. Describe an algorithm to do this.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-19-2007, 11:44 AM
paddy3118 paddy3118 is offline
Newbie
 
Join Date: Apr 2007
Location: UK
Posts: 6
Rep Power: 0
paddy3118 is on a distinguished road
Thumbs up

Quote:
Originally Posted by sovixi View Post
Hey I'm looking for a solution to the below question. Any recommendations on how to write this algorithm???
Thanks:


You are given a sequence s of numbers, in increasing order, for example:

s = 2, 5, 7, 9, 13, 15.

You are also given a number n, say n = 16. You have to try to find two numbers x and y from s, such that x+y = n, if such numbers exist. In this example, you could take x = 7 and y = 9.

However, you must do so as efficiently as possible: you must not perform more additions than the number of elements in s. Describe an algorithm to do this.

The following Python 2.5 program works:

Code:
s = [2, 5, 7, 9, 13, 15]
ss = set(s)
mn = s[0]
n = 16
mx = n - mn

for x in s:
    if x > mx:
        # gone over the max value in s allowed
        break
    if n - x in ss:
        print "Using only these numbers:", s
        print "%s + %s == %s" % (x, n-x, n)
        break
It gives the following result when run:
Code:
Using only these numbers: [2, 5, 7, 9, 13, 15]
7 + 9 == 16
- Paddy.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-19-2007, 12:24 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,418
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default

Easy solution: load you data into an array
sarray = [2,5,7,9,13,15]
i = 0
j = 5
Then start adding sarray[i] + sarray[j] and comparing to the desired result. if too low, increment i. if too high, decrement j. wrap it in a for loop where i<j is required and you're done.

The key requirement is the number of additions to perform, that means each time you perform an addition, you MUST eliminate one of the numbers in the list.

Paddy's solution is clever, in that addition is not even used as an operation.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-19-2007, 02:47 PM
paddy3118 paddy3118 is offline
Newbie
 
Join Date: Apr 2007
Location: UK
Posts: 6
Rep Power: 0
paddy3118 is on a distinguished road
Lightbulb Better Python solution

I started looking at the corner cases and thought "what if the list of numbers has duplicates"? This lead me to update my solution use an implementation of a bag (also known as a multiset), instead of a set in the original.

Here is the extended program:

Code:
def add2x(s, n):
    ss = set(s)
    mn = s[0]
    mx = n - mn
    print "Using only these numbers:", s
    for x in s:
        if x > mx:
            # gone over the max value in s allowed
            break
        if n - x in ss:
            print "%s + %s == %s" % (x, n-x, n)
            return (x, n-x)
    print "Cannot make", n, "from two of the entries"

print "\n# Simple Case"
s = [2, 5, 7, 9, 13, 15]
n = 16
add2x(s, n)

print "\n# But wait..."
s = [2, 5, 7, 9, 13, 15]
assert add2x(s, 10) == (5,5)
print "# this is wrong! Only one 5 is in the list."

def dictasbag(sequence):
    ' Use dict as a bag or multiset'
    from collections import defaultdict

    bag = defaultdict(int)
    for item in sequence:
        bag[item] += 1
    return dict(bag)
    
def add2x_2(s, n):
    sbag = dictasbag(s)
    mn = s[0]
    mx = n - mn
    print "Using only these numbers:", s
    for x in s:
        if x > mx:
            # gone over the max value in s allowed
            break
        sbag[x] -= 1
        needed = n - x
        if needed in sbag and sbag[needed]:
            print "%s + %s == %s" % (x, n-x, n)
            return x, n-x
        sbag[x] += 1
    print "Cannot make", n, "from two of the entries"

print "\n# Using add2x_2"
s = [2, 5, 7, 9, 13, 15]
assert add2x_2(s, 10) == None
s = [2, 5, 5, 7, 9, 13, 15]
assert add2x_2(s, 10) == (5, 5)
And here is the generated output:

Code:
# Simple Case
Using only these numbers: [2, 5, 7, 9, 13, 15]
7 + 9 == 16

# But wait...
Using only these numbers: [2, 5, 7, 9, 13, 15]
5 + 5 == 10
# this is wrong! Only one 5 is in the list.

# Using add2x_2
Using only these numbers: [2, 5, 7, 9, 13, 15]
Cannot make 10 from two of the entries
Using only these numbers: [2, 5, 5, 7, 9, 13, 15]
5 + 5 == 10
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
New Algorithm Matches Any Tumor Cells To Best Possible Anticancer Treatments Kernel Programming News 0 07-29-2007 08:52 PM
Efficient Searching John General Programming 3 04-12-2007 11:59 AM
Algorithm help. patience General Programming 2 03-18-2007 01:09 AM
can sum1 help me in making a CPU scheduling algorithm program in c? bryan General Programming 2 11-28-2006 12:55 PM


All times are GMT -5. The time now is 08:49 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads