THIS QUESTION IS IN PYTHON 3 1.1 Overview and loose description Banks lend money
ID: 3764940 • Letter: T
Question
THIS QUESTION IS IN PYTHON 3
1.1 Overview and loose description
Banks lend money to each other. In tough economic times, if a bank goes bankrupt, it will not be able to pay back
the loan. A bank’s total assets are its current balance plus its loans to other banks. Figure 1 below is a diagram
(a graph) that shows five banks. (The figure is just for illustration purposes). The banks’ current balances are 25,
125, 175, 75, and 181 million dollars, respectively. The directed edge from node 1 to node 2 indicates that bank 1
lends 40 million dollars to bank 2.
If a bank’s total assets are under a certain limit, the bank is unsafe. The money it borrowed cannot be returned to
the lender, and the lender cannot count the loan in its total assets. Consequently, the lender may also be unsafe,
if its total assets are under the limit. Write a program to find all unsafe banks.
1.2 Detailed description
Your program must follow the format indicated in provided file a4_xxxxxx.py. At the minimum it must have the
three specified functions and at least one more other function (of your choice).
In addition to a4_xxxxxx.py you are also provided with the file called a4-input.txt. You should place this file
in the same folder (i.e. directory) as a4_xxxxxx.py.
In a4_xxxxxx.py, in the body of function called read_initial_info, your program reads the input from a file
a4-input.txt (see the docstring of read_initial_info for the details of what read_initial_info has to do).
While your can test your program on a4-input.txt (or a4-input-second.txt). Your program has to work on
any text file called a4-input.txt that follows the following format:
The first line of a4-input.txt contains limit, that is, the minimum total assets for keeping a bank safe. Each of
the remaining lines in the file contains info about one bank. Eg. line 1 contains info about bank with id 0, line 2
about bank with id 1, line 3 about bank with id 2 ... etc. Thus the bank id-s are from 0 to n-1, where n is the total
number of banks (thus the total number of banks is the number of lines in the file minus 1). The first number in the line is the bank’s balance, and the rest are pairs of two numbers. Each pair describes a borrower. The first
number in the pair is the borrower’s id and the second is the amount borrowed.
For example, the input for the five banks in Figure 1 above, is as follows (note that the limit is 201): (The below
is also the content of the file a4-input.txt)
201
25 1 100.5 4 320.5
125 2 40 3 85
175 0 125 3 75
75 0 125
181 2 125
For example, for the above input, the total assets of banks are:
Bank 0: Current assets: 446.0
Bank 1: Current assets: 250.0
Bank 2: Current assets: 375.0
Bank 3: Current assets: 200.0
Bank 4: Current assets: 306.0
The bank 3 has assets 75 + 125= 200, which is under 201. So bank 3 is unsafe. After bank 3 is determined unsafe,
the total assets of the remaining banks change to:
Bank 0: Current assets: 446.0
Bank 1: Current assets: 165.0
Bank 2: Current assets: 300.0
Bank 4: Current assets: 306.0
Thus the assents of bank 1 change to 125 + 40=160 and consequently fall below the limit. Thus bank 1 is also
unsafe. After bank 1 is determined unsafe, the total assets of the remaining banks change to:
Bank 0 Current assets: 345.5
Bank 2 Current assets: 300.0
Bank 4 Current assets: 306.0
None of the remaining banks has assets smaller than the limit, thus we can stop and conclude that the unsafe
banks are 1 and 3 and safe banks are 0,2 and 4.
(Hint: When you find an unsafe bank, say a bank with id i, you should go through the 2D lists banks and for every
bank (i.e. every sublist) that has entry i followed by a number x, you should replace x by zero. Here x represents
3
the number of millions of dollars that that bank lent to the bank i, as explained earlier. And then recompute the
assets of the banks.)
Every time you find one unsafe bank, your program should update the assets of the remaining banks and print
their current assets. To understand what exactly is required of your program and what it must display study
the two example runs below. As always, the example runs are part of the assignment description and have to he
studied to fully understand what is required of your program.
Here is how much various parts of your solution will be worth:
• 10 points - Having your program correctly read the data from any file called a4-input.txt that follows the
above explained format, more specifically implementing function read_initial_info correctly
• 10 points - Printing the initial assets of all the banks and determining the 1st unsafe bank, if there is one.
• 10 points - determining all unsafe banks correctly, and displaying the updated assets of the remaining banks
each time you find one unsafe bank. If there is more than one unsafe bank found in a round. Pick the bank
with the smallest ID first. Update the assets of the remaining banks and repeat if necessary (see Example
run 2 for more)
2 Example run 1
Here is what your program should print if you click run on a4_xxxxxx.py with the provided file a4-input.txt
in the same folder.
>>>
[[25.0, 1, 100.5, 4, 320.5], [125.0, 2, 40.0, 3, 85.0], [175.0, 0, 125.0, 3, 75.0], [75.0, 0, 125.0],
[181.0, 2, 125.0]]
Safe limit is: 201.0 million dollars
Current unsafe banks are:
Current assets of the banks which are not yet in unsafe list:
Bank 0 Current assets: 446.0 millions
Bank 1 Current assets: 250.0 millions
Bank 2 Current assets: 375.0 millions
Bank 3 Current assets: 200.0 millions
Bank 4 Current assets: 306.0 millions
Adding bank 3 to the list of unsafe banks.
Current unsafe banks are: 3
Current assets of the banks which are not yet in unsafe list:
Bank 0 Current assets: 446.0 millions
Bank 1 Current assets: 165.0 millions
Bank 2 Current assets: 300.0 millions
Bank 4 Current assets: 306.0 millions
Adding bank 1 to the list of unsafe banks.
Current unsafe banks are: 3 1
Current assets of the banks which are not yet in unsafe list:
Bank 0 Current assets: 345.5 millions
Bank 2 Current assets: 300.0 millions
Bank 4 Current assets: 306.0 millions
Unsafe banks are: 1 3
Safe banks are: 0 2 4
>>>
3 Example run 2
This assignment provides the 2nd text file, called a4-input-second.txt. The content of that file is:
220
25 1 100.5 4 320.5
125 2 40 3 85
69 0 125 3 75
75 0 125
250 2 125
100 0 80 4 70
150 1 10 2 80 3 40
Copy that file into a4-input.txt (In other words, imagine that the content of a4-input.txt is the same as a4-
input-second.txt). In that case when you press run on a4_xxxxxx.py, your program should print:
>>>
[[25.0, 1, 100.5, 4, 320.5], [125.0, 2, 40.0, 3, 85.0], [69.0, 0, 125.0, 3, 75.0], [75.0, 0, 125.0],
[250.0, 2, 125.0], [100.0, 0, 80.0, 4, 70.0], [150.0, 1, 10.0, 2, 80.0, 3, 40.0]]
Safe limit is: 220.0 million dollars
Current unsafe banks are:
Current assets of the banks which are not yet in unsafe list:
Bank 0 Current assets: 446.0 millions
Bank 1 Current assets: 250.0 millions
Bank 2 Current assets: 269.0 millions
Bank 3 Current assets: 200.0 millions
Bank 4 Current assets: 375.0 millions
Bank 5 Current assets: 250.0 millions
Bank 6 Current assets: 280.0 millions
Adding bank 3 to the list of unsafe banks.
Current unsafe banks are: 3
Current assets of the banks which are not yet in unsafe list:
Bank 0 Current assets: 446.0 millions
Bank 1 Current assets: 165.0 millions
Bank 2 Current assets: 194.0 millions
Bank 4 Current assets: 375.0 millions
Bank 5 Current assets: 250.0 millions
Bank 6 Current assets: 240.0 millions
Adding bank 1 to the list of unsafe banks.
Current unsafe banks are: 3 1
Current assets of the banks which are not yet in unsafe list:
Bank 0 Current assets: 345.5 millions
Bank 2 Current assets: 194.0 millions
Bank 4 Current assets: 375.0 millions
Bank 5 Current assets: 250.0 millions
Bank 6 Current assets: 230.0 millions
Adding bank 2 to the list of unsafe banks.
Current unsafe banks are: 3 1 2
Current assets of the banks which are not yet in unsafe list:
Bank 0 Current assets: 345.5 millions
5
Bank 4 Current assets: 250.0 millions
Bank 5 Current assets: 250.0 millions
Bank 6 Current assets: 150.0 millions
Adding bank 6 to the list of unsafe banks.
Current unsafe banks are: 3 1 2 6
Current assets of the banks which are not yet in unsafe list:
Bank 0 Current assets: 345.5 millions
Bank 4 Current assets: 250.0 millions
Bank 5 Current assets: 250.0 millions
Unsafe banks are: 1 2 3 6
Safe banks are: 0 4 5
>>>
Files Given:
http://tinypic.com/view.php?pic=34834a0&s=9#.VlJ_gHBViko
http://tinypic.com/view.php?pic=33l37fp&s=9#.VlJ_33BViko
Given Code :
def read_initial_info():
''' None->(float, 2D-list)
Reads the file a4-input.txt and returns a tuple. The first element of that tuple is the limit,
the second is a 2D list called banks of a format as follows (for this particular file a4-input.txt_:
[[25.0, 1, 100.5, 4, 320.5], [125.0, 2, 40.0, 3, 85.0], [175.0, 0, 125.0, 3, 75.0], [75.0, 0, 125.0], [181.0, 2, 125.0]]
Before returning the tuple the function should also print the 2D list banks by calling print(banks)
'''
# YOUR CODE GOES HERE
print(banks)
return (limit,banks)
def current_Assets(banks):
''' (2D-list)->list
given the 2D list banks, returns a (1D) list with the current_assets of all banks
Precondition: the format of the 2D list banks is as explained in the assignment
and as produced in read_initial_info()
'''
# YOUR CODE GOES HERE TOO
def find_unsafe():
'''None->None
Your solution goes here. This function should start off by calling read_initial_info()
and then work with the data read_initial_info() returns, i.e. with limit and 2D list banks
It should call other helper functions
'''
pair=read_initial_info()
limit=pair[0]
print("Safe limit is:", limit,"million dollars ")
banks=pair[1]
# YOUR CODE GOES HERE TOO
# main
# main can only have this function call and nothing else. Do not modify after this line
find_unsafe()
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 100;
double balance[SIZE];
double loan[SIZE][SIZE]={{0}};
int nobanks;
int limit;
int i = 0;
int j = 0;
int k = 0;
int noborrowers;
double assets[SIZE];
bool isSafe[SIZE];
bool newunsafefound = true;
cout << "Enter number of banks and the limit:" << endl;
nobanks = 5;
limit = 201;
balance[0] = 25.0;
balance[1] = 125.0;
balance[2] = 175.0;
balance[3] = 75.0;
balance[4] = 181.0;
loan[0][1] = 100.5;
loan[0][4] = 320.5;
loan[1][2] = 40.0;
loan[1][3] = 85.0;
loan[2][0] = 125.0;
loan[2][3] = 75.0;
loan[3][0] = 125.0;
loan[4][2] = 125.0;
for(i = 0; i < nobanks; i++)
{
isSafe[i] = true ;
}
cout << "Unsafe banks are: ";
i=0;
while(isSafe[i] == true)
{
newunsafefound=false;
i=0;
do
{
assets[i] = balance[i];
for (j = 0; j < nobanks; j++)
{
if (loan[i][j] >= 0)
assets[i] += loan[i][j];
}
if (assets[i] < limit)
{
isSafe[i] = false;
newunsafefound = true;
cout << i << " " ;
k=0;
for (k = 0; k < nobanks; k++)
{
loan[i][k] = 0;
k++;
}
}
i++;
}
while(i < nobanks);
}
return (0);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.