Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
pub:labprog:20181105 [2018/11/07 18:06] lucafavalli |
pub:labprog:20181105 [2018/11/07 18:18] (current) lucafavalli |
||
---|---|---|---|
Line 235: | Line 235: | ||
Dati giorno e ora precisa di nascita di qualcuno, calcolare quando ha raggiunto (o raggiungerà) un miliardo di secondi di età | Dati giorno e ora precisa di nascita di qualcuno, calcolare quando ha raggiunto (o raggiungerà) un miliardo di secondi di età | ||
*/ | */ | ||
+ | |||
+ | package main | ||
+ | |||
+ | import( | ||
+ | "time" | ||
+ | "fmt" | ||
+ | ) | ||
+ | |||
+ | func main(){ | ||
+ | var year, month, day, hour int | ||
+ | fmt.Print("Inserire data nel formato \"anno mese giorno ora\" ") | ||
+ | fmt.Scan(&year, &month, &day, &hour) | ||
+ | t := time.Date(year, time.Month(month), day, hour, 0, 0, 0, time.UTC) | ||
+ | nt, _ := time.ParseDuration("1000000000s") | ||
+ | fmt.Println(t.Add(nt)) | ||
+ | } | ||
+ | |||
</code> | </code> | ||
Line 241: | Line 258: | ||
Calcolare la distanza di Hamming tra due stringhe | Calcolare la distanza di Hamming tra due stringhe | ||
*/ | */ | ||
+ | |||
+ | package main | ||
+ | |||
+ | import( | ||
+ | "fmt" | ||
+ | ) | ||
+ | |||
+ | func main(){ | ||
+ | var string1, string2 string | ||
+ | var distance int | ||
+ | fmt.Print("Inserire le due stringhe da confrontare: ") | ||
+ | fmt.Scan(&string1, &string2) | ||
+ | for i, c := range string1 { | ||
+ | for j, k := range string2 { | ||
+ | if i == j && c != k { | ||
+ | distance++ | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | fmt.Println("La distanza di Hamming è", distance) | ||
+ | } | ||
</code> | </code> | ||
<code> | <code> | ||
+ | /* | ||
+ | Scrivere un programma che faccia il conto alla rovescia da 10 a 0 in secondi. | ||
+ | Hint: usare le funzioni Now, Sub e Seconds del package time; | ||
+ | non usare Sleep! | ||
+ | */ | ||
+ | |||
+ | package main | ||
+ | |||
+ | import( | ||
+ | "fmt"; | ||
+ | "time" | ||
+ | ) | ||
+ | func main(){ | ||
+ | seconds := 10 | ||
+ | for ; seconds >= 0; seconds-- { | ||
+ | fmt.Println(seconds) | ||
+ | start := time.Now() | ||
+ | for time.Now().Sub(start).Seconds() < 1 {} | ||
+ | } | ||
+ | } | ||
</code> | </code> |