Showing posts with label if/else. Show all posts
Showing posts with label if/else. Show all posts

Wednesday, January 13, 2016

Limbajul C/C++. Operatorul ternar versus if/else

Sigur ca pare doar un mic amuzament sa rezolvi o problema de decizie cu operatorul ternar, singurul din tot limbajul care functioneaza cu trei operanzi. Uneori insa, in virtutea conciziei, o astfel de optiune este justificata, dupa cum este justificat, ba chiar preferabil, din considerente de eficienta, sa faci atribuirea:

int x = 0;
while(x <= 10) {x += 1;} in loc de "x = x + 1;"

intrucat in al doilea caz x este evaluat de doua ori.

Enuntul de pe programmr.com:

"Take input of experience and age of a person.
If the person is experienced and his age is equal to or more than 35 the slary of the person is 6000. Otherwise, if the person is experienced and his age is equal to or more than 28 but less than 35, then the salary should be 4800.For experienced person below 28 yr of age the salary should be 3000 and for inexperienced person the salary should be 2000."

Tuesday, January 12, 2016

Limbajul PHP. Guessing game. Tablouri multidimensionale

Urmatorul enunt de pe www.programmr.com este o problema simpla cu sase scenarii posibile, raspunsurile de selectie fiind introduse de la tastatura.

Se cere sa se identifice prin doua interogari un obiect intr-o matrice 2 x 3 (screen-ul urmator).

Din pacate, softul de pe programmr.com valideaza si topica si compozitia stringului de iesire, dupa urmatorul pattern.

1) "alive" & "outside" sau "alive" & "inside":  Obviously the living thing on your mind is a ...

2) "not alive" & "outside" sau "not alive" & "inside" : Obviously the nonliving thing on your mind is a ...
3) ("alive" sau "not alive") && "both":   Obviously the nonliving thing inside/outside on your mind is a ...

Altminteri, problema e triviala iar varianta comoda este stocarea entitatilor living/not living etc intr-un tablou bidimensional cu cheile indicate mai jos.


Friday, January 8, 2016

Limbajul PHP. The proper way to adress someone ... in English :-)


Conform multiplelor surse, e o mica bataie de cap sa te adresezi "cu eticheta" unei persoane in limba engleza, presupunand ca nu-i cunosti dinainte sexul, varsta si starea civila.

Teoria spune ca daca e vorba de femei majore, nemaritate, folosesti formula ambigua Ms. in fata numelul de familie (last name) sau chiar in fata numelui intreg. Daca femeia-i maritata, folosesti fara teama Mrs. in fata numelui (intreg sau de familie). Ambele formule isi au originea in "Mistress".

Enuntul urmator de pe programmr.com introduce in plus urmatoarea regula fara discriminare de sex: daca varsta este sub cea a majoratului, persoana este chemata pe numele complet. Daca e vorba de un barbat major, se foloseste Mr. inaintea numelui de familie.

"Write a program which displays an appropriate name for a person, using a combination of nested if's and compound conditions. Ask the user for a gender, first name, last name and age.
If the person is female and 20 or over, ask if she is married. If so, display "Mrs." in front of her name. If not, display "Ms." in front of her name. If the female is under 20, display her first and last name.
If the person is male and 20 or over, display "Mr." in front of his name. Otherwise, display his first and last name.
Note that asking a person if they are married should only be done if they are female and 20 or older."