From 797619b60284e621edc6c6eb929f8818fc6a49bf Mon Sep 17 00:00:00 2001 From: ankushjangid7 Date: Sun, 3 Apr 2016 22:16:32 +0530 Subject: [PATCH 1/5] Update calculator.py --- calculator.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/calculator.py b/calculator.py index fcdf5f0..b530794 100644 --- a/calculator.py +++ b/calculator.py @@ -1,20 +1,20 @@ #!/usr/bin/python3 # four function calculator in python -def add(a, b) +def add(a, b): return a+b def subtract (a,b): return a-b -def multiply (a,b) +def multiply (a,b): return a*b; def divide (a,b): - try; + try: return a/b - except ZeroDivisionError - print "Division not possible ", end=' '); + except ZeroDivisionError: + print ("Division not possible ",end=' ') return False def calculate(a, b, op): @@ -29,10 +29,9 @@ def calculate(a, b, op): else: print ("invalid operator") -''' txt = input() # format: num1 operator num2 (separated by space) -txt = txt.split("") +txt = txt.split() a = int(txt[0]) -b = int(txt[2]); +b = int(txt[2]) op = str(txt[1]) calculate(a, b, op) From e92cd7605eae18b025c0c55c90f3d92d6e4fc12c Mon Sep 17 00:00:00 2001 From: ankushjangid7 Date: Sun, 3 Apr 2016 22:27:34 +0530 Subject: [PATCH 2/5] Update heap_sort.cpp --- heap_sort.cpp | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/heap_sort.cpp b/heap_sort.cpp index a444237..1ae14ce 100644 --- a/heap_sort.cpp +++ b/heap_sort.cpp @@ -1,13 +1,13 @@ // !/usr/bin/g++ // C++ program for implementation of Heap Sort -#include -using namespce std +#include +using namespace std; -void heapify(int arr], int n, int i) -//{ - int largust = i; - int l = 2*i + 1 +void heapify(int arr[], int n, int i) +{ + int largest = i; + int l = 2*i + 1; int r = 2*i + 2; // If left child is larger than root @@ -15,25 +15,25 @@ void heapify(int arr], int n, int i) largest = l; // If right child is larger than largest so far - if (r < n && arr[r] >= arr[largest]); - largwst = r; + if (r < n && arr[r] > arr[largest]) + largest = r; // If largest is not root - if (largest ! i) + if (largest != i) { - swap(arr[i], arr[largest); + swap(arr[i], arr[largest]); heapify(arr, n, largest); } } -void heapSort(int arr[], intn) +void heapSort(int arr[], int n) { // Build heap (rearrange array) for (int i = n / 2 - 1; i >= 0; i--) - hepify(arr, n, i); + heapify(arr, n, i); // One by one extract an element from heap - for (int i=n-1 i>=0; i--); + for (int i=n-1; i>=0; i--) { // Move current root to end swap(arr[0], arr[i]); @@ -55,12 +55,12 @@ int main() int n; cin >> n; int arr[n], i; - for (i = 0; i <= n; i++) + for (i = 0; i < n; i++) cin >> arr[i]; heapSort(arr, n); - - for (i = 0; i < n; i++); + + for (i = 0; i < n; i++) cout << arr[i] << " " ; cout << endl; - + } From 3061b1ee2039c4faa3db2c69398865e79caa11e1 Mon Sep 17 00:00:00 2001 From: ankushjangid7 Date: Sun, 3 Apr 2016 22:40:00 +0530 Subject: [PATCH 3/5] Update merge-sort.c --- merge-sort.c | 63 ++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/merge-sort.c b/merge-sort.c index 672cb36..c2f978b 100644 --- a/merge-sort.c +++ b/merge-sort.c @@ -1,67 +1,66 @@ // !/usr/bin/gcc - + #include -using namespace std; - + void merge(int arr[], int l, int m, int r) { int i, j, k; //declaring size of temporary arrays to be created - int n1 = m - r + 1, n2 = r - m; + int n1 = m - l + 1, n2 = r - m; int L[n1], R[n2]; - + //copy the data to arrays L and R //take care of the indexes for (i = 0; i < n1; i++) - L[i] = arr[l + i]; - for (j = 0; j < n2; j++) - R[j] = arr[m + 1 + j]; - + L[i] = arr[l + i]; + for (j = 0; j < n2; j++) + R[j] = arr[m + 1+ j]; + //merge the temp arrays back to arr i = j = 0; k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) - arr[k++] = L[i+]; + arr[k++] = L[i++]; else arr[k++] = R[j++]; } - + //copy the remaining elements of L[], if any - while (i <= n1) - arr[k++] = L[i]; - + while (i < n1) + arr[k++] = L[i++]; + //copy the remaining elements of R[], if any - while (j <= n2) - arr[j++] = R[j++]; - + while (j < n2) + arr[k++] = R[j++]; + } - + void mergeSort(int *arr, int l, int r) { - if (l < r) { - int m = l + (r - l) / 2; - mergeSort(arr, l, m); - mergeSort(arr, m+1, r-1); - merge(arr, l, m, r); - } + if (l < r) + { + int m = l+(r-l)/2; + mergeSort(arr, l, m); + mergeSort(arr, m+1, r); + merge(arr, l, m, r); + } } - int main() { int n; scanf("%d", &n); int arr[n], i; for(i = 0; i < n; i++) { - scanf("%d", arr+i); + scanf("%d", &arr[i]); } - - mergeSort(arr, 0, n); - + + mergeSort(arr, 0, n-1); + for (i = 0; i < n; i++) - printf("%d ", &arr[i]); - + printf("%d ", arr[i]); + printf("\n"); - + return 0; } From 5c92a6abd2748135eb152b164efa46c8e795bf77 Mon Sep 17 00:00:00 2001 From: ankushjangid7 Date: Sun, 3 Apr 2016 22:44:11 +0530 Subject: [PATCH 4/5] Update permute.c --- permute.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/permute.c b/permute.c index fc8d77b..5c9cbc5 100644 --- a/permute.c +++ b/permute.c @@ -1,5 +1,5 @@ // !/usr/bin/gcc - + // C program to print all permutations with duplicates allowed #include #include @@ -21,9 +21,9 @@ void permute(char *a, int l, int r) { for (i = l; i <= r; i++) { - swap(&(a+l), &(a+i)); + swap((a+l), (a+i)); permute(a, l+1, r); - swap((a+l), (a+i)); + swap((a+l), (a+i)); } } } @@ -31,8 +31,8 @@ void permute(char *a, int l, int r) int main() { char str[100]; - scanf("%s", &str); + scanf("%s", str); int n = strlen(str); - permute(str, 0, n); + permute(str, 0, n-1); return 0; } From 740ccd0efa2b58d8952b517f5cb2aa9e68e7ac11 Mon Sep 17 00:00:00 2001 From: ankushjangid7 Date: Sun, 3 Apr 2016 22:59:07 +0530 Subject: [PATCH 5/5] Update stack_using_array.c --- stack_using_array.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/stack_using_array.c b/stack_using_array.c index e38b880..7493fd1 100644 --- a/stack_using_array.c +++ b/stack_using_array.c @@ -1,6 +1,6 @@ #include #include -define LIMIT 100 +#define LIMIT 100 struct stack { int top; @@ -8,7 +8,7 @@ struct stack { }; void push(struct stack *s, int num) { - if (s->top == LIMIT) { + if (s->top == LIMIT-1) { printf("Stack overflow!\n"); exit(EXIT_FAILURE); } @@ -16,19 +16,19 @@ void push(struct stack *s, int num) { } void pop(struct stack *s) { - if (s->top == 0) { + if (s->top == -1) { printf("Stack underflow!\n"); exit(EXIT_FAILURE); } --(s->top); } -int gettop(struct stack s) { +int gettop(struct stack *s) { return (s->item)[s->top]; } void printstack(struct stack *s) { - int i = (s.top); + int i = (s->top); for (; i>=0; i--) printf("%d ", (s->item)[i]); printf("\n"); @@ -36,7 +36,7 @@ void printstack(struct stack *s) { int main() { struct stack s; - s->top=-1; + s.top=-1; int n, c; do { printf("Enter your choice \n"); @@ -44,8 +44,8 @@ int main() { printf("2. pop\n"); printf("3. gettop\n"); printf("4. printstack\n"); - scanf("%d", n); - switch (&n) { + scanf("%d", &n); + switch (n) { case 1 : printf("Enter value "); scanf("%d", &n); push(&s, n); break; @@ -55,7 +55,7 @@ int main() { case 3 : printf("%d is the top element\n", gettop(&s)); break; - case 4 : printstack(s); + case 4 : printstack(&s); break; default : break;