Initial tex samples with listing code
This commit is contained in:
163
GoBook.tex
Normal file
163
GoBook.tex
Normal file
@@ -0,0 +1,163 @@
|
||||
\documentclass[]{article}
|
||||
\usepackage[utf8]{inputenc}
|
||||
\usepackage[spanish]{babel}
|
||||
\usepackage{listings}
|
||||
\usepackage{xcolor}
|
||||
\usepackage[a4paper, total={6.5in, 8in}]{geometry}
|
||||
|
||||
\definecolor{codegreen}{rgb}{0,0.6,0}
|
||||
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
|
||||
\definecolor{codepurple}{rgb}{0.58,0,0.82}
|
||||
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
|
||||
|
||||
\lstdefinestyle{mystyle}{
|
||||
backgroundcolor=\color{backcolour},
|
||||
commentstyle=\color{codegreen},
|
||||
keywordstyle=\color{magenta},
|
||||
numberstyle=\tiny\color{codegray},
|
||||
stringstyle=\color{codepurple},
|
||||
basicstyle=\ttfamily\footnotesize,
|
||||
breakatwhitespace=false,
|
||||
breaklines=true,
|
||||
captionpos=b,
|
||||
keepspaces=true,
|
||||
numbers=left,
|
||||
numbersep=5pt,
|
||||
showspaces=false,
|
||||
showstringspaces=false,
|
||||
showtabs=false,
|
||||
tabsize=2
|
||||
}
|
||||
|
||||
\lstset{style=mystyle}
|
||||
|
||||
\newcommand{\quotes}[1]{``#1''}
|
||||
|
||||
%opening
|
||||
\title{Manejadores de rutas en HTTP}
|
||||
\author{Asistente de OpenAI}
|
||||
\date{}
|
||||
|
||||
|
||||
|
||||
\begin{document}
|
||||
|
||||
\section{Código en Go - findPeak}
|
||||
|
||||
Aquí está el código de Go para la funcion findPeak
|
||||
|
||||
\begin{lstlisting}[language=Go]
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// FindPeak finds the peak element in the array
|
||||
func findPeak(arr []int) int {
|
||||
n := len(arr)
|
||||
|
||||
// first or last element is peak element
|
||||
if n == 1 {
|
||||
return 0
|
||||
}
|
||||
if arr[0] >= arr[1] {
|
||||
return 0
|
||||
}
|
||||
if arr[n-1] >= arr[n-2] {
|
||||
return n - 1
|
||||
}
|
||||
|
||||
// check for every other element
|
||||
for i := 1; i < n-1; i++ {
|
||||
// check if the neighbors are smaller
|
||||
if arr[i] >= arr[i-1] && arr[i] >= arr[i+1] {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1 // indicating no peak found
|
||||
}
|
||||
|
||||
func main() {
|
||||
arr := []int{1, 3, 20, 4, 1, 0}
|
||||
fmt.Println("Index of a peak point is", findPeak(arr))
|
||||
}
|
||||
\end{lstlisting}
|
||||
|
||||
|
||||
\section{Explicación del código en Go con lstlisting}
|
||||
|
||||
Aquí está el código traducido a Go junto con una explicación de cada línea:
|
||||
|
||||
\begin{lstlisting}[language=Go, numbers=left, stepnumber=1]
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
\end{lstlisting}
|
||||
|
||||
\textbf{Líneas 1-2:} Definimos el paquete principal `main` y luego importamos el paquete `fmt`, que necesitaremos para imprimir resultados en la consola.
|
||||
|
||||
\begin{lstlisting}[language=Go, numbers=left, stepnumber=8]
|
||||
|
||||
func findPeak(arr []int) int {
|
||||
n := len(arr)
|
||||
\end{lstlisting}
|
||||
|
||||
\textbf{Líneas 3-4:} Definimos una función llamada `findPeak` que toma una matriz de enteros como entrada y devuelve un entero (el índice del punto de pico). Calculamos la longitud de la matriz de entrada `arr` y la almacenamos en la variable `n`.
|
||||
|
||||
\begin{lstlisting}[language=Go]
|
||||
|
||||
if n == 1 {
|
||||
return 0
|
||||
}
|
||||
\end{lstlisting}
|
||||
|
||||
\textbf{Líneas 5-6:} Si la longitud de la matriz es 1, significa que hay solo un elemento en la matriz, y ese elemento es el pico. Entonces, devolvemos 0 como el índice del pico.
|
||||
|
||||
\begin{lstlisting}
|
||||
|
||||
if arr[0] >= arr[1] {
|
||||
return 0
|
||||
}
|
||||
\end{lstlisting}
|
||||
|
||||
\textbf{Líneas 7-8:} Comprobamos si el primer elemento de la matriz es mayor o igual que el segundo. Si es así, entonces el primer elemento es el pico y devolvemos 0 como el índice del pico.
|
||||
|
||||
\begin{lstlisting}[language=Go, numbers=left, stepnumber=5, firstnumber=9]
|
||||
|
||||
if arr[n-1] >= arr[n-2] {
|
||||
return n - 1
|
||||
}
|
||||
\end{lstlisting}
|
||||
|
||||
\textbf{Líneas 9-10:} Similar al paso anterior, aquí comprobamos si el último elemento de la matriz es mayor o igual que el segundo desde el final. Si es así, entonces el último elemento es el pico y devolvemos su índice.
|
||||
|
||||
\begin{lstlisting}[language=Go, numbers=left, stepnumber=5, firstnumber=11]
|
||||
|
||||
for i := 1; i < n-1; i++ {
|
||||
\end{lstlisting}
|
||||
|
||||
\textbf{Líneas 11-12:} Este bucle itera sobre los elementos de la matriz, excepto el primero y el último.
|
||||
|
||||
\begin{lstlisting}[language=Go, numbers=left, stepnumber=5, firstnumber=13]
|
||||
|
||||
if arr[i] >= arr[i-1] && arr[i] >= arr[i+1] {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
\end{lstlisting}
|
||||
|
||||
\textbf{Líneas 13-18:} Dentro del bucle, comprobamos si el elemento actual es mayor o igual que sus vecinos. Si es así, entonces el elemento actual es el pico y devolvemos su índice. Si no se encuentra ningún pico, devolvemos -1 para indicar que no se encontró ningún pico en la matriz.
|
||||
|
||||
\begin{lstlisting}[language=Go, numbers=left, stepnumber=5, firstnumber=20]
|
||||
|
||||
func main() {
|
||||
arr := []int{1, 3, 20, 4, 1, 0}
|
||||
fmt.Println("Index of a peak point is", findPeak(arr))
|
||||
}
|
||||
\end{lstlisting}
|
||||
|
||||
\textbf{Líneas 20-22:} Ahora definimos la función `main`, que es la entrada principal del programa. Creamos una matriz de enteros llamada `arr` con valores dados. Llamamos a la función `findPeak` con la matriz `arr` y mostramos el índice del punto de pico en la consola.
|
||||
|
||||
\end{document}
|
||||
Reference in New Issue
Block a user