Saltar al contenido
Underc0de /foro

[Exercises] Perl

🌐 This is an automatic translation of a topic from Underc0de, the largest Spanish-speaking hacking and IT community. Read the original (Spanish) and reply →
Juan ·
Well then, to give this section more life, I came up with a series of exercises. The first one would be to make a pyramid where you ask the user for the height of the pyramid and draw it, e.g.:<br /><br />Entering 5 as the height.<br /><br />
Código: text
Introduce la altura del triangulo: 5<br />     *<br />    **<br />   ***<br />  ****<br /> *****<br />
<br /><br />Go for it!<br />
rollth ·
I'm in, I got a bit tangled up, but here it is.<br /><br />
Código: c
// Programa "piramide" //<br /><br />#include <stdio.h><br /><br />int main() {<br />  int altura;<br />  printf ("Introduce la altura del triangulo: ");<br />  scanf ("%d",&altura);<br />  for (int i=1; i <= altura; i++){<br />    for (int a=1; a <= altura-i; a++){<br />      printf(" ");<br />    }<br />    for (int c=1; c <= i; c++){<br />      printf("*");<br />    }<br />    printf("\n");<br />  }<br />}<br />
<br /><br />EDIT<br /><br />By the way, here's this one that draws the outline of a triangle.<br /><br />
Código: c
// Este programa dibuja el contorno de un triangulo de 'x' altura //<br /><br />#include <stdio.h><br /><br />int main(){<br />  int altura;<br />  printf("Altura del triangulo: ");<br />  scanf("%d",&altura);<br />  for (int i = 1; i <= altura; i++){<br />    if (i == 1){ // Hace la primera linea.<br />      for (int a = 1; a <= altura-i ; a++){<br />        printf(" ");<br />      }<br />      printf("*");<br />    }<br />    else if (i != 1 && i != altura){ // Hace las lineas interioes<br />      for (int a = 1; a <= altura-i; a++){<br />        printf(" ");<br />      }<br />      printf("*");<br />      for (int a = 1; a <= i*2-3; a++){<br />        printf(" ");<br />      }<br />      printf("*");<br />    }<br />    else{ // Hace la ultima linea<br />      for (int a=1; a <= altura*2-1; a++){<br />        printf("*");<br />      }<br />    }<br />    printf("\n");<br />  }<br />}
Juan ·
But it was supposed to be done in perl...
rollth ·
I don't see where you say that, anyway I don't know how to program in perl.
LKI ·
Cita de: rollth en Diciembre 02, 2014, 07:26:29 PM<br />I don't see where you say that, anyway I don't know how to program in perl.<br />
<br /><br /> The section is specifically for scripting, development in perl. And Juan clarified that to revive this section a bit he was going to propose a series of challenges, they were supposed to be about this language..
Once ·
Código: perl
my $hasta;<br /><br />print "Tamano de la piramide: ";<br />$hasta = <>;<br /><br />for ($i=1; $i<=$hasta; $i++) {<br />    print " " x ($hasta - $i) . "*" x $i . "\r\n";<br />}
<br /><br />Greetings!
rollth ·
Cita de: LKI en Diciembre 03, 2014, 06:28:36 PM<br />
Cita de: rollth en Diciembre 02, 2014, 07:26:29 PM<br />I don't see where you say that, anyway I don't know how to program in perl.<br />
<br /><br /> The section is specifically for scripting, development in perl. And Juan clarified that to revive this section a bit he was going to propose a series of challenges, they were supposed to be about this language..<br />
<br /><br />True, I didn't notice it was in the Perl subsection.
Juan ·
Very good solution Once!!! taking advantage of the features of perl :)<br /><br />The loop could also be like this: <br /><br />
Código: perl
for my $i (1..$hasta)
<br /><br />Which is simpler.
Once ·
Cita de: Juan en Diciembre 04, 2014, 02:33:31 PM<br />Very good solution Once!!! taking advantage of the features of perl :)<br /><br />The loop could also be like this: <br /><br />
Código: perl
for my $i (1..$hasta)
<br /><br />Which is simpler.<br />
<br /><br />You're right, I didn't know that syntax.<br /><br />PS: Will there be more exercises?<br /><br />Greetings!
Juan ·
Once, post another one if you want in this same thread, or in another one.
Once ·
Ok, then complete the previous pyramid<br /><br />
Citar<br />Size: 5<br />    *<br />   ***<br />  *****<br /> *******<br />*********<br />
<br /><br />Greetings!
Grupo de WhatsApp