From 5bcff20e82896ec00e5843aed96f0751559f11b1 Mon Sep 17 00:00:00 2001 From: Samiran <126177479+samirankulkarni@users.noreply.github.com> Date: Thu, 31 Oct 2024 01:22:47 +0530 Subject: [PATCH] Add files via upload 31-oct-night --- daa_1.cpp | 27 +++++++++++++++++++++++++++ daa_2.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ daa_3.cpp | 26 ++++++++++++++++++++++++++ daa_4.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 144 insertions(+) create mode 100644 daa_1.cpp create mode 100644 daa_2.py create mode 100644 daa_3.cpp create mode 100644 daa_4.cpp diff --git a/daa_1.cpp b/daa_1.cpp new file mode 100644 index 0000000..9625215 --- /dev/null +++ b/daa_1.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; + +int fib_rec(int n){ + if(n<=1) return n; + return fib_rec(n-1) + fib_rec(n-2); +} + +int fib_iter(int n){ + if(n<=1) return n; + int a = 0; + int b = 1; + int c; + for(int i=2;i<=n;i++){ + c = a + b; + a = b; + b = c; + } + return c; +} + +int main(){ + + cout< None: + self.val = val + self.symbol = symbol + self.huffman = '' + self.l = None + self.r = None + + def __lt__(self, next): + return self.val < next.val + +nodes = [] +char = ['a', 'b', 'c', 'd', 'e'] +freq = [100, 12, 24, 105, 38] + +for i in range(len(char)): + heapq.heappush(nodes, Node(freq[i], char[i])) + +while len(nodes) > 1: + left = heapq.heappop(nodes) + right = heapq.heappop(nodes) + left.huffman = '0' + right.huffman = '1' + + newNode = Node(left.val + right.val, left.symbol + right.symbol) + newNode.l = left + newNode.r = right + heapq.heappush(nodes, newNode) + +def printNodes(node, val=''): + newVal = val + node.huffman + + if node.l: + printNodes(node.l, newVal) + + if node.r: + printNodes(node.r, newVal) + + if not node.l and not node.r: + print(f"{node.symbol} -> {newVal}") + +printNodes(nodes[0]) \ No newline at end of file diff --git a/daa_3.cpp b/daa_3.cpp new file mode 100644 index 0000000..03a6267 --- /dev/null +++ b/daa_3.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + +// 0/1 knapsack + +int knapsack(int i,int n,vector &weights,vector &values,int capacity,vector> &dp){ + if(i == n || capacity == 0) return 0; + if(dp[i][capacity] != -1) return dp[i][capacity]; + // if we could take we take + int ans1 = INT_MIN; + if(weights[i] <= capacity) ans1 = values[i] + knapsack(i+1,n,weights,values,capacity-weights[i],dp); + int ans2 = knapsack(i+1,n,weights,values,capacity,dp); + return dp[i][capacity] = max(ans1,ans2); +} + +int main(){ + vector weights = {95, 4, 60, 32, 23, 72, 80, 62, 65, 46}; + vector values = {55, 10, 47, 5, 4, 50, 8, 61, 85, 87}; + int n = 10; + int capacity = 269; + + vector> dp(n+1,vector(capacity+1,-1)); + cout< +using namespace std; + +void print(vector> &a){ + int n = a.size(); + for(int i=0;i> &a,int i,int j){ + int n = a.size(); + for(int c=i-1;c>=0;c--) if(a[c][j] == 'Q') return false; + for(int x=i,y=j;x>=0 && y>=0;x--,y--) if(a[x][y] == 'Q') return false; + for(int x=i,y=j;x>=0 && y> &a){ + if(i >= n){ + print(a); + return; + } + for(int j=0;j> a(n,vector(n,'-')); + fun(0,n,a); +} + +int main(){ + int n = 5; + solve(n); + + return 0; +} \ No newline at end of file