#ESERCIZIO 1
s = input("Enter a string: ")
l = len(s)
meta = l // 2 # DIVISIONE INTERA PER TROVARE IL PUNTO DI META
if l % 2 == 0: # SE LA LUNGHEZZA è PARI
prima_meta = s[:meta]
seconda_meta = s[meta:]
else: # SE LA LUNGHEZZA è DISPARI
prima_meta = s[:meta + 1]
seconda_meta = s[meta:]
invertita = seconda_meta + prima_meta
print(f"The inverted string is: {invertita}")
#ESERCIZIO 2
a = input("Enter a string:")
b = input("Enter a string:")
c = input("Enter a string:")
n = int(input("Enter a number"))
d = a + b + c #CREAZIONE DI UNA STRINGA CONCATENATA
print(d)
risultato = d * n
print(f"Risultato:{risultato}")
#ESERCIZIO 3
bin_str = input("Insert a binary number:")
dec_val = 0
n = len(bin_str)
for i in range(n): # SCORRE DAI CARATTERI A DESTRA (2^0) A SINISTRA (2^1)
bit = int(bin_str[n-1-i])
dec_val += bit * (2**i) # FORMULA Y=sum(xi*2^i)
#IN QUESTO CASO STIAMO CALCOLANDO LA POTENZA DI 2 PER LA POSIZIONE CORRENTE i (MOLTIPLICATA PER IL BIT)
print(f"The corresponding number in base 10 is: {dec_val}")
#ESERCIZIO 4
import random # GENERA UN NUMERO INTERO CASUALE (ESTREMI INCLUSI)
def scelta(a, b):
return random.randint(a,b)
import M1 # CREAZIONE PROGRAMMA M2.py CHE IMPORTA M1
s = input("Inserisci una stringa di caratteri:")
if len(s)>0:
indice_casuale = M1.scelta(0, len(s)-1) #SI ESTRAE L'INDICE VALIDO
carattere = s[indice_casuale]
print(f"Carattere casuale scelto: {carattere}")
else:
print("La stringa è vuota")
#ESERCIZIO 5
import random
def Write_Output(x, y, f):
riga = str(x) + "" + "".join(map(str, y)) + "\n" #CONVERTE TUTTI GLI ELEM NUM DELLA LISTA y IN STRINGHE
f.write(riga)
if __name__ == "__main__":
with open("myfile.txt", "w") as f: #APRIRE FILE "myfile.txt" IN MODALITA SCRITTURA
for i in range(1,43):
y = [random.randint(0,100) for _ in range(i)]
Write_Output(i,y,f)
#ESERCIZIO 6
import random
def Moneta(p):
r = random.random()
return r <= p
def Secondo_lancio(n,p): #DISTRIBUZ BINOMIALE
y = []
for _ in range(n):
risultato = Moneta(p)
y.append(risultato)
return y
import M3
if __name__ == "__main__":
DN = 100
DP = 0
try:
input_n = input("Inserisci N(>0):")
N = int(input_n) if input_n.strip() != "" else DN
if N <= 0:
N = DN
except ValueError:
N = DN
try:
input_p = input("Inserisci P [0, 1): ")
P = float(input_p) if input_p.strip() != "" else DP
if not (0 <= P < 1):
P = DP
except ValueError:
P = DP
risultato_lista = M3.Repeat_Runs(N, P)
print(f"Risultati delle esecuzioni (N={N}, P={P}):")
print(risultato_lista)
#ESERCIZIO 7
import math
def calcola_tiro():
# COSTANTI NOTE
x1, y1 = 0.0, 1.20 # POSIZIONE INIZIALE FRECCIA
x2, y2 = 2.40, 1.90 # POSIZIONE INIZIALE BERSAGLIO
v = 10.0 # MODULO VELOCITA (m/s)
diametro_bersaglio = 0.45
g = 9.87 # Accelerazione di gravità fornita dal testo
# Acquisizione angolo dall'utente (< 30 gradi)
try:
theta_gradi = float(input("Inserisci un angolo θ (< 30°): "))
if theta_gradi >= 30:
print("Errore: l'angolo deve essere minore di 30°.")
return
except ValueError:
print("Valore non valido.")
return
theta_rad = math.radians(theta_gradi) #CONVERSIONE DELL'ANGOLO IN RADIANTI
# Componenti della velocità iniziale
v_x = v * math.cos(theta_rad)
v_y = v * math.sin(theta_rad)
# Il bersaglio si trova alla coordinata x = x2 = 2.40 m.
# Troviamo il tempo t in cui la freccia raggiunge la coordinata x del bersaglio: x(t) = x1 + v_x * t -> t = (x2 - x1) / v_x
if v_x == 0:
print("Mancato!")
return
t = (x2 - x1) / v_x
# Calcolo posizione y della freccia all'istante t (moto uniformemente accelerato con a_y = -g)
# y_freccia(t) = y1 + v_y * t - 0.5 * g * t^2
y_freccia = y1 + v_y * t - 0.5 * g * (t ** 2)
# Calcolo posizione y del bersaglio all'istante t (cade liberamente da y2 con velocità iniziale 0)
# y_bersaglio(t) = y2 - 0.5 * g * t^2
y_bersaglio = y2 - 0.5 * g * (t ** 2)
# Verifica se la freccia si trova all'interno del bersaglio (diametro 0.45 m -> raggio = 0.225 m dal centro)
raggio = diametro_bersaglio / 2
distanza_verticale = abs(y_freccia - y_bersaglio)
# Controllo se il bersaglio è ancora in aria (y_bersaglio >= 0) e se la freccia lo colpisce nel raggio
if y_bersaglio >= 0 and distanza_verticale <= raggio:
print("Preso!")
else:
print("Mancato!")
if __name__ == "__main__":
calcola_tiro()
To embed this project on your website, copy the following code and paste it into your website's HTML: