├── CSE (IOT) ├── COMPUTER NETWORK LAB │ ├── README.md │ ├── CISCO │ │ ├── dhcp.pkt │ │ ├── first.pkt │ │ ├── ftp 2.pkt │ │ ├── ftp1.pkt │ │ ├── readme.md │ │ ├── vlan.pkt │ │ ├── bus topology.pkt │ │ ├── mesh topology.pkt │ │ ├── star topology.pkt │ │ ├── hybrid topology.pkt │ │ ├── inter vlan routing.pkt │ │ └── router as dhcp server.pkt │ ├── Computer Networks Laboratory_Assignment-1.pdf │ ├── Computer Networks Laboratory_Assignment-2.pdf │ └── Computer Networks Laboratory_Assignment-3.pdf ├── HACKER RANK ASSIGNMENT │ ├── Week 1 │ │ ├── README.md │ │ └── 1.txt │ ├── Week 2 │ │ ├── README.md │ │ └── 2.txt │ ├── README.md │ ├── Week 4 │ │ └── 4.txt │ └── Week 5 │ │ └── 5.md └── COMPILER DESIGN LAB │ ├── README.md │ ├── Compiler Design Full Assignment.pdf │ ├── Week1 │ ├── Q2.md │ └── Q1.md │ ├── Week5 │ ├── q10.md │ └── q9.md │ ├── Week3 │ ├── q6.md │ └── q5.md │ ├── Week4 │ ├── q7.md │ └── q8.md │ └── Week2 │ ├── q3.md │ └── q4.md ├── CSE AIML ├── COMPUTER NETWORK LAB │ ├── README.md │ ├── Assignment 1 │ │ ├── 1.jpg │ │ ├── 2.jpg │ │ ├── 3.jpg │ │ ├── 4.jpg │ │ └── 5.jpg │ └── Assignment 2 │ │ └── Networking Commands.pdf ├── HACKER RANK ASSIGNMENT │ ├── Week 1 │ │ ├── README.md │ │ └── 1.txt │ ├── Week 2 │ │ ├── README.md │ │ └── 2.txt │ ├── README.md │ ├── Week 4 │ │ └── 4.txt │ └── Week 5 │ │ └── 5.md └── COMPILER DESIGN LAB │ ├── Compiler Design Full Assignment.pdf │ └── README.md ├── CSE CORE ├── COMPUTER NETWORK LAB │ ├── README.md │ ├── Assignment 5 │ │ ├── shit.md │ │ └── README.md │ ├── Assignment 6 │ │ ├── cisco.md │ │ └── README.md │ ├── Assignment 2 │ │ ├── README.md │ │ └── 2.md │ ├── Assignment 4 │ │ ├── TCPclient.c │ │ ├── TCPserver.c │ │ ├── TCPclientFILE.c │ │ ├── TCPserverFILE.c │ │ ├── UDPclientFILE.c │ │ ├── UDPserverFILE.c │ │ ├── README.md │ │ └── Write_this.md │ ├── Assignment 1 │ │ ├── README.md │ │ └── 1.md │ └── Assignment 3 │ │ ├── UDPclient2.c │ │ ├── README.md │ │ ├── UDPserver2.c │ │ ├── UDPserver.c │ │ └── UDPclient.c ├── COMPILER DESIGN LAB │ ├── COMPILER DESIGN LAB MANUAL.pdf │ ├── Full │ │ ├── COMPILER DESIGN LAB MANUAL WITH ANSWEARS.pdf │ │ └── README.md │ ├── Assignment 7 │ │ └── Week 8.md │ ├── Assignment 10 │ │ └── Week 11.md │ ├── Assignment 11 │ │ └── Week 12.md │ ├── Assignment 9 │ │ └── Week 10.md │ ├── Assignment 6 │ │ └── Week 7.md │ ├── Assignment 8 │ │ └── week 9.md │ ├── Assignment 5 │ │ └── Week 6.md │ ├── Assignment 3 │ │ └── Week 4.md │ ├── Assignment 4 │ │ └── Week 5.md │ ├── Assignment 2 │ │ └── Week 3.txt │ └── Assignment 1 │ │ └── Week 1 & Week 2.txt └── HACKER RANK ASSIGNMENT │ ├── Week 4 │ ├── README.md │ └── 4.txt │ ├── README.md │ ├── Week 3 │ └── README.md │ ├── Week 5 │ ├── README.md │ └── 5.md │ ├── Week 6 │ └── README.md │ ├── Week 1 │ ├── README.md │ └── 1.txt │ └── Week 2 │ ├── README.md │ └── 2.txt └── Syllabus ├── IOT SYLLABUS.pdf └── UEMK 6th Sem CSE syllabus 2021-2025 batch.pdf /CSE (IOT)/COMPUTER NETWORK LAB/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /CSE AIML/COMPUTER NETWORK LAB/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /CSE AIML/HACKER RANK ASSIGNMENT/Week 1/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 5/shit.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 6/cisco.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /CSE (IOT)/HACKER RANK ASSIGNMENT/Week 1/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /CSE (IOT)/HACKER RANK ASSIGNMENT/Week 2/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /CSE AIML/HACKER RANK ASSIGNMENT/Week 2/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /CSE (IOT)/COMPILER DESIGN LAB/README.md: -------------------------------------------------------------------------------- 1 | ## You can Submit handwriten OR printed outputs 2 | -------------------------------------------------------------------------------- /Syllabus/IOT SYLLABUS.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/Syllabus/IOT SYLLABUS.pdf -------------------------------------------------------------------------------- /CSE (IOT)/COMPUTER NETWORK LAB/CISCO/dhcp.pkt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE (IOT)/COMPUTER NETWORK LAB/CISCO/dhcp.pkt -------------------------------------------------------------------------------- /CSE (IOT)/COMPUTER NETWORK LAB/CISCO/first.pkt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE (IOT)/COMPUTER NETWORK LAB/CISCO/first.pkt -------------------------------------------------------------------------------- /CSE (IOT)/COMPUTER NETWORK LAB/CISCO/ftp 2.pkt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE (IOT)/COMPUTER NETWORK LAB/CISCO/ftp 2.pkt -------------------------------------------------------------------------------- /CSE (IOT)/COMPUTER NETWORK LAB/CISCO/ftp1.pkt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE (IOT)/COMPUTER NETWORK LAB/CISCO/ftp1.pkt -------------------------------------------------------------------------------- /CSE (IOT)/COMPUTER NETWORK LAB/CISCO/readme.md: -------------------------------------------------------------------------------- 1 | #### Packet Tracer Contributions by [@Rajat](https://github.com/RajatKundu853/Rajat-Kundu) 2 | -------------------------------------------------------------------------------- /CSE (IOT)/COMPUTER NETWORK LAB/CISCO/vlan.pkt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE (IOT)/COMPUTER NETWORK LAB/CISCO/vlan.pkt -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 5/README.md: -------------------------------------------------------------------------------- 1 | ## YES THIS FILES ARE NOT YET WRITTEN OR COMPLETE BUT WILL BE DONE BEFORE WE NEED IT ! 2 | -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 6/README.md: -------------------------------------------------------------------------------- 1 | ## YES THIS FILES ARE NOT YET WRITTEN OR COMPLETE BUT WILL BE DONE BEFORE WE NEED IT ! 2 | -------------------------------------------------------------------------------- /CSE AIML/COMPUTER NETWORK LAB/Assignment 1/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE AIML/COMPUTER NETWORK LAB/Assignment 1/1.jpg -------------------------------------------------------------------------------- /CSE AIML/COMPUTER NETWORK LAB/Assignment 1/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE AIML/COMPUTER NETWORK LAB/Assignment 1/2.jpg -------------------------------------------------------------------------------- /CSE AIML/COMPUTER NETWORK LAB/Assignment 1/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE AIML/COMPUTER NETWORK LAB/Assignment 1/3.jpg -------------------------------------------------------------------------------- /CSE AIML/COMPUTER NETWORK LAB/Assignment 1/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE AIML/COMPUTER NETWORK LAB/Assignment 1/4.jpg -------------------------------------------------------------------------------- /CSE AIML/COMPUTER NETWORK LAB/Assignment 1/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE AIML/COMPUTER NETWORK LAB/Assignment 1/5.jpg -------------------------------------------------------------------------------- /CSE (IOT)/COMPUTER NETWORK LAB/CISCO/bus topology.pkt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE (IOT)/COMPUTER NETWORK LAB/CISCO/bus topology.pkt -------------------------------------------------------------------------------- /CSE (IOT)/COMPUTER NETWORK LAB/CISCO/mesh topology.pkt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE (IOT)/COMPUTER NETWORK LAB/CISCO/mesh topology.pkt -------------------------------------------------------------------------------- /CSE (IOT)/COMPUTER NETWORK LAB/CISCO/star topology.pkt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE (IOT)/COMPUTER NETWORK LAB/CISCO/star topology.pkt -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 2/README.md: -------------------------------------------------------------------------------- 1 | # PLEASE WAIT IF U CAN CAUSE I NEED TO VERIFY THESE PROGRAMS ! DONT WRITE I GOT UPDATED QUESTIONS ! 2 | -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 4/TCPclient.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE CORE/COMPUTER NETWORK LAB/Assignment 4/TCPclient.c -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 4/TCPserver.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE CORE/COMPUTER NETWORK LAB/Assignment 4/TCPserver.c -------------------------------------------------------------------------------- /Syllabus/UEMK 6th Sem CSE syllabus 2021-2025 batch.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/Syllabus/UEMK 6th Sem CSE syllabus 2021-2025 batch.pdf -------------------------------------------------------------------------------- /CSE (IOT)/COMPUTER NETWORK LAB/CISCO/hybrid topology.pkt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE (IOT)/COMPUTER NETWORK LAB/CISCO/hybrid topology.pkt -------------------------------------------------------------------------------- /CSE (IOT)/COMPUTER NETWORK LAB/CISCO/inter vlan routing.pkt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE (IOT)/COMPUTER NETWORK LAB/CISCO/inter vlan routing.pkt -------------------------------------------------------------------------------- /CSE CORE/COMPILER DESIGN LAB/COMPILER DESIGN LAB MANUAL.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE CORE/COMPILER DESIGN LAB/COMPILER DESIGN LAB MANUAL.pdf -------------------------------------------------------------------------------- /CSE (IOT)/COMPUTER NETWORK LAB/CISCO/router as dhcp server.pkt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE (IOT)/COMPUTER NETWORK LAB/CISCO/router as dhcp server.pkt -------------------------------------------------------------------------------- /CSE AIML/COMPILER DESIGN LAB/Compiler Design Full Assignment.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE AIML/COMPILER DESIGN LAB/Compiler Design Full Assignment.pdf -------------------------------------------------------------------------------- /CSE (IOT)/COMPILER DESIGN LAB/Compiler Design Full Assignment.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE (IOT)/COMPILER DESIGN LAB/Compiler Design Full Assignment.pdf -------------------------------------------------------------------------------- /CSE AIML/COMPUTER NETWORK LAB/Assignment 2/Networking Commands.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE AIML/COMPUTER NETWORK LAB/Assignment 2/Networking Commands.pdf -------------------------------------------------------------------------------- /CSE (IOT)/COMPUTER NETWORK LAB/Computer Networks Laboratory_Assignment-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE (IOT)/COMPUTER NETWORK LAB/Computer Networks Laboratory_Assignment-1.pdf -------------------------------------------------------------------------------- /CSE (IOT)/COMPUTER NETWORK LAB/Computer Networks Laboratory_Assignment-2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE (IOT)/COMPUTER NETWORK LAB/Computer Networks Laboratory_Assignment-2.pdf -------------------------------------------------------------------------------- /CSE (IOT)/COMPUTER NETWORK LAB/Computer Networks Laboratory_Assignment-3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE (IOT)/COMPUTER NETWORK LAB/Computer Networks Laboratory_Assignment-3.pdf -------------------------------------------------------------------------------- /CSE CORE/COMPILER DESIGN LAB/Full/COMPILER DESIGN LAB MANUAL WITH ANSWEARS.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BEASTgg/6thsem/HEAD/CSE CORE/COMPILER DESIGN LAB/Full/COMPILER DESIGN LAB MANUAL WITH ANSWEARS.pdf -------------------------------------------------------------------------------- /CSE CORE/COMPILER DESIGN LAB/Full/README.md: -------------------------------------------------------------------------------- 1 | # This is the full assignment i got hope its correct , though i will still release every assignment one by one ! 2 | ## This is only for u if u are in a RUSH ! 3 | -------------------------------------------------------------------------------- /CSE AIML/COMPILER DESIGN LAB/README.md: -------------------------------------------------------------------------------- 1 | # The given pdf consists of all the weeks aka all the assignments ! Write those that has been given to u as assignment for that specific week ! Good Luck ... Keep grinding ! 2 | -------------------------------------------------------------------------------- /CSE CORE/HACKER RANK ASSIGNMENT/Week 4/README.md: -------------------------------------------------------------------------------- 1 | # DISCLAIMER - You can use this as reference and do it on your own ! But if u wish to copy from here do it on your own and if the faculty finds out then the repository handler's will not be responsible ! -------------------------------------------------------------------------------- /CSE (IOT)/HACKER RANK ASSIGNMENT/README.md: -------------------------------------------------------------------------------- 1 | ## The Questions are from 3 months preparation kit by HackerRank 2 | 3 | 4 | ## LINK FOR THE PREPARATION KIT : [CLICK HERE](https://www.hackerrank.com/interview/preparation-kits/three-month-preparation-kit) 5 | -------------------------------------------------------------------------------- /CSE AIML/HACKER RANK ASSIGNMENT/README.md: -------------------------------------------------------------------------------- 1 | ## The Questions are from 3 months preparation kit by HackerRank 2 | 3 | 4 | ## LINK FOR THE PREPARATION KIT : [CLICK HERE](https://www.hackerrank.com/interview/preparation-kits/three-month-preparation-kit) 5 | -------------------------------------------------------------------------------- /CSE CORE/HACKER RANK ASSIGNMENT/README.md: -------------------------------------------------------------------------------- 1 | ## The Questions are from 3 months preparation kit by HackerRank 2 | 3 | 4 | ## LINK FOR THE PREPARATION KIT : [CLICK HERE](https://www.hackerrank.com/interview/preparation-kits/three-month-preparation-kit) 5 | -------------------------------------------------------------------------------- /CSE CORE/HACKER RANK ASSIGNMENT/Week 3/README.md: -------------------------------------------------------------------------------- 1 | # DISCLAIMER - You can use this as reference and do it on your own ! But if u wish to copy from here do it on your own and if the faculty finds out then the repository handler's will not be responsible ! 2 | -------------------------------------------------------------------------------- /CSE CORE/HACKER RANK ASSIGNMENT/Week 5/README.md: -------------------------------------------------------------------------------- 1 | # DISCLAIMER - You can use this as reference and do it on your own ! But if u wish to copy from here do it on your own and if the faculty finds out then the repository handler's will not be responsible ! 2 | -------------------------------------------------------------------------------- /CSE CORE/HACKER RANK ASSIGNMENT/Week 6/README.md: -------------------------------------------------------------------------------- 1 | # DISCLAIMER - You can use this as reference and do it on your own ! But if u wish to copy from here do it on your own and if the faculty finds out then the repository handler's will not be responsible ! 2 | -------------------------------------------------------------------------------- /CSE CORE/HACKER RANK ASSIGNMENT/Week 1/README.md: -------------------------------------------------------------------------------- 1 | # DISCLAIMER - You can use this as reference and do it on your own ! But if u wish to copy from here do it on your own risk and if the faculty finds out then the repository handler's will not be responsible ! 2 | -------------------------------------------------------------------------------- /CSE CORE/HACKER RANK ASSIGNMENT/Week 2/README.md: -------------------------------------------------------------------------------- 1 | # DISCLAIMER - You can use this as reference and do it on your own ! But if u wish to copy from here do it on your own risk and if the faculty finds out then the repository handler's will not be responsible ! 2 | -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 1/README.md: -------------------------------------------------------------------------------- 1 | ## I cannot give the ouput of these cause it will reveal my IP publicly which may cause issues if u know u know so please run this codes in CMD ( command prompt ) and take a screenshot as output! 2 | 3 | 4 | 5 | ## PLEASE NOTE : The questions are made by understanding the code not given by college. 6 | -------------------------------------------------------------------------------- /CSE CORE/COMPILER DESIGN LAB/Assignment 7/Week 8.md: -------------------------------------------------------------------------------- 1 | ## 1) Write a YAAC Program to recognize nested IF control statements and display the levels of nesting 2 | 3 | ### Codes - 4 | 5 | ### Yaac - 6 | 7 | ``` 8 | %tokenIFRELOPSNUMBERID 9 | %{ 10 | intcount=0; 11 | %} 12 | %% 13 | stmt:if_stmt{printf(“Noofnestedifstatements=%d\n”,count);exit(0);} 14 | ; 15 | if_stmt:IF‘(‘cond‘)’if_stmt{count++;} 16 | |S; 17 | ; 18 | cond:xRELOPx 19 | ; 20 | x:ID 21 | |NUMBER 22 | ; 23 | %% 24 | intyyerror(char*msg) 25 | { 26 | printf(“InvalidExpression\n”); 27 | exit(0); 28 | } 29 | main() 30 | { 31 | printf(“Enterthestatement”); 32 | yyparse(); 33 | } 34 | ``` 35 | 36 | ### Output - 37 | 38 | ``` 39 | 40 | ``` 41 | -------------------------------------------------------------------------------- /CSE CORE/COMPILER DESIGN LAB/Assignment 10/Week 11.md: -------------------------------------------------------------------------------- 1 | ## 1) Write a YAAC Program to recognize strings ‘aaab’, ‘abbb’, ‘ab’ and ‘a’ using grammar (an b n , n>=0) 2 | 3 | ### Codes - 4 | 5 | ### Lex - 6 | 7 | ``` 8 | %{ 9 | #include “y.tab.h” 10 | %} 11 | %% 12 | [aA] { return A; } 13 | [bB] { return B; } 14 | \n { return NL ;} 15 | . { return yytext[0]; } 16 | %% 17 | ``` 18 | 19 | ### Yaac - 20 | 21 | ``` 22 | %token A B NL 23 | %% 24 | stmt : s NL { printf(“Valid String\n”); exit(0) ;} 25 | ; 26 | s : AsB 27 | | 28 | ; 29 | %% 30 | int yyerror(char *msg) 31 | { 32 | printf(“Invalid String\n”); 33 | exit(0); 34 | } 35 | main () 36 | { 37 | printf(“Enter the String\n”); 38 | yyparse(); 39 | } 40 | ``` 41 | 42 | ### Output - 43 | 44 | ``` 45 | 46 | ``` 47 | -------------------------------------------------------------------------------- /CSE CORE/COMPILER DESIGN LAB/Assignment 11/Week 12.md: -------------------------------------------------------------------------------- 1 | ## 1) Write a YAAC Program to recognize nested IF control statements and display the levels of nesting 2 | 3 | ### Codes - 4 | 5 | ### Lex - 6 | 7 | ``` 8 | %{ 9 | #include “y.tab.h” 10 | %} 11 | %% 12 | [aA] { return A; } 13 | [bB] { return B; } 14 | \n { return NL ;} 15 | . { return yytext[0]; } 16 | %% 17 | ``` 18 | 19 | ### Yaac - 20 | 21 | ``` 22 | %token A B NL 23 | %% 24 | stmt : A A AAAAAAAAsBNL 25 | { 26 | Printf(“Valid”); exit(0); 27 | } 28 | ; 29 | s : s A 30 | | 31 | ; 32 | int yyerror(char *msg) 33 | { 34 | printf(“Invalid String\n”); 35 | exit(0); 36 | } 37 | main () 38 | { 39 | printf(“Enter the String\n”); 40 | yyparse(); 41 | } 42 | ``` 43 | 44 | ### Output - 45 | 46 | ``` 47 | 48 | ``` 49 | -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 4/TCPclientFILE.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #define SERV_PORT 5576 8 | main(int argc,char **argv) 9 | { 10 | int i,j; 11 | ssize_t n; 12 | char filename[80],recvline[80]; 13 | struct sockaddr_in servaddr; 14 | int sockfd; 15 | sockfd=socket(AF_INET,SOCK_STREAM,0); 16 | bzero(&servaddr,sizeof(servaddr)); 17 | servaddr.sin_family=AF_INET; 18 | servaddr.sin_port=htons(SERV_PORT); 19 | inet_pton(AF_INET,argv[1],&servaddr.sin_addr); 20 | connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr)); 21 | printf("Enter the file name: "); 22 | scanf("%s",filename); 23 | write(sockfd,filename,sizeof(filename)); 24 | printf("\n Data from server: \n"); 25 | while(read(sockfd,recvline,80)!=0) 26 | { 27 | fputs(recvline,stdout); 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 4/TCPserverFILE.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #define SERV_PORT 5576 8 | main(int argc,char **argv) 9 | { 10 | int i,j; 11 | ssize_t n; 12 | FILE *fp; 13 | char s[80],f[80]; 14 | struct sockaddr_in servaddr,cliaddr; 15 | int listenfd,connfd,clilen; 16 | listenfd=socket(AF_INET,SOCK_STREAM,0); 17 | bzero(&servaddr,sizeof(servaddr)); 18 | servaddr.sin_family=AF_INET; 19 | servaddr.sin_port=htons(SERV_PORT); 20 | bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr)); 21 | listen(listenfd,1); 22 | clilen=sizeof(cliaddr); 23 | connfd=accept(listenfd,(struct sockaddr*)&cliaddr,&clilen); 24 | printf("\nClient connected. "); 25 | read(connfd,f,80); 26 | fp=fopen(f,"r"); 27 | printf("\nName of the file: %s",f); 28 | while(fgets(s,80,fp)!=NULL) 29 | { 30 | printf("\nContent of the file: %s",s); 31 | write(connfd,s,sizeof(s)); 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /CSE CORE/COMPILER DESIGN LAB/Assignment 9/Week 10.md: -------------------------------------------------------------------------------- 1 | ## 1) Write a YAAC Program to recognize a valid variable, which starts with a letter, followed by any number of letters or digits. 2 | ### Codes - 3 | 4 | ### Lex - 5 | 6 | ``` 7 | %{ 8 | #include “y.tab.h” 9 | %} 10 | %% 11 | [a-zA-Z] { return LETTER ;} 12 | [0-9] { return DIGIT ; } 13 | [\n] { return NL ;} 14 | [_] { return UND; } 15 | . { return yytext[0]; } 16 | %% 17 | ``` 18 | 19 | ### Yaac - 20 | 21 | ``` 22 | %token DIGIT LETTER NL UND 23 | %% 24 | stmt : variable NL { printf(“Valid Identifiers\n”); exit(0);} 25 | ; 26 | variable : LETTER alphanumeric 27 | ; 28 | alphanumeric: LETTER alphanumeric 29 | | DIGIT alphanumeric 30 | | UNDalphanumeric 31 | | LETTER 32 | | DIGIT 33 | | UND 34 | ; 35 | %% 36 | int yyerror(char *msg) 37 | { 38 | printf(“Invalid Expression\n”); 39 | exit(0); 40 | } 41 | main () 42 | { 43 | printf(“Enter the variable name\n”); 44 | yyparse(); 45 | } 46 | ``` 47 | 48 | ### Output - 49 | 50 | ``` 51 | 52 | ``` 53 | -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 4/UDPclientFILE.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include //contains definitions of a number of data types used in system calls. These types are used in the next two include files. 5 | #include //includes a number of definitions of structures needed for sockets. 6 | #include //contains constants and structures needed for internet domain addresses. 7 | #include 8 | #define SERV_PORT 6349 9 | main(int argc,char **argv) 10 | { 11 | char filename[80]; 12 | int sockfd; 13 | struct sockaddr_in servaddr; 14 | sockfd=socket(AF_INET,SOCK_DGRAM,0); 15 | bzero(&servaddr,sizeof(servaddr)); 16 | servaddr.sin_family=AF_INET; 17 | servaddr.sin_port=htons(SERV_PORT); 18 | inet_pton(AF_INET,argv[1],&servaddr.sin_addr); 19 | // The inet_pton() function converts an address in its standard text presentation form into its numeric binary form. 20 | printf("Enter the file name: "); 21 | scanf("%s",filename); 22 | sendto(sockfd,filename,strlen(filename),0,(struct sockaddr*)&servaddr,sizeof(servaddr)); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /CSE (IOT)/COMPILER DESIGN LAB/Week1/Q2.md: -------------------------------------------------------------------------------- 1 | ### Problem Statement: 2 |
3 |

Write a C program to check if a user given C Program Statement is a valid comment or not?

