Skip to content

Commit

Permalink
The Change Problem
Browse files Browse the repository at this point in the history
Solution to Rosalind Textbook Track Problem 5A: The Change Problem
  • Loading branch information
jschendel committed Mar 7, 2014
1 parent 84e0fd5 commit 1405d07
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Textbook_05A.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python
'''
A solution to a code challenges that accompanies Bioinformatics Algorithms: An Active-Learning Approach by Phillip Compeau & Pavel Pevzner.
The textbook is hosted on Stepic and the problem is listed on ROSALIND under the Textbook Track.
Problem Title: The Change Problem
Assignment #: 05
Problem ID: A
URL: http://rosalind.info/problems/5a/
'''


def dp_change(amount, coin_list):
'''Gives the minimum number of coins of denomination in coint_list necessary to create the given amount.'''
# Initiate the amounts larger than zero as a number greater than the upper bound.
min_coins = [0]+[(amount/min(coin_list))+1]*amount

# Use dynamic programming to build up to the desired amount.
for m in xrange(1,amount+1):
for coin in coin_list:
if m >= coin:
if min_coins[m-coin] + 1 < min_coins[m]:
min_coins[m] = min_coins[m-coin] + 1
return min_coins[amount]


def main():
'''Main call. Reads, runs, and saves problem specific data.'''
# Read the input data.
with open('data/textbook/rosalind_5a.txt') as input_data:
amount = int(input_data.readline().strip())
coin_list = map(int, input_data.readline().strip().split(','))

# Get the desired minimum number of coins.
min_number = str(dp_change(amount, coin_list))

# Print and save the answer.
print min_number
with open('output/textbook/Textbook_05A.txt', 'w') as output_data:
output_data.write(min_number)

if __name__ == '__main__':
main()
2 changes: 2 additions & 0 deletions data/textbook/rosalind_5a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
17369
1,3,5,9,11,14,18,19,24
1 change: 1 addition & 0 deletions output/textbook/Textbook_05A.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
725

0 comments on commit 1405d07

Please sign in to comment.