[for Python 3 class; must use stdin and stdout for IO; for libraries, must import by "from x import y"]
The Task
Feeling sorry for all the mischief he has caused recently, Hugh Manatee has agreed to help Professor Smith stock pile bundles of seagrass at one of several docks along the Indian River Lagoon. There are N (1 <= N <= 1,000,000, N) docks conveniently numbered 1..N, initially all of them have no bundles. Professor Smith then gives Hugh a sequence of K instructions (1 <= K <= 25,000), each of the form "A B", meaning that Hugh should add one new seagrass bundle to each dock in the range A to B (inclusive). For example, if Hugh is told "10 13", then he should add a bundle of seagrass to each of the docks 10, 11, 12, and 13. After Hugh finishes, Professor Smith would like to how evenly distributed the bundles are. Please help Hugh determine the answer to Professor Smith's question.
Sample Input
On the first line are two, space-separated integers, N and K. Each of the next K lines contain one of Professor Smith's instructions in the form of two, space-separated integers A B where (1<= A <= B <= N )
7 4 5 5 2 4 4 6 3 5
In this sample input there are N=7 docks, and Professor Smith issues K=4 instructions. The first instruction is to add a bundle of seagrass to dock 5, The second is to add bundles of seagrass to docks 2 through 4, and so on.
Sample Output
The output is one number, the difference between the largest number of bundles and the smallest number of bundles.
3
After Hugh is finished, the docks have 0,1,2,3,3,1,0 bundles. In the sorted order 0,0,1,1,2,3,3 we see the difference betweeen the most and least is three bundles.
If the input is given by the user
n,k = input().split(' ')
n = int(n)
k = int(k)
l = [0]*n
for i in range(0,k):
r1,r2 = input().split(' ')
r1 = int(r1)
r2 = int(r2)
for j in range(r1,r2+1):
l[j]+=1
l = sorted(l)
diff = l[-1] - l[0]
print(diff)
If the input is given via a file, and replace temp.txt with the filename
with open('temp.txt','r') as f:
s = f.read().split('\n')
n,k = s[0].split(' ')
n = int(n)
k = int(k)
l = [0]*n
for i in range(1,k+1):
r1,r2 = s[i].split(' ')
r1 = int(r1)
r2 = int(r2)
print(r1,r2)
for j in range(r1,r2+1):
l[j]+=1
l = sorted(l)
print(l)
diff = l[-1] - l[0]
print(diff)
[for Python 3 class; must use stdin and stdout for IO; for libraries, must import by...
import java.util.*;
public class PairFinder {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Read in the value of k
int k = Integer.parseInt(sc.nextLine());
// Read in the list of numbers
int[] numbers;
String input = sc.nextLine();
if (input.equals("")) {
numbers = new int[0];
} else {
String[] numberStrings = input.split(" ");
numbers = new int[numberStrings.length];
for (int i = 0; i < numberStrings.length; i++) {
numbers[i] = Integer.parseInt(numberStrings[i]);
}
}
System.out.println(findPairs(numbers, k));
}
//method that...
Please find an expert in algorithms to solve this problem
HHackerRank Spirinkle: Unfulfilled Orders Ms. Sugar has decided to use her spirinkle idea as a regular option on the specials menu. To support this, she has purchased & spirinkle machines. There are n customers coming in to the store. The th customer (1く讠 n) places their order at time. requesting a spirinkle of size ky. These orders are to be processed on a 'first come first served basis, however not...
Please use Java for this question.
Input Format Format for Custom Testing Input from stdin will be processed as follows and passed to the function In the first line, there is a single integer n. In the second line, there is a single integer m. In the ph of the next n lines there are m space-separated integers denoting the throw of the initial grid. In the next line, there is a single integer k. In the next line,...
Preferably in python but java is good too
Task 1: Minimum Spanning Trees For this warm-up task you are to implement any efficient minimum spanning tree algorithm that takes a sequence of edge-weighted graphs and outputs the minimum cost weight of a spanning tree of each Input Format For this assignment we use adjacency matrices with positive integer weights. Here a zero entry at row i and column J indicates that no edge i] exists in the graph. The first...
In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a series of characters separated by a single space - The second line contains the number of states as an integer k 2 1; states will be numbered 0,1,..., k -1. The start state is always state O The third line contains a series...
In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a series of characters separated by a single space - The second line contains the number of states as an integer k 2 1; states will be numbered 0,1,..., k -1. The start state is always state O The third line contains a series...
plz use python to answer it, thanks in advance
3. Tree and back arcs in a DFS 40 Marks For a given set of digraphs, write a program that performs DFS on each digraph starting at node 0 and prints out the total number of tree arcs and back arcs resulting from the traversal. Use our standard convention that when there is a choice of white or grey nodes, the one with the lowest index should be chosen. Input format:...
Use the csv file on spotify from any date
Code from lab2
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class SongsReport {
public static void main(String[] args) {
//loading name of file
File file = new File("songs.csv");
//reading data from this file
//scanner to read java file
Scanner reader;
//line to get current line from the
file
String line="";
...
Using Python 3+ for Question P5.9
Instructions: Use one main() to call each of
the functions you created. Only use one value of r and h for all
the function. Ask the user for the r and h in the main() as an
input, and h for all the functions. You should put the
functions for areas in a separate file.
For example, firstDigtec7ze Write a function e digits n) (returning the number of digits in the argument returning...
[Python]
Construct Tree Using Inorder and Preorder
Given Preorder and Inorder traversal of a binary tree, create
the binary tree associated with the traversals.You just need to
construct the tree and return the root.
Note: Assume binary tree contains only unique elements.
Input format :
Line 1 : n (Total number of nodes in binary tree)
Line 2 : Pre order traversal
Line 3 : Inorder Traversal
Output Format :
Elements are printed level wise, each level in new line...