4 |
5 | 6 | ### Flowchart: 7 | 8 | ![image](https://github.com/PixMusicaX/Sem6IoT/assets/129383302/24466322-ba47-4e48-a99f-161c5e604bae) 9 | 10 | 11 | ### Code: 12 | 13 | ```C 14 | #include 15 | #include 16 | #include 17 | 18 | int main() 19 | { 20 | char id[100]; 21 | printf("Enter String: "); 22 | scanf("%s", id); 23 | 24 | if (id[0] == '/' && id[1] == '/') 25 | printf("The statement is a valid comment.\n"); 26 | else 27 | printf("The statement is not a valid comment.\n"); 28 | 29 | return 0; 30 | } 31 | ``` 32 | 33 | ### Output: 34 | ![image](https://github.com/PixMusicaX/Sem6IoT/assets/129383302/5168b6e8-fdda-4506-8566-a381c8406097) 35 | 36 | ![image](https://github.com/PixMusicaX/Sem6IoT/assets/129383302/3fe4801d-7467-421e-a546-e397ea583370) 37 | 38 | 39 | ### Conclusion: 40 | 41 | Above is the simulation of how a compiler can check whether a statement is a valid comment or not. 42 | 43 | -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 3/UDPclient2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | int client_socket; 8 | struct sockaddr_in server_address; 9 | 10 | client_socket = socket(AF_INET, SOCK_STREAM, 0); 11 | if (client_socket == -1) { 12 | perror("Error creating client socket"); 13 | exit(EXIT_FAILURE); 14 | } 15 | 16 | server_address.sin_family = AF_INET; 17 | server_address.sin_addr.s_addr = inet_addr("127.0.0.1"); 18 | server_address.sin_port = htons(12345); 19 | 20 | if (connect(client_socket, (struct sockaddr*)&server_address, sizeof(server_address)) == -1) { 21 | perror("Error connecting to server"); 22 | exit(EXIT_FAILURE); 23 | } 24 | 25 | char buffer[1024]; 26 | int number; 27 | 28 | printf("Enter a number to check if it's prime: "); 29 | fgets(buffer, sizeof(buffer), stdin); 30 | write(client_socket, buffer, sizeof(buffer)); 31 | 32 | read(client_socket, buffer, sizeof(buffer)); 33 | int result = atoi(buffer); 34 | 35 | printf("The result is: %d\n", result); 36 | 37 | close(client_socket); 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 4/UDPserverFILE.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include //contains definitions of a number of data types used in system calls. These types are used in the next two include files. 5 | #include //includes a number of definitions of structures needed for sockets. 6 | #include //contains constants and structures needed for internet domain addresses. 7 | #define SERV_PORT 6349 8 | main(int argc,char **argv) 9 | { 10 | char filename[80],recvline[80]; 11 | FILE *fp; 12 | struct sockaddr_in servaddr,cliaddr; 13 | int clilen,sockfd; 14 | sockfd=socket(AF_INET,SOCK_DGRAM,0); 15 | bzero(&servaddr,sizeof(servaddr)); 16 | servaddr.sin_family=AF_INET; 17 | servaddr.sin_port=htons(SERV_PORT); 18 | bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr)); 19 | clilen=sizeof(cliaddr); 20 | recvfrom(sockfd,filename,80,0,(struct sockaddr*)&cliaddr,&clilen); 21 | //The recvfrom socket function receives data on a socket with descriptor s and stores it in a buffer. 22 | printf("\nData in the file is: \n "); 23 | fp=fopen(filename,"r"); 24 | while(fgets(recvline,80,fp)!=NULL) 25 | { 26 | printf("\n %s\n ",recvline); 27 | } 28 | fclose(fp); 29 | } 30 | 31 | -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 4/README.md: -------------------------------------------------------------------------------- 1 | ### RUN ALL THESE THE SAME WAY AS ASSIGNMENT 3 2 | 3 | ### JUST REMEMBER TO RUN THE SERVER FILE FIRST IN ONE TERMINAL AND OPEN ANOTHER ONE THEN RUN THE CLIENT FILE , ITS THAT EASY !! 4 | 5 | ### THOSE OF YOU WHO DOESNT HAVE UBUNTU ON THEIR PC PLEASE TRY USING VIRTUAL MACHINES ON YOUR OWN PC OR YOU CAN ALSO RUN IT IN CMD BUT YOU NEED TO INSTALL A BUNCH OF DIFFERENT STUFF ! 6 | 7 | ### TUTORIAL LINK FOR CREATING A VIRTUAL MACHINE ON YOUR OWN PC ( UBUNTU ) :- [Click Here](https://www.youtube.com/watch?v=luhHDo4ei34) 8 | 9 | 10 | ## OR 11 | 12 | 13 | ### YOU CAN ALSO USE THIS ONLINE UBUNTU BUT FAIR WARNING ITS TOO SLOW AND CRASHES OR RELOADES SOMETHIMES , ANYWAYS THE LINK IS HERE :- [Click Here](https://www.onworks.net/programs/terminal-online) 14 | 15 | 16 | ## OR 17 | 18 | 19 | ### ➡️ I JUST FOUND A EASIER WAY TO RUN UBUNTU FILES OR RATHER GET UBUNTU TERMINAL IN YOUR OWN PC EASILY , JUST FOLLOW THE SIMPLE STEPS IN THIS VIDEO :- [Click Here](https://www.youtube.com/watch?v=LLlfLpvQg04) 20 | 21 | 22 | ### NOW IF YOU HAVE INSTALLED UBUNTU TERMINAL ON YOUR PC YOU HAVE TO UPDATE SUDO FIRST USING THE COMMAND - 23 | 24 | #### sudo apt update 25 | 26 | ### THEN YOU HAVE TO INSTALL GCC BY GIVING COMMAND - 27 | 28 | #### sudo apt install gcc 29 | 30 | ### THEN RUN THOSE CODES ! 31 | 32 | ### ALSO KEEP IN MIND THAT THESE CODES AND FILES ARE GIVEN BY TEACHER SO IF IT DOESNT WORK IN SOME CASES ITS NOT THE HANDLER's FAULT 33 | -------------------------------------------------------------------------------- /CSE (IOT)/COMPILER DESIGN LAB/Week1/Q1.md: -------------------------------------------------------------------------------- 1 | ### Problem Statement: 2 |
3 |

Write a C program to check if a user given String is a valid indentifier or not?

4 |
5 | 6 | ### Flowchart: 7 | 8 | ![image](https://github.com/PixMusicaX/Sem6IoT/assets/129383302/09899c1e-e144-45f0-9433-35cad8d38277) 9 | 10 | 11 | ### Code: 12 | 13 | ```C 14 | #include 15 | #include 16 | 17 | int main() 18 | { 19 | char id[100]; 20 | printf("Enter String: "); 21 | scanf("%s", id); 22 | int count = 0; 23 | int len = 0; 24 | int i; 25 | len = strlen(id); 26 | if (id[0]=='_' || id[0]>= 'A' && id[0]<='Z' || id[0] >= 'a' && id[0]<='z') 27 | { 28 | for(i=1;i='0' || id[i]>='A' && id[i]<='Z' || id[i]>='a' && id[i]<='z') 31 | ++count; 32 | } 33 | if(count == len - 1) 34 | printf("The identifier is valid.\n"); 35 | else 36 | printf("The identifier is not valid.\n"); 37 | } 38 | else 39 | printf("The identifier is not valid.\n"); 40 | 41 | return 0; 42 | } 43 | ``` 44 | 45 | ### Output: 46 | 47 | ![image](https://github.com/PixMusicaX/Sem6IoT/assets/129383302/c3ffdaeb-2ffa-45d8-b978-17d0d7b0a8c5) 48 | 49 | ![image](https://github.com/PixMusicaX/Sem6IoT/assets/129383302/d51164ef-b319-4e3d-8942-1efb18b57a3a) 50 | 51 | ### Conclusion: 52 | 53 | Above is the simulation of how a compiler can check whether an identifier name is valid or not. 54 | 55 | -------------------------------------------------------------------------------- /CSE (IOT)/COMPILER DESIGN LAB/Week5/q10.md: -------------------------------------------------------------------------------- 1 | ## Write a Lex Program to count the no of ‘scanf’ and ‘printf’ statements in a C program. Replace them with ‘readf’ and ‘writef’ statements respectively. 2 | 3 | ### Algorithm: 4 | 5 | 1. Initialize two counters: a for printf statements and b for scanf statements. 6 | 2. These counters will keep track of the number of each type of statement encountered. 7 | 3. Open the sourcefile.c for reading. 8 | 4. Read the file line by line. 9 | 5.Whenever you encounter the string "printf", Increment the a counter. 10 | 6. Whenever you encounter the string "scanf", Increment the b counter. 11 | 7. Continue reading lines until you reach the end of the file. 12 | 8. After processing the entire file, close the input and output files. 13 | 9. “Printf statement count = a” 14 | 10. “Scanf statement count = b” 15 | 16 | ### Codes: 17 | 18 | ```lex 19 | %{ 20 | #include 21 | int a=0, b=0; 22 | %} 23 | 24 | %% 25 | "scanf" {fprintf(yyout,"readf");b++;} 26 | "printf" {fprintf(yyout,"writef");a++;} 27 | %% 28 | 29 | int main (int argc, char* argv[]) 30 | { 31 | if (argc<2) 32 | { 33 | printf("Usage: \n"); 34 | exit(0); 35 | } 36 | yyin = fopen(argv[1],"r"); 37 | yyout = fopen("output.c","w"); 38 | yylex(); 39 | printf("Printf statement - %d\n", a); 40 | printf("Scanf statement - %d\n", b); 41 | fclose(yyin); 42 | fclose(yyout); 43 | } 44 | int yywrap() 45 | { 46 | return 1; 47 | } 48 | ``` 49 | 50 | ### Output - 51 | 52 | ``` 53 | a.exe a.c 54 | Printf statement - 1 55 | Scanf statement - 1 56 | ``` 57 | -------------------------------------------------------------------------------- /CSE (IOT)/COMPILER DESIGN LAB/Week5/q9.md: -------------------------------------------------------------------------------- 1 | ## Write a Lex Program to count the no of comment line in a given C program. Also eliminate them and copy that program into separate file. 2 | 3 | ### Algorithm: 4 | 5 | 1. Initialize two counters: m1 for multiline comments and s1 for single-line comments. 6 | 2. These counters will keep track of the number of each type of comment encountered. 7 | 3. Open the sourcefile.c for reading. 8 | 4. Read the file line by line. 9 | 5. Multiline comments start with /* and end with */. Single-line comments start with // and continue until the end of the line. Whenever you encounter one of these patterns: 10 | 6. Increment the corresponding counter (m1 for multiline or s1 for single-line). 11 | 7. Continue reading lines until you reach the end of the file. 12 | 8. For each line, check if it contains any comments. 13 | 9. If it does, update the counters accordingly. 14 | 10. If not, move on to the next line. 15 | 11. After processing the entire file, close the input file. 16 | 12. “The number of multiline comments = m1” 17 | 13. “The number of single-line comments = s1” 18 | 19 | ### Code: 20 | 21 | ```lex 22 | %{ 23 | #include 24 | int m1=0, s1=0; 25 | %} 26 | 27 | %% 28 | "/*"[a-zA-Z0-9\n\t ]*"*/" {m1++;} 29 | "//"[a-zA-Z0-9\t ]*"\n" {s1++;} 30 | %% 31 | 32 | int main (int argc, char* argv[]) 33 | { 34 | if (argc<2) 35 | { 36 | printf("Usage: \n"); 37 | exit(0); 38 | } 39 | yyin = fopen(argv[1],"r"); 40 | yyout = fopen("output.c","w"); 41 | yylex(); 42 | printf("The number of multiline comment = %d\n", m1); 43 | printf("The number of singleline comment = %d\n", s1); 44 | fclose(yyin); 45 | fclose(yyout); 46 | } 47 | int yywrap() 48 | { 49 | return 1; 50 | } 51 | ``` 52 | 53 | ### Output - 54 | 55 | ``` 56 | a.exe a.c 57 | The number of multiline comment = 1 58 | The number of singleline comment = 2 59 | ``` 60 | -------------------------------------------------------------------------------- /CSE CORE/COMPILER DESIGN LAB/Assignment 6/Week 7.md: -------------------------------------------------------------------------------- 1 | ## 1) Write a Lex Program to recognize and count the number of identifiers in a given input file. 2 | 3 | ### Codes - 4 | 5 | ``` 6 | %{ 7 | #include 8 | int identifier_count = 0; 9 | %} 10 | 11 | %% 12 | [a-zA-Z][a-zA-Z0-9_]* { identifier_count++; } 13 | . { } 14 | %% 15 | 16 | int main(int argc, char *argv[]) { 17 | if (argc != 2) { 18 | printf("Usage: %s \n", argv[0]); 19 | return 1; 20 | } 21 | 22 | FILE *input_file = fopen(argv[1], "r"); 23 | if (!input_file) { 24 | perror("Error opening file"); 25 | return 1; 26 | } 27 | 28 | yyin = input_file; 29 | yylex(); 30 | 31 | fclose(input_file); 32 | 33 | printf("Number of identifiers: %d\n", identifier_count); 34 | return 0; 35 | } 36 | int yywrap() 37 | { 38 | return 1; 39 | } 40 | 41 | ``` 42 | 43 | ### Output - 44 | 45 | ``` 46 | a.exe a.txt 47 | Number of identifiers: 2 48 | ``` 49 | 50 | ## 2) Write a YAAC Program to test the validity of a simple expression involving operators +,-, * and / 51 | 52 | 53 | ### Codes - 54 | 55 | ### Lex - 56 | 57 | ``` 58 | %{ 59 | #include“y.tab.h” 60 | %} 61 | %% 62 | [0-9]+{returnDIGIT;} 63 | [a-zA-Z][a-zA-Z0-9_]*{returnID;} 64 | \n{returnNL;} 65 | .{returnyytext[0];} 66 | %% 67 | ``` 68 | 69 | ### Yaac - 70 | 71 | ``` 72 | %tokenNUMBERIDNL 73 | %left‘+’‘-‘ 74 | %left‘*’‘/’ 75 | %% 76 | stmt:expNL{printf(“ValidExpression”);exit(0);} 77 | ; 78 | exp:exp‘+’exp 79 | |exp‘-‘exp 80 | |exp‘*’exp 81 | |exp‘/’exp 82 | |‘(‘exp‘)’ 83 | |ID 84 | |NUMBER 85 | ; 86 | %% 87 | intyyerror(char*msg) 88 | { 89 | printf(“InvalidExpression\n”); 90 | exit(0); 91 | } 92 | main() 93 | { 94 | printf(“Entertheexpression\n”); 95 | yyparse(); 96 | } 97 | ``` 98 | 99 | ### Output - 100 | 101 | ``` 102 | 103 | ``` 104 | -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 3/README.md: -------------------------------------------------------------------------------- 1 | ## So here we are once again! 2 | 3 | ### JUST REMEMBER TO RUN THE SERVER FILE FIRST IN ONE TERMINAL AND OPEN ANOTHER ONE THEN RUN THE CLIENT FILE , ITS THAT EASY !! 4 | 5 | ### THOSE OF YOU WHO DOESNT HAVE UBUNTU ON THEIR PC PLEASE TRY USING VIRTUAL MACHINES ON YOUR OWN PC OR YOU CAN ALSO RUN IT IN CMD BUT YOU NEED TO INSTALL A BUNCH OF DIFFERENT STUFF ! 6 | 7 | ### TUTORIAL LINK FOR CREATING A VIRTUAL MACHINE ON YOUR OWN PC ( UBUNTU ) :- [Click Here](https://www.youtube.com/watch?v=luhHDo4ei34) 8 | 9 | 10 | ## OR 11 | 12 | 13 | ### YOU CAN ALSO USE THIS ONLINE UBUNTU BUT FAIR WARNING ITS TOO SLOW AND CRASHES OR RELOADES SOMETHIMES , ANYWAYS THE LINK IS HERE :- [Click Here](https://www.onworks.net/programs/terminal-online) 14 | 15 | 16 | ## OR 17 | 18 | 19 | ### ➡️ I JUST FOUND A EASIER WAY TO RUN UBUNTU FILES OR RATHER GET UBUNTU TERMINAL IN YOUR OWN PC EASILY , JUST FOLLOW THE SIMPLE STEPS IN THIS VIDEO :- [Click Here](https://www.youtube.com/watch?v=LLlfLpvQg04) 20 | 21 | 22 | ### NOW IF YOU HAVE INSTALLED UBUNTU TERMINAL ON YOUR PC YOU HAVE TO UPDATE SUDO FIRST USING THE COMMAND - 23 | 24 | #### sudo apt update 25 | 26 | ### THEN YOU HAVE TO INSTALL GCC BY GIVING COMMAND - 27 | 28 | #### sudo apt install gcc 29 | 30 | ### THEN RUN THOSE CODES ! 31 | 32 | ### ALSO KEEP IN MIND THAT THESE CODES AND FILES ARE GIVEN BY TEACHER SO IF IT DOESNT WORK IN SOME CASES ITS NOT THE HANDLER's FAULT 33 | 34 | ### Listen just open ubuntu and run those files by first typing the commands in following ways - 35 | 36 | #### For Qs 1 - 37 | 38 | ``` 39 | gcc UDPserver.c 40 | ``` 41 | ``` 42 | ./a.out 43 | ``` 44 | 45 | #### Open another terminal and then run the client file by using the code - 46 | 47 | ``` 48 | gcc UDPclient.c 49 | ``` 50 | ``` 51 | ./a.out 52 | ``` 53 | 54 | #### For Qs 2 - 55 | 56 | ``` 57 | gcc UDPserver2.c 58 | ``` 59 | ``` 60 | ./a.out 61 | ``` 62 | 63 | #### Open another terminal and then run the client file by using the code - 64 | 65 | ``` 66 | gcc UDPclient2.c 67 | ``` 68 | ``` 69 | ./a.out 70 | ``` 71 | 72 | ### Then simply take output! 73 | 74 | ## And please dont blindly copy it ! 75 | -------------------------------------------------------------------------------- /CSE CORE/COMPILER DESIGN LAB/Assignment 8/week 9.md: -------------------------------------------------------------------------------- 1 | ## 1) Write a YAAC Program to check the syntax of a simple expression involving operators +,-, * and / 2 | 3 | ### Codes - 4 | 5 | ### Lex - 6 | 7 | ``` 8 | %{ 9 | #include “y.tab.h” 10 | %} 11 | %% 12 | [0-9]+ { return NUMBER; } 13 | [a-zA-Z][a-zA-Z0-9_]* { return ID; } 14 | \n { return NL ;} 15 | . { return yytext[0]; } 16 | %% 17 | ``` 18 | 19 | ### Yaac - 20 | 21 | ``` 22 | %tokenNUMBERIDNL 23 | %left‘+’‘-‘ 24 | %left‘*’‘/’ 25 | %% 26 | stmt:expNL{printf(“ValidExpression”);exit(0);} 27 | ; 28 | exp:exp‘+’exp 29 | |exp‘-‘exp 30 | |exp‘*’exp 31 | |exp‘/’exp 32 | |‘(‘exp‘)’ 33 | |ID 34 | |NUMBER 35 | ; 36 | %% 37 | intyyerror(char*msg) 38 | { 39 | printf(“InvalidExpression\n”); 40 | exit(0); 41 | } 42 | main() 43 | { 44 | printf(“Enter the expression\n”); 45 | yyparse(); 46 | } 47 | ``` 48 | 49 | ### Output - 50 | 51 | ``` 52 | 53 | ``` 54 | 55 | ## 2) Write a YAAC Program to evaluate an arithmetic expression involving operating +,-, * and / 56 | 57 | ### Codes - 58 | 59 | ### Lex - 60 | 61 | ``` 62 | %{ 63 | #include “y.tab.h” 64 | extern int yylval; 65 | %} 66 | %% 67 | [0-9]+ { yylval=atoi(yytext); return NUMBER; } 68 | \n { return NL ;} 69 | . { return yytext[0]; } 70 | %% 71 | ``` 72 | 73 | ### Yaac - 74 | 75 | ``` 76 | %token NUMBER IDNL 77 | %left ‘+’ ‘-‘ 78 | %left ‘*’ ‘/’ 79 | %% 80 | stmt : exp NL { printf(“Value = %d\n”,$1); exit(0);} 81 | ; 82 | exp : exp ‘+’ exp { $$=$1+$3; } 83 | | exp ‘-‘ exp { $$=$1-$3; } 84 | | exp ‘*’ exp { $$=$1*$3; } 85 | | exp ‘/’ exp { if($3==0) 86 | { 87 | printf(“Cannot divide by 0”); 88 | exit(0); 89 | } 90 | else 91 | $$=$1/$3; 92 | } 93 | | ‘(‘ exp ‘)’ { $$=$2; } 94 | | ID { $$=$1; } 95 | | NUMBER{$$=$1; } 96 | ; 97 | %% 98 | int yyerror(char *msg) 99 | { 100 | printf(“Invalid Expression\n”); 101 | exit(0); 102 | } 103 | main () 104 | { 105 | printf(“Enter the expression\n”); 106 | yyparse(); 107 | } 108 | ``` 109 | 110 | ### Output - 111 | 112 | ``` 113 | 114 | ``` 115 | -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 3/UDPserver2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int is_prime(int num) { 7 | if (num < 2) { 8 | return 0; 9 | } 10 | for (int i = 2; i * i <= num; ++i) { 11 | if (num % i == 0) { 12 | return 0; 13 | } 14 | } 15 | return 1; 16 | } 17 | 18 | void handle_client(int client_socket) { 19 | char buffer[1024]; 20 | int num; 21 | 22 | read(client_socket, buffer, sizeof(buffer)); 23 | num = atoi(buffer); 24 | 25 | int result = is_prime(num); 26 | 27 | sprintf(buffer, "%d", result); 28 | write(client_socket, buffer, sizeof(buffer)); 29 | 30 | close(client_socket); 31 | } 32 | 33 | int main() { 34 | int server_socket, client_socket; 35 | struct sockaddr_in server_address, client_address; 36 | socklen_t client_address_len = sizeof(client_address); 37 | 38 | server_socket = socket(AF_INET, SOCK_STREAM, 0); 39 | if (server_socket == -1) { 40 | perror("Error creating server socket"); 41 | exit(EXIT_FAILURE); 42 | } 43 | 44 | server_address.sin_family = AF_INET; 45 | server_address.sin_addr.s_addr = INADDR_ANY; 46 | server_address.sin_port = htons(12345); 47 | 48 | if (bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) == -1) { 49 | perror("Error binding server socket"); 50 | exit(EXIT_FAILURE); 51 | } 52 | 53 | if (listen(server_socket, 5) == -1) { 54 | perror("Error listening on server socket"); 55 | exit(EXIT_FAILURE); 56 | } 57 | 58 | printf("Server listening on port 12345...\n"); 59 | 60 | while (1) { 61 | client_socket = accept(server_socket, (struct sockaddr*)&client_address, &client_address_len); 62 | if (client_socket == -1) { 63 | perror("Error accepting connection"); 64 | exit(EXIT_FAILURE); 65 | } 66 | 67 | printf("Accepted connection from %s:%d\n", inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port)); 68 | 69 | handle_client(client_socket); 70 | } 71 | 72 | close(server_socket); 73 | 74 | return 0; 75 | } -------------------------------------------------------------------------------- /CSE (IOT)/COMPILER DESIGN LAB/Week3/q6.md: -------------------------------------------------------------------------------- 1 | # NOT TO BE WRITTEN: 2 | It was instructed to perform all the lex programs using C. 3 | 4 | 5 | ### Problem Statement: 6 |
7 |

Write a C Program to count the number of vowels and consonants in a given string

8 |
9 | 10 | ### Algorithm: 11 | 12 | 1. Create variables to keep track of the counts for vowels, consonants, digits, and spaces. Set all of them to zero initially. 13 | 2. Prompt the user to input a line of text (string). 14 | 3. Loop through each character in the input string. 15 | 4. convert the character to lowercase (to handle both uppercase and lowercase letters). 16 | 5. If the character is a vowel (i.e., ‘a’, ‘e’, ‘i’, ‘o’, or ‘u’), increment the vowel count. 17 | 6. If the character is an alphabet (i.e., falls between ‘a’ and ‘z’), but not a vowel, increment the consonant count. 18 | 7. Display the final counts of vowels, consonants, digits, and spaces. 19 | 20 | ### Code: 21 | 22 | ```C 23 | #include 24 | #include // For tolower() function 25 | 26 | int main() { 27 | char line[150]; 28 | int vowels, consonants; 29 | 30 | // Initialize all variables to 0 31 | vowels = consonants = 0; 32 | 33 | // Get a full line of string input 34 | printf("Enter a line of string: "); 35 | fgets(line, sizeof(line), stdin); 36 | 37 | // Loop through each character of the string 38 | for (int i = 0; line[i] != '\0'; ++i) { 39 | // Convert character to lowercase 40 | line[i] = tolower(line[i]); 41 | 42 | // Check if the character is a vowel 43 | if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' || line[i] == 'o' || line[i] == 'u') { 44 | ++vowels; 45 | } 46 | // If it is not a vowel and an alphabet, it is a consonant 47 | else if ((line[i] >= 'a' && line[i] <= 'z')) { 48 | ++consonants; 49 | } 50 | } 51 | 52 | printf("Vowels: %d\n", vowels); 53 | printf("Consonants: %d\n", consonants); 54 | 55 | return 0; 56 | } 57 | 58 | ``` 59 | 60 | ### Output: 61 | 62 | ![image](https://github.com/PixMusicaX/6thsem/assets/129383302/ff406b1f-0a73-4db0-b262-932b45b390bf) 63 | 64 | 65 | ### Conclusion: 66 | 67 | Above is the algorithm and code to evaluate a statement and display the count of vowels and consonants. 68 | -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 1/1.md: -------------------------------------------------------------------------------- 1 | ### Which command helps to configure the colour of command prompt? 2 | 3 | #### Code #1 - 4 | 5 | ``` 6 | help color 7 | ``` 8 | 9 | ``` 10 | Output - DO IT YOURSELF ! 11 | ``` 12 | 13 | ### Which command is used to change the colour of the command prompt? 14 | 15 | #### Code #2 - 16 | 17 | ``` 18 | color a 19 | ``` 20 | 21 | ``` 22 | Output - DO IT YOURSELF ! 23 | ``` 24 | 25 | ### Which command is used to display your IP configuration? 26 | 27 | #### Code #3 - 28 | 29 | ``` 30 | ipconfig 31 | ``` 32 | 33 | ``` 34 | Output - DO IT YOURSELF ! 35 | ``` 36 | 37 | ### Which command is used to display all the adapters? 38 | 39 | #### Code #4 - 40 | 41 | ``` 42 | ipconfig/all 43 | ``` 44 | 45 | ``` 46 | Output - DO IT YOURSELF ! 47 | ``` 48 | 49 | ### Which command is used to find the IP address of any website? 50 | 51 | #### Code #5 - 52 | 53 | ``` 54 | nslookup "Website address" 55 | ``` 56 | 57 | ``` 58 | Output - DO IT YOURSELF ! 59 | ``` 60 | 61 | ### Which command is used to check the reachability of any IP address? 62 | 63 | #### Code #6 - 64 | 65 | ``` 66 | ping "type the ip address that is generated from nslookup" 67 | ``` 68 | 69 | ``` 70 | Output - DO IT YOURSELF ! 71 | ``` 72 | 73 | ### Which command is used to find the route through which the packet takes to reach the destination IP address? 74 | 75 | #### Code #7 - 76 | 77 | ``` 78 | tracert "type the ip address that is generated from nslookup" 79 | ``` 80 | 81 | ``` 82 | Output - DO IT YOURSELF ! 83 | ``` 84 | 85 | ### Which command is used to locate spots that have network latency and network loss? 86 | 87 | #### Code #8 - 88 | 89 | ``` 90 | pathping "type the ip address that is generated from nslookup" 91 | ``` 92 | 93 | ``` 94 | Output - DO IT YOURSELF ! 95 | ``` 96 | 97 | ### Which command is used to show the network status? 98 | 99 | #### Code #9 - 100 | 101 | ``` 102 | netstat 103 | ``` 104 | 105 | ``` 106 | Output - DO IT YOURSELF ! 107 | ``` 108 | 109 | ### Which command is used to find all the active devices and their status? 110 | 111 | #### Code #10 - 112 | 113 | ``` 114 | netstat -a 115 | ``` 116 | 117 | ``` 118 | Output - DO IT YOURSELF ! 119 | ``` 120 | -------------------------------------------------------------------------------- /CSE (IOT)/COMPILER DESIGN LAB/Week4/q7.md: -------------------------------------------------------------------------------- 1 | # NOT TO BE WRITTEN: 2 | It was instructed to perform all the lex programs using C. 3 | 4 | 5 | ### Problem Statement: 6 |
7 |

Write a C Program to count the number of characters, words, spaces, end of lines in a given input file.

