Program to print DNA sequence
Last Updated :
20 Feb, 2023
Given the value of n i.e, the number of lobes. Print the double-helix structure of Deoxyribonucleic acid(DNA).
Input: n = 8
Output:
AT
T--A
A----T
T------A
T------A
G----C
T--A
GC
CG
C--G
A----T
A------T
T------A
A----T
A--T
GC
AT
C--G
T----A
C------G
C------G
T----A
G--C
AT
AT
T--A
A----T
T------A
T------A
G----C
T--A
GC
Explanation :
DNA primarily consists of 4 hydrocarbons i.e. cytosine [C], guanine [G], adenine[A], thymine [T].
DNA bases pair up with each other, A with T and C with G, to form units called base pairs.
Below is the implementation to print the double-helix DNA sequence :
CPP
#include <bits/stdc++.h>
using namespace std;
void printUpperHalf(string str)
{
char first, second;
int pos = 0;
for ( int i = 1; i <= 4; i++) {
first = str[pos];
second = str[pos + 1];
pos += 2;
for ( int j = 4 - i; j >= 1; j--)
cout << " " ;
cout << first;
for ( int j = 1; j < i; j++)
cout << "--" ;
cout << second << endl;
}
}
void printLowerHalf(string str)
{
char first, second;
int pos = 0;
for ( int i = 1; i <= 4; i++) {
first = str[pos];
second = str[pos + 1];
pos += 2;
for ( int j = 1; j < i; j++)
cout << " " ;
cout << first;
for ( int j = 4 - i; j >= 1; j--)
cout << "--" ;
cout << second << endl;
}
}
void printDNA(string str[], int n)
{
for ( int i = 0; i < n; i++) {
int x = i % 6;
if (x % 2 == 0)
printUpperHalf(str[x]);
else
printLowerHalf(str[x]);
}
}
int main()
{
int n = 8;
string DNA[] = { "ATTAATTA" , "TAGCTAGC" , "CGCGATAT" ,
"TAATATGC" , "ATCGTACG" , "CGTAGCAT" };
printDNA(DNA, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void printUpperHalf(String str)
{
char first, second;
int pos = 0 ;
for ( int i = 1 ; i <= 4 ; i++) {
first = str.charAt(pos);
second = str.charAt(pos+ 1 );
pos += 2 ;
for ( int j = 4 - i; j >= 1 ; j--)
System.out.print( " " );
System.out.print(first);
for ( int j = 1 ; j < i; j++)
System.out.print( "--" );
System.out.println(second);
}
}
static void printLowerHalf(String str)
{
char first, second;
int pos = 0 ;
for ( int i = 1 ; i <= 4 ; i++) {
first = str.charAt(pos);
second = str.charAt(pos+ 1 );
pos += 2 ;
for ( int j = 1 ; j < i; j++)
System.out.print( " " );
System.out.print(first);
for ( int j = 4 - i; j >= 1 ; j--)
System.out.print( "--" );
System.out.println(second);
}
}
static void printDNA(String str[], int n)
{
for ( int i = 0 ; i < n; i++) {
int x = i % 6 ;
if (x % 2 == 0 )
printUpperHalf(str[x]);
else
printLowerHalf(str[x]);
}
}
public static void main (String[] args) {
int n = 8 ;
String DNA[] = { "ATTAATTA" , "TAGCTAGC" , "CGCGATAT" ,
"TAATATGC" , "ATCGTACG" , "CGTAGCAT" };
printDNA(DNA, n);
}
}
|
Python3
import math
def printUpperHalf( str ):
first = 0
second = 0
pos = 0
for i in range ( 1 , 5 ):
first = str [pos]
second = str [pos + 1 ]
pos + = 2
for j in range ( 4 - i, 0 , - 1 ):
print ( " " ,end = "")
print (first,end = "")
for j in range ( 1 , i):
print ( "--" ,end = "")
print (second)
def printLowerHalf( str ):
first = 0
second = 0
pos = 0
for i in range ( 1 , 5 ):
first = str [pos]
second = str [pos + 1 ]
pos + = 2
for j in range ( 1 ,i):
print ( " " ,end = "")
print (first,end = "")
for j in range ( 4 - i, 0 , - 1 ):
print ( "--" ,end = "")
print (second)
def printDNA( str , n):
for i in range ( 0 ,n):
x = i % 6
if (x % 2 = = 0 ):
printUpperHalf( str [x])
else :
printLowerHalf( str [x])
n = 8
DNA = [ "ATTAATTA" , "TAGCTAGC" , "CGCGATAT" ,
"TAATATGC" , "ATCGTACG" , "CGTAGCAT" ]
printDNA(DNA, n)
|
C#
using System;
class GFG {
static void printUpperHalf( string str)
{
char first, second;
int pos = 0;
for ( int i = 1; i <= 4; i++) {
first = str[pos];
second = str[pos+1];
pos += 2;
for ( int j = 4 - i; j >= 1; j--)
Console.Write( " " );
Console.Write(first);
for ( int j = 1; j < i; j++)
Console.Write( "--" );
Console.WriteLine(second);
}
}
static void printLowerHalf( string str)
{
char first, second;
int pos = 0;
for ( int i = 1; i <= 4; i++) {
first = str[pos];
second = str[pos+1];
pos += 2;
for ( int j = 1; j < i; j++)
Console.Write( " " );
Console.Write(first);
for ( int j = 4 - i; j >= 1; j--)
Console.Write( "--" );
Console.WriteLine(second);
}
}
static void printDNA( string []str, int n)
{
for ( int i = 0; i < n; i++) {
int x = i % 6;
if (x % 2 == 0)
printUpperHalf(str[x]);
else
printLowerHalf(str[x]);
}
}
public static void Main () {
int n = 8;
string []DNA = { "ATTAATTA" , "TAGCTAGC" ,
"CGCGATAT" , "TAATATGC" ,
"ATCGTACG" , "CGTAGCAT" };
printDNA(DNA, n);
}
}
|
PHP
<?php
function printUpperHalf( $str )
{
$pos = 0;
for ( $i = 1; $i <= 4; $i ++)
{
$first = $str [ $pos ];
$second = $str [ $pos + 1];
$pos += 2;
for ( $j = 4 - $i ; $j >= 1; $j --)
echo " " ;
echo $first ;
for ( $j = 1; $j < $i ; $j ++)
echo "--" ;
echo $second . "\n" ;
}
}
function printLowerHalf( $str )
{
$pos = 0;
for ( $i = 1; $i <= 4; $i ++)
{
$first = $str [ $pos ];
$second = $str [ $pos + 1];
$pos += 2;
for ( $j = 1; $j < $i ; $j ++)
echo " " ;
echo $first ;
for ( $j = 4 - $i ; $j >= 1; $j --)
echo "--" ;
echo $second . "\n" ;
}
}
function printDNA( $str , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
$x = $i % 6;
if ( $x % 2 == 0)
printUpperHalf( $str [ $x ]);
else
printLowerHalf( $str [ $x ]);
}
}
$n = 8;
$DNA = array ( "ATTAATTA" , "TAGCTAGC" ,
"CGCGATAT" , "TAATATGC" ,
"ATCGTACG" , "CGTAGCAT" );
printDNA( $DNA , $n );
?>
|
Javascript
<script>
function printUpperHalf(str)
{
var first, second;
var pos = 0;
for ( var i = 1; i <= 4; i++) {
first = str.charAt(pos);
second = str.charAt(pos+1);
pos += 2;
for ( var j = 4 - i; j >= 1; j--)
document.write( " " );
document.write(first);
for ( var j = 1; j < i; j++)
document.write( "--" );
document.write(second+ "<br>" );
}
}
function printLowerHalf(str)
{
var first, second;
var pos = 0;
for ( var i = 1; i <= 4; i++) {
first = str.charAt(pos);
second = str.charAt(pos+1);
pos += 2;
for ( var j = 1; j < i; j++)
document.write( " " );
document.write(first);
for ( var j = 4 - i; j >= 1; j--)
document.write( "--" );
document.write(second+ "<br>" );
}
}
function printDNA(str , n)
{
for ( var i = 0; i < n; i++) {
var x = i % 6;
if (x % 2 == 0)
printUpperHalf(str[x]);
else
printLowerHalf(str[x]);
}
}
var n = 8;
var DNA = [ "ATTAATTA" , "TAGCTAGC" , "CGCGATAT" ,
"TAATATGC" , "ATCGTACG" , "CGTAGCAT" ];
printDNA(DNA, n);
</script>
|
Output :
AT
T--A
A----T
T------A
T------A
G----C
T--A
GC
CG
C--G
A----T
A------T
T------A
A----T
A--T
GC
AT
C--G
T----A
C------G
C------G
T----A
G--C
AT
AT
T--A
A----T
T------A
T------A
G----C
T--A
GC
Time Complexity: O(N), as we are using a loop to traverse N times. Where N is the input given.
Auxiliary Space: O(1), as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...