List of all available Websheets
Viewing java/10-objects/ComplementaryDNA by daveagp@gmail.com. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
Property | Value |
---|---|
Description html markup shown to student | Write a static method <tt>complementWC(String dna)</tt> that takes a String containing only the capital letters <tt>A</tt>, <tt>C</tt>, <tt>T</tt>, <tt>G</tt>, (representing DNA), and returns its <i>Watson-Crick complement</i>: replace <tt>A</tt> with <tt>T</tt>, <tt>C</tt> with <tt>G</tt>, and vice-versa. <p>For example, <tt>complementWC("GATTACA")</tt> should return <tt>"CTAATGT"</tt>. (You may assume the input is valid, there is no need to check for non-DNA characters.) <p> We encourage you to review the <a href="http://introcs.cs.princeton.edu/java/11cheatsheet/#String"> abbreviated String API</a> on the introcs cheatsheet. If you do this, you can solve this problem without using any loops. |
Public permissions | |
Engine | |
Template / Reference solution | public static String complementWC(String dna) { // replace all 'C's with 'G's and vice-versa \[ String tmp = dna.replaceAll("C", "X"); tmp = tmp.replaceAll("G", "C"); tmp = tmp.replaceAll("X", "G"); ]\ // replace all 'A's with 'T's and vice-versa \[ tmp = tmp.replaceAll("A", "X"); tmp = tmp.replaceAll("T", "A"); tmp = tmp.replaceAll("X", "T"); ]\ return \[tmp;]\ } |
Java test suite See manual | test("complementWC", "GATTACA"); test("complementWC", "CAT"); test("complementWC", "TAGACAT"); test("complementWC", "GCGAGTGAGC"); test("complementWC", ""); |
Solution visibility |
Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.