forked from Shivi91/Rosalind-1
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solution to Rosalind Textbook Track Problem 5A: The Change Problem
- Loading branch information
Showing
3 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
17369 | ||
1,3,5,9,11,14,18,19,24 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
725 |