8 |
9 | 10 | ### Algorithm: 11 | 12 | 1. Create variables to keep track of the counts for vowels, consonants, digits, and spaces. Set all of them to zero initially. 13 | 2. Prompt the user to input a line of text (string). 14 | 3. Loop through each character in the input string. 15 | 4. convert the character to lowercase (to handle both uppercase and lowercase letters). 16 | 5. If the character is a vowel (i.e., ‘a’, ‘e’, ‘i’, ‘o’, or ‘u’), increment the vowel count. 17 | 6. If the character is an alphabet (i.e., falls between ‘a’ and ‘z’), but not a vowel, increment the consonant count. 18 | 7. If the character is a digit (i.e., falls between ‘0’ and ‘9’), increment the digit count. 19 | 8. If the character is a space, increment the space count. 20 | 9. Display the final counts of vowels, consonants, digits, and spaces. 21 | 22 | ### Code: 23 | 24 | ```C 25 | #include 26 | #include 27 | 28 | int main() { 29 | char line[150]; 30 | int vowels, consonants, digits, spaces; 31 | 32 | vowels = consonants = digits = spaces = 0; 33 | 34 | printf("Enter a line of string: "); 35 | fgets(line, sizeof(line), stdin); 36 | 37 | for (int i = 0; line[i] != '\0'; ++i) { 38 | line[i] = tolower(line[i]); 39 | 40 | if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' || line[i] == 'o' || line[i] == 'u') { 41 | ++vowels; 42 | } 43 | else if ((line[i] >= 'a' && line[i] <= 'z')) { 44 | ++consonants; 45 | } 46 | else if (line[i] >= '0' && line[i] <= '9') { 47 | ++digits; 48 | } 49 | else if (line[i] == ' ') { 50 | ++spaces; 51 | } 52 | } 53 | 54 | printf("Vowels: %d\n", vowels); 55 | printf("Consonants: %d\n", consonants); 56 | printf("Digits: %d\n", digits); 57 | printf("White spaces: %d\n", spaces); 58 | 59 | return 0; 60 | } 61 | 62 | ``` 63 | 64 | ### Output: 65 | 66 | ![image](https://github.com/PixMusicaX/6thsem/assets/129383302/dca64a3a-2dc3-427e-b1a6-58e97fe39515) 67 | 68 | ### Conclusion: 69 | 70 | Above is the algorithm and code to evaluate a statement and display the count of vowels, consonants, digits and white spaces. 71 | 72 | -------------------------------------------------------------------------------- /CSE (IOT)/COMPILER DESIGN LAB/Week4/q8.md: -------------------------------------------------------------------------------- 1 | ## NOT TO BE WRITTEN: 2 | It was instructed to perform all the lex programs using C.

3 | Believe me, I harbor no animosity towards anyone but lex is an actual compiler based language very hard to simulate with modern code editors because the whole language is very unneeded where C++, Java and Python make up for the majority of actual real-life operations. 4 |
The Code below should use logic only implementable in lex and hence the whole point of writing this code is SIMPLY DUMB. 5 | 6 | 7 | ## Problem Statement: 8 |
9 |

Write a C Program to count no of: a) +ve and –ve integers b) +ve and –ve fractions

10 |
11 | 12 | ### Algorithm: 13 | 14 | 1. Accept the input data (integers and fractions) from the user or a file. 15 | 2. Initialize four counters: posint (positive integers), negint (negative integers), posfraction (positive fractions), and negfraction (negative fractions). Set all counters to zero. 16 | 3. For each number in the input data check if it is an integer or a fraction. 17 | 4. If it is an integer: 18 | 5. If the number is positive, increment posint. 19 | 6. If the number is negative, increment negint. 20 | 7. If it is a fraction: 21 | 8. If the numerator is positive and the denominator is positive, increment posfraction. 22 | 9. If the numerator is negative and the denominator is positive, increment negfraction. 23 | 10. Print the counts of positive and negative integers, as well as positive and negative fractions. 24 | 25 | ### Code: 26 | 27 | ```C 28 | #include 29 | 30 | int main() { 31 | int n; 32 | int positiveIntegers = 0, negativeIntegers = 0; 33 | int positiveFractions = 0, negativeFractions = 0; 34 | 35 | printf("Enter the total number of elements: "); 36 | scanf("%d", &n); 37 | 38 | printf("Enter the integers and fractions (separated by space):\n"); 39 | 40 | for (int i = 0; i < n; i++) { 41 | float num; 42 | scanf("%f", &num); 43 | 44 | if (num > 0) { 45 | if (num == (int)num) { 46 | positiveIntegers++; 47 | } else { 48 | positiveFractions++; 49 | } 50 | } else if (num < 0) { 51 | if (num == (int)num) { 52 | negativeIntegers++; 53 | } else { 54 | negativeFractions++; 55 | } 56 | } 57 | } 58 | 59 | printf("Positive Integers: %d\n", positiveIntegers); 60 | printf("Negative Integers: %d\n", negativeIntegers); 61 | printf("Positive Fractions: %d\n", positiveFractions); 62 | printf("Negative Fractions: %d\n", negativeFractions); 63 | 64 | return 0; 65 | } 66 | 67 | ``` 68 | 69 | ### Output: 70 | 71 | ![image](https://github.com/PixMusicaX/6thsem/assets/129383302/9f6c04a4-9874-469c-b17b-1e6a10f446f2) 72 | 73 | 74 | ### Conclusion: 75 | 76 | Above is the algorithm and code to count positive and negetive fractions and integers. 77 | -------------------------------------------------------------------------------- /CSE CORE/COMPILER DESIGN LAB/Assignment 5/Week 6.md: -------------------------------------------------------------------------------- 1 | ## 1) Write a Lex Program to recognize a valid arithmetic expression and identify the identifiers and operators present. Print them separately. 2 | 3 | ### Codes - 4 | 5 | ``` 6 | %{ 7 | #include 8 | %} 9 | 10 | %% 11 | [0-9]+ { 12 | printf("Number: %s\n", yytext); 13 | } 14 | [a-zA-Z][a-zA-Z0-9]* { 15 | printf("Identifier: %s\n", yytext); 16 | } 17 | [-+*/=] { 18 | printf("Operator: %s\n", yytext); 19 | } 20 | [ \t\n] ; // skip whitespace 21 | . { 22 | printf("Invalid character: %s\n", yytext); 23 | } 24 | %% 25 | 26 | int yywrap() { 27 | return 1; 28 | } 29 | 30 | int main() { 31 | printf("Enter an arithmetic expression:\n"); 32 | yylex(); 33 | return 0; 34 | } 35 | 36 | ``` 37 | 38 | ### Output - 39 | 40 | ``` 41 | Enter an arithmetic expression: 42 | 5+10 43 | Number: 5 44 | Operator: + 45 | Number: 10 46 | ``` 47 | 48 | ## 2) Write a Lex Program to recognize whether a given sentence is simple or compound. 49 | 50 | ### Codes - 51 | 52 | ``` 53 | %{ 54 | #include 55 | %} 56 | 57 | %% 58 | [a-zA-Z0-9 ]+[\.,;?!] { 59 | printf("It is a Simple Sentence\n"); 60 | } 61 | [a-zA-Z0-9 ]+[\.,;?!][a-zA-Z0-9 ]+[\.,;?!] { 62 | printf("It is a Compound Sentence\n"); 63 | } 64 | .|\n ; 65 | %% 66 | 67 | int yywrap() { 68 | return 1; 69 | } 70 | 71 | int main() { 72 | printf("Enter a sentence:\n"); 73 | yylex(); 74 | return 0; 75 | } 76 | ``` 77 | 78 | ### Output - 79 | 80 | ``` 81 | Enter a sentence: 82 | I like to swim, and my brother likes to play soccer. 83 | It is a Compound Sentence 84 | ``` 85 | 86 | ## 3) Write a lex program to take a user input and valided it , if it is integer calculate factorial or else send some message. 87 | 88 | ### Code - 89 | 90 | ``` 91 | %{ 92 | #include 93 | #include 94 | 95 | int factorial(int n) { 96 | if (n == 0 || n == 1) 97 | return 1; 98 | else 99 | return n * factorial(n - 1); 100 | } 101 | 102 | int isValid = 1; 103 | %} 104 | 105 | DIGIT [0-9] 106 | INTEGER {DIGIT}+ 107 | %% 108 | {INTEGER} { 109 | if (isValid) { 110 | int num = atoi(yytext); 111 | int fact = factorial(num); 112 | printf("Factorial of %d is %d\n", num, fact); 113 | } 114 | else { 115 | printf("Invalid input\n"); 116 | } 117 | isValid = 1; 118 | } 119 | . { 120 | if (isValid) { 121 | printf("Invalid input\n"); 122 | isValid = 0; 123 | } 124 | } 125 | %% 126 | 127 | int main() { 128 | yylex(); 129 | return 0; 130 | } 131 | int yywrap() { 132 | return 1; 133 | } 134 | ``` 135 | 136 | ### Output - 137 | 138 | ``` 139 | 8 140 | Factorial of 8 is 40320 141 | 142 | This is a text 143 | Invalid input 144 | ``` 145 | -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 3/UDPserver.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define SERV_PORT 5839 //These lines define two constants: SERV_PORT, which specifies the port number the server will listen on, 9 | #define MAXLINE 20 //and MAXLINE, which specifies the maximum length of a line. 10 | main(int argc,char **argv) 11 | { 12 | int i,j; 13 | ssize_t n; 14 | char line[MAXLINE],recvline[MAXLINE]; 15 | struct sockaddr_in servaddr,cliaddr; //servaddr and cliaddr are structures to hold server and client socket addresses, respectively. 16 | int sockfd,clilen; //sockfd is the socket file descriptor. clilen stores the size of the client address. 17 | sockfd=socket(AF_INET,SOCK_DGRAM,0); /*This line creates a socket using the socket() system call. 18 | It specifies that the socket will use the IPv4 address family (AF_INET) 19 | and datagram service (SOCK_DGRAM), which corresponds to UDP protocol.*/ 20 | bzero(&servaddr,sizeof(servaddr)); /*This line initializes the server address structure servaddr with zeros using the bzero() function. 21 | This is done to ensure that there are no leftover values in the structure.*/ 22 | servaddr.sin_family=AF_INET; //sin_family is set to AF_INET to indicate the use of IPv4. 23 | servaddr.sin_addr.s_addr=htonl(INADDR_ANY); //sin_addr.s_addr is set to htonl(INADDR_ANY), 24 | //which means the server will accept connections from any IP address assigned to the host. 25 | servaddr.sin_port=htons(SERV_PORT); //sin_port is set to the port number defined by SERV_PORT. 26 | bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr)); //This line binds the socket to the server address specified by servaddr. 27 | //The bind() function associates the socket with a specific address and port number. 28 | for( ; ; ) 29 | { 30 | clilen=sizeof(cliaddr); //This begins an infinite loop where the server continuously listens for incoming UDP packets. 31 | //It initializes the variable clilen with the size of the client address structure. 32 | while(1) 33 | { 34 | if((n=recvfrom(sockfd,line,MAXLINE,0,(struct sockaddr*)&cliaddr,&clilen))==0) 35 | //This line receives data from the client using the recvfrom() function. 36 | //It reads at most MAXLINE bytes of data into the line buffer. 37 | //If recvfrom() returns 0, it means the client has closed the connection, so the server breaks out of the inner loop. 38 | break; 39 | printf("\nLine received successfully: %s",line); 40 | line[n-1]='\0'; 41 | //This line ensures that the received line is null-terminated by replacing the newline character (\n) with the null character (\0). 42 | j=0; 43 | for(i=n-2;i>=0;i--) 44 | { 45 | recvline[j++]=line[i]; //This loop reverses the received line (line) and stores it in the recvline buffer. 46 | } 47 | recvline[j]='\0'; 48 | sendto(sockfd,recvline,n,0,(struct sockaddr*)&cliaddr,clilen); 49 | //This line sends the reversed line (recvline) back to the client using the sendto() function. 50 | } 51 | } 52 | } 53 | 54 | -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 3/UDPclient.c: -------------------------------------------------------------------------------- 1 | #include //standard input/output operations (stdio.h), 2 | #include //system calls (unistd.h, sys/socket.h, sys/types.h), 3 | #include 4 | #include //string manipulation (string.h), 5 | #include 6 | #include //network-related operations (netinet/in.h), 7 | #include //and memory allocation (stdlib.h). 8 | #define SERV_PORT 5839 //These lines define two constants: SERV_PORT, which specifies the port number the client will connect to, 9 | #define MAXLINE 20 //and MAXLINE, which specifies the maximum length of a line. 10 | int main(int argc,char **argv) 11 | { 12 | ssize_t n; //n stores the number of bytes received from the server. 13 | struct sockaddr_in servaddr; //servaddr is a structure to hold the server's socket address. 14 | char sendline[MAXLINE],recvline[MAXLINE]; //sendline is an array to store the data to be sent to the server. 15 | //recvline is an array to store the received data from the server. 16 | int sockfd; //sockfd is the socket file descriptor. 17 | if(argc!=2) //This block checks if the program is run with exactly 18 | { //one command-line argument (the IP address of the server). 19 | printf("usage:"); //If not, it prints a usage message and exits the program. 20 | exit(0); 21 | } 22 | bzero(&servaddr,sizeof(servaddr)); //These lines initialize the server address structure servaddr: 23 | //bzero() clears the structure to ensure there are no leftover values. 24 | servaddr.sin_family=AF_INET; //sin_family is set to AF_INET to indicate the use of IPv4. 25 | servaddr.sin_port=htons(SERV_PORT); //sin_port is set to the port number defined by SERV_PORT. 26 | inet_pton(AF_INET,argv[1],&servaddr.sin_addr); //inet_pton() converts the IP address provided as a command-line argument to binary form 27 | //and stores it in servaddr.sin_addr. 28 | sockfd=socket(AF_INET,SOCK_DGRAM,0); //This line creates a UDP socket using the socket() system call. 29 | //It specifies that the socket will use the IPv4 address family (AF_INET) 30 | //and datagram service (SOCK_DGRAM), corresponding to UDP protocol. 31 | printf("enter the data to be send: "); 32 | while(fgets(sendline,MAXLINE,stdin)!=NULL) //This loop reads data from the standard input (stdin) using fgets() function 33 | //until the end-of-file (EOF) or an error occurs. 34 | { 35 | sendto(sockfd,sendline,strlen(sendline),0,(struct sockaddr*)&servaddr,sizeof(servaddr)); 36 | //sendto() sends the data in sendline to the server specified by servaddr. 37 | printf("Line sent. "); //It then prints a message confirming that the line has been sent. 38 | n=recvfrom(sockfd,recvline,MAXLINE,0,NULL,NULL); //recvfrom() receives data from the server into recvline. 39 | recvline[n]='\0'; 40 | //fputs(recvline,stdout); 41 | printf("\nReverse of the sentense is %s",recvline); 42 | //It prints the received data, which is assumed to be the reverse of the sentence sent by the client. 43 | printf("\n"); 44 | printf("Enter the data to be send: "); //Finally, it prompts the user to enter more data to be sent to the server. 45 | } 46 | exit(0); 47 | } 48 | 49 | -------------------------------------------------------------------------------- /CSE (IOT)/COMPILER DESIGN LAB/Week2/q3.md: -------------------------------------------------------------------------------- 1 | ### Problem Statement: 2 |
3 |

Write a C program to read a program written in a file and remove all comments. After removing all comments, rewrite the program in a separate file.

4 |
5 | 6 | ### Algorithm: 7 | 8 | 1. Open the input file "input.c" in read mode. 9 | 2. Check if the file is successfully opened. If not, print an error message and exit the program with a status of 1. 10 | 3. Open the output file "output.c" in write mode. 11 | 4. Check if the output file is successfully opened. If not, print an error message, close the input file, and exit the program with a status of 1. 12 | 5. Use a while loop to read characters from the input file until the end of the file (EOF) is reached. 13 | 6. Inside the loop, check if the current character (ch) is a '/'. 14 | 7. Handling Single-Line Comments: 15 | 8. If yes, read the next character (nextChar). 16 | 9. If nextChar is '/', it indicates a single-line comment. Continue reading characters until a newline character or the end of the file is encountered, effectively skipping the comment. 17 | 10. If nextChar is '', it indicates the beginning of a multi-line comment. Use a do-while loop to skip characters until the end of the comment (/). 18 | 11. If the current character is not '/', write it to the output file. 19 | 12. After processing all characters, close both the input and output files. 20 | 13. Print a success message indicating that comments have been removed and the result has been written to "output.c". 21 | 14. Return 0 to indicate successful execution. 22 | 23 | ### Code: 24 | 25 | ```C 26 | #include 27 | 28 | int main() { 29 | FILE *inputFile, *outputFile; 30 | char ch, nextChar; 31 | 32 | inputFile = fopen("input.c", "r"); 33 | if (inputFile == NULL) { 34 | printf("Error opening input file\n"); 35 | return 1; 36 | } 37 | 38 | outputFile = fopen("output.c", "w"); 39 | if (outputFile == NULL) { 40 | printf("Error opening output file\n"); 41 | fclose(inputFile); 42 | return 1; 43 | } 44 | 45 | while ((ch = fgetc(inputFile)) != EOF) { 46 | if (ch == '/') { 47 | nextChar = fgetc(inputFile); 48 | if (nextChar == '/') { 49 | while ((ch = fgetc(inputFile)) != '\n' && ch != EOF); 50 | } 51 | else if (nextChar == '*') { 52 | do { 53 | while ((ch = fgetc(inputFile)) != '*'); 54 | } while ((ch = fgetc(inputFile)) != '/'); 55 | } 56 | else { 57 | fputc('/', outputFile); 58 | fputc(nextChar, outputFile); 59 | } 60 | } 61 | else { 62 | fputc(ch, outputFile); 63 | } 64 | } 65 | 66 | fclose(inputFile); 67 | fclose(outputFile); 68 | printf("Comments removed and written to output.c successfully.\n"); 69 | return 0; 70 | } 71 | 72 | ``` 73 | 74 | ### Output: 75 | 76 | ![image](https://github.com/PixMusicaX/6thsem/assets/129383302/76dab350-6ed9-418a-947d-c44faddee44c) 77 | 78 | ![image](https://github.com/PixMusicaX/6thsem/assets/129383302/dcfadbe6-7e2b-40f2-88fe-94c47d56323c) 79 | 80 | ### Conclusion: 81 | 82 | Above is the algorithm and code to remove comments from a code in C. 83 | 84 | -------------------------------------------------------------------------------- /CSE CORE/COMPILER DESIGN LAB/Assignment 3/Week 4.md: -------------------------------------------------------------------------------- 1 | ## Week 4 - 2 | 3 | ### Write a Lex Program to count the number of characters, words, spaces, end of lines in a given input file. 4 | 5 | #### Code - 6 | 7 | ``` 8 | %{ 9 | #include 10 | #include 11 | int char_count = 0, word_count = 0, line_count = 0, space_count = 0; 12 | %} 13 | 14 | char [a-zA-Z] 15 | digit [0-9] 16 | CHAR . 17 | word ({char}|{digit})+ 18 | WORD [^ \t\n,.:]+ 19 | line [\n] 20 | 21 | %% 22 | 23 | {char} { char_count++; word_count++; } 24 | \. { char_count++; } 25 | {WORD} { word_count++; char_count += strlen(yytext); } 26 | {line} { line_count++; } 27 | [ \t] { space_count++; } 28 | 29 | %% 30 | 31 | int main(int argc, char *argv[]) { 32 | if (argc != 2) { 33 | printf("Usage: %s \n", argv[0]); 34 | return 1; 35 | } 36 | FILE *inputFile = fopen(argv[1], "r"); 37 | if (!inputFile) { 38 | perror("Error opening file"); 39 | return 1; 40 | } 41 | yyin = inputFile; 42 | yylex(); 43 | printf("\nTotal Character: %d, Total Word: %d, Line Count: %d, Space Count: %d\n", char_count, word_count, line_count, space_count); 44 | fclose(inputFile); 45 | return 0; 46 | } 47 | 48 | int yywrap() { 49 | return 1; 50 | } 51 | ``` 52 | 53 | #### Output - 54 | 55 | ``` 56 | a.exe file.txt 57 | 58 | Total Character: 33, Total Word: 12, Line Count: 2, Space Count: 9 59 | ``` 60 | 61 | ### Write a Lex Program to count no of: a) +ve and –ve integers b) +ve and –ve fractions. 62 | 63 | #### Codes - 64 | 65 | #### a) 66 | 67 | ``` 68 | %{ 69 | #include 70 | int positive_count = 0; 71 | int negative_count = 0; 72 | %} 73 | 74 | positivedigit [+][0-9]+ 75 | negativedigit [-][0-9]+ 76 | 77 | %% 78 | 79 | {positivedigit} { positive_count++; } 80 | {negativedigit} { negative_count++; } 81 | 82 | %% 83 | 84 | int main() 85 | { 86 | printf("\nEnter the Integers: "); 87 | yylex(); 88 | printf("Number of Positive Numbers: %d\nNumber of Negative Numbers: %d\n", positive_count, negative_count); 89 | return 0; 90 | } 91 | 92 | int yywrap() 93 | { 94 | return 1; 95 | } 96 | 97 | ``` 98 | 99 | #### b) 100 | 101 | ``` 102 | %{ 103 | int positivefractions=0; 104 | int negativefractions=0; 105 | %} 106 | DIGIT [0-9] 107 | %% 108 | \+?{DIGIT}*\.{DIGIT}+ positivefractions++; 109 | -{DIGIT}*\.{DIGIT}+ negativefractions++; 110 | . ; 111 | %% 112 | int main() 113 | { 114 | printf("\nEnter the Fractions: "); 115 | yylex(); 116 | printf("\nNumber of Positive Fractions: %d\nNumber of Negative Fractions: %d\n", positivefractions, negativefractions); 117 | return 0; 118 | } 119 | int yywrap() 120 | { 121 | return 1; 122 | } 123 | ``` 124 | 125 | #### Output - 126 | 127 | #### a) 128 | 129 | ``` 130 | Enter the Integers: +5+2+6-2-5-7-2 131 | 132 | ^Z 133 | Number of Positive Numbers: 3 134 | Number of Negative Numbers: 4 135 | ``` 136 | 137 | #### b) 138 | 139 | ``` 140 | Enter the Fractions: +3.6+2.2-3.4-2.7 141 | 142 | ^Z 143 | 144 | Number of Positive Fractions: 2 145 | Number of Negative Fractions: 2 146 | ``` 147 | -------------------------------------------------------------------------------- /CSE CORE/COMPILER DESIGN LAB/Assignment 4/Week 5.md: -------------------------------------------------------------------------------- 1 | ## 1) Write a Lex Program to count the no of comment line in a given C program. Also eliminate them and copy that program into separate file. 2 | 3 | ### Codes - 4 | 5 | ``` 6 | %{ 7 | #include 8 | int m1=0, s1=0; 9 | %} 10 | 11 | %% 12 | "/*"[a-zA-Z0-9\n\t ]*"*/" {m1++;} 13 | "//"[a-zA-Z0-9\t ]*"\n" {s1++;} 14 | %% 15 | 16 | int main (int argc, char* argv[]) 17 | { 18 | if (argc<2) 19 | { 20 | printf("Usage: \n"); 21 | exit(0); 22 | } 23 | yyin = fopen(argv[1],"r"); 24 | yyout = fopen("output.c","w"); 25 | yylex(); 26 | printf("The number of multiline comment = %d\n", m1); 27 | printf("The number of singleline comment = %d\n", s1); 28 | fclose(yyin); 29 | fclose(yyout); 30 | } 31 | int yywrap() 32 | { 33 | return 1; 34 | } 35 | ``` 36 | 37 | ### Output - 38 | 39 | ``` 40 | a.exe a.c 41 | The number of multiline comment = 1 42 | The number of singleline comment = 2 43 | ``` 44 | 45 | ## 2) Write a Lex Program to count the no of ‘scanf’ and ‘printf’ statements in a C program. Replace them with ‘readf’ and ‘writef’ statements respectively. 46 | 47 | ### Codes - 48 | 49 | ``` 50 | %{ 51 | #include 52 | int a=0, b=0; 53 | %} 54 | 55 | %% 56 | "scanf" {fprintf(yyout,"readf");b++;} 57 | "printf" {fprintf(yyout,"writef");a++;} 58 | %% 59 | 60 | int main (int argc, char* argv[]) 61 | { 62 | if (argc<2) 63 | { 64 | printf("Usage: \n"); 65 | exit(0); 66 | } 67 | yyin = fopen(argv[1],"r"); 68 | yyout = fopen("output.c","w"); 69 | yylex(); 70 | printf("Printf statement - %d\n", a); 71 | printf("Scanf statement - %d\n", b); 72 | fclose(yyin); 73 | fclose(yyout); 74 | } 75 | int yywrap() 76 | { 77 | return 1; 78 | } 79 | ``` 80 | 81 | ### Output - 82 | 83 | ``` 84 | a.exe a.c 85 | Printf statement - 1 86 | Scanf statement - 1 87 | ``` 88 | 89 | ## 3) Write a program to find the number is prime or not and reverse the number. 90 | 91 | ### Codes - 92 | 93 | ``` 94 | %{ 95 | #include 96 | int i,j; 97 | %} 98 | 99 | %% 100 | [0-9]+ {i=atoi(yytext); 101 | for(j=2; j 150 | int i; 151 | %} 152 | 153 | %% 154 | [0-9]+ {i=atoi(yytext); 155 | if(i%2==0) 156 | printf("Even"); 157 | else 158 | printf("Odd");} 159 | %% 160 | 161 | int main() 162 | { 163 | printf("Enter the number: \n"); 164 | yylex(); 165 | return 0; 166 | } 167 | int yywrap() 168 | { 169 | return 1; 170 | } 171 | ``` 172 | 173 | ### Output - 174 | 175 | ``` 176 | a.exe 177 | Enter the number: 178 | 3 179 | Odd 180 | ``` -------------------------------------------------------------------------------- /CSE CORE/COMPILER DESIGN LAB/Assignment 2/Week 3.txt: -------------------------------------------------------------------------------- 1 | -- 1) Write a C program to evaluate an arithmetic expression which is given as a string. Consider the input has no parentheses and contains the following operators only:+, -, *, /. 2 | 3 | Code - 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define MAX_EXPRESSION_LENGTH 100 11 | 12 | double evaluateExpression(char expression[MAX_EXPRESSION_LENGTH]); 13 | 14 | int main() { 15 | char expression[MAX_EXPRESSION_LENGTH]; 16 | 17 | printf("Enter an arithmetic expression: "); 18 | fgets(expression, MAX_EXPRESSION_LENGTH, stdin); 19 | 20 | double result = evaluateExpression(expression); 21 | printf("Result: %lf\n", result); 22 | 23 | return 0; 24 | } 25 | 26 | double evaluateExpression(char expression[MAX_EXPRESSION_LENGTH]) { 27 | double result = 0.0; 28 | double number; 29 | char operatorr; 30 | int i = 0; 31 | int operatorCount = 0; 32 | 33 | sscanf(expression, "%lf", &result); 34 | 35 | while (expression[i] != '\0') { 36 | if (expression[i] == '+' || expression[i] == '-' || expression[i] == '*' || expression[i] == '/') { 37 | operatorCount++; 38 | if(operatorCount >= 2) { 39 | printf("Error: Invalid expression. Multiple operators in a row.\n"); 40 | exit(EXIT_FAILURE); 41 | } 42 | operatorr = expression[i]; 43 | i++; 44 | 45 | sscanf(expression + i, "%lf", &number); 46 | 47 | switch (operatorr) { 48 | case '+': 49 | result += number; 50 | break; 51 | case '-': 52 | result -= number; 53 | break; 54 | case '*': 55 | result *= number; 56 | break; 57 | case '/': 58 | if (number != 0) { 59 | result /= number; 60 | } else { 61 | printf("Error: Division by zero\n"); 62 | exit(EXIT_FAILURE); 63 | } 64 | break; 65 | default: 66 | break; 67 | } 68 | 69 | while (isdigit(expression[i]) || expression[i] == '.') { 70 | i++; 71 | } 72 | } else { 73 | i++; 74 | operatorCount = 0; 75 | } 76 | } 77 | 78 | return result; 79 | } 80 | 81 | Output - 82 | 83 | Enter an arithmetic expression: 5+6 84 | Result 11.000000 85 | 86 | -- 2) Write a Lex Program to count the number of vowels and consonants in a given string. 87 | 88 | Code - 89 | 90 | %{ 91 | int vow_count=0; 92 | int const_count =0; 93 | %} 94 | 95 | %% 96 | [aeiouAEIOU] {vow_count++;} 97 | [a-zA-Z] {const_count++;} 98 | %% 99 | int yywrap(){} 100 | int main() 101 | { 102 | printf("Enter the string of vowels and consonants:"); 103 | yylex(); 104 | printf("Number of vowels are: %d\n", vow_count); 105 | printf("Number of consonants are: %d\n", const_count); 106 | return 0; 107 | } 108 | 109 | Output - 110 | 111 | Enter the string of vowels and consonents : GoodDay 112 | 113 | The number of vowels are : 3 114 | The number of consonants are : 4 115 | -------------------------------------------------------------------------------- /CSE (IOT)/COMPILER DESIGN LAB/Week3/q5.md: -------------------------------------------------------------------------------- 1 | # NOT TO BE WRITTEN: 2 | It was instructed to perform all the lex programs using C. 3 | 4 | 5 | ### Problem Statement: 6 |
7 |

Write a C program to evaluate an arithmetic expression which is given as a string. 8 | Consider the input has no parentheses and contains the following operators only: 9 | +, -, *, /

10 |
11 | 12 | ### Algorithm: 13 | 14 | 1. Include necessary headers for standard input/output and string manipulation. 15 | 2. Declare three functions: addsub(), muldiv(), and term(). 16 | 3. Declare a character array input and an integer pos. 17 | 4. Term Function (term()): 18 | 5. Initialize a variable n. 19 | 6. Check if the current character is an opening parenthesis. 20 | 7. If true, increment the position and recursively call addsub(). 21 | 8. If a closing parenthesis is encountered, increment the position and return n. 22 | 9. If not a parenthesis, iterate through consecutive digits and construct an integer n. 23 | 10. MulDiv Function (muldiv()): 24 | 11. Initialize variables first and second by calling the term() function. 25 | 12. Enter an infinite loop. 26 | 13. If the current character is '*', increment the position, call term() for the second operand, and update first. 27 | 14. If the current character is '/', increment the position, call term() for the second operand, and update first. 28 | 15. If neither '*', nor '/', exit the loop and return first. 29 | 16. AddSub Function (addsub()): 30 | 17. Initialize variables first and second by calling the muldiv() function. 31 | 18. Enter an infinite loop. 32 | 19. If the current character is '+', increment the position, call muldiv() for the second operand, and update first. 33 | 20. If the current character is '-', increment the position, call muldiv() for the second operand, and update first. 34 | 21. If neither '+', nor '-', exit the loop and return first. 35 | 22. Main Function (main()): 36 | 23. Declare variables for user input and loop control. 37 | 24. Print a message prompting the user to input an expression. 38 | 25. Use scanf to read the input into the input array. 39 | 26. Call addsub() and print the result. 40 | 41 | ### Code: 42 | 43 | ```C 44 | #include 45 | #include 46 | 47 | int addsub(); 48 | int muldiv(); 49 | int term(); 50 | 51 | char input[100]; 52 | int pos = 0; 53 | 54 | int term() { 55 | int n = 0; 56 | 57 | if (input[pos] == '(') { 58 | pos++; 59 | n = addsub(); 60 | if (input[pos] == ')') { 61 | pos++; 62 | return n; 63 | } 64 | } else { 65 | while ('0' <= input[pos] && input[pos] <= '9') { 66 | n = n * 10 + (input[pos] - '0'); 67 | pos++; 68 | } 69 | } 70 | return n; 71 | } 72 | 73 | int muldiv() { 74 | int first, second; 75 | 76 | first = term(); 77 | for (;;) { 78 | if (input[pos] == '*') { 79 | pos++; 80 | second = term(); 81 | first *= second; 82 | } else if (input[pos] == '/') { 83 | pos++; 84 | second = term(); 85 | first /= second; 86 | } else { 87 | return first; 88 | } 89 | } 90 | } 91 | 92 | int addsub() { 93 | int first, second; 94 | 95 | first = muldiv(); 96 | for (;;) { 97 | if (input[pos] == '+') { 98 | pos++; 99 | second = muldiv(); 100 | first += second; 101 | } else if (input[pos] == '-') { 102 | pos++; 103 | second = muldiv(); 104 | first -= second; 105 | } else { 106 | return first; 107 | } 108 | } 109 | } 110 | 111 | int main() { 112 | int n, i, j; 113 | printf("Input an expression: "); 114 | scanf("%s", input); 115 | printf("%d\n", addsub()); 116 | return 0; 117 | } 118 | 119 | ``` 120 | 121 | ### Output: 122 | 123 | ![image](https://github.com/PixMusicaX/6thsem/assets/129383302/b109de57-737d-4953-8257-9a0466a9f173) 124 | 125 | ### Conclusion: 126 | 127 | Above is the algorithm and code to evaluate an arithmetic expression passed as a String. 128 | -------------------------------------------------------------------------------- /CSE (IOT)/COMPILER DESIGN LAB/Week2/q4.md: -------------------------------------------------------------------------------- 1 | ### Problem Statement: 2 |
3 |

Write a C program to convert an infix statement into a postfix statement.

4 |
5 | 6 | ### Algorithm: 7 | 8 | 1. Create a character array for the infix expression (infix) and the postfix expression (postfix). 9 | 2. Create a stack (stack) to store operators during the conversion. 10 | 3. Initialize top of the stack to -1. 11 | 4. Prompt the user to enter the infix expression. 12 | 5. Read and store the infix expression in the infix array. 13 | 6. Process Each Character in Infix Expression: 14 | 7. Initialize a loop to iterate through each character in the infix expression. 15 | 8. Retrieve the current character ch at the current position. 16 | 9. If ch is an alphanumeric character, append it to the postfix expression. 17 | 10. If ch is '(', push it onto the stack. 18 | 11. If ch is ')', pop operators from the stack and append them to postfix until an opening parenthesis '(' is encountered. Pop and discard the '('. 19 | 12. If ch is an operator (+, -, *, /), compare its precedence with the operator at the top of the stack. 20 | 13. Pop and append operators from the stack to postfix while the top operator has higher or equal precedence than ch. 21 | 14. Push ch onto the stack. 22 | 15. Repeat steps 8-14 until you reach the end of the infix expression. 23 | 16. After processing the entire infix expression, pop any remaining operators from the stack and append them to postfix. 24 | 17. The postfix array now contains the postfix expression. 25 | 18. Display the postfix array. 26 | 27 | ### Code: 28 | 29 | ```C 30 | #include 31 | #include 32 | #include 33 | 34 | #define MAX_SIZE 100 35 | 36 | struct Stack { 37 | int top; 38 | char items[MAX_SIZE]; 39 | }; 40 | 41 | void push(struct Stack *stack, char item); 42 | char pop(struct Stack *stack); 43 | int isOperator(char ch); 44 | int precedence(char ch); 45 | void infixToPostfix(char infix[], char postfix[]); 46 | 47 | int main() { 48 | char infix[MAX_SIZE], postfix[MAX_SIZE]; 49 | 50 | printf("Enter the infix expression: "); 51 | scanf("%s", infix); 52 | 53 | 54 | infixToPostfix(infix, postfix); 55 | 56 | printf("Postfix expression: %s\n", postfix); 57 | 58 | return 0; 59 | } 60 | 61 | void push(struct Stack *stack, char item) { 62 | if (stack->top == MAX_SIZE - 1) { 63 | printf("Stack overflow\n"); 64 | exit(EXIT_FAILURE); 65 | } 66 | stack->items[++stack->top] = item; 67 | } 68 | 69 | char pop(struct Stack *stack) { 70 | if (stack->top == -1) { 71 | printf("Stack underflow\n"); 72 | exit(EXIT_FAILURE); 73 | } 74 | return stack->items[stack->top--]; 75 | } 76 | 77 | int isOperator(char ch) { 78 | return (ch == '+' || ch == '-' || ch == '*' || ch == '/'); 79 | } 80 | 81 | int precedence(char ch) { 82 | if (ch == '+' || ch == '-') 83 | return 1; 84 | else if (ch == '*' || ch == '/') 85 | return 2; 86 | else 87 | return 0; } 88 | 89 | void infixToPostfix(char infix[], char postfix[]) { 90 | struct Stack stack; 91 | stack.top = -1; 92 | int i, j; 93 | char ch, popped; 94 | 95 | j = 0; 96 | 97 | for (i = 0; infix[i] != '\0'; i++) { 98 | ch = infix[i]; 99 | 100 | if (isalnum(ch)) { 101 | postfix[j++] = ch; 102 | } else if (ch == '(') { 103 | push(&stack, ch); 104 | } else if (ch == ')') { 105 | while ((popped = pop(&stack)) != '(') { 106 | postfix[j++] = popped; 107 | } 108 | } else if (isOperator(ch)) { 109 | while (stack.top != -1 && precedence(stack.items[stack.top]) >= precedence(ch)) { 110 | postfix[j++] = pop(&stack); 111 | } 112 | push(&stack, ch); 113 | } 114 | } 115 | 116 | while (stack.top != -1) { 117 | postfix[j++] = pop(&stack); 118 | } 119 | 120 | postfix[j] = '\0'; 121 | } 122 | 123 | 124 | ``` 125 | 126 | ### Output: 127 | 128 | ![image](https://github.com/PixMusicaX/6thsem/assets/129383302/11578a7d-e56b-4eac-b231-dbed815b0400) 129 | 130 | ### Conclusion: 131 | 132 | Above is the algorithm and code to convert an infix expression to postfix expression using Stack. 133 | -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 2/2.md: -------------------------------------------------------------------------------- 1 | ### Write a C program to perform checksum for error detection for both the sender and receiver side. 2 | 3 | #### Code - 4 | 5 | ``` 6 | #include 7 | #define DATA_SIZE 10 8 | int calculateChecksum(int data[]) { 9 | int checksum = 0; 10 | for (int i = 0; i < DATA_SIZE; i++) { 11 | checksum += data[i]; 12 | } 13 | return checksum; 14 | } 15 | int main() { 16 | int data[DATA_SIZE]; 17 | int senderChecksum, receiverChecksum; 18 | printf("Sender Side:\n"); 19 | printf("Enter %d integers:\n", DATA_SIZE); 20 | for (int i = 0; i < DATA_SIZE; i++) { 21 | scanf("%d", &data[i]); 22 | } 23 | senderChecksum = calculateChecksum(data); 24 | printf("Checksum generated at sender side: %d\n", senderChecksum); 25 | printf("\nReceiver Side:\n"); 26 | printf("Enter %d integers received:\n", DATA_SIZE); 27 | for (int i = 0; i < DATA_SIZE; i++) { 28 | scanf("%d", &data[i]); 29 | } 30 | receiverChecksum = calculateChecksum(data); 31 | printf("Checksum generated at receiver side: %d\n", receiverChecksum); 32 | if (senderChecksum == receiverChecksum) { 33 | printf("Checksums match. No errors detected.\n"); 34 | } else { 35 | printf("Checksums do not match. Errors detected.\n"); 36 | } 37 | return 0; 38 | } 39 | ``` 40 | 41 | #### Output - 42 | 43 | ``` 44 | Sender Side: 45 | Enter 10 integers: 46 | 56 47 | 35 48 | 8 49 | 67 50 | 23 51 | 56 52 | 34 53 | 21 54 | 34 55 | 56 56 | Checksum generated at sender side: 390 57 | 58 | Receiver Side: 59 | Enter 10 integers received: 60 | 56 61 | 35 62 | 10 63 | 67 64 | 23 65 | 56 66 | 34 67 | 21 68 | 36 69 | 56 70 | Checksum generated at receiver side: 394 71 | Checksums do not match. Errors detected. 72 | ``` 73 | 74 | ### Write a C program to perform parity checker for error detection for both the sender and receiver side. 75 | 76 | #### Code - 77 | 78 | ``` 79 | #include 80 | #define DATA_SIZE 8 81 | int calculateParityBit(int data[]) { 82 | int count = 0; 83 | for (int i = 0; i < DATA_SIZE; i++) { 84 | if (data[i] == 1) { 85 | count++; 86 | } 87 | } 88 | return count % 2; 89 | } 90 | int main() { 91 | int data[DATA_SIZE]; 92 | int senderParity, receiverParity; 93 | printf("Sender Side:\n"); 94 | printf("Enter %d bits (0s and 1s):\n", DATA_SIZE); 95 | for (int i = 0; i < DATA_SIZE; i++) { 96 | scanf("%d", &data[i]); 97 | } 98 | senderParity = calculateParityBit(data); 99 | printf("Parity bit generated at sender side: %d\n", senderParity); 100 | printf("\nReceiver Side:\n"); 101 | printf("Enter %d bits received (0s and 1s):\n", DATA_SIZE); 102 | for (int i = 0; i < DATA_SIZE; i++) { 103 | scanf("%d", &data[i]); 104 | } 105 | receiverParity = calculateParityBit(data); 106 | printf("Parity bit generated at receiver side: %d\n", receiverParity); 107 | if (senderParity == receiverParity) { 108 | printf("Parity bits match. No errors detected.\n"); 109 | } else { 110 | printf("Parity bits do not match. Errors detected.\n"); 111 | } 112 | 113 | return 0; 114 | } 115 | ``` 116 | 117 | #### Output - 118 | 119 | ``` 120 | Sender Side: 121 | Enter 8 bits (0s and 1s): 122 | 0 123 | 1 124 | 0 125 | 0 126 | 1 127 | 1 128 | 0 129 | 1 130 | Parity bit generated at sender side: 0 131 | 132 | Receiver Side: 133 | Enter 8 bits received (0s and 1s): 134 | 1 135 | 1 136 | 1 137 | 0 138 | 0 139 | 1 140 | 1 141 | 1 142 | Parity bit generated at receiver side: 0 143 | Parity bits match. No errors detected. 144 | ``` 145 | 146 | ### Write a C program to perform CRC (Cyclic Redundancy Check) for error detection for both the sender and receiver side. 147 | 148 | #### Code - 149 | 150 | ``` 151 | #include 152 | #include 153 | #define POLYNOMIAL 0x04C11DB7 154 | unsigned int crc32(const unsigned char *message, unsigned int size) { 155 | unsigned int crc = 0xffffffff; 156 | 157 | for (unsigned int i = 0; i < size; i++) { 158 | crc ^= message[i]; 159 | 160 | for (int j = 0; j < 8; j++) { 161 | if (crc & 0x80000000) { 162 | crc = (crc << 1) ^ POLYNOMIAL; 163 | } else { 164 | crc = crc << 1; 165 | } 166 | } 167 | } 168 | return crc; 169 | } 170 | int main() { 171 | unsigned char message[100]; 172 | unsigned int senderCRC, receiverCRC; 173 | printf("Sender Side:\n"); 174 | printf("Enter message to send: "); 175 | fgets((char *)message, sizeof(message), stdin); 176 | message[strlen((char *)message) - 1] = '\0'; 177 | unsigned int size = strlen((char *)message); 178 | senderCRC = crc32(message, size); 179 | printf("CRC generated at sender side: 0x%08X\n", senderCRC); 180 | printf("\nReceiver Side:\n"); 181 | printf("Enter received message: "); 182 | fgets((char *)message, sizeof(message), stdin); 183 | message[strlen((char *)message) - 1] = '\0'; 184 | size = strlen((char *)message); 185 | receiverCRC = crc32(message, size); 186 | printf("CRC generated at receiver side: 0x%08X\n", receiverCRC); 187 | if (senderCRC == receiverCRC) { 188 | printf("CRCs match. No errors detected.\n"); 189 | } else { 190 | printf("CRCs do not match. Errors detected.\n"); 191 | } 192 | 193 | return 0; 194 | } 195 | ``` 196 | 197 | #### Output - 198 | 199 | ``` 200 | Sender Side: 201 | Enter message to send: 0xABBBBBBBB 202 | CRC generated at sender side: 0x2A2185B9 203 | 204 | Receiver Side: 205 | Enter received message: 0xABBBBBBBB 206 | CRC generated at receiver side: 0x2A2185B9 207 | CRCs match. No errors detected. 208 | ``` 209 | -------------------------------------------------------------------------------- /CSE CORE/COMPILER DESIGN LAB/Assignment 1/Week 1 & Week 2.txt: -------------------------------------------------------------------------------- 1 | Week 1 - 2 | 3 | 1) Write a C program to check if a user given string is a valid identifier or not? 4 | 5 | Code - 6 | 7 | #include 8 | #include 9 | 10 | void checkidentifier(char inputt[]) 11 | { 12 | if (inputt[0] == '_' || (inputt[0] >= 'A' && inputt[0] <= 'Z') || (inputt[0] >= 'a' && inputt[0] <= 'z')) 13 | { 14 | printf("Valid Identifier"); 15 | } 16 | else 17 | { 18 | printf("Invalid Identifier"); 19 | } 20 | } 21 | 22 | int main() 23 | { 24 | char inputt[100]; 25 | printf("Insert a string to check - "); 26 | gets(inputt); 27 | checkidentifier(inputt); 28 | } 29 | 30 | Output - 31 | 32 | Insert a string to check - _ABC 33 | Valid Identifier 34 | 35 | 2) Write a C program to check if a user given C program statement is a valid Comment or not? 36 | 37 | Code - 38 | 39 | #include 40 | #include 41 | 42 | void checkcomment(char inputt[]) 43 | { 44 | if (inputt[0] == '/') 45 | { 46 | if (inputt[1] == '/' || (inputt[1] == '*' && inputt[strlen(inputt) - 1] == '/' && inputt[strlen(inputt) - 2] == '*')) 47 | { 48 | printf("This is a Comment"); 49 | } 50 | else 51 | { 52 | printf("This is not a Comment"); 53 | } 54 | } 55 | else 56 | { 57 | printf("This is not a Comment"); 58 | } 59 | } 60 | 61 | int main() 62 | { 63 | char inputt[100]; 64 | printf("Insert a string to check - "); 65 | gets(inputt); 66 | checkcomment(inputt); 67 | return 0; 68 | } 69 | 70 | Output - 71 | 72 | Insert a string to check - // comment 73 | This is a comment 74 | 75 | Week 2 - 76 | 77 | 3) Write a C program to read a program written in a file and remove all comments. After removing all comments, rewrite the program in a separate file. 78 | 79 | code - 80 | 81 | #include 82 | #include 83 | #include 84 | 85 | void removeComments(FILE *sourceFile, FILE *outputFile) { 86 | int currentChar, nextChar; 87 | while ((currentChar = fgetc(sourceFile)) != EOF) { 88 | if (currentChar == '/') { 89 | nextChar = fgetc(sourceFile); 90 | if (nextChar == '/') { 91 | while ((currentChar = fgetc(sourceFile)) != '\n' && currentChar != EOF); 92 | } else if (nextChar == '*') { 93 | do { 94 | while ((currentChar = fgetc(sourceFile)) != '*') 95 | if (currentChar == EOF) break; 96 | nextChar = fgetc(sourceFile); 97 | } while (nextChar != '/' && currentChar != EOF); 98 | } else { 99 | fputc(currentChar, outputFile); 100 | fputc(nextChar, outputFile); 101 | } 102 | } else { 103 | fputc(currentChar, outputFile); 104 | } 105 | } 106 | } 107 | int main() { 108 | FILE *sourceFile, *outputFile; 109 | char sourceFileName[100], outputFileName[100]; 110 | 111 | printf("Enter the name of the source code file: "); 112 | scanf("%s", sourceFileName); 113 | 114 | sourceFile = fopen(sourceFileName, "r"); 115 | 116 | if (sourceFile == NULL) { 117 | printf("Error opening source file.\n"); 118 | return 1; 119 | } 120 | 121 | printf("Enter the name of the output file: "); 122 | scanf("%s", outputFileName); 123 | 124 | outputFile = fopen(outputFileName, "w"); 125 | 126 | if (outputFile == NULL) { 127 | printf("Error opening output file.\n"); 128 | fclose(sourceFile); 129 | return 1; 130 | } 131 | 132 | removeComments(sourceFile, outputFile); 133 | 134 | printf("Comments removed successfully. Check the file %s\n", outputFileName); 135 | 136 | fclose(sourceFile); 137 | fclose(outputFile); 138 | 139 | return 0; 140 | } 141 | 142 | Output - 143 | 144 | Enter the name of the source code file: file1.txt 145 | Enter the name of the output file: file2.txt 146 | Comments removed successfully. Check the file file2.txt 147 | 148 | 4) Write a C program to convert an infix statement into a postfix statement. 149 | 150 | code - 151 | 152 | #include 153 | #include 154 | #include 155 | #include 156 | #define MAX_EXPR_SIZE 100 157 | 158 | int precedence(char operatorr) { 159 | switch (operatorr) { 160 | case '+': 161 | case '-': 162 | return 1; 163 | case '*': 164 | case '/': 165 | return 2; 166 | case '^': 167 | return 3; 168 | default: 169 | return -1; 170 | } 171 | } 172 | 173 | int isOperator(char ch) { 174 | return (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^'); 175 | } 176 | 177 | char *infixToPostfix(char *infix) { 178 | int i, j; 179 | int len = strlen(infix); 180 | char *postfix = (char *)malloc(sizeof(char) * (len + 2)); 181 | char stack[MAX_EXPR_SIZE]; 182 | int top = -1; 183 | 184 | for (i = 0, j = 0; i < len; i++) { 185 | if (infix[i] == ' ' || infix[i] == '\t') 186 | continue; 187 | if (isalnum(infix[i])) { 188 | postfix[j++] = infix[i]; 189 | } else if (infix[i] == '(') { 190 | stack[++top] = infix[i]; 191 | } else if (infix[i] == ')') { 192 | while (top > -1 && stack[top] != '(') 193 | postfix[j++] = stack[top--]; 194 | if (top > -1 && stack[top] != '(') 195 | return "Invalid Expression"; 196 | else 197 | top--; 198 | } else if (isOperator(infix[i])) { 199 | while (top > -1 && precedence(stack[top]) >= precedence(infix[i])) 200 | postfix[j++] = stack[top--]; 201 | stack[++top] = infix[i]; 202 | } 203 | } 204 | 205 | while (top > -1) 206 | postfix[j++] = stack[top--]; 207 | 208 | postfix[j] = '\0'; 209 | return postfix; 210 | } 211 | 212 | int main() { 213 | char infix[MAX_EXPR_SIZE]; 214 | printf("Enter an infix expression: "); 215 | fgets(infix, sizeof(infix), stdin); 216 | char *postfix = infixToPostfix(infix); 217 | printf("Postfix expression: %s\n", postfix); 218 | free(postfix); 219 | return 0; 220 | } 221 | 222 | Output - 223 | 224 | Enter a infix Statement: ((A + B) – C * (D / E)) + F 225 | PostFix Expression: AB+CDE/*-F+ 226 | -------------------------------------------------------------------------------- /CSE CORE/COMPUTER NETWORK LAB/Assignment 4/Write_this.md: -------------------------------------------------------------------------------- 1 | ### 1. Write a TCP client server program to send hello message from client to server. Server reverses the message and sends back to client. Client print the reversed message. 2 | 3 | Server Code - 4 | 5 | ``` 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #define MAXLINE 20 14 | #define SERV_PORT 5777 15 | main(int argc,char *argv) 16 | { 17 | int i,j; 18 | ssize_t n; 19 | char line[MAXLINE],revline[MAXLINE]; 20 | int listenfd,connfd,clilen; 21 | struct sockaddr_in servaddr,cliaddr; 22 | listenfd=socket(AF_INET,SOCK_STREAM,0); 23 | bzero(&servaddr,sizeof(servaddr)); 24 | servaddr.sin_family=AF_INET; 25 | servaddr.sin_port=htons(SERV_PORT); 26 | bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr)); 27 | listen(listenfd,1); 28 | for( ; ; ) 29 | { 30 | clilen=sizeof(cliaddr); 31 | connfd=accept(listenfd,(struct sockaddr*)&cliaddr,&clilen); 32 | printf("Connect to client.\n"); 33 | 34 | while(1) 35 | { 36 | if((n=read(connfd,line,MAXLINE))==0) 37 | break; 38 | printf("Data Received.\n"); 39 | printf("Data: %s",line); 40 | line[n-1]='\0'; 41 | j=0; 42 | for(i=n-2;i>=0;i--) 43 | revline[j++]=line[i]; 44 | revline[j]='\0'; 45 | write(connfd,revline,n); //This line writes the reversed data (revline) back to the client using the write() system call. 46 | } 47 | } 48 | } 49 | ``` 50 | Client Code - 51 | 52 | ``` 53 | #include 54 | #include 55 | #include 56 | #include 57 | #include 58 | #include 59 | #include 60 | #define MAXLINE 20 61 | #define SERV_PORT 5777 62 | main(int argc,char *argv) 63 | { 64 | char sendline[MAXLINE],revline[MAXLINE]; 65 | int sockfd; 66 | struct sockaddr_in servaddr; 67 | sockfd=socket(AF_INET,SOCK_STREAM,0); 68 | bzero(&servaddr,sizeof(servaddr)); 69 | servaddr.sin_family=AF_INET; 70 | servaddr.sin_port=ntohs(SERV_PORT); 71 | connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr)); 72 | printf("\n Enter the data to be send: "); 73 | while(fgets(sendline,MAXLINE,stdin)!=NULL) 74 | { 75 | write(sockfd,sendline,strlen(sendline)); 76 | printf("\n Line send"); 77 | read(sockfd,revline,MAXLINE); 78 | printf("\n Reverse of the given sentence is : %s",revline); 79 | printf("\n"); 80 | printf("\n Enter the data to be send: "); 81 | } 82 | exit(0); 83 | } 84 | ``` 85 | 86 | Output - 87 | 88 | ``` 89 | Enter the data to be send: this is a test 90 | 91 | Line send 92 | Reverse of the given sentence is : tset a si siht 93 | ``` 94 | 95 | ### 2. Write a UDP client server message where the client sends a file to the server. The server reads the content of the file and prints it on the console. 96 | 97 | Server Code - 98 | 99 | ``` 100 | #include 101 | #include 102 | #include 103 | #include 104 | #include 105 | #include 106 | #define SERV_PORT 6349 107 | main(int argc,char **argv) 108 | { 109 | char filename[80],recvline[80]; 110 | FILE *fp; 111 | struct sockaddr_in servaddr,cliaddr; 112 | int clilen,sockfd; 113 | sockfd=socket(AF_INET,SOCK_DGRAM,0); 114 | bzero(&servaddr,sizeof(servaddr)); 115 | servaddr.sin_family=AF_INET; 116 | servaddr.sin_port=htons(SERV_PORT); 117 | bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr)); 118 | clilen=sizeof(cliaddr); 119 | recvfrom(sockfd,filename,80,0,(struct sockaddr*)&cliaddr,&clilen); 120 | printf("\nData in the file is: \n "); 121 | fp=fopen(filename,"r"); 122 | while(fgets(recvline,80,fp)!=NULL) 123 | { 124 | printf("\n %s\n ",recvline); 125 | } 126 | fclose(fp); 127 | } 128 | ``` 129 | Client Code - 130 | ``` 131 | #include 132 | #include 133 | #include 134 | #include 135 | #include 136 | #include 137 | #include 138 | #define SERV_PORT 6349 139 | main(int argc,char **argv) 140 | { 141 | char filename[80]; 142 | int sockfd; 143 | struct sockaddr_in servaddr; 144 | sockfd=socket(AF_INET,SOCK_DGRAM,0); 145 | bzero(&servaddr,sizeof(servaddr)); 146 | servaddr.sin_family=AF_INET; 147 | servaddr.sin_port=htons(SERV_PORT); 148 | inet_pton(AF_INET,argv[1],&servaddr.sin_addr); 149 | printf("Enter the file name: "); 150 | scanf("%s",filename); 151 | sendto(sockfd,filename,strlen(filename),0,(struct sockaddr*)&servaddr,sizeof(servaddr)); 152 | } 153 | ``` 154 | 155 | Output - 156 | 157 | ``` 158 | ./a.out 8080 159 | Enter the file name: a.txt 160 | 161 | Data in the file is: 162 | 163 | this is a test 164 | ``` 165 | 166 | ### 3. Write a TCP client server message where the client sends a file to the server. The server reads the content of the file and prints it on the console. 167 | 168 | Server Code - 169 | 170 | ``` 171 | #include 172 | #include 173 | #include 174 | #include 175 | #include 176 | #include 177 | #define SERV_PORT 5576 178 | main(int argc,char **argv) 179 | { 180 | int i,j; 181 | ssize_t n; 182 | FILE *fp; 183 | char s[80],f[80]; 184 | struct sockaddr_in servaddr,cliaddr; 185 | int listenfd,connfd,clilen; 186 | listenfd=socket(AF_INET,SOCK_STREAM,0); 187 | bzero(&servaddr,sizeof(servaddr)); 188 | servaddr.sin_family=AF_INET; 189 | servaddr.sin_port=htons(SERV_PORT); 190 | bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr)); 191 | listen(listenfd,1); 192 | clilen=sizeof(cliaddr); 193 | connfd=accept(listenfd,(struct sockaddr*)&cliaddr,&clilen); 194 | printf("\nClient connected. "); 195 | read(connfd,f,80); 196 | fp=fopen(f,"r"); 197 | printf("\nName of the file: %s",f); 198 | while(fgets(s,80,fp)!=NULL) 199 | { 200 | printf("\nContent of the file: %s",s); 201 | write(connfd,s,sizeof(s)); 202 | } 203 | } 204 | ``` 205 | Client Code - 206 | 207 | ``` 208 | #include 209 | #include 210 | #include 211 | #include 212 | #include 213 | #include 214 | #define SERV_PORT 5576 215 | main(int argc,char **argv) 216 | { 217 | int i,j; 218 | ssize_t n; 219 | char filename[80],recvline[80]; 220 | struct sockaddr_in servaddr; 221 | int sockfd; 222 | sockfd=socket(AF_INET,SOCK_STREAM,0); 223 | bzero(&servaddr,sizeof(servaddr)); 224 | servaddr.sin_family=AF_INET; 225 | servaddr.sin_port=htons(SERV_PORT); 226 | inet_pton(AF_INET,argv[1],&servaddr.sin_addr); 227 | connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr)); 228 | printf("Enter the file name: "); 229 | scanf("%s",filename); 230 | write(sockfd,filename,sizeof(filename)); 231 | printf("\n Data from server: \n"); 232 | while(read(sockfd,recvline,80)!=0) 233 | { 234 | fputs(recvline,stdout); 235 | } 236 | } 237 | ``` 238 | 239 | Output - 240 | 241 | ``` 242 | ./a.out 8080 243 | Enter the file name: a.txt 244 | 245 | Data from server: 246 | this is a test 247 | ``` 248 | 249 | ### 4. Write a TCP client server program where client will send an input file to the server. Inside that input file a sentence will be there. After receiving the file server reverse individual word of that sentence and write it into another file and reply back that new file to the client. Client will read the file and print the content of the file on the console. 250 | 251 | Server Code - 252 | 253 | ``` 254 | 255 | ``` 256 | Client Code - 257 | 258 | ``` 259 | 260 | ``` 261 | 262 | Output - 263 | 264 | ``` 265 | 🖕 266 | ``` 267 | -------------------------------------------------------------------------------- /CSE (IOT)/HACKER RANK ASSIGNMENT/Week 2/2.txt: -------------------------------------------------------------------------------- 1 | Week 2 - 2 | 3 | 1) Lonely Integer 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | import java.util.Scanner; 8 | 9 | public class UniqueElementFinder { 10 | 11 | public static void main(String[] args) { 12 | Scanner scanner = new Scanner(System.in); 13 | 14 | int n = scanner.nextInt(); 15 | 16 | int[] arr = new int[n]; 17 | 18 | for (int i = 0; i < n; i++) { 19 | arr[i] = scanner.nextInt(); 20 | } 21 | 22 | int uniqueElement = findUniqueElement(arr); 23 | System.out.println(uniqueElement); 24 | 25 | scanner.close(); 26 | } 27 | 28 | static int findUniqueElement(int[] arr) { 29 | Map frequencyMap = new HashMap<>(); 30 | 31 | for (int num : arr) { 32 | frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1); 33 | } 34 | 35 | for (Map.Entry entry : frequencyMap.entrySet()) { 36 | if (entry.getValue() == 1) { 37 | return entry.getKey(); 38 | } 39 | } 40 | 41 | return -1; 42 | } 43 | } 44 | 45 | 2) Grading Students 46 | 47 | import java.util.Scanner; 48 | 49 | public class GradeRounding { 50 | 51 | public static void main(String[] args) { 52 | Scanner scanner = new Scanner(System.in); 53 | 54 | int n = scanner.nextInt(); 55 | 56 | int[] grades = new int[n]; 57 | 58 | 59 | for (int i = 0; i < n; i++) { 60 | grades[i] = scanner.nextInt(); 61 | } 62 | 63 | int[] roundedGrades = roundGrades(grades); 64 | 65 | 66 | for (int i = 0; i < n; i++) { 67 | System.out.println(roundedGrades[i]); 68 | } 69 | 70 | scanner.close(); 71 | } 72 | 73 | static int[] roundGrades(int[] grades) { 74 | int[] roundedGrades = new int[grades.length]; 75 | 76 | for (int i = 0; i < grades.length; i++) { 77 | if (grades[i] < 38) { 78 | roundedGrades[i] = grades[i]; 79 | } else { 80 | int nextMultipleOf5 = ((grades[i] + 4) / 5) * 5; 81 | if (nextMultipleOf5 - grades[i] < 3) { 82 | roundedGrades[i] = nextMultipleOf5; 83 | } else { 84 | roundedGrades[i] = grades[i]; 85 | } 86 | } 87 | } 88 | 89 | return roundedGrades; 90 | } 91 | } 92 | 93 | 3) Flipping Bits 94 | 95 | import java.util.Scanner; 96 | 97 | public class Solution { 98 | 99 | public static void main(String[] args) { 100 | Scanner scanner = new Scanner(System.in); 101 | 102 | int T = scanner.nextInt(); 103 | 104 | long mask = (1L << 32) - 1; 105 | 106 | for (int t = 0; t < T; t++) { 107 | long n = scanner.nextLong(); 108 | System.out.println(n ^ mask); 109 | } 110 | 111 | scanner.close(); 112 | 113 | } 114 | 115 | } 116 | 117 | 4) Diagonal Difference 118 | 119 | import java.util.Scanner; 120 | 121 | public class DiagonalDifference { 122 | public static void main(String[] args) { 123 | Scanner scanner = new Scanner(System.in); 124 | int size = scanner.nextInt(); 125 | int[][] matrix = new int[size][size]; 126 | for (int i = 0; i < size; i++) { 127 | for (int j = 0; j < size; j++) { 128 | matrix[i][j] = scanner.nextInt(); 129 | } 130 | } 131 | 132 | int diagonalDifference = calculateDiagonalDifference(matrix); 133 | System.out.println(diagonalDifference); 134 | scanner.close(); 135 | } 136 | 137 | public static int calculateDiagonalDifference(int[][] matrix) { 138 | int leftToRightSum = 0; 139 | int rightToLeftSum = 0; 140 | 141 | for (int i = 0; i < matrix.length; i++) { 142 | leftToRightSum += matrix[i][i]; 143 | rightToLeftSum += matrix[i][matrix.length - 1 - i]; 144 | } 145 | 146 | return Math.abs(leftToRightSum - rightToLeftSum); 147 | } 148 | } 149 | 150 | 5) Counting Storts 151 | 152 | import java.io.*; 153 | import java.util.*; 154 | import java.text.*; 155 | import java.math.*; 156 | import java.util.regex.*; 157 | 158 | public class Solution { 159 | 160 | static void printArray(int[] ar) { 161 | for (int n : ar) { 162 | System.out.print(n + " "); 163 | } 164 | System.out.println(""); 165 | } 166 | 167 | public static void main(String[] args) { 168 | Scanner in = new Scanner(System.in); 169 | int n = in.nextInt(); 170 | int[] ar = new int[100]; 171 | for (int i = 0; i < n; i++) { 172 | ar[in.nextInt()]++; 173 | } 174 | 175 | printArray(ar); 176 | } 177 | 178 | } 179 | 180 | 6) Counting Valleys 181 | 182 | import java.util.Scanner; 183 | 184 | public class CountValleys { 185 | public static void main(String[] args) { 186 | Scanner scanner = new Scanner(System.in); 187 | int steps = scanner.nextInt(); 188 | String path = scanner.next(); 189 | 190 | // Calculate and print the number of valleys walked through 191 | int valleys = countValleys(steps, path); 192 | System.out.println(valleys); 193 | 194 | scanner.close(); 195 | } 196 | private static int countValleys(int steps, String path) { 197 | int level = 0; 198 | int valleys = 0; 199 | 200 | for (char step : path.toCharArray()) { 201 | if (step == 'U') { 202 | level++; 203 | } else if (step == 'D') { 204 | level--; 205 | } 206 | if (step == 'U' && level == 0) { 207 | valleys++; 208 | } 209 | } 210 | 211 | return valleys; 212 | } 213 | } 214 | 215 | 7) Pangrams 216 | 217 | import java.util.HashSet; 218 | import java.util.Scanner; 219 | import java.util.Set; 220 | 221 | public class PangramChecker { 222 | public static void main(String[] args) { 223 | Scanner scanner = new Scanner(System.in); 224 | String sentence = scanner.nextLine(); 225 | boolean isPangram = checkPangram(sentence); 226 | if (isPangram) { 227 | System.out.println("pangram"); 228 | } else { 229 | System.out.println("not pangram"); 230 | } 231 | 232 | scanner.close(); 233 | } 234 | private static boolean checkPangram(String sentence) { 235 | Set alphabetSet = new HashSet<>(); 236 | for (char c : sentence.toLowerCase().toCharArray()) { 237 | if (Character.isLetter(c)) { 238 | alphabetSet.add(c); 239 | } 240 | } 241 | return alphabetSet.size() == 26; 242 | } 243 | } 244 | 245 | 8) Mars Exploration 246 | 247 | import java.util.Scanner; 248 | 249 | public class MarsSOS { 250 | public static void main(String[] args) { 251 | Scanner scanner = new Scanner(System.in); 252 | String signal = scanner.next(); 253 | int alteredLetters = countAlteredLetters(signal); 254 | System.out.println(alteredLetters); 255 | scanner.close(); 256 | } 257 | private static int countAlteredLetters(String signal) { 258 | String sos = "SOS"; 259 | int alteredLetters = 0; 260 | 261 | for (int i = 0; i < signal.length(); i++) { 262 | if (signal.charAt(i) != sos.charAt(i % 3)) { 263 | alteredLetters++; 264 | } 265 | } 266 | return alteredLetters; 267 | } 268 | } 269 | -------------------------------------------------------------------------------- /CSE AIML/HACKER RANK ASSIGNMENT/Week 2/2.txt: -------------------------------------------------------------------------------- 1 | Week 2 - 2 | 3 | 1) Lonely Integer 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | import java.util.Scanner; 8 | 9 | public class UniqueElementFinder { 10 | 11 | public static void main(String[] args) { 12 | Scanner scanner = new Scanner(System.in); 13 | 14 | int n = scanner.nextInt(); 15 | 16 | int[] arr = new int[n]; 17 | 18 | for (int i = 0; i < n; i++) { 19 | arr[i] = scanner.nextInt(); 20 | } 21 | 22 | int uniqueElement = findUniqueElement(arr); 23 | System.out.println(uniqueElement); 24 | 25 | scanner.close(); 26 | } 27 | 28 | static int findUniqueElement(int[] arr) { 29 | Map frequencyMap = new HashMap<>(); 30 | 31 | for (int num : arr) { 32 | frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1); 33 | } 34 | 35 | for (Map.Entry entry : frequencyMap.entrySet()) { 36 | if (entry.getValue() == 1) { 37 | return entry.getKey(); 38 | } 39 | } 40 | 41 | return -1; 42 | } 43 | } 44 | 45 | 2) Grading Students 46 | 47 | import java.util.Scanner; 48 | 49 | public class GradeRounding { 50 | 51 | public static void main(String[] args) { 52 | Scanner scanner = new Scanner(System.in); 53 | 54 | int n = scanner.nextInt(); 55 | 56 | int[] grades = new int[n]; 57 | 58 | 59 | for (int i = 0; i < n; i++) { 60 | grades[i] = scanner.nextInt(); 61 | } 62 | 63 | int[] roundedGrades = roundGrades(grades); 64 | 65 | 66 | for (int i = 0; i < n; i++) { 67 | System.out.println(roundedGrades[i]); 68 | } 69 | 70 | scanner.close(); 71 | } 72 | 73 | static int[] roundGrades(int[] grades) { 74 | int[] roundedGrades = new int[grades.length]; 75 | 76 | for (int i = 0; i < grades.length; i++) { 77 | if (grades[i] < 38) { 78 | roundedGrades[i] = grades[i]; 79 | } else { 80 | int nextMultipleOf5 = ((grades[i] + 4) / 5) * 5; 81 | if (nextMultipleOf5 - grades[i] < 3) { 82 | roundedGrades[i] = nextMultipleOf5; 83 | } else { 84 | roundedGrades[i] = grades[i]; 85 | } 86 | } 87 | } 88 | 89 | return roundedGrades; 90 | } 91 | } 92 | 93 | 3) Flipping Bits 94 | 95 | import java.util.Scanner; 96 | 97 | public class Solution { 98 | 99 | public static void main(String[] args) { 100 | Scanner scanner = new Scanner(System.in); 101 | 102 | int T = scanner.nextInt(); 103 | 104 | long mask = (1L << 32) - 1; 105 | 106 | for (int t = 0; t < T; t++) { 107 | long n = scanner.nextLong(); 108 | System.out.println(n ^ mask); 109 | } 110 | 111 | scanner.close(); 112 | 113 | } 114 | 115 | } 116 | 117 | 4) Diagonal Difference 118 | 119 | import java.util.Scanner; 120 | 121 | public class DiagonalDifference { 122 | public static void main(String[] args) { 123 | Scanner scanner = new Scanner(System.in); 124 | int size = scanner.nextInt(); 125 | int[][] matrix = new int[size][size]; 126 | for (int i = 0; i < size; i++) { 127 | for (int j = 0; j < size; j++) { 128 | matrix[i][j] = scanner.nextInt(); 129 | } 130 | } 131 | 132 | int diagonalDifference = calculateDiagonalDifference(matrix); 133 | System.out.println(diagonalDifference); 134 | scanner.close(); 135 | } 136 | 137 | public static int calculateDiagonalDifference(int[][] matrix) { 138 | int leftToRightSum = 0; 139 | int rightToLeftSum = 0; 140 | 141 | for (int i = 0; i < matrix.length; i++) { 142 | leftToRightSum += matrix[i][i]; 143 | rightToLeftSum += matrix[i][matrix.length - 1 - i]; 144 | } 145 | 146 | return Math.abs(leftToRightSum - rightToLeftSum); 147 | } 148 | } 149 | 150 | 5) Counting Storts 151 | 152 | import java.io.*; 153 | import java.util.*; 154 | import java.text.*; 155 | import java.math.*; 156 | import java.util.regex.*; 157 | 158 | public class Solution { 159 | 160 | static void printArray(int[] ar) { 161 | for (int n : ar) { 162 | System.out.print(n + " "); 163 | } 164 | System.out.println(""); 165 | } 166 | 167 | public static void main(String[] args) { 168 | Scanner in = new Scanner(System.in); 169 | int n = in.nextInt(); 170 | int[] ar = new int[100]; 171 | for (int i = 0; i < n; i++) { 172 | ar[in.nextInt()]++; 173 | } 174 | 175 | printArray(ar); 176 | } 177 | 178 | } 179 | 180 | 6) Counting Valleys 181 | 182 | import java.util.Scanner; 183 | 184 | public class CountValleys { 185 | public static void main(String[] args) { 186 | Scanner scanner = new Scanner(System.in); 187 | int steps = scanner.nextInt(); 188 | String path = scanner.next(); 189 | 190 | // Calculate and print the number of valleys walked through 191 | int valleys = countValleys(steps, path); 192 | System.out.println(valleys); 193 | 194 | scanner.close(); 195 | } 196 | private static int countValleys(int steps, String path) { 197 | int level = 0; 198 | int valleys = 0; 199 | 200 | for (char step : path.toCharArray()) { 201 | if (step == 'U') { 202 | level++; 203 | } else if (step == 'D') { 204 | level--; 205 | } 206 | if (step == 'U' && level == 0) { 207 | valleys++; 208 | } 209 | } 210 | 211 | return valleys; 212 | } 213 | } 214 | 215 | 7) Pangrams 216 | 217 | import java.util.HashSet; 218 | import java.util.Scanner; 219 | import java.util.Set; 220 | 221 | public class PangramChecker { 222 | public static void main(String[] args) { 223 | Scanner scanner = new Scanner(System.in); 224 | String sentence = scanner.nextLine(); 225 | boolean isPangram = checkPangram(sentence); 226 | if (isPangram) { 227 | System.out.println("pangram"); 228 | } else { 229 | System.out.println("not pangram"); 230 | } 231 | 232 | scanner.close(); 233 | } 234 | private static boolean checkPangram(String sentence) { 235 | Set alphabetSet = new HashSet<>(); 236 | for (char c : sentence.toLowerCase().toCharArray()) { 237 | if (Character.isLetter(c)) { 238 | alphabetSet.add(c); 239 | } 240 | } 241 | return alphabetSet.size() == 26; 242 | } 243 | } 244 | 245 | 8) Mars Exploration 246 | 247 | import java.util.Scanner; 248 | 249 | public class MarsSOS { 250 | public static void main(String[] args) { 251 | Scanner scanner = new Scanner(System.in); 252 | String signal = scanner.next(); 253 | int alteredLetters = countAlteredLetters(signal); 254 | System.out.println(alteredLetters); 255 | scanner.close(); 256 | } 257 | private static int countAlteredLetters(String signal) { 258 | String sos = "SOS"; 259 | int alteredLetters = 0; 260 | 261 | for (int i = 0; i < signal.length(); i++) { 262 | if (signal.charAt(i) != sos.charAt(i % 3)) { 263 | alteredLetters++; 264 | } 265 | } 266 | return alteredLetters; 267 | } 268 | } 269 | -------------------------------------------------------------------------------- /CSE CORE/HACKER RANK ASSIGNMENT/Week 2/2.txt: -------------------------------------------------------------------------------- 1 | Week 2 - 2 | 3 | 1) Lonely Integer 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | import java.util.Scanner; 8 | 9 | public class UniqueElementFinder { 10 | 11 | public static void main(String[] args) { 12 | Scanner scanner = new Scanner(System.in); 13 | 14 | int n = scanner.nextInt(); 15 | 16 | int[] arr = new int[n]; 17 | 18 | for (int i = 0; i < n; i++) { 19 | arr[i] = scanner.nextInt(); 20 | } 21 | 22 | int uniqueElement = findUniqueElement(arr); 23 | System.out.println(uniqueElement); 24 | 25 | scanner.close(); 26 | } 27 | 28 | static int findUniqueElement(int[] arr) { 29 | Map frequencyMap = new HashMap<>(); 30 | 31 | for (int num : arr) { 32 | frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1); 33 | } 34 | 35 | for (Map.Entry entry : frequencyMap.entrySet()) { 36 | if (entry.getValue() == 1) { 37 | return entry.getKey(); 38 | } 39 | } 40 | 41 | return -1; 42 | } 43 | } 44 | 45 | 2) Grading Students 46 | 47 | import java.util.Scanner; 48 | 49 | public class GradeRounding { 50 | 51 | public static void main(String[] args) { 52 | Scanner scanner = new Scanner(System.in); 53 | 54 | int n = scanner.nextInt(); 55 | 56 | int[] grades = new int[n]; 57 | 58 | 59 | for (int i = 0; i < n; i++) { 60 | grades[i] = scanner.nextInt(); 61 | } 62 | 63 | int[] roundedGrades = roundGrades(grades); 64 | 65 | 66 | for (int i = 0; i < n; i++) { 67 | System.out.println(roundedGrades[i]); 68 | } 69 | 70 | scanner.close(); 71 | } 72 | 73 | static int[] roundGrades(int[] grades) { 74 | int[] roundedGrades = new int[grades.length]; 75 | 76 | for (int i = 0; i < grades.length; i++) { 77 | if (grades[i] < 38) { 78 | roundedGrades[i] = grades[i]; 79 | } else { 80 | int nextMultipleOf5 = ((grades[i] + 4) / 5) * 5; 81 | if (nextMultipleOf5 - grades[i] < 3) { 82 | roundedGrades[i] = nextMultipleOf5; 83 | } else { 84 | roundedGrades[i] = grades[i]; 85 | } 86 | } 87 | } 88 | 89 | return roundedGrades; 90 | } 91 | } 92 | 93 | 3) Flipping Bits 94 | 95 | import java.util.Scanner; 96 | 97 | public class Solution { 98 | 99 | public static void main(String[] args) { 100 | Scanner scanner = new Scanner(System.in); 101 | 102 | int T = scanner.nextInt(); 103 | 104 | long mask = (1L << 32) - 1; 105 | 106 | for (int t = 0; t < T; t++) { 107 | long n = scanner.nextLong(); 108 | System.out.println(n ^ mask); 109 | } 110 | 111 | scanner.close(); 112 | 113 | } 114 | 115 | } 116 | 117 | 4) Diagonal Difference 118 | 119 | import java.util.Scanner; 120 | 121 | public class DiagonalDifference { 122 | public static void main(String[] args) { 123 | Scanner scanner = new Scanner(System.in); 124 | int size = scanner.nextInt(); 125 | int[][] matrix = new int[size][size]; 126 | for (int i = 0; i < size; i++) { 127 | for (int j = 0; j < size; j++) { 128 | matrix[i][j] = scanner.nextInt(); 129 | } 130 | } 131 | 132 | int diagonalDifference = calculateDiagonalDifference(matrix); 133 | System.out.println(diagonalDifference); 134 | scanner.close(); 135 | } 136 | 137 | public static int calculateDiagonalDifference(int[][] matrix) { 138 | int leftToRightSum = 0; 139 | int rightToLeftSum = 0; 140 | 141 | for (int i = 0; i < matrix.length; i++) { 142 | leftToRightSum += matrix[i][i]; 143 | rightToLeftSum += matrix[i][matrix.length - 1 - i]; 144 | } 145 | 146 | return Math.abs(leftToRightSum - rightToLeftSum); 147 | } 148 | } 149 | 150 | 5) Counting Storts 151 | 152 | import java.io.*; 153 | import java.util.*; 154 | import java.text.*; 155 | import java.math.*; 156 | import java.util.regex.*; 157 | 158 | public class Solution { 159 | 160 | static void printArray(int[] ar) { 161 | for (int n : ar) { 162 | System.out.print(n + " "); 163 | } 164 | System.out.println(""); 165 | } 166 | 167 | public static void main(String[] args) { 168 | Scanner in = new Scanner(System.in); 169 | int n = in.nextInt(); 170 | int[] ar = new int[100]; 171 | for (int i = 0; i < n; i++) { 172 | ar[in.nextInt()]++; 173 | } 174 | 175 | printArray(ar); 176 | } 177 | 178 | } 179 | 180 | 6) Counting Valleys 181 | 182 | import java.util.Scanner; 183 | 184 | public class CountValleys { 185 | public static void main(String[] args) { 186 | Scanner scanner = new Scanner(System.in); 187 | int steps = scanner.nextInt(); 188 | String path = scanner.next(); 189 | 190 | // Calculate and print the number of valleys walked through 191 | int valleys = countValleys(steps, path); 192 | System.out.println(valleys); 193 | 194 | scanner.close(); 195 | } 196 | private static int countValleys(int steps, String path) { 197 | int level = 0; 198 | int valleys = 0; 199 | 200 | for (char step : path.toCharArray()) { 201 | if (step == 'U') { 202 | level++; 203 | } else if (step == 'D') { 204 | level--; 205 | } 206 | if (step == 'U' && level == 0) { 207 | valleys++; 208 | } 209 | } 210 | 211 | return valleys; 212 | } 213 | } 214 | 215 | 7) Pangrams 216 | 217 | import java.util.HashSet; 218 | import java.util.Scanner; 219 | import java.util.Set; 220 | 221 | public class PangramChecker { 222 | public static void main(String[] args) { 223 | Scanner scanner = new Scanner(System.in); 224 | String sentence = scanner.nextLine(); 225 | boolean isPangram = checkPangram(sentence); 226 | if (isPangram) { 227 | System.out.println("pangram"); 228 | } else { 229 | System.out.println("not pangram"); 230 | } 231 | 232 | scanner.close(); 233 | } 234 | private static boolean checkPangram(String sentence) { 235 | Set alphabetSet = new HashSet<>(); 236 | for (char c : sentence.toLowerCase().toCharArray()) { 237 | if (Character.isLetter(c)) { 238 | alphabetSet.add(c); 239 | } 240 | } 241 | return alphabetSet.size() == 26; 242 | } 243 | } 244 | 245 | 8) Mars Exploration 246 | 247 | import java.util.Scanner; 248 | 249 | public class MarsSOS { 250 | public static void main(String[] args) { 251 | Scanner scanner = new Scanner(System.in); 252 | String signal = scanner.next(); 253 | int alteredLetters = countAlteredLetters(signal); 254 | System.out.println(alteredLetters); 255 | scanner.close(); 256 | } 257 | private static int countAlteredLetters(String signal) { 258 | String sos = "SOS"; 259 | int alteredLetters = 0; 260 | 261 | for (int i = 0; i < signal.length(); i++) { 262 | if (signal.charAt(i) != sos.charAt(i % 3)) { 263 | alteredLetters++; 264 | } 265 | } 266 | return alteredLetters; 267 | } 268 | } 269 | -------------------------------------------------------------------------------- /CSE (IOT)/HACKER RANK ASSIGNMENT/Week 1/1.txt: -------------------------------------------------------------------------------- 1 | Hacker Rank - 2 | 3 | 1) Plus Minus 4 | 5 | import java.util.Scanner; 6 | 7 | public class UserInputElementRatios { 8 | 9 | public static void main(String[] args) { 10 | Scanner scanner = new Scanner(System.in); 11 | 12 | int n = scanner.nextInt(); 13 | 14 | int[] arr = new int[n]; 15 | 16 | 17 | for (int i = 0; i < n; i++) { 18 | arr[i] = scanner.nextInt(); 19 | } 20 | 21 | calculateRatios(arr); 22 | scanner.close(); 23 | } 24 | 25 | static void calculateRatios(int[] arr) { 26 | int positiveCount = 0; 27 | int negativeCount = 0; 28 | int zeroCount = 0; 29 | 30 | for (int element : arr) { 31 | if (element > 0) { 32 | positiveCount++; 33 | } else if (element < 0) { 34 | negativeCount++; 35 | } else { 36 | zeroCount++; 37 | } 38 | } 39 | 40 | int n = arr.length; 41 | 42 | // Calculate ratios and print with 6 decimal places 43 | System.out.printf("%.6f%n", (double) positiveCount / n); 44 | System.out.printf("%.6f%n", (double) negativeCount / n); 45 | System.out.printf("%.6f%n", (double) zeroCount / n); 46 | } 47 | } 48 | 49 | 50 | 2) MaxMin - 51 | 52 | import java.util.Arrays; 53 | import java.util.Scanner; 54 | 55 | public class UserInputMinMaxSum { 56 | 57 | public static void main(String[] args) { 58 | Scanner scanner = new Scanner(System.in); 59 | int[] arr = new int[5]; 60 | 61 | 62 | for (int i = 0; i < 5; i++) { 63 | arr[i] = scanner.nextInt(); 64 | 65 | while (arr[i] <= 0) { 66 | arr[i] = scanner.nextInt(); 67 | } 68 | } 69 | 70 | findMinMaxSum(arr); 71 | scanner.close(); 72 | } 73 | 74 | static void findMinMaxSum(int[] arr) { 75 | Arrays.sort(arr); 76 | 77 | long minSum = 0; 78 | long maxSum = 0; 79 | 80 | for (int i = 0; i < arr.length - 1; i++) { 81 | minSum += arr[i]; 82 | } 83 | 84 | for (int i = 1; i < arr.length; i++) { 85 | maxSum += arr[i]; 86 | } 87 | 88 | System.out.println(minSum+ " " +maxSum); 89 | } 90 | } 91 | 92 | 3) Time Conversion 93 | 94 | 95 | import java.util.Scanner; 96 | 97 | public class TimeConversion { 98 | 99 | public static void main(String[] args) { 100 | Scanner scanner = new Scanner(System.in); 101 | 102 | String time12Hour = scanner.nextLine(); 103 | 104 | String militaryTime = convertToMilitaryTime(time12Hour); 105 | System.out.println(militaryTime); 106 | 107 | scanner.close(); 108 | } 109 | 110 | static String convertToMilitaryTime(String time12Hour) { 111 | String[] parts = time12Hour.split(":"); 112 | int hours = Integer.parseInt(parts[0]); 113 | int minutes = Integer.parseInt(parts[1]); 114 | int seconds = Integer.parseInt(parts[2].substring(0, 2)); 115 | String amPm = parts[2].substring(2); 116 | 117 | if (amPm.equalsIgnoreCase("PM") && hours != 12) { 118 | hours += 12; 119 | } else if (amPm.equalsIgnoreCase("AM") && hours == 12) { 120 | hours = 0; 121 | } 122 | 123 | return String.format("%02d:%02d:%02d", hours, minutes, seconds); 124 | } 125 | } 126 | 127 | 4) Breaking the Records 128 | 129 | import java.util.Scanner; 130 | 131 | public class BasketballRecord { 132 | 133 | public static void main(String[] args) { 134 | Scanner scanner = new Scanner(System.in); 135 | 136 | int n = scanner.nextInt(); 137 | 138 | int[] scores = new int[n]; 139 | 140 | 141 | for (int i = 0; i < n; i++) { 142 | scores[i] = scanner.nextInt(); 143 | } 144 | 145 | calculateRecordBreaks(scores); 146 | scanner.close(); 147 | } 148 | 149 | static void calculateRecordBreaks(int[] scores) { 150 | int minScore = scores[0]; 151 | int maxScore = scores[0]; 152 | int minBreaks = 0; 153 | int maxBreaks = 0; 154 | 155 | for (int i = 1; i < scores.length; i++) { 156 | if (scores[i] < minScore) { 157 | minScore = scores[i]; 158 | minBreaks++; 159 | } else if (scores[i] > maxScore) { 160 | maxScore = scores[i]; 161 | maxBreaks++; 162 | } 163 | } 164 | 165 | System.out.println(maxBreaks+ " " +minBreaks); 166 | } 167 | } 168 | 169 | 5) Camel Case 4 170 | 171 | import java.io.*; 172 | import java.util.*; 173 | 174 | public class CamelCase { 175 | 176 | public static void main(String[] args) { 177 | Scanner sc = new Scanner(System.in); 178 | while (sc.hasNextLine()) { 179 | String input = sc.nextLine(); 180 | String[] split = input.split(";"); 181 | char op = split[0].charAt(0); 182 | char type = split[1].charAt(0); 183 | String words = split[2]; 184 | char[] wordsArr = words.toCharArray(); 185 | String output = ""; 186 | 187 | if (op == 'S') { 188 | for (char ch: wordsArr) { 189 | if (Character.isUpperCase(ch)) { 190 | output += " " + Character.toLowerCase(ch); 191 | } else if (ch != '(' && ch != ')') { 192 | output += ch; 193 | } 194 | } 195 | } else if (op == 'C') { 196 | for (int i = 0; i < wordsArr.length; i++) { 197 | char currentChar = wordsArr[i]; 198 | if (currentChar != ' ') { 199 | if (i > 0 && wordsArr[i - 1] == ' ') { 200 | currentChar = Character.toUpperCase(currentChar); 201 | } 202 | output += currentChar; 203 | } 204 | } 205 | if (type == 'M') { 206 | output += "()"; 207 | } else if (type == 'C') { 208 | output = output.substring(0, 1).toUpperCase() + output.substring(1); 209 | } 210 | } 211 | 212 | System.out.println(output.trim()); 213 | } 214 | } 215 | } 216 | 217 | 218 | 6) Divisible Sum Pairs 219 | 220 | import java.util.Scanner; 221 | 222 | public class DivisiblePairs { 223 | 224 | public static void main(String[] args) { 225 | Scanner scanner = new Scanner(System.in); 226 | 227 | int n = scanner.nextInt(); 228 | int k = scanner.nextInt(); 229 | 230 | int[] ar = new int[n]; 231 | 232 | 233 | for (int i = 0; i < n; i++) { 234 | ar[i] = scanner.nextInt(); 235 | } 236 | 237 | int result = countDivisiblePairs(ar, k); 238 | System.out.println(result); 239 | 240 | scanner.close(); 241 | } 242 | 243 | static int countDivisiblePairs(int[] ar, int k) { 244 | int count = 0; 245 | 246 | for (int i = 0; i < ar.length - 1; i++) { 247 | for (int j = i + 1; j < ar.length; j++) { 248 | if ((ar[i] + ar[j]) % k == 0) { 249 | count++; 250 | } 251 | } 252 | } 253 | 254 | return count; 255 | } 256 | } 257 | 258 | 7) Sparse Arrays 259 | 260 | import java.util.ArrayList; 261 | import java.util.HashMap; 262 | import java.util.List; 263 | import java.util.Map; 264 | import java.util.Scanner; 265 | 266 | public class StringQueries { 267 | 268 | public static void main(String[] args) { 269 | Scanner scanner = new Scanner(System.in); 270 | 271 | String newline = System.lineSeparator(); 272 | 273 | int n = scanner.nextInt(); 274 | scanner.nextLine(); // Consume the newline character 275 | 276 | List strings = new ArrayList<>(); 277 | 278 | 279 | for (int i = 0; i < n; i++) { 280 | strings.add(scanner.nextLine()); 281 | } 282 | 283 | int m = scanner.nextInt(); 284 | scanner.nextLine(); // Consume the newline character 285 | 286 | List queries = new ArrayList<>(); 287 | 288 | 289 | for (int i = 0; i < m; i++) { 290 | queries.add(scanner.nextLine()); 291 | } 292 | 293 | int[] results = countStringOccurrences(strings, queries); 294 | 295 | 296 | for (int result : results) { 297 | System.out.print(result + newline); 298 | } 299 | 300 | scanner.close(); 301 | } 302 | 303 | static int[] countStringOccurrences(List strings, List queries) { 304 | Map occurrencesMap = new HashMap<>(); 305 | 306 | for (String str : strings) { 307 | occurrencesMap.put(str, occurrencesMap.getOrDefault(str, 0) + 1); 308 | } 309 | 310 | int[] results = new int[queries.size()]; 311 | 312 | for (int i = 0; i < queries.size(); i++) { 313 | results[i] = occurrencesMap.getOrDefault(queries.get(i), 0); 314 | } 315 | 316 | return results; 317 | } 318 | } 319 | -------------------------------------------------------------------------------- /CSE AIML/HACKER RANK ASSIGNMENT/Week 1/1.txt: -------------------------------------------------------------------------------- 1 | Hacker Rank - 2 | 3 | 1) Plus Minus 4 | 5 | import java.util.Scanner; 6 | 7 | public class UserInputElementRatios { 8 | 9 | public static void main(String[] args) { 10 | Scanner scanner = new Scanner(System.in); 11 | 12 | int n = scanner.nextInt(); 13 | 14 | int[] arr = new int[n]; 15 | 16 | 17 | for (int i = 0; i < n; i++) { 18 | arr[i] = scanner.nextInt(); 19 | } 20 | 21 | calculateRatios(arr); 22 | scanner.close(); 23 | } 24 | 25 | static void calculateRatios(int[] arr) { 26 | int positiveCount = 0; 27 | int negativeCount = 0; 28 | int zeroCount = 0; 29 | 30 | for (int element : arr) { 31 | if (element > 0) { 32 | positiveCount++; 33 | } else if (element < 0) { 34 | negativeCount++; 35 | } else { 36 | zeroCount++; 37 | } 38 | } 39 | 40 | int n = arr.length; 41 | 42 | // Calculate ratios and print with 6 decimal places 43 | System.out.printf("%.6f%n", (double) positiveCount / n); 44 | System.out.printf("%.6f%n", (double) negativeCount / n); 45 | System.out.printf("%.6f%n", (double) zeroCount / n); 46 | } 47 | } 48 | 49 | 50 | 2) MaxMin - 51 | 52 | import java.util.Arrays; 53 | import java.util.Scanner; 54 | 55 | public class UserInputMinMaxSum { 56 | 57 | public static void main(String[] args) { 58 | Scanner scanner = new Scanner(System.in); 59 | int[] arr = new int[5]; 60 | 61 | 62 | for (int i = 0; i < 5; i++) { 63 | arr[i] = scanner.nextInt(); 64 | 65 | while (arr[i] <= 0) { 66 | arr[i] = scanner.nextInt(); 67 | } 68 | } 69 | 70 | findMinMaxSum(arr); 71 | scanner.close(); 72 | } 73 | 74 | static void findMinMaxSum(int[] arr) { 75 | Arrays.sort(arr); 76 | 77 | long minSum = 0; 78 | long maxSum = 0; 79 | 80 | for (int i = 0; i < arr.length - 1; i++) { 81 | minSum += arr[i]; 82 | } 83 | 84 | for (int i = 1; i < arr.length; i++) { 85 | maxSum += arr[i]; 86 | } 87 | 88 | System.out.println(minSum+ " " +maxSum); 89 | } 90 | } 91 | 92 | 3) Time Conversion 93 | 94 | 95 | import java.util.Scanner; 96 | 97 | public class TimeConversion { 98 | 99 | public static void main(String[] args) { 100 | Scanner scanner = new Scanner(System.in); 101 | 102 | String time12Hour = scanner.nextLine(); 103 | 104 | String militaryTime = convertToMilitaryTime(time12Hour); 105 | System.out.println(militaryTime); 106 | 107 | scanner.close(); 108 | } 109 | 110 | static String convertToMilitaryTime(String time12Hour) { 111 | String[] parts = time12Hour.split(":"); 112 | int hours = Integer.parseInt(parts[0]); 113 | int minutes = Integer.parseInt(parts[1]); 114 | int seconds = Integer.parseInt(parts[2].substring(0, 2)); 115 | String amPm = parts[2].substring(2); 116 | 117 | if (amPm.equalsIgnoreCase("PM") && hours != 12) { 118 | hours += 12; 119 | } else if (amPm.equalsIgnoreCase("AM") && hours == 12) { 120 | hours = 0; 121 | } 122 | 123 | return String.format("%02d:%02d:%02d", hours, minutes, seconds); 124 | } 125 | } 126 | 127 | 4) Breaking the Records 128 | 129 | import java.util.Scanner; 130 | 131 | public class BasketballRecord { 132 | 133 | public static void main(String[] args) { 134 | Scanner scanner = new Scanner(System.in); 135 | 136 | int n = scanner.nextInt(); 137 | 138 | int[] scores = new int[n]; 139 | 140 | 141 | for (int i = 0; i < n; i++) { 142 | scores[i] = scanner.nextInt(); 143 | } 144 | 145 | calculateRecordBreaks(scores); 146 | scanner.close(); 147 | } 148 | 149 | static void calculateRecordBreaks(int[] scores) { 150 | int minScore = scores[0]; 151 | int maxScore = scores[0]; 152 | int minBreaks = 0; 153 | int maxBreaks = 0; 154 | 155 | for (int i = 1; i < scores.length; i++) { 156 | if (scores[i] < minScore) { 157 | minScore = scores[i]; 158 | minBreaks++; 159 | } else if (scores[i] > maxScore) { 160 | maxScore = scores[i]; 161 | maxBreaks++; 162 | } 163 | } 164 | 165 | System.out.println(maxBreaks+ " " +minBreaks); 166 | } 167 | } 168 | 169 | 5) Camel Case 4 170 | 171 | import java.io.*; 172 | import java.util.*; 173 | 174 | public class CamelCase { 175 | 176 | public static void main(String[] args) { 177 | Scanner sc = new Scanner(System.in); 178 | while (sc.hasNextLine()) { 179 | String input = sc.nextLine(); 180 | String[] split = input.split(";"); 181 | char op = split[0].charAt(0); 182 | char type = split[1].charAt(0); 183 | String words = split[2]; 184 | char[] wordsArr = words.toCharArray(); 185 | String output = ""; 186 | 187 | if (op == 'S') { 188 | for (char ch: wordsArr) { 189 | if (Character.isUpperCase(ch)) { 190 | output += " " + Character.toLowerCase(ch); 191 | } else if (ch != '(' && ch != ')') { 192 | output += ch; 193 | } 194 | } 195 | } else if (op == 'C') { 196 | for (int i = 0; i < wordsArr.length; i++) { 197 | char currentChar = wordsArr[i]; 198 | if (currentChar != ' ') { 199 | if (i > 0 && wordsArr[i - 1] == ' ') { 200 | currentChar = Character.toUpperCase(currentChar); 201 | } 202 | output += currentChar; 203 | } 204 | } 205 | if (type == 'M') { 206 | output += "()"; 207 | } else if (type == 'C') { 208 | output = output.substring(0, 1).toUpperCase() + output.substring(1); 209 | } 210 | } 211 | 212 | System.out.println(output.trim()); 213 | } 214 | } 215 | } 216 | 217 | 218 | 6) Divisible Sum Pairs 219 | 220 | import java.util.Scanner; 221 | 222 | public class DivisiblePairs { 223 | 224 | public static void main(String[] args) { 225 | Scanner scanner = new Scanner(System.in); 226 | 227 | int n = scanner.nextInt(); 228 | int k = scanner.nextInt(); 229 | 230 | int[] ar = new int[n]; 231 | 232 | 233 | for (int i = 0; i < n; i++) { 234 | ar[i] = scanner.nextInt(); 235 | } 236 | 237 | int result = countDivisiblePairs(ar, k); 238 | System.out.println(result); 239 | 240 | scanner.close(); 241 | } 242 | 243 | static int countDivisiblePairs(int[] ar, int k) { 244 | int count = 0; 245 | 246 | for (int i = 0; i < ar.length - 1; i++) { 247 | for (int j = i + 1; j < ar.length; j++) { 248 | if ((ar[i] + ar[j]) % k == 0) { 249 | count++; 250 | } 251 | } 252 | } 253 | 254 | return count; 255 | } 256 | } 257 | 258 | 7) Sparse Arrays 259 | 260 | import java.util.ArrayList; 261 | import java.util.HashMap; 262 | import java.util.List; 263 | import java.util.Map; 264 | import java.util.Scanner; 265 | 266 | public class StringQueries { 267 | 268 | public static void main(String[] args) { 269 | Scanner scanner = new Scanner(System.in); 270 | 271 | String newline = System.lineSeparator(); 272 | 273 | int n = scanner.nextInt(); 274 | scanner.nextLine(); // Consume the newline character 275 | 276 | List strings = new ArrayList<>(); 277 | 278 | 279 | for (int i = 0; i < n; i++) { 280 | strings.add(scanner.nextLine()); 281 | } 282 | 283 | int m = scanner.nextInt(); 284 | scanner.nextLine(); // Consume the newline character 285 | 286 | List queries = new ArrayList<>(); 287 | 288 | 289 | for (int i = 0; i < m; i++) { 290 | queries.add(scanner.nextLine()); 291 | } 292 | 293 | int[] results = countStringOccurrences(strings, queries); 294 | 295 | 296 | for (int result : results) { 297 | System.out.print(result + newline); 298 | } 299 | 300 | scanner.close(); 301 | } 302 | 303 | static int[] countStringOccurrences(List strings, List queries) { 304 | Map occurrencesMap = new HashMap<>(); 305 | 306 | for (String str : strings) { 307 | occurrencesMap.put(str, occurrencesMap.getOrDefault(str, 0) + 1); 308 | } 309 | 310 | int[] results = new int[queries.size()]; 311 | 312 | for (int i = 0; i < queries.size(); i++) { 313 | results[i] = occurrencesMap.getOrDefault(queries.get(i), 0); 314 | } 315 | 316 | return results; 317 | } 318 | } 319 | -------------------------------------------------------------------------------- /CSE CORE/HACKER RANK ASSIGNMENT/Week 1/1.txt: -------------------------------------------------------------------------------- 1 | Hacker Rank - 2 | 3 | 1) Plus Minus 4 | 5 | import java.util.Scanner; 6 | 7 | public class UserInputElementRatios { 8 | 9 | public static void main(String[] args) { 10 | Scanner scanner = new Scanner(System.in); 11 | 12 | int n = scanner.nextInt(); 13 | 14 | int[] arr = new int[n]; 15 | 16 | 17 | for (int i = 0; i < n; i++) { 18 | arr[i] = scanner.nextInt(); 19 | } 20 | 21 | calculateRatios(arr); 22 | scanner.close(); 23 | } 24 | 25 | static void calculateRatios(int[] arr) { 26 | int positiveCount = 0; 27 | int negativeCount = 0; 28 | int zeroCount = 0; 29 | 30 | for (int element : arr) { 31 | if (element > 0) { 32 | positiveCount++; 33 | } else if (element < 0) { 34 | negativeCount++; 35 | } else { 36 | zeroCount++; 37 | } 38 | } 39 | 40 | int n = arr.length; 41 | 42 | // Calculate ratios and print with 6 decimal places 43 | System.out.printf("%.6f%n", (double) positiveCount / n); 44 | System.out.printf("%.6f%n", (double) negativeCount / n); 45 | System.out.printf("%.6f%n", (double) zeroCount / n); 46 | } 47 | } 48 | 49 | 50 | 2) MaxMin - 51 | 52 | import java.util.Arrays; 53 | import java.util.Scanner; 54 | 55 | public class UserInputMinMaxSum { 56 | 57 | public static void main(String[] args) { 58 | Scanner scanner = new Scanner(System.in); 59 | int[] arr = new int[5]; 60 | 61 | 62 | for (int i = 0; i < 5; i++) { 63 | arr[i] = scanner.nextInt(); 64 | 65 | while (arr[i] <= 0) { 66 | arr[i] = scanner.nextInt(); 67 | } 68 | } 69 | 70 | findMinMaxSum(arr); 71 | scanner.close(); 72 | } 73 | 74 | static void findMinMaxSum(int[] arr) { 75 | Arrays.sort(arr); 76 | 77 | long minSum = 0; 78 | long maxSum = 0; 79 | 80 | for (int i = 0; i < arr.length - 1; i++) { 81 | minSum += arr[i]; 82 | } 83 | 84 | for (int i = 1; i < arr.length; i++) { 85 | maxSum += arr[i]; 86 | } 87 | 88 | System.out.println(minSum+ " " +maxSum); 89 | } 90 | } 91 | 92 | 3) Time Conversion 93 | 94 | 95 | import java.util.Scanner; 96 | 97 | public class TimeConversion { 98 | 99 | public static void main(String[] args) { 100 | Scanner scanner = new Scanner(System.in); 101 | 102 | String time12Hour = scanner.nextLine(); 103 | 104 | String militaryTime = convertToMilitaryTime(time12Hour); 105 | System.out.println(militaryTime); 106 | 107 | scanner.close(); 108 | } 109 | 110 | static String convertToMilitaryTime(String time12Hour) { 111 | String[] parts = time12Hour.split(":"); 112 | int hours = Integer.parseInt(parts[0]); 113 | int minutes = Integer.parseInt(parts[1]); 114 | int seconds = Integer.parseInt(parts[2].substring(0, 2)); 115 | String amPm = parts[2].substring(2); 116 | 117 | if (amPm.equalsIgnoreCase("PM") && hours != 12) { 118 | hours += 12; 119 | } else if (amPm.equalsIgnoreCase("AM") && hours == 12) { 120 | hours = 0; 121 | } 122 | 123 | return String.format("%02d:%02d:%02d", hours, minutes, seconds); 124 | } 125 | } 126 | 127 | 4) Breaking the Records 128 | 129 | import java.util.Scanner; 130 | 131 | public class BasketballRecord { 132 | 133 | public static void main(String[] args) { 134 | Scanner scanner = new Scanner(System.in); 135 | 136 | int n = scanner.nextInt(); 137 | 138 | int[] scores = new int[n]; 139 | 140 | 141 | for (int i = 0; i < n; i++) { 142 | scores[i] = scanner.nextInt(); 143 | } 144 | 145 | calculateRecordBreaks(scores); 146 | scanner.close(); 147 | } 148 | 149 | static void calculateRecordBreaks(int[] scores) { 150 | int minScore = scores[0]; 151 | int maxScore = scores[0]; 152 | int minBreaks = 0; 153 | int maxBreaks = 0; 154 | 155 | for (int i = 1; i < scores.length; i++) { 156 | if (scores[i] < minScore) { 157 | minScore = scores[i]; 158 | minBreaks++; 159 | } else if (scores[i] > maxScore) { 160 | maxScore = scores[i]; 161 | maxBreaks++; 162 | } 163 | } 164 | 165 | System.out.println(maxBreaks+ " " +minBreaks); 166 | } 167 | } 168 | 169 | 5) Camel Case 4 170 | 171 | import java.io.*; 172 | import java.util.*; 173 | 174 | public class CamelCase { 175 | 176 | public static void main(String[] args) { 177 | Scanner sc = new Scanner(System.in); 178 | while (sc.hasNextLine()) { 179 | String input = sc.nextLine(); 180 | String[] split = input.split(";"); 181 | char op = split[0].charAt(0); 182 | char type = split[1].charAt(0); 183 | String words = split[2]; 184 | char[] wordsArr = words.toCharArray(); 185 | String output = ""; 186 | 187 | if (op == 'S') { 188 | for (char ch: wordsArr) { 189 | if (Character.isUpperCase(ch)) { 190 | output += " " + Character.toLowerCase(ch); 191 | } else if (ch != '(' && ch != ')') { 192 | output += ch; 193 | } 194 | } 195 | } else if (op == 'C') { 196 | for (int i = 0; i < wordsArr.length; i++) { 197 | char currentChar = wordsArr[i]; 198 | if (currentChar != ' ') { 199 | if (i > 0 && wordsArr[i - 1] == ' ') { 200 | currentChar = Character.toUpperCase(currentChar); 201 | } 202 | output += currentChar; 203 | } 204 | } 205 | if (type == 'M') { 206 | output += "()"; 207 | } else if (type == 'C') { 208 | output = output.substring(0, 1).toUpperCase() + output.substring(1); 209 | } 210 | } 211 | 212 | System.out.println(output.trim()); 213 | } 214 | } 215 | } 216 | 217 | 218 | 6) Divisible Sum Pairs 219 | 220 | import java.util.Scanner; 221 | 222 | public class DivisiblePairs { 223 | 224 | public static void main(String[] args) { 225 | Scanner scanner = new Scanner(System.in); 226 | 227 | int n = scanner.nextInt(); 228 | int k = scanner.nextInt(); 229 | 230 | int[] ar = new int[n]; 231 | 232 | 233 | for (int i = 0; i < n; i++) { 234 | ar[i] = scanner.nextInt(); 235 | } 236 | 237 | int result = countDivisiblePairs(ar, k); 238 | System.out.println(result); 239 | 240 | scanner.close(); 241 | } 242 | 243 | static int countDivisiblePairs(int[] ar, int k) { 244 | int count = 0; 245 | 246 | for (int i = 0; i < ar.length - 1; i++) { 247 | for (int j = i + 1; j < ar.length; j++) { 248 | if ((ar[i] + ar[j]) % k == 0) { 249 | count++; 250 | } 251 | } 252 | } 253 | 254 | return count; 255 | } 256 | } 257 | 258 | 7) Sparse Arrays 259 | 260 | import java.util.ArrayList; 261 | import java.util.HashMap; 262 | import java.util.List; 263 | import java.util.Map; 264 | import java.util.Scanner; 265 | 266 | public class StringQueries { 267 | 268 | public static void main(String[] args) { 269 | Scanner scanner = new Scanner(System.in); 270 | 271 | String newline = System.lineSeparator(); 272 | 273 | int n = scanner.nextInt(); 274 | scanner.nextLine(); // Consume the newline character 275 | 276 | List strings = new ArrayList<>(); 277 | 278 | 279 | for (int i = 0; i < n; i++) { 280 | strings.add(scanner.nextLine()); 281 | } 282 | 283 | int m = scanner.nextInt(); 284 | scanner.nextLine(); // Consume the newline character 285 | 286 | List queries = new ArrayList<>(); 287 | 288 | 289 | for (int i = 0; i < m; i++) { 290 | queries.add(scanner.nextLine()); 291 | } 292 | 293 | int[] results = countStringOccurrences(strings, queries); 294 | 295 | 296 | for (int result : results) { 297 | System.out.print(result + newline); 298 | } 299 | 300 | scanner.close(); 301 | } 302 | 303 | static int[] countStringOccurrences(List strings, List queries) { 304 | Map occurrencesMap = new HashMap<>(); 305 | 306 | for (String str : strings) { 307 | occurrencesMap.put(str, occurrencesMap.getOrDefault(str, 0) + 1); 308 | } 309 | 310 | int[] results = new int[queries.size()]; 311 | 312 | for (int i = 0; i < queries.size(); i++) { 313 | results[i] = occurrencesMap.getOrDefault(queries.get(i), 0); 314 | } 315 | 316 | return results; 317 | } 318 | } 319 | -------------------------------------------------------------------------------- /CSE (IOT)/HACKER RANK ASSIGNMENT/Week 4/4.txt: -------------------------------------------------------------------------------- 1 | Week 4 - 2 | 3 | 1) Picking Numbers 4 | 5 | import java.io.*; 6 | import java.util.*; 7 | import java.text.*; 8 | import java.math.*; 9 | import java.util.regex.*; 10 | 11 | public class Solution { 12 | 13 | public static void main(String[] args) { 14 | Scanner in = new Scanner(System.in); 15 | int n = in.nextInt(); 16 | int[] a = new int[n]; 17 | for(int a_i=0; a_i < n; a_i++){ 18 | a[a_i] = in.nextInt(); 19 | } 20 | 21 | int[] freq = new int[100]; 22 | for(int i = 0; i < a.length; ++i) 23 | { 24 | freq[a[i]]++; 25 | } 26 | 27 | int curBest = 0; 28 | for(int i = 0; i < 99; ++i) 29 | { 30 | curBest = Math.max(curBest, freq[i]+freq[i+1]); 31 | } 32 | System.out.println(curBest); 33 | } 34 | } 35 | 36 | 2) Left Rotation 37 | 38 | import java.io.*; 39 | import java.util.*; 40 | 41 | public class Solution { 42 | 43 | public static void main(String[] args) { 44 | Scanner scan = new Scanner(System.in); 45 | int N = scan.nextInt(); 46 | int n = scan.nextInt(); 47 | int[] A = new int[N]; 48 | for (int i=0; ix2) 86 | { 87 | if(v1x1) 101 | { 102 | System.out.println("NO"); 103 | break; 104 | } 105 | } 106 | } 107 | } 108 | else 109 | System.out.println("NO"); 110 | 111 | } 112 | else if(x1v2) 115 | { 116 | for(;;) 117 | { 118 | x1=x1+v1; 119 | x2=x2+v2; 120 | if (x1==x2) 121 | { 122 | System.out.println("YES"); 123 | break; 124 | } 125 | else 126 | { 127 | if (x1>x2) 128 | { 129 | System.out.println("NO"); 130 | break; 131 | } 132 | } 133 | } 134 | } 135 | else 136 | System.out.println("NO"); 137 | } 138 | else 139 | { 140 | if(v1==v2) 141 | System.out.println("YES"); 142 | else 143 | System.out.println("NO"); 144 | } 145 | } 146 | } 147 | 148 | 4) Separate the Numbers 149 | 150 | import java.io.*; 151 | import java.util.StringTokenizer; 152 | 153 | 154 | public class Main { 155 | private void solve() { 156 | int n = rw.nextInt(); 157 | main: 158 | for (int i = 0; i < n; ++i) { 159 | String s = rw.next(); 160 | if (s.startsWith("0") || s.length() == 1) { 161 | rw.println("NO"); 162 | continue; 163 | } 164 | long x, cur; 165 | cy: 166 | for (int j = 1; j <= s.length() / 2; ++j) { 167 | x = Long.parseLong(s.substring(0, j)); 168 | cur = x + 1; 169 | int c = j; 170 | while (c < s.length()) { 171 | String p = String.valueOf(cur); 172 | cur += 1; 173 | if (s.startsWith(p, c)) { 174 | c += p.length(); 175 | } else { 176 | continue cy; 177 | } 178 | } 179 | rw.println("YES" + " " + x); 180 | continue main; 181 | } 182 | rw.println("NO"); 183 | } 184 | 185 | } 186 | 187 | private RW rw; 188 | private String FILE_NAME = "file"; 189 | 190 | public static void main(String[] args) { 191 | new Main().run(); 192 | } 193 | 194 | private void run() { 195 | rw = new RW(FILE_NAME + ".in", FILE_NAME + ".out"); 196 | solve(); 197 | rw.close(); 198 | } 199 | 200 | private class RW { 201 | private StringTokenizer st; 202 | private PrintWriter out; 203 | private BufferedReader br; 204 | private boolean eof; 205 | 206 | RW(String inputFile, String outputFile) { 207 | br = new BufferedReader(new InputStreamReader(System.in)); 208 | out = new PrintWriter(new OutputStreamWriter(System.out)); 209 | 210 | File f = new File(inputFile); 211 | if (f.exists() && f.canRead()) { 212 | try { 213 | br = new BufferedReader(new FileReader(inputFile)); 214 | out = new PrintWriter(new FileWriter(outputFile)); 215 | } catch (IOException e) { 216 | e.printStackTrace(); 217 | } 218 | } 219 | } 220 | 221 | private String nextLine() { 222 | String s = ""; 223 | try { 224 | s = br.readLine(); 225 | } catch (IOException e) { 226 | e.printStackTrace(); 227 | } 228 | return s; 229 | } 230 | 231 | private String next() { 232 | while (st == null || !st.hasMoreTokens()) { 233 | try { 234 | st = new StringTokenizer(br.readLine()); 235 | } catch (IOException e) { 236 | eof = true; 237 | return "-1"; 238 | } 239 | } 240 | return st.nextToken(); 241 | } 242 | 243 | private long nextLong() { 244 | return Long.parseLong(next()); 245 | } 246 | 247 | private int nextInt() { 248 | return Integer.parseInt(next()); 249 | } 250 | 251 | private void println() { 252 | out.println(); 253 | } 254 | 255 | private void println(Object o) { 256 | out.println(o); 257 | } 258 | 259 | private void print(Object o) { 260 | out.print(o); 261 | } 262 | 263 | private void close() { 264 | try { 265 | br.close(); 266 | } catch (IOException e) { 267 | e.printStackTrace(); 268 | } 269 | out.close(); 270 | } 271 | } 272 | } 273 | 274 | 5) Closest Numbers 275 | 276 | import java.io.*; 277 | import java.util.*; 278 | import java.text.*; 279 | import java.math.*; 280 | import java.util.regex.*; 281 | 282 | public class Solution { 283 | 284 | public static void main(String[] args) { 285 | Scanner in = new Scanner(System.in); 286 | int n = in.nextInt(); 287 | int[] dat = new int[n]; 288 | for(int i = 0; i < n; i++) { 289 | dat[i] = in.nextInt(); 290 | } 291 | Arrays.sort(dat); 292 | int minDiff = Integer.MAX_VALUE; 293 | String out = ""; 294 | for(int i = 0; i < n - 1; i++) { 295 | if(dat[i + 1] - dat[i] <= minDiff) { 296 | if(dat[i + 1] - dat[i] == minDiff) { 297 | out += " " + dat[i] + " " + dat[i + 1]; 298 | } else { 299 | out = dat[i] + " " + dat[i + 1]; 300 | } 301 | minDiff = dat[i + 1] - dat[i]; 302 | } 303 | } 304 | System.out.println(out); 305 | } 306 | } 307 | 308 | 6) Tower Breakers 309 | 310 | import java.io.*; 311 | import java.util.*; 312 | import java.text.*; 313 | import java.math.*; 314 | import java.util.regex.*; 315 | 316 | public class Solution { 317 | 318 | private static int numPrimeFactors(int n) { 319 | int answer = 0; 320 | for (int i=2; i<=n; i++) { 321 | if (n%i == 0) { 322 | answer++; 323 | n /= i; 324 | i = 1; 325 | } 326 | } 327 | return answer; 328 | } 329 | 330 | public static void main(String[] args) { 331 | Scanner sc = new Scanner(System.in); 332 | int T = sc.nextInt(); 333 | for (int t=0; t= 'a') base = 'a'; 407 | return (char)(((c - base + shift) % 26) + base); 408 | } 409 | } -------------------------------------------------------------------------------- /CSE AIML/HACKER RANK ASSIGNMENT/Week 4/4.txt: -------------------------------------------------------------------------------- 1 | Week 4 - 2 | 3 | 1) Picking Numbers 4 | 5 | import java.io.*; 6 | import java.util.*; 7 | import java.text.*; 8 | import java.math.*; 9 | import java.util.regex.*; 10 | 11 | public class Solution { 12 | 13 | public static void main(String[] args) { 14 | Scanner in = new Scanner(System.in); 15 | int n = in.nextInt(); 16 | int[] a = new int[n]; 17 | for(int a_i=0; a_i < n; a_i++){ 18 | a[a_i] = in.nextInt(); 19 | } 20 | 21 | int[] freq = new int[100]; 22 | for(int i = 0; i < a.length; ++i) 23 | { 24 | freq[a[i]]++; 25 | } 26 | 27 | int curBest = 0; 28 | for(int i = 0; i < 99; ++i) 29 | { 30 | curBest = Math.max(curBest, freq[i]+freq[i+1]); 31 | } 32 | System.out.println(curBest); 33 | } 34 | } 35 | 36 | 2) Left Rotation 37 | 38 | import java.io.*; 39 | import java.util.*; 40 | 41 | public class Solution { 42 | 43 | public static void main(String[] args) { 44 | Scanner scan = new Scanner(System.in); 45 | int N = scan.nextInt(); 46 | int n = scan.nextInt(); 47 | int[] A = new int[N]; 48 | for (int i=0; ix2) 86 | { 87 | if(v1x1) 101 | { 102 | System.out.println("NO"); 103 | break; 104 | } 105 | } 106 | } 107 | } 108 | else 109 | System.out.println("NO"); 110 | 111 | } 112 | else if(x1v2) 115 | { 116 | for(;;) 117 | { 118 | x1=x1+v1; 119 | x2=x2+v2; 120 | if (x1==x2) 121 | { 122 | System.out.println("YES"); 123 | break; 124 | } 125 | else 126 | { 127 | if (x1>x2) 128 | { 129 | System.out.println("NO"); 130 | break; 131 | } 132 | } 133 | } 134 | } 135 | else 136 | System.out.println("NO"); 137 | } 138 | else 139 | { 140 | if(v1==v2) 141 | System.out.println("YES"); 142 | else 143 | System.out.println("NO"); 144 | } 145 | } 146 | } 147 | 148 | 4) Separate the Numbers 149 | 150 | import java.io.*; 151 | import java.util.StringTokenizer; 152 | 153 | 154 | public class Main { 155 | private void solve() { 156 | int n = rw.nextInt(); 157 | main: 158 | for (int i = 0; i < n; ++i) { 159 | String s = rw.next(); 160 | if (s.startsWith("0") || s.length() == 1) { 161 | rw.println("NO"); 162 | continue; 163 | } 164 | long x, cur; 165 | cy: 166 | for (int j = 1; j <= s.length() / 2; ++j) { 167 | x = Long.parseLong(s.substring(0, j)); 168 | cur = x + 1; 169 | int c = j; 170 | while (c < s.length()) { 171 | String p = String.valueOf(cur); 172 | cur += 1; 173 | if (s.startsWith(p, c)) { 174 | c += p.length(); 175 | } else { 176 | continue cy; 177 | } 178 | } 179 | rw.println("YES" + " " + x); 180 | continue main; 181 | } 182 | rw.println("NO"); 183 | } 184 | 185 | } 186 | 187 | private RW rw; 188 | private String FILE_NAME = "file"; 189 | 190 | public static void main(String[] args) { 191 | new Main().run(); 192 | } 193 | 194 | private void run() { 195 | rw = new RW(FILE_NAME + ".in", FILE_NAME + ".out"); 196 | solve(); 197 | rw.close(); 198 | } 199 | 200 | private class RW { 201 | private StringTokenizer st; 202 | private PrintWriter out; 203 | private BufferedReader br; 204 | private boolean eof; 205 | 206 | RW(String inputFile, String outputFile) { 207 | br = new BufferedReader(new InputStreamReader(System.in)); 208 | out = new PrintWriter(new OutputStreamWriter(System.out)); 209 | 210 | File f = new File(inputFile); 211 | if (f.exists() && f.canRead()) { 212 | try { 213 | br = new BufferedReader(new FileReader(inputFile)); 214 | out = new PrintWriter(new FileWriter(outputFile)); 215 | } catch (IOException e) { 216 | e.printStackTrace(); 217 | } 218 | } 219 | } 220 | 221 | private String nextLine() { 222 | String s = ""; 223 | try { 224 | s = br.readLine(); 225 | } catch (IOException e) { 226 | e.printStackTrace(); 227 | } 228 | return s; 229 | } 230 | 231 | private String next() { 232 | while (st == null || !st.hasMoreTokens()) { 233 | try { 234 | st = new StringTokenizer(br.readLine()); 235 | } catch (IOException e) { 236 | eof = true; 237 | return "-1"; 238 | } 239 | } 240 | return st.nextToken(); 241 | } 242 | 243 | private long nextLong() { 244 | return Long.parseLong(next()); 245 | } 246 | 247 | private int nextInt() { 248 | return Integer.parseInt(next()); 249 | } 250 | 251 | private void println() { 252 | out.println(); 253 | } 254 | 255 | private void println(Object o) { 256 | out.println(o); 257 | } 258 | 259 | private void print(Object o) { 260 | out.print(o); 261 | } 262 | 263 | private void close() { 264 | try { 265 | br.close(); 266 | } catch (IOException e) { 267 | e.printStackTrace(); 268 | } 269 | out.close(); 270 | } 271 | } 272 | } 273 | 274 | 5) Closest Numbers 275 | 276 | import java.io.*; 277 | import java.util.*; 278 | import java.text.*; 279 | import java.math.*; 280 | import java.util.regex.*; 281 | 282 | public class Solution { 283 | 284 | public static void main(String[] args) { 285 | Scanner in = new Scanner(System.in); 286 | int n = in.nextInt(); 287 | int[] dat = new int[n]; 288 | for(int i = 0; i < n; i++) { 289 | dat[i] = in.nextInt(); 290 | } 291 | Arrays.sort(dat); 292 | int minDiff = Integer.MAX_VALUE; 293 | String out = ""; 294 | for(int i = 0; i < n - 1; i++) { 295 | if(dat[i + 1] - dat[i] <= minDiff) { 296 | if(dat[i + 1] - dat[i] == minDiff) { 297 | out += " " + dat[i] + " " + dat[i + 1]; 298 | } else { 299 | out = dat[i] + " " + dat[i + 1]; 300 | } 301 | minDiff = dat[i + 1] - dat[i]; 302 | } 303 | } 304 | System.out.println(out); 305 | } 306 | } 307 | 308 | 6) Tower Breakers 309 | 310 | import java.io.*; 311 | import java.util.*; 312 | import java.text.*; 313 | import java.math.*; 314 | import java.util.regex.*; 315 | 316 | public class Solution { 317 | 318 | private static int numPrimeFactors(int n) { 319 | int answer = 0; 320 | for (int i=2; i<=n; i++) { 321 | if (n%i == 0) { 322 | answer++; 323 | n /= i; 324 | i = 1; 325 | } 326 | } 327 | return answer; 328 | } 329 | 330 | public static void main(String[] args) { 331 | Scanner sc = new Scanner(System.in); 332 | int T = sc.nextInt(); 333 | for (int t=0; t= 'a') base = 'a'; 407 | return (char)(((c - base + shift) % 26) + base); 408 | } 409 | } -------------------------------------------------------------------------------- /CSE CORE/HACKER RANK ASSIGNMENT/Week 4/4.txt: -------------------------------------------------------------------------------- 1 | Week 4 - 2 | 3 | 1) Picking Numbers 4 | 5 | import java.io.*; 6 | import java.util.*; 7 | import java.text.*; 8 | import java.math.*; 9 | import java.util.regex.*; 10 | 11 | public class Solution { 12 | 13 | public static void main(String[] args) { 14 | Scanner in = new Scanner(System.in); 15 | int n = in.nextInt(); 16 | int[] a = new int[n]; 17 | for(int a_i=0; a_i < n; a_i++){ 18 | a[a_i] = in.nextInt(); 19 | } 20 | 21 | int[] freq = new int[100]; 22 | for(int i = 0; i < a.length; ++i) 23 | { 24 | freq[a[i]]++; 25 | } 26 | 27 | int curBest = 0; 28 | for(int i = 0; i < 99; ++i) 29 | { 30 | curBest = Math.max(curBest, freq[i]+freq[i+1]); 31 | } 32 | System.out.println(curBest); 33 | } 34 | } 35 | 36 | 2) Left Rotation 37 | 38 | import java.io.*; 39 | import java.util.*; 40 | 41 | public class Solution { 42 | 43 | public static void main(String[] args) { 44 | Scanner scan = new Scanner(System.in); 45 | int N = scan.nextInt(); 46 | int n = scan.nextInt(); 47 | int[] A = new int[N]; 48 | for (int i=0; ix2) 86 | { 87 | if(v1x1) 101 | { 102 | System.out.println("NO"); 103 | break; 104 | } 105 | } 106 | } 107 | } 108 | else 109 | System.out.println("NO"); 110 | 111 | } 112 | else if(x1v2) 115 | { 116 | for(;;) 117 | { 118 | x1=x1+v1; 119 | x2=x2+v2; 120 | if (x1==x2) 121 | { 122 | System.out.println("YES"); 123 | break; 124 | } 125 | else 126 | { 127 | if (x1>x2) 128 | { 129 | System.out.println("NO"); 130 | break; 131 | } 132 | } 133 | } 134 | } 135 | else 136 | System.out.println("NO"); 137 | } 138 | else 139 | { 140 | if(v1==v2) 141 | System.out.println("YES"); 142 | else 143 | System.out.println("NO"); 144 | } 145 | } 146 | } 147 | 148 | 4) Separate the Numbers 149 | 150 | import java.io.*; 151 | import java.util.StringTokenizer; 152 | 153 | 154 | public class Main { 155 | private void solve() { 156 | int n = rw.nextInt(); 157 | main: 158 | for (int i = 0; i < n; ++i) { 159 | String s = rw.next(); 160 | if (s.startsWith("0") || s.length() == 1) { 161 | rw.println("NO"); 162 | continue; 163 | } 164 | long x, cur; 165 | cy: 166 | for (int j = 1; j <= s.length() / 2; ++j) { 167 | x = Long.parseLong(s.substring(0, j)); 168 | cur = x + 1; 169 | int c = j; 170 | while (c < s.length()) { 171 | String p = String.valueOf(cur); 172 | cur += 1; 173 | if (s.startsWith(p, c)) { 174 | c += p.length(); 175 | } else { 176 | continue cy; 177 | } 178 | } 179 | rw.println("YES" + " " + x); 180 | continue main; 181 | } 182 | rw.println("NO"); 183 | } 184 | 185 | } 186 | 187 | private RW rw; 188 | private String FILE_NAME = "file"; 189 | 190 | public static void main(String[] args) { 191 | new Main().run(); 192 | } 193 | 194 | private void run() { 195 | rw = new RW(FILE_NAME + ".in", FILE_NAME + ".out"); 196 | solve(); 197 | rw.close(); 198 | } 199 | 200 | private class RW { 201 | private StringTokenizer st; 202 | private PrintWriter out; 203 | private BufferedReader br; 204 | private boolean eof; 205 | 206 | RW(String inputFile, String outputFile) { 207 | br = new BufferedReader(new InputStreamReader(System.in)); 208 | out = new PrintWriter(new OutputStreamWriter(System.out)); 209 | 210 | File f = new File(inputFile); 211 | if (f.exists() && f.canRead()) { 212 | try { 213 | br = new BufferedReader(new FileReader(inputFile)); 214 | out = new PrintWriter(new FileWriter(outputFile)); 215 | } catch (IOException e) { 216 | e.printStackTrace(); 217 | } 218 | } 219 | } 220 | 221 | private String nextLine() { 222 | String s = ""; 223 | try { 224 | s = br.readLine(); 225 | } catch (IOException e) { 226 | e.printStackTrace(); 227 | } 228 | return s; 229 | } 230 | 231 | private String next() { 232 | while (st == null || !st.hasMoreTokens()) { 233 | try { 234 | st = new StringTokenizer(br.readLine()); 235 | } catch (IOException e) { 236 | eof = true; 237 | return "-1"; 238 | } 239 | } 240 | return st.nextToken(); 241 | } 242 | 243 | private long nextLong() { 244 | return Long.parseLong(next()); 245 | } 246 | 247 | private int nextInt() { 248 | return Integer.parseInt(next()); 249 | } 250 | 251 | private void println() { 252 | out.println(); 253 | } 254 | 255 | private void println(Object o) { 256 | out.println(o); 257 | } 258 | 259 | private void print(Object o) { 260 | out.print(o); 261 | } 262 | 263 | private void close() { 264 | try { 265 | br.close(); 266 | } catch (IOException e) { 267 | e.printStackTrace(); 268 | } 269 | out.close(); 270 | } 271 | } 272 | } 273 | 274 | 5) Closest Numbers 275 | 276 | import java.io.*; 277 | import java.util.*; 278 | import java.text.*; 279 | import java.math.*; 280 | import java.util.regex.*; 281 | 282 | public class Solution { 283 | 284 | public static void main(String[] args) { 285 | Scanner in = new Scanner(System.in); 286 | int n = in.nextInt(); 287 | int[] dat = new int[n]; 288 | for(int i = 0; i < n; i++) { 289 | dat[i] = in.nextInt(); 290 | } 291 | Arrays.sort(dat); 292 | int minDiff = Integer.MAX_VALUE; 293 | String out = ""; 294 | for(int i = 0; i < n - 1; i++) { 295 | if(dat[i + 1] - dat[i] <= minDiff) { 296 | if(dat[i + 1] - dat[i] == minDiff) { 297 | out += " " + dat[i] + " " + dat[i + 1]; 298 | } else { 299 | out = dat[i] + " " + dat[i + 1]; 300 | } 301 | minDiff = dat[i + 1] - dat[i]; 302 | } 303 | } 304 | System.out.println(out); 305 | } 306 | } 307 | 308 | 6) Tower Breakers 309 | 310 | import java.io.*; 311 | import java.util.*; 312 | import java.text.*; 313 | import java.math.*; 314 | import java.util.regex.*; 315 | 316 | public class Solution { 317 | 318 | private static int numPrimeFactors(int n) { 319 | int answer = 0; 320 | for (int i=2; i<=n; i++) { 321 | if (n%i == 0) { 322 | answer++; 323 | n /= i; 324 | i = 1; 325 | } 326 | } 327 | return answer; 328 | } 329 | 330 | public static void main(String[] args) { 331 | Scanner sc = new Scanner(System.in); 332 | int T = sc.nextInt(); 333 | for (int t=0; t= 'a') base = 'a'; 407 | return (char)(((c - base + shift) % 26) + base); 408 | } 409 | } -------------------------------------------------------------------------------- /CSE (IOT)/HACKER RANK ASSIGNMENT/Week 5/5.md: -------------------------------------------------------------------------------- 1 | ## Week 5 - 2 | 3 | ### 1) Max Min 4 | 5 | #### Code - 6 | 7 | ``` 8 | import java.io.*; 9 | import java.util.*; 10 | import java.text.*; 11 | import java.math.*; 12 | import java.util.regex.*; 13 | 14 | public class Solution { 15 | 16 | public static void main(String[] args) { 17 | Scanner in = new Scanner(System.in); 18 | int n = in.nextInt(), k = in.nextInt(); 19 | int[] x = new int[n]; 20 | for(int i = 0; i < n; i++) x[i] = in.nextInt(); 21 | Arrays.sort(x); 22 | System.out.println(f(n, k, x)); 23 | } 24 | 25 | private static int f(int n, int k, int[] x){ 26 | int min = 100000000; 27 | for(int i = 0; i + k-1 < x.length; i++){ 28 | if(x[i+k-1] - x[i] < min) min = x[i+k-1]-x[i]; 29 | } 30 | return min; 31 | } 32 | } 33 | ``` 34 | #### Input - 35 | 36 | ``` 37 | 7 38 | 3 39 | 10 40 | 100 41 | 300 42 | 200 43 | 1000 44 | 20 45 | 30 46 | ``` 47 | #### Output - 48 | 49 | ``` 50 | 20 51 | ``` 52 | 53 | ### 2) Strong Password 54 | 55 | #### Code - 56 | 57 | ``` 58 | import java.io.*; 59 | import java.util.*; 60 | import java.text.*; 61 | import java.math.*; 62 | import java.util.regex.*; 63 | 64 | public class Solution { 65 | 66 | static int minimumNumber(int n, String pwd) { 67 | if(n<=3) return 6-n; 68 | boolean num = false, lower = false, upper = false, special = false; 69 | for(char c : pwd.toCharArray()){ 70 | if(isNum(c)) num = true; 71 | else if(isLower(c)) lower = true; 72 | else if(isUpper(c)) upper = true; 73 | else special = true; 74 | } 75 | boolean length = (n>=6); 76 | int count = 0; 77 | if(!num) count++; 78 | if(!lower) count++; 79 | if(!upper) count++; 80 | if(!special) count++; 81 | return (count+n < 6) ? 6-n : count; 82 | } 83 | 84 | static boolean isNum(char c){ 85 | return (c>='0' && c<='9'); 86 | } 87 | 88 | static boolean isLower(char c){ 89 | return (c>='a' && c<='z'); 90 | } 91 | 92 | static boolean isUpper(char c){ 93 | return (c>='A' && c<='Z'); 94 | } 95 | 96 | public static void main(String[] args) { 97 | Scanner in = new Scanner(System.in); 98 | int n = in.nextInt(); 99 | String password = in.next(); 100 | int answer = minimumNumber(n, password); 101 | System.out.println(answer); 102 | in.close(); 103 | } 104 | } 105 | ``` 106 | #### Input - 107 | 108 | ``` 109 | 3 110 | Ab1 111 | ``` 112 | #### Output - 113 | ``` 114 | 3 115 | ``` 116 | 117 | ### 3) Dynamic Array 118 | 119 | #### Code - 120 | 121 | ``` 122 | import java.io.*; 123 | import java.util.*; 124 | 125 | public class Solution { 126 | 127 | public static void main(String[] args) { 128 | Scanner in = new Scanner(System.in); 129 | int n = in.nextInt(); 130 | int q = in.nextInt(); 131 | List[] sequences = new List[n]; 132 | for (int n_i = 0; n_i < n; n_i++) { 133 | sequences[n_i] = new ArrayList<>(); 134 | } 135 | int lastans = 0; 136 | for (int i = 0; i < q; i++) { 137 | int t = in.nextInt(); 138 | int x = in.nextInt(); 139 | int y = in.nextInt(); 140 | List sequence = sequences[(x^lastans)%n]; 141 | switch (t) { 142 | case 1: 143 | sequence.add(y); 144 | break; 145 | case 2: 146 | lastans = sequence.get(y%sequence.size()); 147 | System.out.println(lastans); 148 | break; 149 | } 150 | } 151 | } 152 | } 153 | ``` 154 | #### Input - 155 | 156 | ``` 157 | 2 5 158 | 1 0 5 159 | 1 1 7 160 | 1 0 3 161 | 2 1 0 162 | 2 1 1 163 | ``` 164 | 165 | #### Output - 166 | 167 | ``` 168 | 7 169 | 3 170 | ``` 171 | 172 | ### 4) Smart Number 2 173 | 174 | #### Code- 175 | 176 | ``` 177 | 178 | ``` 179 | #### Input - 180 | 181 | ``` 182 | 183 | ``` 184 | 185 | #### Output- 186 | 187 | ``` 188 | 189 | ``` 190 | 191 | ### 5) Missing Numbers 192 | 193 | #### Code - 194 | 195 | ``` 196 | import java.io.*; 197 | import java.util.*; 198 | import java.text.*; 199 | import java.math.*; 200 | import java.util.regex.*; 201 | 202 | public class Solution { 203 | 204 | static void ans(int a[],int b[]){ 205 | int count_array1[] = new int[101]; 206 | int count_array2[] = new int[101]; 207 | int min = min(b); 208 | int max = max(b); 209 | int diff = max - min; 210 | for(int i=0;i 0){ 219 | System.out.print((i+min) + " "); 220 | } 221 | 222 | } 223 | } 224 | static int min(int a[]){ 225 | int min = a[0]; 226 | for(int i=1;i<=a.length-1;i++){ 227 | if(a[i] < min){ 228 | min = a[i]; 229 | } 230 | } 231 | return min; 232 | } 233 | static int max(int b[]){ 234 | int max = b[0]; 235 | for(int i=1;i<=b.length-1;i++){ 236 | if(b[i] > max){ 237 | max = b[i]; 238 | } 239 | } 240 | return max; 241 | } 242 | public static void main(String[] args) { 243 | Scanner s = new Scanner(System.in); 244 | int N,M; 245 | N = s.nextInt(); 246 | int array1[] = new int[N]; 247 | for(int i=0;i<=N-1;i++){ 248 | array1[i] = s.nextInt(); 249 | } 250 | M = s.nextInt(); 251 | int array2[] = new int[M]; 252 | for(int i=0;i<=M-1;i++){ 253 | array2[i] = s.nextInt(); 254 | } 255 | ans(array1,array2); 256 | 257 | } 258 | } 259 | ``` 260 | #### Input - 261 | 262 | ``` 263 | 10 264 | 203 204 205 206 207 208 203 204 205 206 265 | 13 266 | 203 204 204 205 206 207 205 208 203 206 205 206 204 267 | ``` 268 | 269 | #### Output - 270 | 271 | ``` 272 | 204 205 206 273 | ``` 274 | 275 | ### 6) The Full Counting Sort 276 | 277 | #### Code - 278 | 279 | ``` 280 | import java.io.*; 281 | import java.util.*; 282 | import java.text.*; 283 | import java.math.*; 284 | import java.util.regex.*; 285 | 286 | public class Solution { 287 | public static void main(String[] args) throws Exception { 288 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 289 | int n = Integer.parseInt(in.readLine()); 290 | StringBuffer[] map = new StringBuffer[100]; 291 | for(int i = 0; i < 100; i++) { 292 | map[i] = new StringBuffer(); 293 | } 294 | for(int i = 0; i < n; i++) { 295 | StringTokenizer tok = new StringTokenizer(in.readLine()); 296 | int v = Integer.parseInt(tok.nextToken()); 297 | String s = tok.nextToken(); 298 | map[v].append(i < n / 2 ? "-" : s).append(" "); 299 | } 300 | for(int i = 0; i < 100; i++) { 301 | System.out.print(map[i]); 302 | } 303 | System.out.println(); 304 | } 305 | } 306 | ``` 307 | #### Input - 308 | 309 | ``` 310 | 20 311 | 0 ab 312 | 6 cd 313 | 0 ef 314 | 6 gh 315 | 4 ij 316 | 0 ab 317 | 6 cd 318 | 0 ef 319 | 6 gh 320 | 0 ij 321 | 4 that 322 | 3 be 323 | 0 to 324 | 1 be 325 | 5 question 326 | 1 or 327 | 2 not 328 | 4 is 329 | 2 to{-truncated-} 330 | ``` 331 | 332 | #### Output - 333 | 334 | ``` 335 | - - - - - to be or not to be - that is the question - - - - 336 | ``` 337 | 338 | ### 7) Grid Challenge 339 | 340 | #### Code - 341 | 342 | ``` 343 | import java.io.*; 344 | import java.util.*; 345 | 346 | public class Solution { 347 | 348 | public static void main(String[] args) throws IOException { 349 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 350 | int t = Integer.parseInt(br.readLine()); 351 | while(t-->0){ 352 | int n = Integer.parseInt(br.readLine()); 353 | String[]grid = new String[n]; 354 | for(int i=0;i map = new HashMap(); 424 | long result = 0; 425 | for(int i = 0; i < count; i++){ 426 | int numCount = (i + 1)*(count - i); 427 | int tmp = Integer.valueOf(strs[i]); 428 | if(map.containsKey(tmp)){ 429 | map.put(tmp, numCount+map.get(tmp)); 430 | }else{ 431 | map.put(tmp, numCount); 432 | } 433 | } 434 | 435 | for(int k: map.keySet()){ 436 | int value = map.get(k); 437 | if(value%2!=0){ 438 | result = result^k; 439 | } 440 | } 441 | 442 | System.out.println(result); 443 | } 444 | } 445 | ``` 446 | #### Input - 447 | 448 | ``` 449 | 2 450 | 3 451 | 1 2 3 452 | 4 453 | 4 5 7 5 454 | ``` 455 | 456 | #### Output - 457 | 458 | ``` 459 | 2 460 | 0 461 | ``` 462 | -------------------------------------------------------------------------------- /CSE AIML/HACKER RANK ASSIGNMENT/Week 5/5.md: -------------------------------------------------------------------------------- 1 | ## Week 5 - 2 | 3 | ### 1) Max Min 4 | 5 | #### Code - 6 | 7 | ``` 8 | import java.io.*; 9 | import java.util.*; 10 | import java.text.*; 11 | import java.math.*; 12 | import java.util.regex.*; 13 | 14 | public class Solution { 15 | 16 | public static void main(String[] args) { 17 | Scanner in = new Scanner(System.in); 18 | int n = in.nextInt(), k = in.nextInt(); 19 | int[] x = new int[n]; 20 | for(int i = 0; i < n; i++) x[i] = in.nextInt(); 21 | Arrays.sort(x); 22 | System.out.println(f(n, k, x)); 23 | } 24 | 25 | private static int f(int n, int k, int[] x){ 26 | int min = 100000000; 27 | for(int i = 0; i + k-1 < x.length; i++){ 28 | if(x[i+k-1] - x[i] < min) min = x[i+k-1]-x[i]; 29 | } 30 | return min; 31 | } 32 | } 33 | ``` 34 | #### Input - 35 | 36 | ``` 37 | 7 38 | 3 39 | 10 40 | 100 41 | 300 42 | 200 43 | 1000 44 | 20 45 | 30 46 | ``` 47 | #### Output - 48 | 49 | ``` 50 | 20 51 | ``` 52 | 53 | ### 2) Strong Password 54 | 55 | #### Code - 56 | 57 | ``` 58 | import java.io.*; 59 | import java.util.*; 60 | import java.text.*; 61 | import java.math.*; 62 | import java.util.regex.*; 63 | 64 | public class Solution { 65 | 66 | static int minimumNumber(int n, String pwd) { 67 | if(n<=3) return 6-n; 68 | boolean num = false, lower = false, upper = false, special = false; 69 | for(char c : pwd.toCharArray()){ 70 | if(isNum(c)) num = true; 71 | else if(isLower(c)) lower = true; 72 | else if(isUpper(c)) upper = true; 73 | else special = true; 74 | } 75 | boolean length = (n>=6); 76 | int count = 0; 77 | if(!num) count++; 78 | if(!lower) count++; 79 | if(!upper) count++; 80 | if(!special) count++; 81 | return (count+n < 6) ? 6-n : count; 82 | } 83 | 84 | static boolean isNum(char c){ 85 | return (c>='0' && c<='9'); 86 | } 87 | 88 | static boolean isLower(char c){ 89 | return (c>='a' && c<='z'); 90 | } 91 | 92 | static boolean isUpper(char c){ 93 | return (c>='A' && c<='Z'); 94 | } 95 | 96 | public static void main(String[] args) { 97 | Scanner in = new Scanner(System.in); 98 | int n = in.nextInt(); 99 | String password = in.next(); 100 | int answer = minimumNumber(n, password); 101 | System.out.println(answer); 102 | in.close(); 103 | } 104 | } 105 | ``` 106 | #### Input - 107 | 108 | ``` 109 | 3 110 | Ab1 111 | ``` 112 | #### Output - 113 | ``` 114 | 3 115 | ``` 116 | 117 | ### 3) Dynamic Array 118 | 119 | #### Code - 120 | 121 | ``` 122 | import java.io.*; 123 | import java.util.*; 124 | 125 | public class Solution { 126 | 127 | public static void main(String[] args) { 128 | Scanner in = new Scanner(System.in); 129 | int n = in.nextInt(); 130 | int q = in.nextInt(); 131 | List[] sequences = new List[n]; 132 | for (int n_i = 0; n_i < n; n_i++) { 133 | sequences[n_i] = new ArrayList<>(); 134 | } 135 | int lastans = 0; 136 | for (int i = 0; i < q; i++) { 137 | int t = in.nextInt(); 138 | int x = in.nextInt(); 139 | int y = in.nextInt(); 140 | List sequence = sequences[(x^lastans)%n]; 141 | switch (t) { 142 | case 1: 143 | sequence.add(y); 144 | break; 145 | case 2: 146 | lastans = sequence.get(y%sequence.size()); 147 | System.out.println(lastans); 148 | break; 149 | } 150 | } 151 | } 152 | } 153 | ``` 154 | #### Input - 155 | 156 | ``` 157 | 2 5 158 | 1 0 5 159 | 1 1 7 160 | 1 0 3 161 | 2 1 0 162 | 2 1 1 163 | ``` 164 | 165 | #### Output - 166 | 167 | ``` 168 | 7 169 | 3 170 | ``` 171 | 172 | ### 4) Smart Number 2 173 | 174 | #### Code- 175 | 176 | ``` 177 | 178 | ``` 179 | #### Input - 180 | 181 | ``` 182 | 183 | ``` 184 | 185 | #### Output- 186 | 187 | ``` 188 | 189 | ``` 190 | 191 | ### 5) Missing Numbers 192 | 193 | #### Code - 194 | 195 | ``` 196 | import java.io.*; 197 | import java.util.*; 198 | import java.text.*; 199 | import java.math.*; 200 | import java.util.regex.*; 201 | 202 | public class Solution { 203 | 204 | static void ans(int a[],int b[]){ 205 | int count_array1[] = new int[101]; 206 | int count_array2[] = new int[101]; 207 | int min = min(b); 208 | int max = max(b); 209 | int diff = max - min; 210 | for(int i=0;i 0){ 219 | System.out.print((i+min) + " "); 220 | } 221 | 222 | } 223 | } 224 | static int min(int a[]){ 225 | int min = a[0]; 226 | for(int i=1;i<=a.length-1;i++){ 227 | if(a[i] < min){ 228 | min = a[i]; 229 | } 230 | } 231 | return min; 232 | } 233 | static int max(int b[]){ 234 | int max = b[0]; 235 | for(int i=1;i<=b.length-1;i++){ 236 | if(b[i] > max){ 237 | max = b[i]; 238 | } 239 | } 240 | return max; 241 | } 242 | public static void main(String[] args) { 243 | Scanner s = new Scanner(System.in); 244 | int N,M; 245 | N = s.nextInt(); 246 | int array1[] = new int[N]; 247 | for(int i=0;i<=N-1;i++){ 248 | array1[i] = s.nextInt(); 249 | } 250 | M = s.nextInt(); 251 | int array2[] = new int[M]; 252 | for(int i=0;i<=M-1;i++){ 253 | array2[i] = s.nextInt(); 254 | } 255 | ans(array1,array2); 256 | 257 | } 258 | } 259 | ``` 260 | #### Input - 261 | 262 | ``` 263 | 10 264 | 203 204 205 206 207 208 203 204 205 206 265 | 13 266 | 203 204 204 205 206 207 205 208 203 206 205 206 204 267 | ``` 268 | 269 | #### Output - 270 | 271 | ``` 272 | 204 205 206 273 | ``` 274 | 275 | ### 6) The Full Counting Sort 276 | 277 | #### Code - 278 | 279 | ``` 280 | import java.io.*; 281 | import java.util.*; 282 | import java.text.*; 283 | import java.math.*; 284 | import java.util.regex.*; 285 | 286 | public class Solution { 287 | public static void main(String[] args) throws Exception { 288 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 289 | int n = Integer.parseInt(in.readLine()); 290 | StringBuffer[] map = new StringBuffer[100]; 291 | for(int i = 0; i < 100; i++) { 292 | map[i] = new StringBuffer(); 293 | } 294 | for(int i = 0; i < n; i++) { 295 | StringTokenizer tok = new StringTokenizer(in.readLine()); 296 | int v = Integer.parseInt(tok.nextToken()); 297 | String s = tok.nextToken(); 298 | map[v].append(i < n / 2 ? "-" : s).append(" "); 299 | } 300 | for(int i = 0; i < 100; i++) { 301 | System.out.print(map[i]); 302 | } 303 | System.out.println(); 304 | } 305 | } 306 | ``` 307 | #### Input - 308 | 309 | ``` 310 | 20 311 | 0 ab 312 | 6 cd 313 | 0 ef 314 | 6 gh 315 | 4 ij 316 | 0 ab 317 | 6 cd 318 | 0 ef 319 | 6 gh 320 | 0 ij 321 | 4 that 322 | 3 be 323 | 0 to 324 | 1 be 325 | 5 question 326 | 1 or 327 | 2 not 328 | 4 is 329 | 2 to{-truncated-} 330 | ``` 331 | 332 | #### Output - 333 | 334 | ``` 335 | - - - - - to be or not to be - that is the question - - - - 336 | ``` 337 | 338 | ### 7) Grid Challenge 339 | 340 | #### Code - 341 | 342 | ``` 343 | import java.io.*; 344 | import java.util.*; 345 | 346 | public class Solution { 347 | 348 | public static void main(String[] args) throws IOException { 349 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 350 | int t = Integer.parseInt(br.readLine()); 351 | while(t-->0){ 352 | int n = Integer.parseInt(br.readLine()); 353 | String[]grid = new String[n]; 354 | for(int i=0;i map = new HashMap(); 424 | long result = 0; 425 | for(int i = 0; i < count; i++){ 426 | int numCount = (i + 1)*(count - i); 427 | int tmp = Integer.valueOf(strs[i]); 428 | if(map.containsKey(tmp)){ 429 | map.put(tmp, numCount+map.get(tmp)); 430 | }else{ 431 | map.put(tmp, numCount); 432 | } 433 | } 434 | 435 | for(int k: map.keySet()){ 436 | int value = map.get(k); 437 | if(value%2!=0){ 438 | result = result^k; 439 | } 440 | } 441 | 442 | System.out.println(result); 443 | } 444 | } 445 | ``` 446 | #### Input - 447 | 448 | ``` 449 | 2 450 | 3 451 | 1 2 3 452 | 4 453 | 4 5 7 5 454 | ``` 455 | 456 | #### Output - 457 | 458 | ``` 459 | 2 460 | 0 461 | ``` 462 | -------------------------------------------------------------------------------- /CSE CORE/HACKER RANK ASSIGNMENT/Week 5/5.md: -------------------------------------------------------------------------------- 1 | ## Week 5 - 2 | 3 | ### 1) Max Min 4 | 5 | #### Code - 6 | 7 | ``` 8 | import java.io.*; 9 | import java.util.*; 10 | import java.text.*; 11 | import java.math.*; 12 | import java.util.regex.*; 13 | 14 | public class Solution { 15 | 16 | public static void main(String[] args) { 17 | Scanner in = new Scanner(System.in); 18 | int n = in.nextInt(), k = in.nextInt(); 19 | int[] x = new int[n]; 20 | for(int i = 0; i < n; i++) x[i] = in.nextInt(); 21 | Arrays.sort(x); 22 | System.out.println(f(n, k, x)); 23 | } 24 | 25 | private static int f(int n, int k, int[] x){ 26 | int min = 100000000; 27 | for(int i = 0; i + k-1 < x.length; i++){ 28 | if(x[i+k-1] - x[i] < min) min = x[i+k-1]-x[i]; 29 | } 30 | return min; 31 | } 32 | } 33 | ``` 34 | #### Input - 35 | 36 | ``` 37 | 7 38 | 3 39 | 10 40 | 100 41 | 300 42 | 200 43 | 1000 44 | 20 45 | 30 46 | ``` 47 | #### Output - 48 | 49 | ``` 50 | 20 51 | ``` 52 | 53 | ### 2) Strong Password 54 | 55 | #### Code - 56 | 57 | ``` 58 | import java.io.*; 59 | import java.util.*; 60 | import java.text.*; 61 | import java.math.*; 62 | import java.util.regex.*; 63 | 64 | public class Solution { 65 | 66 | static int minimumNumber(int n, String pwd) { 67 | if(n<=3) return 6-n; 68 | boolean num = false, lower = false, upper = false, special = false; 69 | for(char c : pwd.toCharArray()){ 70 | if(isNum(c)) num = true; 71 | else if(isLower(c)) lower = true; 72 | else if(isUpper(c)) upper = true; 73 | else special = true; 74 | } 75 | boolean length = (n>=6); 76 | int count = 0; 77 | if(!num) count++; 78 | if(!lower) count++; 79 | if(!upper) count++; 80 | if(!special) count++; 81 | return (count+n < 6) ? 6-n : count; 82 | } 83 | 84 | static boolean isNum(char c){ 85 | return (c>='0' && c<='9'); 86 | } 87 | 88 | static boolean isLower(char c){ 89 | return (c>='a' && c<='z'); 90 | } 91 | 92 | static boolean isUpper(char c){ 93 | return (c>='A' && c<='Z'); 94 | } 95 | 96 | public static void main(String[] args) { 97 | Scanner in = new Scanner(System.in); 98 | int n = in.nextInt(); 99 | String password = in.next(); 100 | int answer = minimumNumber(n, password); 101 | System.out.println(answer); 102 | in.close(); 103 | } 104 | } 105 | ``` 106 | #### Input - 107 | 108 | ``` 109 | 3 110 | Ab1 111 | ``` 112 | #### Output - 113 | ``` 114 | 3 115 | ``` 116 | 117 | ### 3) Dynamic Array 118 | 119 | #### Code - 120 | 121 | ``` 122 | import java.io.*; 123 | import java.util.*; 124 | 125 | public class Solution { 126 | 127 | public static void main(String[] args) { 128 | Scanner in = new Scanner(System.in); 129 | int n = in.nextInt(); 130 | int q = in.nextInt(); 131 | List[] sequences = new List[n]; 132 | for (int n_i = 0; n_i < n; n_i++) { 133 | sequences[n_i] = new ArrayList<>(); 134 | } 135 | int lastans = 0; 136 | for (int i = 0; i < q; i++) { 137 | int t = in.nextInt(); 138 | int x = in.nextInt(); 139 | int y = in.nextInt(); 140 | List sequence = sequences[(x^lastans)%n]; 141 | switch (t) { 142 | case 1: 143 | sequence.add(y); 144 | break; 145 | case 2: 146 | lastans = sequence.get(y%sequence.size()); 147 | System.out.println(lastans); 148 | break; 149 | } 150 | } 151 | } 152 | } 153 | ``` 154 | #### Input - 155 | 156 | ``` 157 | 2 5 158 | 1 0 5 159 | 1 1 7 160 | 1 0 3 161 | 2 1 0 162 | 2 1 1 163 | ``` 164 | 165 | #### Output - 166 | 167 | ``` 168 | 7 169 | 3 170 | ``` 171 | 172 | ### 4) Smart Number 2 173 | 174 | #### Code- 175 | 176 | ``` 177 | 178 | ``` 179 | #### Input - 180 | 181 | ``` 182 | 183 | ``` 184 | 185 | #### Output- 186 | 187 | ``` 188 | 189 | ``` 190 | 191 | ### 5) Missing Numbers 192 | 193 | #### Code - 194 | 195 | ``` 196 | import java.io.*; 197 | import java.util.*; 198 | import java.text.*; 199 | import java.math.*; 200 | import java.util.regex.*; 201 | 202 | public class Solution { 203 | 204 | static void ans(int a[],int b[]){ 205 | int count_array1[] = new int[101]; 206 | int count_array2[] = new int[101]; 207 | int min = min(b); 208 | int max = max(b); 209 | int diff = max - min; 210 | for(int i=0;i 0){ 219 | System.out.print((i+min) + " "); 220 | } 221 | 222 | } 223 | } 224 | static int min(int a[]){ 225 | int min = a[0]; 226 | for(int i=1;i<=a.length-1;i++){ 227 | if(a[i] < min){ 228 | min = a[i]; 229 | } 230 | } 231 | return min; 232 | } 233 | static int max(int b[]){ 234 | int max = b[0]; 235 | for(int i=1;i<=b.length-1;i++){ 236 | if(b[i] > max){ 237 | max = b[i]; 238 | } 239 | } 240 | return max; 241 | } 242 | public static void main(String[] args) { 243 | Scanner s = new Scanner(System.in); 244 | int N,M; 245 | N = s.nextInt(); 246 | int array1[] = new int[N]; 247 | for(int i=0;i<=N-1;i++){ 248 | array1[i] = s.nextInt(); 249 | } 250 | M = s.nextInt(); 251 | int array2[] = new int[M]; 252 | for(int i=0;i<=M-1;i++){ 253 | array2[i] = s.nextInt(); 254 | } 255 | ans(array1,array2); 256 | 257 | } 258 | } 259 | ``` 260 | #### Input - 261 | 262 | ``` 263 | 10 264 | 203 204 205 206 207 208 203 204 205 206 265 | 13 266 | 203 204 204 205 206 207 205 208 203 206 205 206 204 267 | ``` 268 | 269 | #### Output - 270 | 271 | ``` 272 | 204 205 206 273 | ``` 274 | 275 | ### 6) The Full Counting Sort 276 | 277 | #### Code - 278 | 279 | ``` 280 | import java.io.*; 281 | import java.util.*; 282 | import java.text.*; 283 | import java.math.*; 284 | import java.util.regex.*; 285 | 286 | public class Solution { 287 | public static void main(String[] args) throws Exception { 288 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 289 | int n = Integer.parseInt(in.readLine()); 290 | StringBuffer[] map = new StringBuffer[100]; 291 | for(int i = 0; i < 100; i++) { 292 | map[i] = new StringBuffer(); 293 | } 294 | for(int i = 0; i < n; i++) { 295 | StringTokenizer tok = new StringTokenizer(in.readLine()); 296 | int v = Integer.parseInt(tok.nextToken()); 297 | String s = tok.nextToken(); 298 | map[v].append(i < n / 2 ? "-" : s).append(" "); 299 | } 300 | for(int i = 0; i < 100; i++) { 301 | System.out.print(map[i]); 302 | } 303 | System.out.println(); 304 | } 305 | } 306 | ``` 307 | #### Input - 308 | 309 | ``` 310 | 20 311 | 0 ab 312 | 6 cd 313 | 0 ef 314 | 6 gh 315 | 4 ij 316 | 0 ab 317 | 6 cd 318 | 0 ef 319 | 6 gh 320 | 0 ij 321 | 4 that 322 | 3 be 323 | 0 to 324 | 1 be 325 | 5 question 326 | 1 or 327 | 2 not 328 | 4 is 329 | 2 to{-truncated-} 330 | ``` 331 | 332 | #### Output - 333 | 334 | ``` 335 | - - - - - to be or not to be - that is the question - - - - 336 | ``` 337 | 338 | ### 7) Grid Challenge 339 | 340 | #### Code - 341 | 342 | ``` 343 | import java.io.*; 344 | import java.util.*; 345 | 346 | public class Solution { 347 | 348 | public static void main(String[] args) throws IOException { 349 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 350 | int t = Integer.parseInt(br.readLine()); 351 | while(t-->0){ 352 | int n = Integer.parseInt(br.readLine()); 353 | String[]grid = new String[n]; 354 | for(int i=0;i map = new HashMap(); 424 | long result = 0; 425 | for(int i = 0; i < count; i++){ 426 | int numCount = (i + 1)*(count - i); 427 | int tmp = Integer.valueOf(strs[i]); 428 | if(map.containsKey(tmp)){ 429 | map.put(tmp, numCount+map.get(tmp)); 430 | }else{ 431 | map.put(tmp, numCount); 432 | } 433 | } 434 | 435 | for(int k: map.keySet()){ 436 | int value = map.get(k); 437 | if(value%2!=0){ 438 | result = result^k; 439 | } 440 | } 441 | 442 | System.out.println(result); 443 | } 444 | } 445 | ``` 446 | #### Input - 447 | 448 | ``` 449 | 2 450 | 3 451 | 1 2 3 452 | 4 453 | 4 5 7 5 454 | ``` 455 | 456 | #### Output - 457 | 458 | ``` 459 | 2 460 | 0 461 | ``` 462 | --------------------------------------------------------------------------------