Keyboard game [] (https://www.codechef.com/PATN2021/problems/KEYBO3D)

Practice

Author: Aryan KD
Tester: Aryan KD
Editorialist: Aryan KD

DIFFICULTY:

CAKEWALK, SIMPLE, EASY.

PREREQUISITES:

Math .

PROBLEM:

Devil is playing game on keyboard he closing his eyes and typing some word in keyboard . after typing the word , when he opens his eyes and see the words if the words are not according to Game rule then he makes changes in the word according to game rule , but now he is busy then we have to do his work.

Lets consider word is typed by closing his eyes

Rules for changing the word if :

  • All letters except for the first one are uppercase ,
  • It only contains uppercase letters ,
  • or All letters except for last are uppercase .

In above cases we should automatically change the case of all letters. For example, the case of the letters that form words “hELLO”, “HTTP” ,hTTP" should be changed .

Write a program that applies the rule mentioned above. If the rule cannot be applied, the program should leave the word unchanged.

EXPLANATION:

In above problem we have scan one string and we have to check whether the given string follows all the rules according to problem then print according to it

lets take a rule 1 if all letters except for the first one are uppercase then we have to case of all letters
for example

aNUR

in above example we have to check wether the first letter is small and all the latter is in upper case if it is true then we have to change the case of all letters so the output is Anur

now rule 2 is saying if all letters are capital letters then we have to change the case of all the latters .

now 3rd rule this rule is pretty much similar to first rule only differ is that we have to check except for last letter all are in uppercase in that situation he have to change case of all letters

if the word is not follow above rule then we have to print the word as it is .

SOLUTIONS:

Setter's Solution
  1. //keyboard game

  2. #include<bits/stdc++.h>

  3. using namespace std;

  4. void testcase()

  5. {

  6. string s;

  7. cin>>s;

  8. int pt=s.size();

  9. int l,k;

  10. int flag1=0,flag2=0,flag3=0,flag4=0,flag5=0;

  11. for(int i=1;i<s.length();i++){

  12. if(s[i]<=‘Z’ && s[i]>=‘A’){

  13. continue;

  14. }

  15. else flag3=1;

  16. }

  17. for(int i=0;i<s.length()-1;i++){

  18. if(s[i]<=‘Z’ && s[i]>=‘A’){

  19. continue;

  20. }

  21. else flag4=1;

  22. }

  • if(s[0]>=‘a’ && s[0]<=‘z’){

  • flag2=1;

  1. }

  2. if(s[pt-1]>=‘a’ && s[pt-1]<=‘z’){

  • flag5=1;
  1. }
  • for(int i=0;i<s.length();i++){
  1. if(s[i]<=‘Z’ && s[i]>=‘A’){

  2. continue;

  3. }

  4. else flag1=1;

  5. }

  6. if((s[0]>=‘a’ && s[0]<=‘z’)&&(s.length()==1) ){

  7. s[0]-=32;

  8. cout<<s;

  9. }

  10. else if(flag2 && flag5 && pt==2){

  11. for(int i=0;i<pt;i++){

  12. s[i]-=32;

  13. }

  14. cout<<s;

  15. }

  16. else if((s[pt-1]>=‘a’ && s[pt-1]<=‘z’)&&(!flag4)){

  17. //s[0]-=32;

  18. s[pt-1]-=32;

  19. for(int i=0;i<s.length()-1;i++){

  20. s[i]+=32;

  21. }

  22. cout<<s;

  23. }

  24. else if(!flag1){

  25. //cout<<flag1;

  26. //cout<<“no”;

  27. for(int i=0;i<s.length();i++){

  28. s[i]+=32;

  29. }

  30. cout<<s;

  31. }

  • else if(!flag3&&flag2){
  1. s[0]-=32;

  2. for(int i=1;i<s.length();i++){

  3. s[i]+=32;

  4. }

  5. cout<<s;

  6. }

  7. else{

  8. cout<<s;

  9. }

  • }
  1. int main()

  2. {

  3. int t;

  4. cin>>t;

  5. while(t–)

  6. {

  7. testcase();

  8. cout<<endl;

  9. }

  10. return 0;

  11. }

Tester's Solution
  1. //keyboard game

  2. #include<bits/stdc++.h>

  3. using namespace std;

  4. void testcase()

  5. {

  6. string s;

  7. cin>>s;

  8. int pt=s.size();

  9. int l,k;

  10. int flag1=0,flag2=0,flag3=0,flag4=0,flag5=0;

  11. for(int i=1;i<s.length();i++){

  12. if(s[i]<=‘Z’ && s[i]>=‘A’){

  13. continue;

  14. }

  15. else flag3=1;

  16. }

  17. for(int i=0;i<s.length()-1;i++){

  18. if(s[i]<=‘Z’ && s[i]>=‘A’){

  19. continue;

  20. }

  21. else flag4=1;

  22. }

  • if(s[0]>=‘a’ && s[0]<=‘z’){

  • flag2=1;

  1. }

  2. if(s[pt-1]>=‘a’ && s[pt-1]<=‘z’){

  • flag5=1;
  1. }
  • for(int i=0;i<s.length();i++){
  1. if(s[i]<=‘Z’ && s[i]>=‘A’){

  2. continue;

  3. }

  4. else flag1=1;

  5. }

  6. if((s[0]>=‘a’ && s[0]<=‘z’)&&(s.length()==1) ){

  7. s[0]-=32;

  8. cout<<s;

  9. }

  10. else if(flag2 && flag5 && pt==2){

  11. for(int i=0;i<pt;i++){

  12. s[i]-=32;

  13. }

  14. cout<<s;

  15. }

  16. else if((s[pt-1]>=‘a’ && s[pt-1]<=‘z’)&&(!flag4)){

  17. //s[0]-=32;

  18. s[pt-1]-=32;

  19. for(int i=0;i<s.length()-1;i++){

  20. s[i]+=32;

  21. }

  22. cout<<s;

  23. }

  24. else if(!flag1){

  25. //cout<<flag1;

  26. //cout<<“no”;

  27. for(int i=0;i<s.length();i++){

  28. s[i]+=32;

  29. }

  30. cout<<s;

  31. }

  • else if(!flag3&&flag2){
  1. s[0]-=32;

  2. for(int i=1;i<s.length();i++){

  3. s[i]+=32;

  4. }

  5. cout<<s;

  6. }

  7. else{

  8. cout<<s;

  9. }

  • }
  1. int main()

  2. {

  3. int t;

  4. cin>>t;

  5. while(t–)

  6. {

  7. testcase();

  8. cout<<endl;

  9. }

  10. return 0;

  11. }

Editorialist's Solution
  1. //keyboard game

  2. #include<bits/stdc++.h>

  3. using namespace std;

  4. void testcase()

  5. {

  6. string s;

  7. cin>>s;

  8. int pt=s.size();

  9. int l,k;

  10. int flag1=0,flag2=0,flag3=0,flag4=0,flag5=0;

  11. for(int i=1;i<s.length();i++){

  12. if(s[i]<=‘Z’ && s[i]>=‘A’){

  13. continue;

  14. }

  15. else flag3=1;

  16. }

  17. for(int i=0;i<s.length()-1;i++){

  18. if(s[i]<=‘Z’ && s[i]>=‘A’){

  19. continue;

  20. }

  21. else flag4=1;

  22. }

  • if(s[0]>=‘a’ && s[0]<=‘z’){

  • flag2=1;

  1. }

  2. if(s[pt-1]>=‘a’ && s[pt-1]<=‘z’){

  • flag5=1;
  1. }
  • for(int i=0;i<s.length();i++){
  1. if(s[i]<=‘Z’ && s[i]>=‘A’){

  2. continue;

  3. }

  4. else flag1=1;

  5. }

  6. if((s[0]>=‘a’ && s[0]<=‘z’)&&(s.length()==1) ){

  7. s[0]-=32;

  8. cout<<s;

  9. }

  10. else if(flag2 && flag5 && pt==2){

  11. for(int i=0;i<pt;i++){

  12. s[i]-=32;

  13. }

  14. cout<<s;

  15. }

  16. else if((s[pt-1]>=‘a’ && s[pt-1]<=‘z’)&&(!flag4)){

  17. //s[0]-=32;

  18. s[pt-1]-=32;

  19. for(int i=0;i<s.length()-1;i++){

  20. s[i]+=32;

  21. }

  22. cout<<s;

  23. }

  24. else if(!flag1){

  25. //cout<<flag1;

  26. //cout<<“no”;

  27. for(int i=0;i<s.length();i++){

  28. s[i]+=32;

  29. }

  30. cout<<s;

  31. }

  • else if(!flag3&&flag2){
  1. s[0]-=32;

  2. for(int i=1;i<s.length();i++){

  3. s[i]+=32;

  4. }

  5. cout<<s;

  6. }

  7. else{

  8. cout<<s;

  9. }

  • }
  1. int main()

  2. {

  3. int t;

  4. cin>>t;

  5. while(t–)

  6. {

  7. testcase();

  8. cout<<endl;

  9. }

  10. return 0;

  11. }