programa para imprimir secuencia de adn

Dado el valor de n, es decir, el número de lóbulos. Imprima la estructura de doble hélice del ácido desoxirribonucleico (ADN).
 

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

Explicación: 
el ADN consta principalmente de 4 hidrocarburos, es decir, citosina [C], guanina [G], adenina [A], timina [T].
Las bases del ADN se emparejan entre sí, A con T y C con G, para formar unidades llamadas pares de bases.
A continuación se muestra la implementación para imprimir la secuencia de ADN de doble hélice:
 

CPP

// CPP Program to print the
// 'n' lobes of DNA pattern
#include <bits/stdc++.h>
using namespace std;
 
// Function to print upper half
// of the DNA or the upper lobe
void printUpperHalf(string str)
{
    char first, second;
    int pos = 0;
     
    // Each half of the DNA is made of
    // combination of two compounds
    for (int i = 1; i <= 4; i++) {
 
        // Taking the two carbon
        // compounds from the string
        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;
    }
}
 
// Function to print lower half
// of the DNA or the lower lobe
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;
    }
}
 
// Function to print 'n' parts of DNA
void printDNA(string str[], int n)
{
    for (int i = 0; i < n; i++) {
 
        int x = i % 6;
         
        // Calling for upperhalf
        if (x % 2 == 0)
            printUpperHalf(str[x]);
        else
         
            // Calling for lowerhalf
            printLowerHalf(str[x]);
    }
}
 
// Driver function
int main()
{
 
    int n = 8;
     
    // combinations stored in the array
    string DNA[] = { "ATTAATTA", "TAGCTAGC", "CGCGATAT",
                   "TAATATGC", "ATCGTACG", "CGTAGCAT" };
    printDNA(DNA, n);
     
    return 0;
}

Java

// Java Program to print the
// 'n' lobes of DNA pattern
 
import java.io.*;
 
class GFG {
 
// Function to print upper half
// of the DNA or the upper lobe
static void printUpperHalf(String str)
{
    char first, second;
    int pos = 0;
      
    // Each half of the DNA is made of
    // combination of two compounds
    for (int i = 1; i <= 4; i++) {
  
        // Taking the two carbon
        // compounds from the string
        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);
    }
}
  
// Function to print lower half
// of the DNA or the lower lobe
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);
    }
}
  
// Function to print 'n' parts of DNA
static void printDNA(String str[], int n)
{
    for (int i = 0; i < n; i++) {
  
        int x = i % 6;
          
        // Calling for upperhalf
        if (x % 2 == 0)
            printUpperHalf(str[x]);
        else
          
            // Calling for lowerhalf
            printLowerHalf(str[x]);
    }
}
public static void main (String[] args) {
    int n = 8;
      
    // combinations stored in the array
    String DNA[] = { "ATTAATTA", "TAGCTAGC", "CGCGATAT",
                   "TAATATGC", "ATCGTACG", "CGTAGCAT" };
    printDNA(DNA, n);
            
    }
}
 
// This code is contributed by Gitanjali

Python3

# Python  Program to print the
# 'n' lobes of DNA pattern
import math
 
# Function to print upper half
# of the DNA or the upper lobe
def printUpperHalf(str):
 
    first=0
    second=0
    pos = 0
     
    # Each half of the DNA is made of
    # combination of two compounds
    for i in range(1,5):
  
        # Taking the two carbon
        # compounds from the string
        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)
     
 
  
# Function to print lower half
# of the DNA or the lower lobe
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)
 
  
# Function to print 'n' parts of DNA
def printDNA( str,  n):
 
    for i in range(0,n):
  
        x = i % 6
          
        # Calling for upperhalf
        if (x % 2 == 0):
            printUpperHalf(str[x])
        else:
          
            # Calling for lowerhalf
            printLowerHalf(str[x])
     
# driver code
n = 8
 
# combinations stored in the array
DNA = [ "ATTAATTA", "TAGCTAGC", "CGCGATAT",
      "TAATATGC", "ATCGTACG", "CGTAGCAT" ]
 
