From 39a1e587fdd834051dba8c1cc83d1319b21aca09 Mon Sep 17 00:00:00 2001 From: prabal255 <51309311+prabal255@users.noreply.github.com> Date: Tue, 15 Oct 2019 14:11:18 +0530 Subject: [PATCH 1/3] solution of fractional knapsack --- fractional_knapsack.c | 77 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 fractional_knapsack.c diff --git a/fractional_knapsack.c b/fractional_knapsack.c new file mode 100644 index 0000000..467661a --- /dev/null +++ b/fractional_knapsack.c @@ -0,0 +1,77 @@ +// C/C++ program to solve fractional Knapsack Problem +#include + +using namespace std; + +// Structure for an item which stores weight and corresponding +// value of Item +struct Item +{ + int value, weight; + + // Constructor + Item(int value, int weight) : value(value), weight(weight) + {} +}; + +// Comparison function to sort Item according to val/weight ratio +bool cmp(struct Item a, struct Item b) +{ + double r1 = (double)a.value / a.weight; + double r2 = (double)b.value / b.weight; + return r1 > r2; +} + +// Main greedy function to solve problem +double fractionalKnapsack(int W, struct Item arr[], int n) +{ + // sorting Item on basis of ratio + sort(arr, arr + n, cmp); + + // Uncomment to see new order of Items with their ratio + /* + for (int i = 0; i < n; i++) + { + cout << arr[i].value << " " << arr[i].weight << " : " + << ((double)arr[i].value / arr[i].weight) << endl; + } + */ + + int curWeight = 0; // Current weight in knapsack + double finalvalue = 0.0; // Result (value in Knapsack) + + // Looping through all Items + for (int i = 0; i < n; i++) + { + // If adding Item won't overflow, add it completely + if (curWeight + arr[i].weight <= W) + { + curWeight += arr[i].weight; + finalvalue += arr[i].value; + } + + // If we can't add current Item, add fractional part of it + else + { + int remain = W - curWeight; + finalvalue += arr[i].value * ((double) remain / arr[i].weight); + break; + } + } + + // Returning final value + return finalvalue; +} + +// driver program to test above function +int main() +{ + int W = 50; // Weight of knapsack + Item arr[] = {{60, 10}, {100, 20}, {120, 30}}; + + int n = sizeof(arr) / sizeof(arr[0]); + + cout << "Maximum value we can obtain = " + << fractionalKnapsack(W, arr, n); + return 0; +} From e28c07113bd6068e0bd63a1b8b47e78b7bee47ed Mon Sep 17 00:00:00 2001 From: prabal255 <51309311+prabal255@users.noreply.github.com> Date: Sat, 31 Oct 2020 20:32:00 +0530 Subject: [PATCH 2/3] Add files via upload --- operator_overloading.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 operator_overloading.py diff --git a/operator_overloading.py b/operator_overloading.py new file mode 100644 index 0000000..306cc5d --- /dev/null +++ b/operator_overloading.py @@ -0,0 +1,17 @@ +import math + +class circle: + def __init__(self,radius): + self.radius=radius + def set_radius(self): + self.radius=radius + def get_radius(self): + return self.radius + def __add__(self,circle_obj,c3): + return (self.radius+circle_obj.radius+c3.radius) + +c1=circle(2) +c2=circle(3) +c3=circle(4) + +print(c1+c2+c3) From 54fe5b3c83764fea657f5f3a3c17d9bf04b06f94 Mon Sep 17 00:00:00 2001 From: prabal255 <51309311+prabal255@users.noreply.github.com> Date: Sat, 31 Oct 2020 20:41:19 +0530 Subject: [PATCH 3/3] Add files via upload --- composition.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 composition.py diff --git a/composition.py b/composition.py new file mode 100644 index 0000000..070030e --- /dev/null +++ b/composition.py @@ -0,0 +1,21 @@ +class Salary: + def __init__(self,pay,bonus): + self.pay=pay + self.bonus=bonus + def annual(self): + return (self.pay*12)+self.bonus + +class employee: + def __init__(self,name,age,pay,bonus): + self.name=name + self.age=age + self.pay=pay + self.bonus=bonus + self.obj_salary=Salary(pay,bonus) + + def total_salary(self): + return self.obj_salary.annual() + +emp=employee('ajay',12,1,1) + +print(emp.total_salary()) \ No newline at end of file