<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://so.v2.cs.unibo.it/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Antonio.lagana2</id>
	<title>Sistemi Operativi - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://so.v2.cs.unibo.it/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Antonio.lagana2"/>
	<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php/Special:Contributions/Antonio.lagana2"/>
	<updated>2026-04-26T11:19:37Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.35.5</generator>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Esercizi_a_caso_del_Prof.&amp;diff=229</id>
		<title>Esercizi a caso del Prof.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Esercizi_a_caso_del_Prof.&amp;diff=229"/>
		<updated>2013-11-14T17:48:57Z</updated>

		<summary type="html">&lt;p&gt;Antonio.lagana2: /* Programmi semplici */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Cominciamo da una sfida:&lt;br /&gt;
&lt;br /&gt;
=== Il programma che stampa se stesso ===&lt;br /&gt;
scrivere un programma che produca in output se stesso: i.e. '''non prevede input''' e l'output deve essere identico al sorgente.&lt;br /&gt;
&lt;br /&gt;
Vince chi produce il programma piu' compatto (in ogni linguaggio).&lt;br /&gt;
&lt;br /&gt;
Per&amp;amp;ograve; non dovete fare come in questo [[tentativo]]. il programma deve stampare se stesso '''senza''' leggere il proprio sorgente! (&amp;amp;egrave; proprio quella la sfida divertente!, rd 20131027). Per il Pyhton il programma vuoto non vale, &amp;amp;egrave; una soluzione banale.&lt;br /&gt;
Ecco un altro [[tentativo]] errato. &lt;br /&gt;
   print(__file__) &lt;br /&gt;
non e' non Quine in Python 3.x perch&amp;amp;eacute; stampa il proprio parametro, non il proprio sorgente.&lt;br /&gt;
In caso contrario anche il comando echo sarebbe un Quine!&lt;br /&gt;
&lt;br /&gt;
* [Python] beccatevi questo (da Federico A. e Alessio G.):&lt;br /&gt;
*; La prima versione con due variabili : &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt; a='a=%r;b=%r;print a%%(a,b)';b='b=%r;print a%%(a,b)';print a%(a,b) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
*; Per capire meglio la logica del format ci siamo sbizzarriti: &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt; a = 'a = %r ; b = %r ; c = %r ; print a%%(a,b,c) ' ; b = 'b = %r ; c = %r ; print a%%(a,b,c) ' ; c = 'c = %r ; print a%%(a,b,c) ' ; print a%(a,b,c) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
# %% fuori stringa corrisponde ad un solo %.&lt;br /&gt;
# print a%(a,b,c) corrisponderebbe in C a printf(a,a,b,c).&lt;br /&gt;
&lt;br /&gt;
* [C] beccatevi anche questo (da Alessio G. e Federico A.):&lt;br /&gt;
Usando la stessa logica del programma in &amp;quot;pitone&amp;quot; lo abbiamo rielaborato in C:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*Printf stampa la stringa s e sostituisce ad ogni occorrenza di %c i caratteri ascii corrispondenti ai numeri: 9 = \t , 10 = \n , 34 = \&amp;quot;  .&lt;br /&gt;
Il %s corrisponde alla stringa.*/&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
	char s[] = &amp;quot;#include &amp;lt;stdio.h&amp;gt;%c%cchar s[]=%c%s%c;%cvoid main(void){%c%cprintf(s,10,9,34,s,34,10,9,10,9,10);%c%c}%c&amp;quot;;&lt;br /&gt;
void main(void){&lt;br /&gt;
       printf(s,10,9,34,s,34,10,10,9,10,9,10);  &lt;br /&gt;
	}	 &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