printDNA(DNA, n)
 
# This code is contributed by Gitanjali.

C#

// C# Program to print the 'n' lobes of
// DNA pattern
using System;
 
class GFG {
 
    // Function to print upper half
    // of the DNA or the upper lobe
    static void printUpperHalf(string str)
    {
         
        char first, second;
        int pos = 0;
         
        // Each half of the DNA is made of
        // combination of two compounds
        for (int i = 1; i <= 4; i++) {
     
            // Taking the two carbon
            // compounds from the string
            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);
        }
    }
 
    // Function to print lower half
    // of the DNA or the lower lobe
    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);
        }
    }
 
    // Function to print 'n' parts of DNA
    static void printDNA(string []str, int n)
    {
        for (int i = 0; i < n; i++) {
     
            int x = i % 6;
             
            // Calling for upperhalf
            if (x % 2 == 0)
                printUpperHalf(str[x]);
            else
             
                // Calling for lowerhalf
                printLowerHalf(str[x]);
        }
    }
 
    public static void Main () {
     
    int n = 8;
     
    // combinations stored in the array
    string []DNA = { "ATTAATTA", "TAGCTAGC",
                     "CGCGATAT", "TAATATGC",
                     "ATCGTACG", "CGTAGCAT" };
                      
    printDNA(DNA, n);
    }
}
 
// This code is contributed by vt_m.

PHP

<?php
// PHP implementation to print the
// 'n' lobes of DNA pattern
 
// Function to print upper half
// of the DNA or the upper lobe
function printUpperHalf($str)
{
    $pos = 0;
     
    // Each half of the DNA is made of
    // combination of two compounds
    for($i = 1; $i <= 4; $i++)
    {
 
        // Taking the two carbon
        // compounds from the string
        $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 to print lower half
// of the DNA or the lower lobe
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 to print 'n' parts of DNA
function printDNA($str, $n)
{
    for ($i = 0; $i < $n; $i++)
    {
 
        $x = $i % 6;
         
        // Calling for upperhalf
        if ($x % 2 == 0)
            printUpperHalf($str[$x]);
        else
         
            // Calling for lowerhalf
            printLowerHalf($str[$x]);
    }
}
 
// Driver code
$n = 8;
$DNA = array( "ATTAATTA", "TAGCTAGC",
              "CGCGATAT", "TAATATGC",
              "ATCGTACG", "CGTAGCAT" );
printDNA($DNA, $n);
 
// This code is contributed by mits.
?>

Javascript

<script>
 
// javascript Program to print the
// 'n' lobes of DNA pattern
// Function to print upper half
// of the DNA or the upper lobe
function printUpperHalf(str)
{
    var first, second;
    var pos = 0;
      
    // Each half of the DNA is made of
    // combination of two compounds
    for (var i = 1; i <= 4; i++) {
  
        // Taking the two carbon
        // compounds from the string
        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 to print lower half
// of the DNA or the lower lobe
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 to print 'n' parts of DNA
function printDNA(str , n)
{
    for (var i = 0; i < n; i++) {
  
        var x = i % 6;
          
        // Calling for upperhalf
        if (x % 2 == 0)
            printUpperHalf(str[x]);
        else
          
            // Calling for lowerhalf
            printLowerHalf(str[x]);
    }
}
var n = 8;
 
// combinations stored in the array
var DNA = [ "ATTAATTA", "TAGCTAGC", "CGCGATAT",
               "TAATATGC", "ATCGTACG", "CGTAGCAT" ];
printDNA(DNA, n);
 
// This code contributed by Princi Singh
</script>

Producción : 
 

   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

 Complejidad de tiempo: O (N), ya que estamos usando un bucle para atravesar N veces. Donde N es la entrada dada.

Espacio auxiliar: O(1), ya que no estamos utilizando ningún espacio adicional.

Publicación traducida automáticamente

Artículo escrito por parthendo y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *