python - 从转换为二维数组的两个管道分隔文件之间执行的操作得到错误的输出
问题描述
我是编码新手,我对这个问题的说明是:“给定管道分隔的文件 F1 和 F2,其中 F1 包含具有 ACCOUNT NUM|PIN|BALANCE 字段的帐户,F2 包含交易指令 COMMAND|AMOUNT|ACCOUNT NUM|PIN,执行事务,将结果存储回 F1。
COMMAND 字段将是 add 或 sub,表示从帐户中添加或减去。
应忽略未提供正确 PIN 码或试图将帐户置于零以下的交易。”
我的输出是:
1000|1234|10000
1020|2222|0
3000|3344|1000
2020|1234|90000
正确的输出是:
1000|1234|11000
1020|2222|0
3000|3344|0
2020|1234|90000
我写的代码是:
import sys
F1= sys.argv[1]
F2= sys.argv[2]
records=[]
with open(F1,'r') as fp:
content=fp.readlines()
for row in content:
recList=row.strip("\n").split('|')
records.append(recList)
records2=[]
with open(F2,'r') as fp:
content=fp.readlines()
for row in content:
recList2=row.strip("\n").split('|')
records2.append(recList2)
for i in range(len(records)):
row=records[i]
for i in range(len(records2)):
row=records2[i]
for row in records and records2:
if records[i][1]==records2[i][3]:
if records2[i][0]=="add":
records[i][2]=int(records[i][2])+int(records2[i][1])
elif records2[i][0]=="sub":
if int(records[i][2])>=int(records2[i][1]):
records[i][2]=int(records[i][2])-int(records2[i][1])
else:
records[i][2]=records[i][2]
else:
break
print(records)
解决方案
# Create a function that turns pipe-delimited strings into 2d arrays
def pipe2a(text):
records= text.split("\n")
for i in range(0, len(records)):
records[i]= records[i].split("|")
return records
# Create a function that turns 2d arrays into pipe-delimited strings.
def a2pipe(a):
text= ""
for i in range(0, len(a)):
account= a[i]
for j in range(0, len(account)):
account[j]= str(account[j])
text = text + "|".join(account) + "\n"
return text;
# Read in the accounts and transactions
accounts= pipe2a(open(F1, 'r').read())
transactions= pipe2a(open(F2, 'r').read())
# Main Section
# for each transaction
for transactionIndex in range(0, len(transactions)):
transaction= transactions[transactionIndex]
if(len(transaction) >= 4):
# look through the accounts for the matching account
for accountIndex in range(0,len(accounts)):
account= accounts[accountIndex]
if(len(account) >= 3): # make sure we have
balance= int(account[2]) # enough fields
transactionAmount= int(transaction[1])
if(account[0] == transaction[2]): # account matches?
if(account[1] == transaction[3]): # pin code matches?
if(transaction[0] == 'add'):
accounts[accountIndex][2]= balance + transactionAmount
elif (transaction[0] == 'sub' and transactionAmount <= balance):
accounts[accountIndex][2]= balance - transactionAmount
# Write the answer back out to the original file
open(F1, 'w').write(a2pipe(accounts))
有用
推荐阅读
- airflow - Airflow UI 将执行日期时间更改为可读格式
- typescript - Typescript 接口从同一接口的另一个属性中设置属性类型
- graphql - Hasura - 未应用权限迁移
- javascript - Google Scripts/HTML/JavaScript Upload File to Folder 错误参数不能为空:blob
- java - 不能在再保证请求中将 Json 数组作为正文传递 - java.lang.IllegalStateException:不是 JSON 对象
- arrays - Hackerrank 上的数组操作
- java - Java - 访问 JSON 元素 (llegalFormatConversionException: d !=java.lang.String)
- mongodb - MongoDB在多个子文档中搜索多个字段
- python - 在张量流中用完 VRAM
- flutter - 颤振错误“方法'toDouble'在null上被调用”