Skip to content

Commit

Permalink
Merge pull request #6214 from anita-87/main
Browse files Browse the repository at this point in the history
Reto #16 - Go
  • Loading branch information
Roswell468 authored Jan 9, 2024
2 parents 942e99e + 15f4178 commit 49d54e0
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Retos/Reto #16 - LA ESCALERA [Media]/go/anita-87.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)

func main() {
// Read the number from console
fmt.Println("Enter the number of steps")
reader := bufio.NewReader(os.Stdin)
str, err := reader.ReadString('\n')
checkError(err)
str = strings.Trim(str, "\n")

// Convert to number
steps, err := strconv.Atoi(str)
checkError(err)

// Print steps
printSteps(steps)
}

func printSteps(steps int) {
result := ""
if steps == 0 {
result = "__"
}
if steps > 0 {
for i := steps - 1; i >= 0; i-- {
spaces := strings.Repeat(" ", i*2)
result += fmt.Sprintf("%s_|\n", spaces)
}
}
if steps < 0 {
steps = steps * -1
for i := 0; i <= steps-1; i++ {
spaces := strings.Repeat(" ", i*2)
result += fmt.Sprintf("%s|_\n", spaces)
}
}
fmt.Println(result)
}

func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}

0 comments on commit 49d54e0

Please sign in to comment.