In: Computer Science
In Python
Create a function called ????. The function receives a "string" that represents a year (the variable with this "String" will be called uve) and a list containing "strings" representing bank accounts (call this list ????).
• Each account is represented by 8 characters. The format of each account number is "** - ** - **", where the asterisks are replaced by numeric characters.
o For example, “59-04-23”.
• The two central characters of the "string" of each account represent the year in which the account was created.
o For example, the account “59-04-23” was created in 2004.
o Assume that all years are from the year 2000 onwards.
• The year in uve is represented by four characters.
o For example, "2001"
• The function must return a list with all the accounts that were created in the year indicated in ???, with each counts without the “-“ symbols.
o For example, if the accounts are [“49-01-26”, “19-01-33”, “99-01-53”, “59-04-23”] and the year of interest is "2001", then the function should return ["490126", "190133", "990153"]; note that the accounts continue in the form of a "string".
• In addition, the function must return the percentage of accounts that were created in the year indicated by ???.
o For example, if the accounts are [“49-01-26”, “19-01-33”, “99-01-53”, “59-04-23”] and the year of interest is "2001", so the function should return 75% (three accounts were created in 2001, and there are four accounts, therefore 100 × 3/4 = 75%)
Solution:
The code is in ython 3.
For percentage I typecasted as str(int(percent)) to return percentage without any decimal ponts.
If you need a different format please comment and will do the needful.
I have also added comments in the code for understanding
Code:
def pic(uve,bere):
year=uve[2:4]
accounts=[]
#iterating all accounts
for x in bere:
#if accounts matches the year
if(x[3:5]==year):
#append the account and remove '-'s
accounts.append(x.replace('-',''))
percent=100*len(accounts)/len(bere)
#To print percent exacyly without decimls along with '%'
percentage=str(int(percent))+'%'
#returning both accounts and percentage
return accounts,percentage
#Code for testing
accounts,percentage=pic("2001",["49-01-26", "19-01-33", "99-01-53", "59-04-23"])
print(accounts)
print(percentage)
Screenshot of sample output and code: