8TH Merged
8TH Merged
Experiment-3.1
Student Name: Shaswat Paidisetty UID: 21BCS2258
Branch: BE-CSE Section/Group: 21BCS-902-A
Semester: 5th Date of Performance: 20/10/23
Subject Name: Advance Programming Lab-1 Subject Code: 21CSP-314
Objective: (a) Your goal is to find the number of ways to construct an array such that
consecutive positions contain different values. Specifically, we want to construct an array with
elements such that each element between and, inclusive. We also want the first and last elements
of the array to be 1 and x.
Code:
(b) Christy is interning at Hacker Rank. One day she must distribute some chocolates to her colleagues. She is
biased towards her friends and plans to give them more than the others. One of the program managers hears
of this and tells her to make sure everyone gets the same number.
To make things difficult, she must equalize the number of chocolates in a series of operations. For each
operation, she can give 1,2 or 5 pieces to all but one colleague. Everyone who gets a piece in a round
receives the same number of pieces.Given a starting distribution, calculate the minimum number of
operations needed so that every colleague has the same number of pieces.
Code:
import java.util.Scanner;
public class Solution { static
int offset = 100;
static Integer f[][] = new Integer[1111][1111]; static
int a[] = new int[11111];
// From i down to j static
Integer F(int i, int j) {
if (i < j) return Integer.MAX_VALUE / 2;
if (f[i + offset][j + offset] != null) return f[i + offset][j + offset]; if
(i == j) return 0;
int ans = Integer.MAX_VALUE / 2;
ans = Math.min(ans, F(i - 1, j) + 1);
ans = Math.min(ans, F(i - 2, j) + 1);
ans = Math.min(ans, F(i - 5, j) + 1);
return f[i + offset][j + offset] = ans;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt(); for (int t = 0; t <
T; t++) {
int n = scanner.nextInt();
int min = Integer.MAX_VALUE;
for (int i = 0; i < n; i++)
{ a[i] = scanner.nextInt();
min = Math.min(min,
a[i]);
}
int ans = Integer.MAX_VALUE;
for (int i = min; i >= min - 30; i--)
{ int tmp = 0; for (int j = 0; j < n;
j++) { tmp += F(a[j], i); }
ans = Math.min(ans, tmp);
}
System.out.println(ans);}}}
OUTPUT:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment 9
Objective:
1. You are given a number . In one operation, you can either increase the value of by
1 or decrease the value of by 1. Determine the minimum number of operations
required (possibly zero) to convert number to a number such that binary
representation of is a palindrome. Note: A binary representation is said to be a
palindrome if it reads the same from left-right and right-left.
2. The Fibonacci sequence appears in nature all around us, in the arrangement of
seeds in a sunflower and the spiral of a nautilus for example. The Fibonacci
sequence begins with and as its first and second terms. After these first two
elements, each subsequent element is equal to the sum of the previous two
elements.
Code:
a.)
import java.util.*; import
java.lang.Math;
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
OUTPUT:
COMPUTER
SCIENCE
DEPARTMENT OF
& ENGINEERING
Experiment 8
2. Objective:
To Demonstrate homophily in a network.
To Measure the tendency of nodes to form connections based on their attributes or
characteristics.
3. Code:
import networkx as nx import
matplotlib.pyplot as plt
G=nx.Graph()
G.add_nodes_from([1,2,3,4,5,6,7,8,9,10,11,12,13,14])
edges = [(1, 2), (1,5),(2, 6), (3, 6), (2, 5), (5, 4), (6, 4), (7, 8), (8, 5), (9, 11), (10,11),
(11,12), (7,12), (8,4), (7,10), (9,12) , (8,12), (9,13), (10,12), (11,13), (7,14), (6,14), (3,5),
(6,7), (2,6), (4,6), (4,7),(3,7),(8,10),(1,4),(1,8)]
G.add_edges_from(edges)
COMPUTER SCIENCE
attributes = {1: 'Teenage', 2: 'Teenage', 3: 'old', 4: 'Adult', 5: 'Adult', 6: 'old', 7: 'old', 8:
'Adult', 9: 'Teenage', 10: 'Teenage',11: 'old',12: 'Teenage',13: 'Teenage',14: 'Teenage'}
pos = nx.circular_layout(G)
nx.draw(G, pos, with_labels=True, node_size=2000,labels=attributes
node_color=node_colors) plt.title("Homophily Network") plt.show()
#calculate homophily
nx.set_node_attributes(G,attributes,'attribute')
homophily_coefficient = nx.attribute_assortativity_coefficient(G,'attribute')
print("Homophily Coefficient:", homophily_coefficient)
4. OUTPUT:
5. LEARNING OUTCOMES: