Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
aa:lab:1 [2023/10/15 16:39] vlad.juja |
aa:lab:1 [2024/10/16 23:21] (current) dmihai [Exerciții] |
||
---|---|---|---|
Line 56: | Line 56: | ||
- | ==== Exerciții basic ==== | + | - Scrieți o Mașină Turing care primește un șir binar și verifică dacă începe și se termină cu simboluri **distincte** (e.g. **1**0011011**0**). |
- | 1. Scrieți o Mașină Turing care primește un șir binar și verifică dacă începe și se termină cu simboluri **distincte**. | + | - Rezolvați exercițiul anterior pentru input în baza 10. |
- | Ex: "100110110" -> False | + | - Scrieți o Mașină Turing care primește un șir binar și verifică dacă are lungime impară și simbolul din mijloc este 0 (e.g. 1011**0**0011). |
+ | - Scrieți o Mașină Turing care primește un șir binar și lasă pe bandă complementul lui (e.g. "100110100" -> "011001011") | ||
+ | - Scrieți o Mașină Turing care curăță toată banda (atât la stânga cât și la dreapta) și apoi lasă scris doar "1". | ||
+ | - Scrieți o Mașină Turing care inversează cuvântul primit pe bandă. | ||
+ | - Scrieți o Mașină Turing care primește un șir de "X"-uri și verifică dacă lungimea acestuia este o putere a lui 2. | ||
+ | - Scrieți o Mașină Turing care primește un șir de paranteze "(", ")" și verifică dacă sunt echilibrate. | ||
+ | - Scrieți o Mașină Turing care primește un număr în baza 2 și verifică dacă e divizibil cu 5. | ||
+ | - Scrieți o Mașină Turing care primește un cuvânt binar, găsește primul simbol "0" și inserează un "1" în stânga lui (deci tot ce apare la dreapta va trebui mutat cu o poziție). | ||
+ | - Scrieți o Mașină Turing care primește două numere în baza 2, big-endian, separate de un # și lasă pe bandă suma lor (e.g. "1011#11001" -> "100100"). | ||
+ | - Scrieți o Mașină Turing care primește un șir binar și lasă pe bandă, după un caracter "#", numărul de 0-uri, în bază 2 (e.g. "100010110" -> "100010110#101"). | ||
- | /* | + | <note> |
- | <hidden> | + | Soluțiile pentru exercițiile din laborator, în format pentru simulatorul online și xlsx, se pot găsi [[aa:lab:sol:1|aici]]; (soluțiile nu sunt unice, există abordări corecte, total diferite de cele prezentate de noi). |
- | Soluție [[https://docs.google.com/spreadsheets/d/1U-T08UyTtM58RZ2j2aY05HFWKENFPPL1/edit?usp=sharing&ouid=113096868136191520794&rtpof=true&sd=true/|Excel.]] | + | </note> |
- | <code> | + | |
- | name: ex1 | + | |
- | init: start | + | |
- | accept: H, Y | + | |
- | + | ||
- | //starea de start este bineînțeles "start" | + | |
- | //folosind stările "expect0" si "expect1", practic "ținem in memorie" primul număr din șir | + | |
- | //stările "found0?" si "found1?" verifică dacă ultima cifră este aceeași cu prima. | + | |
- | + | ||
- | start,0 | + | |
- | expect0,0,> | + | |
- | + | ||
- | start,1 | + | |
- | expect1,1,> | + | |
- | + | ||
- | start,_ | + | |
- | N,_,- | + | |
- | + | ||
- | expect0,0 | + | |
- | expect0,0,> | + | |
- | + | ||
- | expect0,1 | + | |
- | expect0,1,> | + | |
- | + | ||
- | expect0,_ | + | |
- | found0?,_,< | + | |
- | + | ||
- | found0?,0 | + | |
- | Y,0,- | + | |
- | + | ||
- | found0?,1 | + | |
- | N,1,- | + | |
- | + | ||
- | found0?,_ | + | |
- | N,_,- | + | |
- | + | ||
- | expect1,0 | + | |
- | expect1,0,> | + | |
- | + | ||
- | expect1,1 | + | |
- | expect1,1,> | + | |
- | + | ||
- | expect1,_ | + | |
- | found1?,_,< | + | |
- | + | ||
- | found1?,0 | + | |
- | N,0,- | + | |
- | + | ||
- | found1?,1 | + | |
- | Y,1,- | + | |
- | + | ||
- | found1?,_ | + | |
- | N,_,- | + | |
- | + | ||
- | </code> | + | |
- | </hidden> | + | |
- | */ | + | |
- | + | ||
- | 2. Scrieți o Mașină Turing care primește un șir binar și verifică dacă are lungime impară și simbolul din mijloc este 0. | + | |
- | Ex: "101100011" -> True | + | |
- | + | ||
- | /* | + | |
- | <hidden> | + | |
- | Soluție [[https://docs.google.com/spreadsheets/d/10P5eq41BncWpAgPVf5O_xeRHGzNYmjyF/edit?usp=sharing&ouid=113096868136191520794&rtpof=true&sd=true|Excel.]] | + | |
- | <code> | + | |
- | name: ex2 | + | |
- | init: start | + | |
- | accept: H, Y | + | |
- | + | ||
- | //Strategie: ștergem prima cifra, mergem până la final, ștergem a doua cifră | + | |
- | //Stările de tipul "FindLastX", șterg prima cifră, si merg până dau de "_". | + | |
- | //Stările de tipul "EraseLastX", șterg ultima cifră, apoi dau reset. | + | |
- | //Dacă o stare de tipul "EraseLastX" descoperă tot _, înseamnă ca starea "FindLastX" a șters ultima cifră. | + | |
- | //Deci șirul are un număr impar de cifre. iar, in funcție de valoarea lui X vedem dacă se află un 0 sau un 1 la mijloc. | + | |
- | + | ||
- | start,0 | + | |
- | FindLast0,_,> | + | |
- | + | ||
- | start,1 | + | |
- | FindLast1,_,> | + | |
- | + | ||
- | start,_ | + | |
- | N,_,- | + | |
- | + | ||
- | FindLast0,0 | + | |
- | FindLast0,0,> | + | |
- | + | ||
- | FindLast0,1 | + | |
- | FindLast0,1,> | + | |
- | + | ||
- | FindLast0,_ | + | |
- | EraseLast0,_,< | + | |
- | + | ||
- | EraseLast0,0 | + | |
- | Reset,_,< | + | |
- | + | ||
- | EraseLast0,1 | + | |
- | Reset,_,< | + | |
- | + | ||
- | EraseLast0,_ | + | |
- | Y,_,- | + | |
- | + | ||
- | FindLast1,0 | + | |
- | FindLast1,0,> | + | |
- | + | ||
- | FindLast1,1 | + | |
- | FindLast1,1,> | + | |
- | + | ||
- | FindLast1,_ | + | |
- | EraseLast1,_,< | + | |
- | + | ||
- | EraseLast1,0 | + | |
- | Reset,_,< | + | |
- | + | ||
- | EraseLast1,1 | + | |
- | Reset,_,< | + | |
- | + | ||
- | EraseLast1,_ | + | |
- | N,_,- | + | |
- | + | ||
- | Reset,0 | + | |
- | Reset,0,< | + | |
- | + | ||
- | Reset,1 | + | |
- | Reset,1,< | + | |
- | + | ||
- | Reset,_ | + | |
- | start,_,> | + | |
- | + | ||
- | + | ||
- | + | ||
- | </code> | + | |
- | </hidden> | + | |
- | */ | + | |
- | + | ||
- | 3. Scrieți o Mașină Turing care primește un șir binar și lasă pe bandă complementul lui. | + | |
- | Ex: "000110111" -> "111001000" | + | |
- | + | ||
- | /* | + | |
- | <hidden> | + | |
- | Soluție [[https://docs.google.com/spreadsheets/d/1oLU7_5MJ3YAVLeXSjCShGKFjYSWi4HBk/edit?usp=sharing&ouid=113096868136191520794&rtpof=true&sd=true|Excel.]] | + | |
- | <code> | + | |
- | name: ex3 | + | |
- | init: start | + | |
- | accept: H, Y | + | |
- | + | ||
- | //parcurgem șirul, înlocuind 0 cu 1 si 1 cu 0. Trivial nu-i așa? (: | + | |
- | start,0 | + | |
- | start,1,> | + | |
- | + | ||
- | start,1 | + | |
- | start,0,> | + | |
- | + | ||
- | start,_ | + | |
- | H,_,- | + | |
- | + | ||
- | + | ||
- | + | ||
- | </code> | + | |
- | </hidden> | + | |
- | */ | + | |
- | + | ||
- | 4. Scrieți o Mașină Turing care curăță toată banda (atât la stânga cât și la dreapta) și apoi lasă scris doar "1". | + | |
- | + | ||
- | /* | + | |
- | <hidden> | + | |
- | Soluție [[https://docs.google.com/spreadsheets/d/1sMjIBxlEJVgZsG2WHKzMb80MCdWQNBY3/edit?usp=sharing&ouid=113096868136191520794&rtpof=true&sd=true|Excel.]] | + | |
- | <code> | + | |
- | name: ex4 | + | |
- | init: cleanRight | + | |
- | accept: H, Y | + | |
- | + | ||
- | //Strategie: ștergem la stânga până dăm de "_", apoi ne întoarcem până dăm de 0 sau 1. | + | |
- | //După ștergem la dreapta până dam de "_" si atunci scriem "1". | + | |
- | + | ||
- | cleanRight,0 | + | |
- | cleanRight,_,< | + | |
- | + | ||
- | cleanRight,1 | + | |
- | cleanRight,_,< | + | |
- | + | ||
- | cleanRight,_ | + | |
- | Reset,_,> | + | |
- | + | ||
- | Reset,0 | + | |
- | cleanLeft,_,> | + | |
- | + | ||
- | Reset,1 | + | |
- | cleanLeft,_,> | + | |
- | + | ||
- | Reset,_ | + | |
- | Reset,_,> | + | |
- | + | ||
- | cleanLeft,0 | + | |
- | cleanLeft,_,> | + | |
- | + | ||
- | cleanLeft,1 | + | |
- | cleanLeft,_,> | + | |
- | + | ||
- | cleanLeft,_ | + | |
- | Y,1,- | + | |
- | + | ||
- | + | ||
- | </code> | + | |
- | </hidden> | + | |
- | */ | + | |
- | + | ||
- | 5. Scrieți o Mașină Turing care inversează cuvântul primit pe bandă. | + | |
- | + | ||
- | /* | + | |
- | <hidden> | + | |
- | Soluție [[https://docs.google.com/spreadsheets/d/1lc2s-U7TZV814FoH3f6G2xH_gIV1QlVg/edit?usp=sharing&ouid=113096868136191520794&rtpof=true&sd=true|Excel.]] | + | |
- | <code> | + | |
- | name: ex5 | + | |
- | init: addXatEnd | + | |
- | accept: H, Y | + | |
- | + | ||
- | //Strategie: Primul pas adăugăm un X la final. | + | |
- | //Pasul 2: Ne uităm la ultima cifră ce nu e X, o înlocuim cu un X, o adăugăm la capătul șirului nou format. | + | |
- | //La final ștergem toți X. | + | |
- | + | ||
- | addXatEnd,0 | + | |
- | addXatEnd,0,> | + | |
- | + | ||
- | addXatEnd,1 | + | |
- | addXatEnd,1,> | + | |
- | + | ||
- | addXatEnd,_ | + | |
- | readFirstX,X,< | + | |
- | + | ||
- | addXatEnd,X | + | |
- | N,X,- | + | |
- | + | ||
- | readLastX,0 | + | |
- | readLastX,0,< | + | |
- | + | ||
- | readLastX,1 | + | |
- | readLastX,1,< | + | |
- | + | ||
- | readLastX,_ | + | |
- | stergeX,_,> | + | |
- | + | ||
- | readLastX,X | + | |
- | readFirstX,X,< | + | |
- | + | ||
- | readFirstX,0 | + | |
- | keep0,X,> | + | |
- | + | ||
- | readFirstX,1 | + | |
- | keep1,X,> | + | |
- | + | ||
- | readFirstX,_ | + | |
- | stergeX,_,> | + | |
- | + | ||
- | readFirstX,X | + | |
- | readFirstX,X,< | + | |
- | + | ||
- | keep0,0 | + | |
- | keep0,0,> | + | |
- | + | ||
- | keep0,1 | + | |
- | keep0,1,> | + | |
- | + | ||
- | keep0,_ | + | |
- | readLastX,0,< | + | |
- | + | ||
- | keep0,X | + | |
- | keep0,X,> | + | |
- | + | ||
- | keep1,0 | + | |
- | keep1,0,> | + | |
- | + | ||
- | keep1,1 | + | |
- | keep1,1,> | + | |
- | + | ||
- | keep1,_ | + | |
- | readLastX,1,< | + | |
- | + | ||
- | keep1,X | + | |
- | keep1,X,> | + | |
- | + | ||
- | stergeX,0 | + | |
- | stergeX,0,< | + | |
- | + | ||
- | stergeX,1 | + | |
- | stergeX,1,< | + | |
- | + | ||
- | stergeX,_ | + | |
- | Y,_,- | + | |
- | + | ||
- | stergeX,X | + | |
- | stergeX,_,> | + | |
- | + | ||
- | name: machine | + | |
- | init: addXatEnd | + | |
- | accept: H, Y | + | |
- | + | ||
- | addXatEnd,0 | + | |
- | addXatEnd,0,> | + | |
- | + | ||
- | addXatEnd,1 | + | |
- | addXatEnd,1,> | + | |
- | + | ||
- | addXatEnd,_ | + | |
- | readFirstX,X,< | + | |
- | + | ||
- | addXatEnd,X | + | |
- | N,X,- | + | |
- | + | ||
- | readLastX,0 | + | |
- | readLastX,0,< | + | |
- | + | ||
- | readLastX,1 | + | |
- | readLastX,1,< | + | |
- | + | ||
- | readLastX,_ | + | |
- | stergeX,_,> | + | |
- | + | ||
- | readLastX,X | + | |
- | readFirstX,X,< | + | |
- | + | ||
- | readFirstX,0 | + | |
- | keep0,X,> | + | |
- | + | ||
- | readFirstX,1 | + | |
- | keep1,X,> | + | |
- | + | ||
- | readFirstX,_ | + | |
- | stergeX,_,> | + | |
- | + | ||
- | readFirstX,X | + | |
- | readFirstX,X,< | + | |
- | + | ||
- | keep0,0 | + | |
- | keep0,0,> | + | |
- | + | ||
- | keep0,1 | + | |
- | keep0,1,> | + | |
- | + | ||
- | keep0,_ | + | |
- | readLastX,0,< | + | |
- | + | ||
- | keep0,X | + | |
- | keep0,X,> | + | |
- | + | ||
- | keep1,0 | + | |
- | keep1,0,> | + | |
- | + | ||
- | keep1,1 | + | |
- | keep1,1,> | + | |
- | + | ||
- | keep1,_ | + | |
- | readLastX,1,< | + | |
- | + | ||
- | keep1,X | + | |
- | keep1,X,> | + | |
- | + | ||
- | stergeX,0 | + | |
- | stergeX,0,< | + | |
- | + | ||
- | stergeX,1 | + | |
- | stergeX,1,< | + | |
- | + | ||
- | stergeX,_ | + | |
- | Y,_,- | + | |
- | + | ||
- | stergeX,X | + | |
- | stergeX,_,> | + | |
- | + | ||
- | + | ||
- | </code> | + | |
- | </hidden> | + | |
- | */ | + | |
- | + | ||
- | + | ||
- | ==== Exerciții fun ==== | + | |
- | 6. Alex ascultă muzică doar dacă numărul melodiilor din playlist este putere a lui 2. Dându-se un șir de "M"-uri, verifică dacă Alex poate asculta acest playlist sau nu. | + | |
- | + | ||
- | /* | + | |
- | <hidden> | + | |
- | Soluție [[https://docs.google.com/spreadsheets/d/1NX6EwAsnc7RTa3brV8h5soj2x6Myfto1/edit?usp=sharing&ouid=113096868136191520794&rtpof=true&sd=true|Excel.]] | + | |
- | <code> | + | |
- | name: ex6 | + | |
- | init: start | + | |
- | accept: H, Y | + | |
- | + | ||
- | //Strategie: Pentru fiecare doua caractere de "M", adăugăm un caracter de "A" la final. | + | |
- | //Apoi, pentru fiecare doua caractere de "A", adăugăm un M, and so on, pana rămânem cu doar 2 caractere pe banda. | + | |
- | + | ||
- | start,M | + | |
- | findOneMoreM,_,> | + | |
- | + | ||
- | start,_ | + | |
- | N,_,- | + | |
- | + | ||
- | start,A | + | |
- | findOneMoreA,_,> | + | |
- | + | ||
- | findOneMoreM,M | + | |
- | checkIfEmptyM,_,> | + | |
- | + | ||
- | findOneMoreM,_ | + | |
- | N,_,- | + | |
- | + | ||
- | findOneMoreM,A | + | |
- | N,_,- | + | |
- | + | ||
- | AddAnA,M | + | |
- | AddAnA,M,> | + | |
- | + | ||
- | AddAnA,_ | + | |
- | reset,A,< | + | |
- | + | ||
- | AddAnA,A | + | |
- | AddAnA,A,> | + | |
- | + | ||
- | reset,M | + | |
- | reset,M,< | + | |
- | + | ||
- | reset,_ | + | |
- | start,_,> | + | |
- | + | ||
- | reset,A | + | |
- | reset,A,< | + | |
- | + | ||
- | findOneMoreA,M | + | |
- | N,_,- | + | |
- | + | ||
- | findOneMoreA,_ | + | |
- | N,_,- | + | |
- | + | ||
- | findOneMoreA,A | + | |
- | checkIfEmptyA,_,> | + | |
- | + | ||
- | AddAnM,M | + | |
- | AddAnM,M,> | + | |
- | + | ||
- | AddAnM,_ | + | |
- | reset,M,< | + | |
- | + | ||
- | AddAnM,A | + | |
- | AddAnM,A,> | + | |
- | + | ||
- | checkIfEmptyM,M | + | |
- | AddAnA,M,> | + | |
- | + | ||
- | checkIfEmptyM,_ | + | |
- | Y,_,- | + | |
- | + | ||
- | checkIfEmptyM,A | + | |
- | AddAnA,A,> | + | |
- | + | ||
- | checkIfEmptyA,M | + | |
- | AddAnM,M,> | + | |
- | + | ||
- | checkIfEmptyA,_ | + | |
- | Y,_,- | + | |
- | + | ||
- | checkIfEmptyA,A | + | |
- | AddAnM,A,> | + | |
- | + | ||
- | + | ||
- | </code> | + | |
- | </hidden> | + | |
- | */ | + | |
- | + | ||
- | 7. Cățelul Max dorește sa-și facă tema la SDA, dar, din păcate, ca orice alt câine, acesta este color blind și, deci, nu vede dacă anumite paranteze sunt roși. Ajută-l pe Max să-și dea seama dacă parantezele din codul său sunt echilibrate. | + | |
- | Ex: ()((()())) -> True | + | |
- | ()((()())( -> False | + | |
- | + | ||
- | /* | + | |
- | <hidden> | + | |
- | Soluție [[https://docs.google.com/spreadsheets/d/17aCsQi6oEl5s9r9azQ4W972yh0Ur-rY3/edit?usp=sharing&ouid=113096868136191520794&rtpof=true&sd=true|Excel.]] | + | |
- | <code> | + | |
- | name: ex7 | + | |
- | init: start | + | |
- | accept: H, Y | + | |
- | + | ||
- | //Strategie: căutăm ultima paranteza deschisa "(", îi căutăm pereche și le marcăm pe ambele cu X | + | |
- | //Daca la final rămân doar X-uri pe banda înseamnă ca șirul este echilibrat. | + | |
- | + | ||
- | start,( | + | |
- | start,(,> | + | |
- | + | ||
- | start,) | + | |
- | start,),> | + | |
- | + | ||
- | start,X | + | |
- | N,X,- | + | |
- | + | ||
- | start,_ | + | |
- | FindLast(,_,< | + | |
- | + | ||
- | FindLast(,( | + | |
- | FindPereche,X,> | + | |
- | + | ||
- | FindLast(,) | + | |
- | FindLast(,),< | + | |
- | + | ||
- | FindLast(,X | + | |
- | FindLast(,X,< | + | |
- | + | ||
- | FindLast(,_ | + | |
- | clearX,_,> | + | |
- | + | ||
- | FindPereche,( | + | |
- | N,(,- | + | |
- | + | ||
- | FindPereche,) | + | |
- | FindLast(,X,< | + | |
- | + | ||
- | FindPereche,X | + | |
- | FindPereche,X,> | + | |
- | + | ||
- | FindPereche,_ | + | |
- | N,_,- | + | |
- | + | ||
- | clearX,( | + | |
- | N,(,- | + | |
- | + | ||
- | clearX,) | + | |
- | N,),- | + | |
- | + | ||
- | clearX,X | + | |
- | clearX,_,> | + | |
- | + | ||
- | clearX,_ | + | |
- | Y,_,- | + | |
- | + | ||
- | + | ||
- | </code> | + | |
- | </hidden> | + | |
- | */ | + | |
- | + | ||
- | 8. Diana are in frigider doar conserve de mazăre("M") și sticle de lapte("L"). Aceasta a mai cumpărat lapte de la magazin și vrea să așeze câte o sticlă de lapte după fiecare conservă de mazăre, mutând restul conținutului din frigider la dreapta. | + | |
- | Ex: "MML" -> "MLMLL" | + | |
- | + | ||
- | /* | + | |
- | <hidden> | + | |
- | Soluție [[https://docs.google.com/spreadsheets/d/1cSAH-z8hmqGZ49-u4dzlld4wh883fu0u/edit?usp=sharing&ouid=113096868136191520794&rtpof=true&sd=true|Excel.]] | + | |
- | <code> | + | |
- | name: ex8 | + | |
- | init: start | + | |
- | accept: H, Y | + | |
- | + | ||
- | //Strategie: Parcurgem șirul, când găsim un M, schimbăm simbolul din dreapta cu X, mutând toate caracterele o poziție la dreapta, | + | |
- | //ținem minte fiecare caracter abia schimbat cu ajutorul stărilor, | + | |
- | //apoi ne întoarcem, înlocuim X cu L si căutăm următorul M. | + | |
- | + | ||
- | start,M | + | |
- | startShift,M,> | + | |
- | + | ||
- | start,L | + | |
- | start,L,> | + | |
- | + | ||
- | start,X | + | |
- | N,X,- | + | |
- | + | ||
- | start,_ | + | |
- | Y,_,- | + | |
- | + | ||
- | startShift,M | + | |
- | keepM,X,> | + | |
- | + | ||
- | startShift,L | + | |
- | keepL,X,> | + | |
- | + | ||
- | startShift,X | + | |
- | N,X,- | + | |
- | + | ||
- | startShift,_ | + | |
- | Y,L,- | + | |
- | + | ||
- | keepM,M | + | |
- | keepM,M,> | + | |
- | + | ||
- | keepM,L | + | |
- | keepL,M,> | + | |
- | + | ||
- | keepM,X | + | |
- | N,X,- | + | |
- | + | ||
- | keepM,_ | + | |
- | reset,M,< | + | |
- | + | ||
- | keepL,M | + | |
- | keepM,L,> | + | |
- | + | ||
- | keepL,L | + | |
- | keepL,L,> | + | |
- | + | ||
- | keepL,X | + | |
- | N,X,- | + | |
- | + | ||
- | keepL,_ | + | |
- | reset,L,< | + | |
- | + | ||
- | reset,M | + | |
- | reset,M,< | + | |
- | + | ||
- | reset,L | + | |
- | reset,L,< | + | |
- | + | ||
- | reset,X | + | |
- | start,L,> | + | |
- | + | ||
- | reset,_ | + | |
- | Y,_,- | + | |
- | + | ||
- | + | ||
- | </code> | + | |
- | </hidden> | + | |
- | */ | + | |
- | + | ||
- | 9. Johnny maimuța se afla la capătul unui șir format din banane("B") si mere("M"). Ajutați-l pe Johnny să numere bananele și, apoi, să fugă cu ele la capătul șirului. | + | |
- | Ex: "JBBMBMBMB" -> "BBMBMBMBJ101" | + | |
- | + | ||
- | /* | + | |
- | <hidden> | + | |
- | Soluție [[https://docs.google.com/spreadsheets/d/14EUgDigN133zj3xH9Lh_9T7dKuPr1Llh/edit?usp=sharing&ouid=113096868136191520794&rtpof=true&sd=true|Excel.]] | + | |
- | <code> | + | |
- | name: ex9 | + | |
- | init: start | + | |
- | accept: H, Y | + | |
- | + | ||
- | //Strategie: Primul pas, Johnny retine numărul 0, adică adăugăm un 0 la stânga lui J. | + | |
- | //După parcurgem șirul căutând banane. Când am găsit o banană mergem la stânga lui Johnny și facem un binary increment, exact ca în //exemplul introductiv. | + | |
- | //La final îl mutam pe Johnny și pe numărul din mintea sa la dreapta șirului. | + | |
- | + | ||
- | start,0 | + | |
- | add0,0,< | + | |
- | + | ||
- | start,1 | + | |
- | add0,1,< | + | |
- | + | ||
- | start,_ | + | |
- | Y,0,- | + | |
- | + | ||
- | start,J | + | |
- | add0,J,< | + | |
- | + | ||
- | start,X | + | |
- | N,_,- | + | |
- | + | ||
- | start,B | + | |
- | add0,B,< | + | |
- | + | ||
- | start,M | + | |
- | add0,M,< | + | |
- | + | ||
- | findB,0 | + | |
- | findB,0,> | + | |
- | + | ||
- | findB,1 | + | |
- | findB,1,> | + | |
- | + | ||
- | findB,_ | + | |
- | moveNrLast,J,< | + | |
- | + | ||
- | findB,J | + | |
- | findB,J,> | + | |
- | + | ||
- | findB,X | + | |
- | findB,X,> | + | |
- | + | ||
- | findB,B | + | |
- | goBI,X,< | + | |
- | + | ||
- | findB,M | + | |
- | findB,M,> | + | |
- | + | ||
- | goBI,0 | + | |
- | goBI,0,< | + | |
- | + | ||
- | goBI,1 | + | |
- | goBI,1,< | + | |
- | + | ||
- | goBI,_ | + | |
- | N,_,- | + | |
- | + | ||
- | goBI,J | + | |
- | doBI,J,< | + | |
- | + | ||
- | goBI,X | + | |
- | goBI,X,< | + | |
- | + | ||
- | goBI,B | + | |
- | goBI,B,< | + | |
- | + | ||
- | goBI,M | + | |
- | goBI,M,< | + | |
- | + | ||
- | add0,0 | + | |
- | N,_,- | + | |
- | + | ||
- | add0,1 | + | |
- | N,_,- | + | |
- | + | ||
- | add0,_ | + | |
- | findB,0,> | + | |
- | + | ||
- | add0,J | + | |
- | N,_,- | + | |
- | + | ||
- | add0,X | + | |
- | N,_,- | + | |
- | + | ||
- | add0,B | + | |
- | N,_,- | + | |
- | + | ||
- | add0,M | + | |
- | N,_,- | + | |
- | + | ||
- | doBI,0 | + | |
- | gotoJ,1,> | + | |
- | + | ||
- | doBI,1 | + | |
- | doBI,0,< | + | |
- | + | ||
- | doBI,_ | + | |
- | gotoJ,1,> | + | |
- | + | ||
- | doBI,J | + | |
- | N,_,- | + | |
- | + | ||
- | doBI,X | + | |
- | N,_,- | + | |
- | + | ||
- | doBI,B | + | |
- | N,_,- | + | |
- | + | ||
- | doBI,M | + | |
- | N,_,- | + | |
- | + | ||
- | gotoJ,0 | + | |
- | gotoJ,0,> | + | |
- | + | ||
- | gotoJ,1 | + | |
- | gotoJ,1,> | + | |
- | + | ||
- | gotoJ,_ | + | |
- | N,_,- | + | |
- | + | ||
- | gotoJ,J | + | |
- | findB,J,> | + | |
- | + | ||
- | gotoJ,X | + | |
- | gotoJ,X,> | + | |
- | + | ||
- | gotoJ,B | + | |
- | N,_,- | + | |
- | + | ||
- | gotoJ,M | + | |
- | N,_,- | + | |
- | + | ||
- | moveNrLast,0 | + | |
- | moveNrLast,0,< | + | |
- | + | ||
- | moveNrLast,1 | + | |
- | moveNrLast,1,< | + | |
- | + | ||
- | moveNrLast,_ | + | |
- | goBack,_,> | + | |
- | + | ||
- | moveNrLast,J | + | |
- | moveNrLast,J,< | + | |
- | + | ||
- | moveNrLast,X | + | |
- | moveNrLast,B,< | + | |
- | + | ||
- | moveNrLast,B | + | |
- | moveNrLast,B,< | + | |
- | + | ||
- | moveNrLast,M | + | |
- | moveNrLast,M,< | + | |
- | + | ||
- | goBack,0 | + | |
- | keep0,_,> | + | |
- | + | ||
- | goBack,1 | + | |
- | keep1,_,> | + | |
- | + | ||
- | goBack,_ | + | |
- | N,_,- | + | |
- | + | ||
- | goBack,J | + | |
- | Y,_,- | + | |
- | + | ||
- | goBack,X | + | |
- | N,_,- | + | |
- | + | ||
- | goBack,B | + | |
- | N,_,- | + | |
- | + | ||
- | goBack,M | + | |
- | N,_,- | + | |
- | + | ||
- | keep0,0 | + | |
- | keep0,0,> | + | |
- | + | ||
- | keep0,1 | + | |
- | keep0,1,> | + | |
- | + | ||
- | keep0,_ | + | |
- | N,_,- | + | |
- | + | ||
- | keep0,J | + | |
- | keep02,J,> | + | |
- | + | ||
- | keep0,X | + | |
- | N,_,- | + | |
- | + | ||
- | keep0,B | + | |
- | keep0,B,> | + | |
- | + | ||
- | keep0,M | + | |
- | keep0,M,> | + | |
- | + | ||
- | keep1,0 | + | |
- | keep1,0,> | + | |
- | + | ||
- | keep1,1 | + | |
- | keep1,1,> | + | |
- | + | ||
- | keep1,_ | + | |
- | N,_,- | + | |
- | + | ||
- | keep1,J | + | |
- | keep12,J,> | + | |
- | + | ||
- | keep1,X | + | |
- | N,_,- | + | |
- | + | ||
- | keep1,B | + | |
- | keep1,B,> | + | |
- | + | ||
- | keep1,M | + | |
- | keep1,M,> | + | |
- | + | ||
- | put0,0 | + | |
- | put0,0,> | + | |
- | + | ||
- | put0,1 | + | |
- | put0,1,> | + | |
- | + | ||
- | put0,_ | + | |
- | moveNrLast,0,< | + | |
- | + | ||
- | put0,J | + | |
- | N,_,- | + | |
- | + | ||
- | put0,X | + | |
- | N,_,- | + | |
- | + | ||
- | put0,B | + | |
- | put0,B,> | + | |
- | + | ||
- | put0,M | + | |
- | put0,M,> | + | |
- | + | ||
- | put1,0 | + | |
- | put1,0,> | + | |
- | + | ||
- | put1,1 | + | |
- | put1,1,> | + | |
- | + | ||
- | put1,_ | + | |
- | moveNrLast,1,< | + | |
- | + | ||
- | put1,J | + | |
- | N,_,- | + | |
- | + | ||
- | put1,X | + | |
- | N,_,- | + | |
- | + | ||
- | put1,B | + | |
- | put1,B,> | + | |
- | + | ||
- | put1,M | + | |
- | put1,M,> | + | |
- | + | ||
- | keep02,0 | + | |
- | keep02,0,> | + | |
- | + | ||
- | keep02,1 | + | |
- | keep02,1,> | + | |
- | + | ||
- | keep02,_ | + | |
- | N,_,- | + | |
- | + | ||
- | keep02,J | + | |
- | put0,J,> | + | |
- | + | ||
- | keep02,X | + | |
- | N,_,- | + | |
- | + | ||
- | keep02,B | + | |
- | keep02,B,> | + | |
- | + | ||
- | keep02,M | + | |
- | keep02,M,> | + | |
- | + | ||
- | keep12,0 | + | |
- | keep12,0,> | + | |
- | + | ||
- | keep12,1 | + | |
- | keep12,1,> | + | |
- | + | ||
- | keep12,_ | + | |
- | N,_,- | + | |
- | + | ||
- | keep12,J | + | |
- | put1,J,> | + | |
- | + | ||
- | keep12,X | + | |
- | N,_,- | + | |
- | + | ||
- | keep12,B | + | |
- | keep12,B,> | + | |
- | + | ||
- | keep12,M | + | |
- | keep12,M,> | + | |
- | + | ||
- | + | ||
- | </code> | + | |
- | </hidden> | + | |
- | */ | + | |
- | + | ||
- | ==== Exerciții tryhard ==== | + | |
- | 10. Scrieți o Mașină Turing care primește două numere în baza 2, big-endian, separate de un # și lasă pe bandă suma lor. | + | |
- | Ex: "1011#11001" -> "100100" | + | |
- | + | ||
- | /* | + | |
- | <hidden> | + | |
- | Soluție [[https://docs.google.com/spreadsheets/d/1ba-I4AgH9O7bdXox1zX_p26Ymb_-D85N/edit?usp=sharing&ouid=113096868136191520794&rtpof=true&sd=true|Excel.]] | + | |
- | <code> | + | |
- | + | ||
- | name: ex10 | + | |
- | init: q1 | + | |
- | accept: halt | + | |
- | + | ||
- | // q1 parcurge primul numar | + | |
- | // pana la separatorul dintre numere | + | |
- | q1,0 | + | |
- | q1,0,> | + | |
- | + | ||
- | q1,1 | + | |
- | q1,1,> | + | |
- | + | ||
- | q1,# | + | |
- | get_adding_number,#,< | + | |
- | + | ||
- | // get_adding_number ia urmatorul digit din primul numar | + | |
- | // pentru a-l adauga la al doilea numar, si il inlocuieste cu | + | |
- | // un X (care inseamna ca am folosit digit-ul) | + | |
- | get_adding_number,0 | + | |
- | go_second_0,X,> | + | |
- | + | ||
- | get_adding_number,1 | + | |
- | go_second_1,X,> | + | |
- | + | ||
- | get_adding_number,X | + | |
- | get_adding_number,X,< | + | |
- | + | ||
- | get_adding_number,_ | + | |
- | finish,_,> | + | |
- | + | ||
- | // go_second_0 si go_second_1 parcurge pana la al doilea numar | + | |
- | // retinand cat trebuie adaugat | + | |
- | go_second_0,# | + | |
- | go_to_place_0,#,> | + | |
- | + | ||
- | go_second_0,X | + | |
- | go_second_0,X,> | + | |
- | + | ||
- | go_second_1,# | + | |
- | go_to_place_1,#,> | + | |
- | + | ||
- | go_second_1,X | + | |
- | go_second_1,X,> | + | |
- | + | ||
- | // go_to_place_0 si go_to_place_1 parcurg al doilea numar pana | + | |
- | // unde am ajuns cu adunarea (cifrele deja parcurse au fost | + | |
- | // inlocuite cu a si b in loc de 0 si 1) | + | |
- | go_to_place_0,0 | + | |
- | go_to_place_0,0,> | + | |
- | + | ||
- | go_to_place_0,1 | + | |
- | go_to_place_0,1,> | + | |
- | + | ||
- | go_to_place_0,a | + | |
- | add_0,a,< | + | |
- | + | ||
- | go_to_place_0,b | + | |
- | add_0,b,< | + | |
- | + | ||
- | go_to_place_0,_ | + | |
- | add_0,_,< | + | |
- | + | ||
- | go_to_place_1,0 | + | |
- | go_to_place_1,0,> | + | |
- | + | ||
- | go_to_place_1,1 | + | |
- | go_to_place_1,1,> | + | |
- | + | ||
- | go_to_place_1,a | + | |
- | add_1,a,< | + | |
- | + | ||
- | go_to_place_1,b | + | |
- | add_1,b,< | + | |
- | + | ||
- | go_to_place_1,_ | + | |
- | add_1,_,< | + | |
- | + | ||
- | // go_first_number se intoarce spre dreapta la primul numar | + | |
- | go_first_number,0 | + | |
- | go_first_number,0,< | + | |
- | + | ||
- | go_first_number,1 | + | |
- | go_first_number,1,< | + | |
- | + | ||
- | go_first_number,a | + | |
- | go_first_number,a,< | + | |
- | + | ||
- | go_first_number,b | + | |
- | go_first_number,b,< | + | |
- | + | ||
- | go_first_number,# | + | |
- | get_adding_number,#,< | + | |
- | + | ||
- | // adaugam 0 la numarul curent si inlocuim numarul cu a/b pt 0/1 | + | |
- | add_0,0 | + | |
- | go_first_number,a,< | + | |
- | + | ||
- | add_0,1 | + | |
- | go_first_number,b,< | + | |
- | + | ||
- | add_0,# | + | |
- | translate_right_a,#,> | + | |
- | + | ||
- | // adaugam 1 la numarul curent si inlocuim numarul cu a/b pt 0/1 | + | |
- | add_1,0 | + | |
- | go_first_number,b,< | + | |
- | + | ||
- | add_1,1 | + | |
- | add_second_1,a,< | + | |
- | + | ||
- | add_1,# | + | |
- | translate_right_b,#,> | + | |
- | + | ||
- | add_second_1,0 | + | |
- | go_first_number,1,< | + | |
- | + | ||
- | add_second_1,1 | + | |
- | add_second_1,0,< | + | |
- | + | ||
- | add_second_1,# | + | |
- | translate_right_1,#,> | + | |
- | + | ||
- | // functii pentru translatarea la dreapta a celui de-al doilea numar, | + | |
- | // unde caracterele posibile pot fi 0,1,a,b | + | |
- | translate_right_0,0 | + | |
- | translate_right_0,0,> | + | |
- | + | ||
- | translate_right_0,1 | + | |
- | translate_right_1,0,> | + | |
- | + | ||
- | translate_right_0,a | + | |
- | translate_right_a,0,> | + | |
- | + | ||
- | translate_right_0,b | + | |
- | translate_right_b,0,> | + | |
- | + | ||
- | translate_right_0,_ | + | |
- | go_first_number,0,< | + | |
- | + | ||
- | translate_right_1,0 | + | |
- | translate_right_0,1,> | + | |
- | + | ||
- | translate_right_1,1 | + | |
- | translate_right_1,1,> | + | |
- | + | ||
- | translate_right_1,a | + | |
- | translate_right_a,1,> | + | |
- | + | ||
- | translate_right_1,b | + | |
- | translate_right_b,1,> | + | |
- | + | ||
- | translate_right_1,_ | + | |
- | go_first_number,1,< | + | |
- | + | ||
- | translate_right_a,0 | + | |
- | translate_right_0,a,> | + | |
- | + | ||
- | translate_right_a,1 | + | |
- | translate_right_1,a,> | + | |
- | + | ||
- | translate_right_a,a | + | |
- | translate_right_a,a,> | + | |
- | + | ||
- | translate_right_a,b | + | |
- | translate_right_b,a,> | + | |
- | + | ||
- | translate_right_a,_ | + | |
- | go_first_number,a,< | + | |
- | + | ||
- | translate_right_b,0 | + | |
- | translate_right_0,b,> | + | |
- | + | ||
- | translate_right_b,1 | + | |
- | translate_right_1,b,> | + | |
- | + | ||
- | translate_right_b,a | + | |
- | translate_right_a,b,> | + | |
- | + | ||
- | translate_right_b,b | + | |
- | translate_right_b,b,> | + | |
- | + | ||
- | translate_right_b,_ | + | |
- | go_first_number,b,< | + | |
- | + | ||
- | // cand ajungem in finish trebuie sa facem clean-up la banda | + | |
- | // sa stergem X-uri si # | + | |
- | // sa transformam a in 0 si b in 1 | + | |
- | + | ||
- | finish,X | + | |
- | finish,_,> | + | |
- | + | ||
- | finish,# | + | |
- | finish,_,> | + | |
- | + | ||
- | finish,1 | + | |
- | finish,1,> | + | |
- | + | ||
- | finish,0 | + | |
- | finish,0,> | + | |
- | + | ||
- | finish,a | + | |
- | finish,0,> | + | |
- | + | ||
- | finish,b | + | |
- | finish,1,> | + | |
- | + | ||
- | finish,_ | + | |
- | halt,_,- | + | |
- | </code> | + | |
- | </hidden> | + | |
- | */ | + |