|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
# Q1. DNA Complement
---
## Learning objectives
* Experience solving a real-life inspired problem using programming
* Solve a problem using what we have learnt from week 2, including:
* Conditional statements
* Loops
---
## Background information
DNA, an abbreviation of deoxyribonucleic acid, codes genetic information for organisms. A DNA molecule consists of two strands coiled around each other, with each strand being held together by bonds between the bases: Adenine (A) pairs with thymine (T), whilst cytosine (C) pairs with guanine (G).
The two strands of DNA are complementary: each base in one strand is bonded to its complement in the other strand. For example, if one strand of DNA is 'AAAGTCTGAC', the complementary strand would be 'TTTCAGACTG'.
---
## Instruction
Write your code in [src/dna_complement.py](src/dna_complement.py) to implement the program that does the following:
> Get a string from a user that represents a DNA strand and print out its complement. The printout from the program follows the ones from the `"Example"` section below.
---
## Assumptions
* The given string is non-empty and is always composed of 'A', 'T', 'C' and/or 'G'
---
## Example
```
>>> Enter a DNA strand: AAAA
TTTT
```
```
>>> Enter a DNA strand: AAAGTCTGAC
TTTCAGACTG
```
Note here:
* `Enter a DNA strand: ` is the input prompt
* `TTTT` and `TTTCAGACTG` are printouts
* `AAAA` and `AAAGTCTGAC` are inputs from users
请问大神们这个代码该怎么写呀
这道题可以使用循环和条件语句来解决。我们可以遍历输入的DNA链,然后对于每个碱基,找到它的互补碱基并将其附加到一个新的字符串中,最后打印出这个新的字符串即可。以下是代码示例:
- # -*- coding: utf-8 -*-
- def complement(base):
- """
- 返回碱基的互补碱基
- """
- if base == 'A':
- return 'T'
- elif base == 'T':
- return 'A'
- elif base == 'C':
- return 'G'
- elif base == 'G':
- return 'C'
- else:
- return ''
- def dna_complement(dna):
- """
- 返回DNA链的互补链
- """
- complement_dna = ''
- for base in dna:
- complement_dna += complement(base)
- return complement_dna
- if __name__ == '__main__':
- dna = input('Enter a DNA strand: ')
- print(dna_complement(dna))
复制代码
在这个程序中,我们定义了两个函数: complement 和 dna_complement 。 complement 函数接收一个碱基,返回它的互补碱基。 dna_complement 函数接收一个DNA链,遍历它的每个碱基,并使用 complement 函数找到它的互补碱基,将其附加到一个新的字符串中。最后, dna_complement 函数返回这个新的字符串。
在程序的主函数中,我们使用 input 函数获取用户输入的DNA链,然后调用 dna_complement 函数,将其返回值打印出来。
|
|