Last active
April 13, 2026 23:58
-
-
Save HiroNakamura/cbf3ec7580569d0185fc0313c4628f6c to your computer and use it in GitHub Desktop.
Fundamentos de programación en Ballerina, Go, Rust, etc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ballerina/io; | |
| const int MAX = 100; | |
| public function main() { | |
| io:println("\t ******************************************"); | |
| io:println("\t [ Fundamentos de programación en Ballerina ]"); | |
| test1(); | |
| test2(); | |
| io:println("\t ******************************************"); | |
| } | |
| function test2() { | |
| int compra = 0; | |
| int descuento = 0; | |
| io:print("Introduce valor de la compra: "); | |
| var input = io:readln(); | |
| var parsed = int:fromString(input.trim()); | |
| if parsed is int { | |
| compra = parsed; | |
| } else { | |
| io:println("Debes ingresar un número válido."); | |
| return; | |
| } | |
| io:println(string `El valor de la compra es de $${compra}`); | |
| if compra >= 500 { | |
| descuento = compra - (compra * 25 / 100); // 25% de descuento | |
| } | |
| if descuento > 0 { | |
| io:println(string `El cliente solo paga $${descuento}`); | |
| } else { | |
| io:println(string `El cliente paga $${compra}`); | |
| } | |
| } | |
| function test1() { | |
| io:println("\t [ Tipos de datos en Ballerina ]"); | |
| int numeroInt = 340; | |
| int numeroIntTwo = 90; | |
| boolean band = true; | |
| float flotante = 45.32; | |
| string[] libros = []; | |
| libros.push("Nacida de un sueño."); | |
| libros.push("Elisa."); | |
| libros.push("La torre."); | |
| libros.push("El no nacido."); | |
| map<string> mapaLibros = {}; | |
| int cont = 0; | |
| io:println("Entero: " + numeroInt.toString()); | |
| io:println("Flotante: " + flotante.toString()); | |
| if band { | |
| io:println("Esto es verdadero."); | |
| band = false; | |
| } | |
| io:println(""); | |
| if band { | |
| io:println("Esto es falso."); | |
| } else if !band { | |
| io:println("Esto es verdadero."); | |
| } else { | |
| io:println("Esto es falso."); | |
| } | |
| io:println(""); | |
| while numeroIntTwo < MAX { | |
| io:println(string `Hola ${numeroIntTwo}`); | |
| numeroIntTwo += 1; | |
| } | |
| io:println(""); | |
| foreach int i in 0 ..< MAX { | |
| if i % 3 == 0 && i % 5 == 0 { | |
| io:println(string `\t Hola ${i}`); | |
| } else { | |
| io:println(string `Goodbye ${i}`); | |
| } | |
| } | |
| io:println(""); | |
| foreach var libro in libros { | |
| io:println(string `\t Título: ${libro}`); | |
| } | |
| io:println(""); | |
| foreach var libro in libros { | |
| mapaLibros[cont.toString()] = libro; | |
| cont += 1; | |
| } | |
| io:println(""); | |
| foreach string clave in mapaLibros.keys() { | |
| string? titulo = mapaLibros[clave]; // Acceso seguro | |
| if titulo is string { | |
| io:println(string `\t indice: ${clave} - Título: ${titulo}`); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ballerina/io; | |
| const MAX = 200; | |
| type Bailarin record{ | |
| string id?; | |
| int numero; | |
| boolean registrado; | |
| string nombre?; | |
| }; | |
| public function calcularProbabilidad(int casosFavorables, int totalCasos) returns float|error { | |
| if totalCasos <= 0 { | |
| return error("El total de casos debe ser mayor que 0"); | |
| } | |
| if casosFavorables < 0 { | |
| return error("Los casos favorables no pueden ser negativos"); | |
| } | |
| if casosFavorables > totalCasos { | |
| return error("Los casos favorables no pueden ser mayores que el total de casos"); | |
| } | |
| float probabilidad = <float>casosFavorables / <float>totalCasos; | |
| return probabilidad; | |
| } | |
| # Función principal del programa. | |
| /// La función principal que recibe argumentos desde la línea de comandos. | |
| /// | |
| /// + args - Argumentos pasados desde la línea de comandos | |
| public function main(string... args) { | |
| io:println("\t [ Fundamentos de programación en Ballerina ]"); | |
| testA(); | |
| testB(); | |
| testC(); | |
| if args.length() == 0 { | |
| io:println("No se proporcionaron argumentos."); | |
| return; | |
| } | |
| io:println("Argumentos recibidos:"); | |
| foreach var arg in args { | |
| io:println(arg); | |
| } | |
| foreach int i in 0 ..< args.length() { | |
| io:println("Argumento ", i + 1, ": ", args[i]); | |
| } | |
| if args.length() >= 2 { | |
| var num1 = int:fromString(args[0]); | |
| var num2 = int:fromString(args[1]); | |
| if num1 is int && num2 is int { | |
| io:println("Suma de ", num1, " y ", num2, ": ", num1 + num2); | |
| } else { | |
| io:println("Error: Los argumentos deben ser números enteros."); | |
| } | |
| } else { | |
| io:println("Por favor, proporcione al menos dos argumentos numéricos."); | |
| } | |
| } | |
| public function testC(){ | |
| int numCasosFav = 23; | |
| int totalCasos = 56; | |
| io:println("\t [ Calculando la probabilidad ]"); | |
| io:println("No. casos favorables: ",numCasosFav); | |
| io:println("No. casos realizados: ",totalCasos); | |
| io:println("Probabilidad: ",calcularProbabilidad(numCasosFav,totalCasos)); | |
| } | |
| public function testB(){ | |
| Bailarin bailarin = {id: "1222",numero: 45,registrado: true, nombre:"Jean Pierre"}; | |
| io:println("\t [ Uso de estructuras en Ballerina]"); | |
| io:println("Bailarin: ",bailarin); | |
| io:println("Bailarin.nombre: ",bailarin.nombre); | |
| } | |
| # Función para tipos simples en Ballerina. | |
| public function testA(){ | |
| byte bite = 11; | |
| int entero = 33; | |
| boolean isTrue = false; | |
| string cadena = "FERROCARRIL"; | |
| string caracter = cadena[4]; | |
| float flotante = 89.32f; | |
| io:println("\t [ Variables en Ballerina ]"); | |
| io:println("Byte: ",bite); | |
| io:println("Integer: ",entero); | |
| io:println("Boolean: ",isTrue); | |
| io:println("String: ",cadena); | |
| io:println("Char: ",caracter); | |
| io:println("Float: ",flotante); | |
| io:println("Constante: ",MAX); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import "fmt" | |
| func main() { | |
| fmt.Println("\t ******************************************") | |
| fmt.Println("\t [ Fundamentos de programación en Go ]") | |
| test1() | |
| test2() | |
| test3() | |
| test4() | |
| test5() | |
| fmt.Println("\t ******************************************") | |
| } | |
| type Persona struct { | |
| Nombre string | |
| Edad int | |
| } | |
| func cumple(p *Persona) { | |
| p.Edad++ // modificamos directamente la edad | |
| } | |
| func test5() { | |
| p := Persona{"Thomas Muller", 30} | |
| fmt.Println(p) // {Thoms Muller 30} | |
| cumple(&p) | |
| fmt.Println(p) // {Thoms Muller 31} | |
| } | |
| func incrementar(n *int) { | |
| *n = *n + 1 // modificamos el valor apuntado | |
| } | |
| func test4() { | |
| x := 5 | |
| fmt.Println("Valor de x:", x) | |
| incrementar(&x) // pasamos la dirección de x | |
| fmt.Println("Valor de x después:", x) // imprime 6 | |
| incrementar(&x) // pasamos la dirección de x | |
| fmt.Println("Valor de x después:", x) // imprime 7 | |
| } | |
| func test3(){ | |
| var numeroInt int = 10 | |
| punteroInt := &numeroInt // punteroInt es un puntero a numeroInt | |
| fmt.Println("Valor de numeroInt:", numeroInt) | |
| fmt.Println("Dirección de numeroInt:", punteroInt) | |
| fmt.Println("Valor apuntado por punteroInt:", *punteroInt) | |
| } | |
| func test2() { | |
| var compra float64 | |
| descuento := 0.0 | |
| fmt.Print("Introduce valor de la compra: ") | |
| fmt.Scan(&compra) | |
| fmt.Printf("El valor de la compra es de $%d\n", compra) | |
| if compra >= 500 { | |
| descuento = compra - (compra * 0.25) | |
| } | |
| if descuento > 0 { | |
| fmt.Printf("El cliente solo paga $%f\n", descuento) | |
| } else { | |
| fmt.Printf("El cliente paga $%f\n", compra) | |
| } | |
| } | |
| func test1() { | |
| fmt.Println("\t [ Tipos de datos en Go ]") | |
| var numeroInt int32 = 340 | |
| numeroIntTwo := 90 | |
| const MAX = 100 | |
| band := true | |
| var flotante float32 = float32(45.32) | |
| libros := []string{} | |
| libros = append(libros, "Nacida de un sueño.") | |
| libros = append(libros, "Elisa.") | |
| libros = append(libros, "La torre.") | |
| libros = append(libros, "El no nacido.") | |
| mapaLibros := make(map[int]string) | |
| cont := 0 | |
| fmt.Println("Entero: ", numeroInt) | |
| fmt.Printf("Flotante: %f\n", flotante) | |
| if band { | |
| fmt.Println("Esto es verdadero.") | |
| band = false | |
| } | |
| fmt.Println("") | |
| if band { | |
| fmt.Println("Esto es falso.") | |
| } else if !band { | |
| fmt.Println("Esto es verdadero.") | |
| } else { | |
| fmt.Println("Esto es falso.") | |
| } | |
| fmt.Println("") | |
| for numeroIntTwo < MAX { | |
| fmt.Printf("Hola %d\n", numeroIntTwo) | |
| numeroIntTwo++ | |
| } | |
| fmt.Println("") | |
| for i := 0; i < MAX; i++ { | |
| if i%3 == 0 && i%5 == 0 { | |
| fmt.Printf("\t Hola %d\n", i) | |
| } else { | |
| fmt.Printf("Goodbye %d\n", i) | |
| } | |
| } | |
| fmt.Println("") | |
| for _, libro := range libros { | |
| fmt.Printf("\t Título: %s\n", libro) | |
| } | |
| fmt.Println("") | |
| for _, libro := range libros { | |
| mapaLibros[cont] = libro | |
| cont++ | |
| } | |
| fmt.Println("") | |
| for clave, titulo := range mapaLibros { | |
| fmt.Printf("\t indice: %d - Título: %s\n", clave, titulo) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def test1(): | |
| print("\t [ Tipos de datos en Python ]") | |
| numero_int = 340 | |
| numero_int_two = 90 | |
| MAX = 100 | |
| band = True | |
| flotante = 45.32 | |
| libros = [] | |
| libros.append("Nacida de un sueño.") | |
| libros.append("Elisa.") | |
| libros.append("La torre.") | |
| libros.append("El no nacido.") | |
| mapa_libros = {} | |
| cont = 0 | |
| print("Entero:", numero_int) | |
| print(f"Flotante: {flotante:.2f}") | |
| if band: | |
| print("Esto es verdadero.") | |
| band = False | |
| print("") | |
| if band: | |
| print("Esto es falso.") | |
| elif not band: | |
| print("Esto es verdadero.") | |
| else: | |
| print("Esto es falso.") | |
| print("") | |
| while numero_int_two < MAX: | |
| print(f"Hola {numero_int_two}") | |
| numero_int_two += 1 | |
| print("") | |
| for i in range(MAX): | |
| if i % 3 == 0 and i % 5 == 0: | |
| print(f"\t Hola {i}") | |
| else: | |
| print(f"Goodbye {i}") | |
| print("") | |
| for libro in libros: | |
| print(f"\t Título: {libro}") | |
| print("") | |
| for libro in libros: | |
| mapa_libros[cont] = libro | |
| cont += 1 | |
| print("") | |
| for clave, titulo in mapa_libros.items(): | |
| print(f"\t indice: {clave} - Título: {titulo}") | |
| def test2(): | |
| descuento = 0 | |
| compra = int(input("Introduce valor de la compra: ")) | |
| print(f"El valor de la compra es de ${compra}") | |
| if compra >= 500: | |
| descuento = compra - (compra * 0.25) | |
| if descuento > 0: | |
| print(f"El cliente solo paga ${descuento}") | |
| else: | |
| print(f"El cliente paga ${compra}") | |
| def main(): | |
| print("\t ******************************************") | |
| print("\t [ Fundamentos de programación en Python ]") | |
| test1() | |
| test2() | |
| print("\t ******************************************") | |
| if __name__ == "__main__": | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::collections::HashMap; | |
| use std::io; | |
| fn main() { | |
| println!("\t ******************************************"); | |
| println!("\t [ Fundamentos de programación en Rust ]"); | |
| test1(); | |
| test2(); | |
| println!("\t ******************************************"); | |
| } | |
| fn test2() { | |
| let mut compra = String::new(); | |
| let mut descuento: i32 = 0; | |
| println!("Introduce valor de la compra: "); | |
| io::stdin().read_line(&mut compra).expect("Error al leer entrada"); | |
| let compra: i32 = compra.trim().parse().expect("Debes ingresar un número"); | |
| println!("El valor de la compra es de ${}", compra); | |
| if compra >= 500 { | |
| descuento = compra - (compra * 25 / 100); // 25% de descuento | |
| } | |
| if descuento > 0 { | |
| println!("El cliente solo paga ${}", descuento); | |
| } else { | |
| println!("El cliente paga ${}", compra); | |
| } | |
| } | |
| fn test1() { | |
| println!("\t [ Tipos de datos en Rust ]"); | |
| let numero_int: i32 = 340; | |
| let mut numero_int_two = 90; | |
| const MAX: i32 = 100; | |
| let mut band = true; | |
| let flotante: f32 = 45.32; | |
| let mut libros: Vec<String> = Vec::new(); | |
| libros.push("Nacida de un sueño.".to_string()); | |
| libros.push("Elisa.".to_string()); | |
| libros.push("La torre.".to_string()); | |
| libros.push("El no nacido.".to_string()); | |
| let mut mapa_libros: HashMap<i32, String> = HashMap::new(); | |
| let mut cont = 0; | |
| println!("Entero: {}", numero_int); | |
| println!("Flotante: {}", flotante); | |
| if band { | |
| println!("Esto es verdadero."); | |
| band = false; | |
| } | |
| println!(""); | |
| if band { | |
| println!("Esto es falso."); | |
| } else if !band { | |
| println!("Esto es verdadero."); | |
| } else { | |
| println!("Esto es falso."); | |
| } | |
| println!(""); | |
| while numero_int_two < MAX { | |
| println!("Hola {}", numero_int_two); | |
| numero_int_two += 1; | |
| } | |
| println!(""); | |
| for i in 0..MAX { | |
| if i % 3 == 0 && i % 5 == 0 { | |
| println!("\t Hola {}", i); | |
| } else { | |
| println!("Goodbye {}", i); | |
| } | |
| } | |
| println!(""); | |
| for libro in &libros { | |
| println!("\t Título: {}", libro); | |
| } | |
| println!(""); | |
| for libro in &libros { | |
| mapa_libros.insert(cont, libro.clone()); | |
| cont += 1; | |
| } | |
| println!(""); | |
| for (clave, titulo) in &mapa_libros { | |
| println!("\t indice: {} - Título: {}", clave, titulo); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main; | |
| import java.util.List; | |
| import java.util.ArrayList; | |
| import java.util.Map; | |
| import java.util.HashMap; | |
| import java.util.Scanner; | |
| public class TestFundamentos{ | |
| public static void main(String[] args){ | |
| System.out.println("\t***************************************************"); | |
| System.out.println("\t [Fundamentos de programación en Java]"); | |
| test1(); | |
| test2(); | |
| System.out.println("\t***************************************************"); | |
| } | |
| public static void test1(){ | |
| System.out.println("\t [ Tipos de datos en Java ]"); | |
| int numeroInt = 340; | |
| int numeroIntTwo = 90; | |
| final int MAX = 100; | |
| boolean band = true; | |
| float flotante = 45.32f; | |
| List<String> libros = new ArrayList<>(); | |
| libros.add("Nacida de un sueño."); | |
| libros.add("Elisa."); | |
| libros.add("La torre."); | |
| libros.add("El no nacido."); | |
| Map<Integer, String> mapaLibros = new HashMap<>(); | |
| int cont = 0; | |
| System.out.println("Entero: " + numeroInt); | |
| System.out.printf("Flotante: %.2f%n", flotante); | |
| if (band) { | |
| System.out.println("Esto es verdadero."); | |
| band = false; | |
| } | |
| System.out.println(""); | |
| if (band) { | |
| System.out.println("Esto es falso."); | |
| } else if (!band) { | |
| System.out.println("Esto es verdadero."); | |
| } else { | |
| System.out.println("Esto es falso."); | |
| } | |
| System.out.println(""); | |
| while (numeroIntTwo < MAX) { | |
| System.out.println("Hola " + numeroIntTwo); | |
| numeroIntTwo++; | |
| } | |
| System.out.println(""); | |
| for (int i = 0; i < MAX; i++) { | |
| if (i % 3 == 0 && i % 5 == 0) { | |
| System.out.println("\t Hola " + i); | |
| } else { | |
| System.out.println("Goodbye " + i); | |
| } | |
| } | |
| System.out.println(""); | |
| for (String libro : libros) { | |
| System.out.println("\t Título: " + libro); | |
| } | |
| System.out.println(""); | |
| for (String libro : libros) { | |
| mapaLibros.put(cont, libro); | |
| cont++; | |
| } | |
| System.out.println(""); | |
| for (Map.Entry<Integer, String> entry : mapaLibros.entrySet()) { | |
| System.out.println("\t indice: " + entry.getKey() + " - Título: " + entry.getValue()); | |
| } | |
| } | |
| public static void test2(){ | |
| int compra; | |
| Scanner entrada = new Scanner(System.in); | |
| int descuento = 0; | |
| System.out.println("Introduce valor de la compra: "); | |
| compra = entrada.nextInt(); | |
| System.out.printf("Valor de la compra: $%d\n", compra); | |
| if(compra >= 500){ | |
| descuento = compra - (compra * 25 / 100); | |
| } | |
| if (descuento > 0) { | |
| System.out.printf("El cliente solo paga $%d\n", descuento); | |
| } else { | |
| System.out.printf("El cliente paga $%d\n", compra); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment