Cody

Cábula de Linguagens

Python

texto = "123"
numero = int(texto)
resultado = eval('2+3*4')
c = 'A'
ascii_val = ord(c)
c = chr(ascii_val)

lista = []
lista.append(valor)
del lista[posicao]  #  ou lista.pop(posicao) , removem elemento
lista.pop()          # remove ultimo elemento
len(lista)
lista.sort()
lista.sort(reverse=True)
lista.sort(key=lambda item: item)

numeros = "1 2 3 4"
lista = numeros.split(" ")  # ['1','2','3','4']

for n in lista:
    print(f"O numero: {n}")

traduz = {}
traduz["um"] = "one"
for chave, valor in traduz.items():
    print(chave, valor)

# Abrir e ler ficheiro
with open("exemplo.txt", "r") as f:
    for linha in f:
        print(linha.strip())

Java


String texto = "123";
int numero = Integer.parseInt(texto);
double numero2 = Double.parseDouble("34.5");

String texto = "Olá mundo";
char c = texto.charAt(1); // letra 'l'
int tamanho = texto.length();

char c = 'A';
int asciiVal = (int) c;  // converte char para int

String numeros = "1 2 3 4";
String[] lista = numeros.split(" ");  // ["1", "2", "3", "4"]

import java.util.ArrayList;
import java.util.Collections;

ArrayList lista = new ArrayList<>();
lista.add(valor);
lista.remove(posicao);
lista.remove(Integer.valueOf(valor));
lista.size();
lista.isEmpty();
Collections.sort(lista);

for (int n : lista) {
    System.out.println("O numero: " + n);
}

import java.util.HashMap;
import java.util.Map;

Map traduz = new HashMap<>();
traduz.put("um", "one");

for (Map.Entry entry : traduz.entrySet()) {
    System.out.println(entry.getKey() + " -> " + entry.getValue());
}

// Abrir e ler ficheiro
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

try (BufferedReader br = new BufferedReader(new FileReader("exemplo.txt"))) {
    String linha;
    while ((linha = br.readLine()) != null) {
        System.out.println(linha);
    }
} catch (IOException e) {
    e.printStackTrace();
}

Kotlin

// String
val texto = "123"
val numero : Int = texto.toInt()
var numero2 : Double = "34.5".toDouble()


var texto = "Olá mundo"
var c = texto[1] // letra 'l'
var tamanho = texto.length

val c: Char = 'A'
val asciiVal: Int = c.code  // retorna o valor numérico do caracter

val numeros = "1 2 3 4"
val listaStrings = numeros.split(" ")  // ["1","2","3","4"]

// Listas / Arrays
val lista = mutableListOf()
lista.add(10)

lista.removeAt(posicao)
lista.remove(valor)

lista.size
lista.isEmpty()
lista.sort()
lista.sortDescending()
lista.sortBy { it }

for (n in lista) {
    println("O numero: $n")
}

val traduz = mutableMapOf()
traduz["um"] = "one"

for ((chave, valor) in traduz) {
    println("$chave -> $valor")
}

val x = -5
val absX = kotlin.math.abs(x)

// Abrir e ler ficheiro
import java.io.File

val file = File("exemplo.txt")
file.forEachLine { linha ->
    println(linha)
}

C

#include <stdio.h>
#include <stdlib.h>   // atoi, abs
#include <string.h>   // strtok, strcpy

char texto[] = "123";
int numero = atoi(texto);  
printf("%d\n", numero);

char c = 'A';
int asciiVal = (int)c;   // converte char para int   

char numeros[] = "1 2 3 4";
char *token = strtok(numeros, " "); // divide string em tokens usando separador

int lista[10];   
// imprimir a lista
for (int i = 0; i < 10; i++) {
    lista[i] = 10 * i;
}

int x = -5;
int positivo = abs(x);

// Abrir e ler ficheiro
FILE *f = fopen("exemplo.txt", "r");
if (f != NULL) {
    char linha[256];
    while (fgets(linha, sizeof(linha), f)) {
        printf("%s", linha);
    }
    fclose(f);
} else {
    printf("Erro ao abrir o ficheiro\n");
}