Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions C/plagiarism_detector .c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node * next;
}NODE;
void printList(NODE *);//function to print the solution
NODE* input();//Function to take input
NODE* solve(NODE*);//function to solve the problem

NODE* createNode(int);
NODE* solve(NODE*);
int main(){
NODE* head=input();
printList(solve(head));
return 0;
}
NODE* solve(NODE* A){
if(A==NULL||A->next==NULL)
return A;
NODE* ptr=A,*pre=A->next,*root=createNode(-1),*ans=root;
int flag;
while(pre!=NULL){
flag=0;
if(ptr->data==pre->data){
while(pre!=NULL&&ptr->data==pre->data){
ptr=pre;
pre=pre->next;
}
flag=1;
}
else{
ans->next=ptr;
ans=ans->next;
}
ptr=pre;
if(pre)
pre=pre->next;
if(flag==1&&pre==NULL){
ans->next=ptr;
ans=ans->next;
}
}
//if(root->next!=NULL)
return root->next;
}
void printList(NODE* head){
NODE* ptr=head;
if(head==NULL)
return;
while(ptr!=NULL){
if(ptr!=head)
printf("->");
printf("%d",ptr->data);
ptr=ptr->next;
}
printf("\n");
}//Fuction used to print the linked list
NODE* createNode(int dt){
NODE* n=(NODE*)malloc(sizeof(NODE));
n->data=dt;
n->next=NULL;
return n;
}//Function that creates and returns a pointer to a NODE
NODE* input(){
NODE* head=NULL,*tail=NULL,*pt;
int num,n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&num);
pt=createNode(num);
if(head==NULL)
head=tail=pt;
else{
tail->next=pt;
tail=pt;
}
}
return head;
}//function that takes in the input in the form of a linked list and returns the head
70 changes: 70 additions & 0 deletions C/sudoku .c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <stdio.h>


const int M=144;
int N, R, C;

int OKrow(int a[M], int x, int y, int v) {
int j;
for(j=0; j<N; j++)
if(a[x*N+j] == v)
return 0;
return 1;
}
int OKcol(int a[M], int x, int y, int v) {
int i;
for(i=0; i<N; i++)
if(a[i*N+y] == v)
return 0;
return 1;
}
int OKbox(int a[M], int x, int y, int v) {
int bi=x/R, bj=y/C, i, j;
for(i=0; i<R; i++)
for(j=0; j<C; j++)
if(a[(i + bi*R)*N + (j + bj*C)] == v)
return 0;
return 1;
}
int OK(int a[M], int x, int y, int v) {
return OKrow(a,x,y,v) && OKcol(a,x,y,v) && OKbox(a,x,y,v);
}

void print(int a[M]) {
int i, j;
for(i=0; i<N; i++)
for(j=0; j<N; j++)
printf("%d%c", a[i*N+j], (j==N-1 ? '\n':' '));
}

int solve(int a[M]) {
int i, j, v, rem=0;
for(i=0; i<N; i++) {
for(j=0; j<N; j++) {
if(a[i*N+j] == 0) {
rem = 1;
for(v=1; v<=N; v++) {
if(OK(a,i,j,v)) {
a[i*N+j] = v;
if(solve(a)) return 1;
a[i*N+j] = 0;
}
}
}
}
}
if(rem == 0) return 1;
return 0;
}

int main() {
scanf("%d%d%d", &N, &R, &C);
int a[M], i, j;
for(i=0; i<N; i++)
for(j=0; j<N; j++)
scanf("%d", &a[i*N+j]);

if(solve(a)) print(a);
else printf("Invalid\n");
return 0;
}