diff --git a/c/p1.cpp b/c/p1.cpp index f1af0d7..b5db2e3 100644 --- a/c/p1.cpp +++ b/c/p1.cpp @@ -55,5 +55,5 @@ int main(){ cout << "target: "; cin >> t; - checkSum(a, n, t); + checkSum(a, n, t); } diff --git a/c/p10.cpp b/c/p10.cpp index 08b90e0..c230e36 100644 --- a/c/p10.cpp +++ b/c/p10.cpp @@ -2,22 +2,23 @@ #include using namespace std; -struct node{ //defining the structure of the node + +struct node { int data; struct node *next; }; -//class definition for circular linked list -class CLL{ + +class CLL { private: struct node *tail; public: CLL(){ - this->tail=NULL; //constructor to initialize the tail pointer + this->tail=NULL; } void insert(int,bool); - void deletenode(); //member function + void deletenode(); void display(); - ~CLL(){ //destructor to delete the list + ~CLL(){ struct node *ptr=tail->next; while(ptr!=tail){ struct node *temp=ptr; @@ -27,110 +28,103 @@ class CLL{ delete ptr; } }; -//member function definitions -void CLL::insert(int a,bool x) -{ - struct node *temp=new struct node; - temp->data=a; - temp->next=nullptr; - if(tail==nullptr){ - tail=temp; - tail->next=temp; + +void CLL::insert(int a, bool x) { + struct node *temp = new struct node; + temp->data = a; + temp->next = nullptr; + if (tail == nullptr) { + tail = temp; + tail->next = temp; return; } - temp->next=tail->next; - tail->next=temp; - if(x){ - tail=temp; + temp->next = tail->next; + tail->next = temp; + if (x) { + tail = temp; } } -//delete function to delete at end -void CLL::deletenode() -{ - if(tail==nullptr) - { +void CLL::deletenode() { + if (tail == nullptr) { cout << "Invalid, List is already empty" << endl; return; } - if(tail->next==tail) - { - struct node *ptr=tail; - tail=nullptr; + if (tail->next == tail) { + struct node *ptr = tail; + tail = nullptr; delete ptr; return; } - struct node *temp=tail; - struct node *ptr=tail->next; - while(ptr->next!=tail) - ptr=ptr->next; - ptr->next=tail->next; - tail=ptr; + struct node *temp = tail; + struct node *ptr = tail->next; + while (ptr->next != tail) + ptr = ptr->next; + ptr->next = tail->next; + tail = ptr; delete temp; return; } -//display function to display the list -void CLL::display() -{ - if (tail==nullptr) - { +void CLL::display() { + if (tail == nullptr) { cout << "List is empty" << endl; return; } - struct node *ptr=tail->next; - while(ptr->next!=tail->next) - { + struct node *ptr = tail->next; + while (ptr->next != tail->next) { cout << ptr->data << " -> "; - ptr=ptr->next; + ptr = ptr->next; } cout << ptr->data << endl; } int main(){ - int choice=1;CLL list1; - //menu driven program - do{ + int choice=1; + CLL list1; + do { cout << "1.Insert"<> choice; - int a,b;bool x; - switch(choice) - { + cin >> choice; + int a; + bool x; + switch (choice) { case 1:{ cout << "Enter the element to be inserted: "; - cin>>a; + cin >> a; cout << "0. Insert the element at beginning"<>choice; - if(choice==0 || choice==1) //inserting at beginning or end - { - x=choice; - list1.insert(a,x); - choice=-1; + cin >> choice; + if (choice == 0 || choice == 1) { + x = (choice == 1); // Convert choice to boolean + list1.insert(a, x); + choice = -1; } - else{ + else { cout << "Invalid choice" << endl; } break; } - case 2:{ //deleting at end + case 2:{ list1.deletenode(); break; } - case 3:{ //displaying the list - cout << "List"< -//Definition for a binary tree node. struct TreeNode { int val; struct TreeNode *left; @@ -12,8 +11,8 @@ int isSameTree(struct TreeNode* p, struct TreeNode* q){ if(p == NULL && q == NULL){ return 1; } - if(p->val == q->val){ - return isSameTree(p->left, q->left) || isSameTree(p->right, q->right); + if(p != NULL && q != NULL && p->val == q->val){ + return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); } return 0; } diff --git a/c/p12.cpp b/c/p12.cpp index 29594f4..2acc7c3 100644 --- a/c/p12.cpp +++ b/c/p12.cpp @@ -1,49 +1,51 @@ //program to find the longest common subsequence (LCS) in two strings //e.g. ABCDE and AFCZE have a common subsequence of ACE -#include +#include using namespace std; void LCS(string a, string b, int m, int n) { - int dp[m + 1][n + 1]; - - for (int i = 0; i <= m; i++) { - for (int j = 0; j <= n; j++) { - if (i == 0 || j == 0) - dp[i][j] = 0; - else if (a[i - 1] == b[j - 1]) - dp[i][j] = dp[i - 1][j - 1] + 1; - else - dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); + int dp[m + 1][n + 1]; + + for (int i = 0; i <= m; i++) { + for (int j = 0; j <= n; j++) { + if (i == 0 || j == 0) + dp[i][j] = 0; + else if (a[i - 1] == b[j - 1]) + dp[i][j] = dp[i - 1][j - 1] + 1; + else + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); + } } - } - cout << dp[m][n] << endl; //length of LCS - - int index = dp[m][n]; - char lcs[index + 1]; - lcs[index] = '\0'; - - int i = m, j = n; - while (i > 0 && j > 0) { - if (a[i - 1] == b[j - 1]) { - lcs[index - 1] = a[i - 1]; - i--; - j--; + cout << dp[m][n] << endl; + + int index = dp[m][n]; + char lcs[index + 1]; + lcs[index] = '\0'; + + int i = m, j = n; + while (i > 0 && j > 0) { + if (a[i - 1] == b[j - 1]) { + lcs[index - 1] = a[i - 1]; + i--; + j--; + index--; + } else if (dp[i - 1][j] > dp[i][j - 1]) + i--; + else + j--; } - else if (dp[i - 1][j] > dp[i][j - 1]) i--; - else j--; - index--; - } - cout << "LCS: " << lcs << endl; + cout << "LCS: " << lcs << endl; } int main() { - string a = "ABFCDE"; - string b = "AFCZE"; - int m = a.length(); - int n = b.length(); + string a = "ABFCDE"; + string b = "AFCZE"; + int m = a.length(); + int n = b.length(); - LCS(a, b, m, n); + LCS(a, b, m, n); +    return 0; } diff --git a/c/p13.c b/c/p13.c index 753cad1..b2c4100 100644 --- a/c/p13.c +++ b/c/p13.c @@ -3,21 +3,19 @@ #include #include -int i = 0; - int main(){ - int i = i; int a[] = {1, 2, 3, 4, 5, 6}; int b[] = {7, 8, 9, 10, 11, 12, 13}; int n = sizeof(a)/sizeof(a[0]); int m = sizeof(b)/sizeof(b[0]); - for(int i=0;i int romanToInt(char * s){ - int *a = (int *)malloc(strlen(s) * sizeof(int)); - for(int i=0;i +#include using namespace std; +int Check(char *S); + void Upper(char *word) { for(int k = 0; word[k]; k ++) word[k] = toupper(word[k]); @@ -9,13 +12,14 @@ void Upper(char *word) { } int Check(char *S) { - for(int k = 0, v = 0; S[k]; k++) + int v = 0; + for(int k = 0; S[k]; k++) switch(S[k]) { - case A: - case E: - case I: - case O: - case U: v++; + case 'A': + case 'E': + case 'I': + case 'O': + case 'U': v++; } return v; } @@ -30,7 +34,7 @@ int main() { cout << Check(str) << endl; cout << "Type yes to continue, no to exit."; cin >> next; - } while(strcmp(next, "yes")); + } while(strcmp(next, "yes") == 0); - return 0; +    return 0; } diff --git a/c/p2.cpp b/c/p2.cpp index c1a5fa7..245a3c0 100644 --- a/c/p2.cpp +++ b/c/p2.cpp @@ -1,34 +1,36 @@ #include using namespace std; -class A{ +class A { int variable; public: - A(){ + A() { cout << "Object created" << endl; this->variable = 10; } - A(int variable){ + A(int variable) { this->variable = variable; } int func(); - ~A(){ + ~A() { cout << "Object destroyed" << endl; } + friend int anotherFunc(A& a); }; -int A::func(){ +int A::func() { variable += 2; return variable; } -int anotherFunc(A a){ +int anotherFunc(A& a) { a.variable += 3; return a.variable; } -int main(){ +int main() { A a; cout << a.func() + anotherFunc(a) << endl; return 0; } + diff --git a/c/p3.cpp b/c/p3.cpp index 212f550..b6f265f 100644 --- a/c/p3.cpp +++ b/c/p3.cpp @@ -28,3 +28,4 @@ int main(){ cout< using namespace std; -string caesar(string s, int k, string direction){ +string caesar(string s, int k, string direction) { string ans = ""; - for(int i=0;i -void main(){ - int a=10; +int main() { + int a = 10; int *ptr = &a; - *ptr++; + (*ptr)++; printf("%d", *ptr); +    return 0; } diff --git a/c/p6.c b/c/p6.c index 2e46837..1f61e90 100644 --- a/c/p6.c +++ b/c/p6.c @@ -1,14 +1,17 @@ //allocating memory to a pointer through a function -#include -int *fun() -{ - int x = 5; - return &x; +#include +#include + +int *fun() { + int *x = (int *)malloc(sizeof(int)); + *x = 5; + return x; } -int main() -{ - int *ptr = fun (); - printf ("%d", *ptr); - return 0; + +int main() { + int *ptr = fun(); + printf("%d", *ptr); + free(ptr); +    return 0; } diff --git a/c/p7.cpp b/c/p7.cpp index c5ed765..c2c5e27 100644 --- a/c/p7.cpp +++ b/c/p7.cpp @@ -2,17 +2,13 @@ #include #include -using namespace std; - -int func(string s){ - int counter=-1; - while(counter -float computeAverage(float* x, size_t* N) { - float numerator = 0.0; - float denominator = 0.0; - for (size_t i = 0; i < (size_t)N; i++) { - numerator += x[i]; - denominator++; +float computeAverage(float* x, size_t N) { + float sum = 0.0; + for (size_t i = 0; i < N; i++) { + sum += x[i]; } - return numerator/denominator; + return sum / N; } -int main() -{ +int main() { float x[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; size_t N = 10; - printf("%.3g\n", computeAverage(x, &N)); + printf("%.3g\n", computeAverage(x, N)); return 0; } diff --git a/c/p9.c b/c/p9.c index 62ae10a..5db67a5 100644 --- a/c/p9.c +++ b/c/p9.c @@ -9,19 +9,20 @@ */ #include +#include int* filterMultiples(int* arr, int N, int* NOut, int num) { int numPrimes = 0; *NOut = 0; for (int i = 0; i < N; i++) { - if (arr[i] >= num && arr[i]%num == 0) { - *NOut++; + if (arr[i] >= num && arr[i] % num == 0) { + (*NOut)++; } } - int* multiples = malloc((*NOut)*sizeof(int)); + int* multiples = malloc((*NOut) * sizeof(int)); int idx = 0; - for (int i = 0; i < *NOut; i++) { - if (arr[i] >= num && arr[i]%num == 0) { + for (int i = 0; i < N; i++) { + if (arr[i] >= num && arr[i] % num == 0) { multiples[idx] = arr[i]; idx++; } @@ -31,12 +32,13 @@ int* filterMultiples(int* arr, int N, int* NOut, int num) { int main() { int arr[10] = {5, 6, 12, 20, 18, 24, 48, 58, 60, 68}; - // Multiples of 6 int NOut; int* multiples = filterMultiples(arr, 10, &NOut, 6); for (int i = 0; i < NOut; i++) { printf("%i ", multiples[i]); } printf("\n"); + free(multiples); return 0; } + diff --git a/py/p1.py b/py/p1.py index b648ec0..93f6be6 100644 --- a/py/p1.py +++ b/py/p1.py @@ -4,13 +4,15 @@ # *** # ***** -def asterisk_tree(height,level): - if level > height: - return - for j in range(2*level-1): - print("*", end="") - print() - asterisk_tree(height, level + 1) +def asterisk_tree(height, level): + if level > height: + return + for _ in range(height - level): + print(" ", end="") + for _ in range(2 * level - 1): + print("*", end="") + print() + asterisk_tree(height, level + 1) -n=int(input("Enter height of tree : ")) -asterisk_tree(n, 1) +n = int(input("Enter height of tree: ")) +asterisk_tree(n, 1) diff --git a/py/p11.py b/py/p11.py index 3d35661..9bf7779 100644 --- a/py/p11.py +++ b/py/p11.py @@ -1,10 +1,10 @@ # Function to convert decimal to binary number def binaryconversion(n): - print(n % 2,end = '') - if n > 1: - binaryconversion(n/2) + print(n % 2, end='') + if n > 1: + binaryconversion(n // 2) -number=int(input("Enter Number: ")) +number = int(input("Enter Number: ")) binaryconversion(number) print() diff --git a/py/p2.py b/py/p2.py index 237c9b9..964361b 100644 --- a/py/p2.py +++ b/py/p2.py @@ -1,9 +1,11 @@ #program to print an input as the only member of an array everytime the function is called # input- 1 -> output- [1], input- 2 -> output- [2] -def add_item(item, items=[]): - items.append(item) - return items +def add_item(item, items=None): + if items is None: + items = [] + items.append(item) + return items print(add_item(1)) print(add_item(2)) diff --git a/py/p3.py b/py/p3.py index c83d5ad..099c735 100644 --- a/py/p3.py +++ b/py/p3.py @@ -1,13 +1,11 @@ #program to remove all even numbers from the beginning of a list #eg: [2, 4, 6, 17, 10] -> [17, 10] -def delete_starting_evens(list): - for item in list: - if list[0] % 2 == 0: - list.pop(0) - else: - break - return list +The error is in line 3 +Solution:- def delete_starting_evens(lst): + while lst and lst[0] % 2 == 0: + lst.pop(0) + return lst -list = [2, 8, 10, 11] -print(delete_starting_evens(list)) +lst = [2, 8, 10, 11] +print(delete_starting_evens(lst)) diff --git a/py/p4.py b/py/p4.py index 2ae30d4..d6a9e2a 100644 --- a/py/p4.py +++ b/py/p4.py @@ -2,17 +2,17 @@ def countSort(arr): k = max(arr) - count = [0 for i in range(k+1)] - ans = [0 for i in range(len(arr))] + count = [0 for _ in range(k+1)] + ans = [0 for _ in range(len(arr))] for i in arr: count[i] += 1 - for i in range(k+1): + for i in range(1, k+1): count[i] += count[i-1] for i in range(len(arr)-1, -1, -1): - ans[count[arr[i]]] = arr[i] + ans[count[arr[i]] - 1] = arr[i] count[arr[i]] -= 1 return ans arr = [3, 4, 12, 2, 4, 5, 5, 6, 12, 33, 10, 1, 2, 8, 6] ans = countSort(arr) -print (f"Sorted character array is {ans}") +print(f"Sorted character array is {ans}") diff --git a/py/p5.py b/py/p5.py index 01e1d53..92d259d 100644 --- a/py/p5.py +++ b/py/p5.py @@ -12,6 +12,6 @@ def get_number(): return str_to_print except ValueError: - get_number() + return get_number() print(get_number()) diff --git a/py/p6.py b/py/p6.py index fc571ba..5b0bdf1 100644 --- a/py/p6.py +++ b/py/p6.py @@ -7,15 +7,15 @@ def addBinary(a: str, b: str) -> str: j = len(b) - 1 while i >= 0 or j >= 0: - if i >= 0: - carry += int(a[i]) - i -= 1 - if j >= 0: - carry += int(b[j]) - j -= 1 - s.append(str(carry % 2)) - carry //= 2 + if i >= 0: + carry += ord(a[i]) - ord('0') + i -= 1 + if j >= 0: + carry += ord(b[j]) - ord('0') + j -= 1 + s.append(str(carry % 2)) + carry //= 2 return ''.join(reversed(s)) -print(addBinary("11", "0")) +print(addBinary("11", "0")) diff --git a/py/p7.py b/py/p7.py index ad7b53f..5243eb0 100644 --- a/py/p7.py +++ b/py/p7.py @@ -2,8 +2,9 @@ #e.g. 2->4->3 + 5->6->4 = 7->0->8 (342 + 465 = 807) # Definition for singly-linked list. + class ListNode(object): - def __init__(self, val=0, next=None): + def _init_(self, val=0, next=None): self.val = val self.next = next @@ -31,4 +32,4 @@ def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: result = dummyHead.next dummyHead.next = None - return result +    return result diff --git a/py/p8.py b/py/p8.py index 4418074..c387bfe 100644 --- a/py/p8.py +++ b/py/p8.py @@ -1,20 +1,18 @@ #Python program to create a doubly linked list and print nodes from beginning -class Node(object): - # Doubly linked node - def __init__(self, data=None, next=None, prev=None): + class Node(object): + def _init_(self, data=None, next=None, prev=None): self.data = data self.next = next self.prev = prev class doubly_linked_list(object): - def __init__(self): + def _init_(self): self.head = None self.tail = None self.count = 0 def append_item(self, data): - # Append an item new_item = Node(data, None, None) if self.head is None: self.head = new_item @@ -27,10 +25,10 @@ def append_item(self, data): self.count += 1 def print_foward(self): - print(self.iter()) + for item in self.iter(): + print(item) def iter(self): - # Iterate the list current = self.head while current: item_val = current.data diff --git a/py/p9.py b/py/p9.py index 4ea1c58..66125d0 100644 --- a/py/p9.py +++ b/py/p9.py @@ -4,14 +4,11 @@ import string import sys -# Define a function named 'ispangram' that checks if a string is a pangram def ispangram(str1, alphabet=string.ascii_lowercase): alphaset = list(alphabet) - str = list(str1.lower()) + str_list = list(str1.lower()) - # Check if all lowercase characters in the input string covers all characters in 'alphaset' - return alphaset <= str + return alphaset <= str_list -# Print the result of checking if the string is a pangram by calling the 'ispangram' function -print(ispangram('The quick brown fox jumps over the lazy dog')) +print(ispangram('The quick brown fox jumps over the lazy dog')) diff --git a/readme.md b/readme.md index 38eb713..8c8a6e4 100644 --- a/readme.md +++ b/readme.md @@ -1,3 +1,9 @@ +# Code Commandos + +Me and my team participated in an event **"Bug Hunt"** organized by in their college fest named as **"Vashisht"** organized by **IIITDM Kancheepuram** where we contributed by finding the bug, creating the issue and pull request to a GitHub repo which consists of a total of **27 files** where **15 consist of C/C++ code** and **12 of python code** and receive **7th positition** in the event. + +Below is the rules of the BugHunt Vashisht Rules. + # 🐞BugHunt Vashisht 2024 Rules🐞 - You can participate in teams of up to 3 members.