(bravi Alessio e Federico, questi sono proprio Quine!, rd 20131101)&lt;br /&gt;
&lt;br /&gt;
* [Python3] (io ho fatto un Quine in python3 con il metodo ''format''... perch&amp;amp;eacute; non ci provate ache voi? rd20131101)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Grazie a questa pagina [http://www.python.org/dev/peps/pep-3101/  Advanced String Formatting ] ho scoperto che con !r è possibile usare la funzione repr() solo su certi fields della stringa il che era l'ultimo bit che mi mancava per riuscirci.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a='a={0!r}{1}print(a.format(a,{1!r}))'&lt;br /&gt;
print(a.format(a,'\n'))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
-Fede&lt;br /&gt;
&lt;br /&gt;
*Come non detto facendo qualche esperimento sono riuscito a compattarla ancora di più&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a='a={0};print(a.format(repr(a)))';print(a.format(repr(a)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
oppure 10 caratteri in meno:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a='a={0!r};print(a.format(a))';print(a.format(a))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Questo codice funziona sia in python 3 che 2.&lt;br /&gt;
&lt;br /&gt;
*questo invece è il mio primo tentativo sbagliato ma che ha messo in evidenza la differenza tra % e .format.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a='a={0}\nprint(a.format(a))'&lt;br /&gt;
print(a.format(a))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
-Fede&lt;br /&gt;
&lt;br /&gt;
Ottimo Fede, il tuo Quine e' anche piu' corto del mio. Io usavo la funzione chr, ma il formato !r toglie ancora qualche carattere (rd 20131101):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
f=&amp;quot;f={0}{1}{0};print(f.format(chr(34),f))&amp;quot;;print(f.format(chr(34),f))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Ho provato a rendere ancora più corto il codice cercando di rimuovere l'assegnamento ma in realtà lo allunga.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print('print({0!r}.format({0!r}))'.format('print({0!r}).format({0!r}))'))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
*purtroppo % non assegna a più fields la stessa stringa ma richiede tanti argomenti quanti fields sono presenti nella stringa... se non fosse così la soluzione migliore sarebbe...&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print('print(%r%%%r)'%'print(%r%%%r)')&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
*che non funziona (e di cui non sono neanche certissimo prof. lei che dice, funzionerebbe se % &amp;quot;capisse&amp;quot; che il suo argomento va messo in tutte i%r?).&lt;br /&gt;
&lt;br /&gt;
-Fede&lt;br /&gt;
&lt;br /&gt;
=== Programmi semplici ===&lt;br /&gt;
&lt;br /&gt;
riscrivere le funzioni strcpy, strcmp, strncmp etc... (quelle elencate da &amp;quot;man 3 string&amp;quot;) in C e in Python.&lt;br /&gt;
&lt;br /&gt;
Implementazione in Python di strcpy, strcmp, strncmp.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def strcpy(s):&lt;br /&gt;
    return s&lt;br /&gt;
&lt;br /&gt;
def strcmp(s1, s2):&lt;br /&gt;
    if s1 == s2: return 0&lt;br /&gt;
    elif s1 &amp;lt; s2: return -1&lt;br /&gt;
    else: return 1&lt;br /&gt;
&lt;br /&gt;
def strncmp(s1, s2, n):&lt;br /&gt;
    i = 0;&lt;br /&gt;
    res = 0;&lt;br /&gt;
    if(len(s1) &amp;lt; n):&lt;br /&gt;
        n = len(s1)&lt;br /&gt;
    if(len(s2) &amp;lt; n):&lt;br /&gt;
        n = len(s2)&lt;br /&gt;
    while n &amp;gt; 0  and res == 0:&lt;br /&gt;
        if s1[i] == s2[i]: res = 0&lt;br /&gt;
        elif s1[i] &amp;lt; s2[i]: res = -1&lt;br /&gt;
        else: res = 1&lt;br /&gt;
        n += 1&lt;br /&gt;
        i += 1&lt;br /&gt;
    return res&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
- Alberto&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementazione in C di strcoll.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int strcoll(char *string1, char *string2){&lt;br /&gt;
	while(*string1!='\0' || *string2!='\0'){&lt;br /&gt;
		if(*string1==*string2){&lt;br /&gt;
			string1++;&lt;br /&gt;
			string2++;					&lt;br /&gt;
		}&lt;br /&gt;
		else if(*string1&amp;lt;*string2)&lt;br /&gt;
			return -1;&lt;br /&gt;
		else&lt;br /&gt;
			return 1;&lt;br /&gt;
	}&lt;br /&gt;
return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
- Tony&lt;br /&gt;
&lt;br /&gt;
scrivere una funzione che controlli se una stringa e' [[palindroma]] (C python)&lt;br /&gt;
&lt;br /&gt;
=== Una miniera di esercizi ===&lt;br /&gt;
&lt;br /&gt;
[http://rosettacode.org/wiki/Category:Programming_Tasks Rosetta Stone]. Tantissimi problemi risolti con tantissimi linguaggi.&lt;/div&gt;</summary>
		<author><name>Antonio.lagana2</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Esercizi_a_caso_del_Prof.&amp;diff=228</id>
		<title>Esercizi a caso del Prof.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Esercizi_a_caso_del_Prof.&amp;diff=228"/>
		<updated>2013-11-14T17:48:35Z</updated>

		<summary type="html">&lt;p&gt;Antonio.lagana2: /* Programmi semplici */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Cominciamo da una sfida:&lt;br /&gt;
&lt;br /&gt;
=== Il programma che stampa se stesso ===&lt;br /&gt;
scrivere un programma che produca in output se stesso: i.e. '''non prevede input''' e l'output deve essere identico al sorgente.&lt;br /&gt;
&lt;br /&gt;
Vince chi produce il programma piu' compatto (in ogni linguaggio).&lt;br /&gt;
&lt;br /&gt;
Per&amp;amp;ograve; non dovete fare come in questo [[tentativo]]. il programma deve stampare se stesso '''senza''' leggere il proprio sorgente! (&amp;amp;egrave; proprio quella la sfida divertente!, rd 20131027). Per il Pyhton il programma vuoto non vale, &amp;amp;egrave; una soluzione banale.&lt;br /&gt;
Ecco un altro [[tentativo]] errato. &lt;br /&gt;
   print(__file__) &lt;br /&gt;
non e' non Quine in Python 3.x perch&amp;amp;eacute; stampa il proprio parametro, non il proprio sorgente.&lt;br /&gt;
In caso contrario anche il comando echo sarebbe un Quine!&lt;br /&gt;
&lt;br /&gt;
* [Python] beccatevi questo (da Federico A. e Alessio G.):&lt;br /&gt;
*; La prima versione con due variabili : &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt; a='a=%r;b=%r;print a%%(a,b)';b='b=%r;print a%%(a,b)';print a%(a,b) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
*; Per capire meglio la logica del format ci siamo sbizzarriti: &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt; a = 'a = %r ; b = %r ; c = %r ; print a%%(a,b,c) ' ; b = 'b = %r ; c = %r ; print a%%(a,b,c) ' ; c = 'c = %r ; print a%%(a,b,c) ' ; print a%(a,b,c) &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
# %% fuori stringa corrisponde ad un solo %.&lt;br /&gt;
# print a%(a,b,c) corrisponderebbe in C a printf(a,a,b,c).&lt;br /&gt;
&lt;br /&gt;
* [C] beccatevi anche questo (da Alessio G. e Federico A.):&lt;br /&gt;
Usando la stessa logica del programma in &amp;quot;pitone&amp;quot; lo abbiamo rielaborato in C:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*Printf stampa la stringa s e sostituisce ad ogni occorrenza di %c i caratteri ascii corrispondenti ai numeri: 9 = \t , 10 = \n , 34 = \&amp;quot;  .&lt;br /&gt;
Il %s corrisponde alla stringa.*/&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
	char s[] = &amp;quot;#include &amp;lt;stdio.h&amp;gt;%c%cchar s[]=%c%s%c;%cvoid main(void){%c%cprintf(s,10,9,34,s,34,10,9,10,9,10);%c%c}%c&amp;quot;;&lt;br /&gt;
void main(void){&lt;br /&gt;
       printf(s,10,9,34,s,34,10,10,9,10,9,10);  &lt;br /&gt;
	}	 &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
(bravi Alessio e Federico, questi sono proprio Quine!, rd 20131101)&lt;br /&gt;
&lt;br /&gt;
* [Python3] (io ho fatto un Quine in python3 con il metodo ''format''... perch&amp;amp;eacute; non ci provate ache voi? rd20131101)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Grazie a questa pagina [http://www.python.org/dev/peps/pep-3101/  Advanced String Formatting ] ho scoperto che con !r è possibile usare la funzione repr() solo su certi fields della stringa il che era l'ultimo bit che mi mancava per riuscirci.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a='a={0!r}{1}print(a.format(a,{1!r}))'&lt;br /&gt;
print(a.format(a,'\n'))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
-Fede&lt;br /&gt;
&lt;br /&gt;
*Come non detto facendo qualche esperimento sono riuscito a compattarla ancora di più&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a='a={0};print(a.format(repr(a)))';print(a.format(repr(a)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
oppure 10 caratteri in meno:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a='a={0!r};print(a.format(a))';print(a.format(a))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Questo codice funziona sia in python 3 che 2.&lt;br /&gt;
&lt;br /&gt;
*questo invece è il mio primo tentativo sbagliato ma che ha messo in evidenza la differenza tra % e .format.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
a='a={0}\nprint(a.format(a))'&lt;br /&gt;
print(a.format(a))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
-Fede&lt;br /&gt;
&lt;br /&gt;
Ottimo Fede, il tuo Quine e' anche piu' corto del mio. Io usavo la funzione chr, ma il formato !r toglie ancora qualche carattere (rd 20131101):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
f=&amp;quot;f={0}{1}{0};print(f.format(chr(34),f))&amp;quot;;print(f.format(chr(34),f))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Ho provato a rendere ancora più corto il codice cercando di rimuovere l'assegnamento ma in realtà lo allunga.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print('print({0!r}.format({0!r}))'.format('print({0!r}).format({0!r}))'))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
*purtroppo % non assegna a più fields la stessa stringa ma richiede tanti argomenti quanti fields sono presenti nella stringa... se non fosse così la soluzione migliore sarebbe...&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print('print(%r%%%r)'%'print(%r%%%r)')&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
*che non funziona (e di cui non sono neanche certissimo prof. lei che dice, funzionerebbe se % &amp;quot;capisse&amp;quot; che il suo argomento va messo in tutte i%r?).&lt;br /&gt;
&lt;br /&gt;
-Fede&lt;br /&gt;
&lt;br /&gt;
=== Programmi semplici ===&lt;br /&gt;
&lt;br /&gt;
riscrivere le funzioni strcpy, strcmp, strncmp etc... (quelle elencate da &amp;quot;man 3 string&amp;quot;) in C e in Python.&lt;br /&gt;
&lt;br /&gt;
Implementazione in Python di strcpy, strcmp, strncmp.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def strcpy(s):&lt;br /&gt;
    return s&lt;br /&gt;
&lt;br /&gt;
def strcmp(s1, s2):&lt;br /&gt;
    if s1 == s2: return 0&lt;br /&gt;
    elif s1 &amp;lt; s2: return -1&lt;br /&gt;
    else: return 1&lt;br /&gt;
&lt;br /&gt;
def strncmp(s1, s2, n):&lt;br /&gt;
    i = 0;&lt;br /&gt;
    res = 0;&lt;br /&gt;
    if(len(s1) &amp;lt; n):&lt;br /&gt;
        n = len(s1)&lt;br /&gt;
    if(len(s2) &amp;lt; n):&lt;br /&gt;
        n = len(s2)&lt;br /&gt;
    while n &amp;gt; 0  and res == 0:&lt;br /&gt;
        if s1[i] == s2[i]: res = 0&lt;br /&gt;
        elif s1[i] &amp;lt; s2[i]: res = -1&lt;br /&gt;
        else: res = 1&lt;br /&gt;
        n += 1&lt;br /&gt;
        i += 1&lt;br /&gt;
    return res&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
- Alberto&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementazione in C di strcoll.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int strcoll(char *string1, char *string2){&lt;br /&gt;
	while(*string1!='\0' || *string2!='\0'){&lt;br /&gt;
		if(*string1==*string2){&lt;br /&gt;
			string1++;&lt;br /&gt;
			string2++;					&lt;br /&gt;
		}&lt;br /&gt;
		else if(*string1&amp;lt;*string2)&lt;br /&gt;
			return -1;&lt;br /&gt;
		else&lt;br /&gt;
			return 1;&lt;br /&gt;
	}&lt;br /&gt;
return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
-Tony&lt;br /&gt;
&lt;br /&gt;
scrivere una funzione che controlli se una stringa e' [[palindroma]] (C python)&lt;br /&gt;
&lt;br /&gt;
=== Una miniera di esercizi ===&lt;br /&gt;
&lt;br /&gt;
[http://rosettacode.org/wiki/Category:Programming_Tasks Rosetta Stone]. Tantissimi problemi risolti con tantissimi linguaggi.&lt;/div&gt;</summary>
		<author><name>Antonio.lagana2</name></author>
	</entry>
</feed>