理解Python实现的伪代码

时间:2015-07-23 16:13:28

标签: python pseudocode

我得到了一些伪造的代码,这些伪代码是由不再提问的人编写的。我被告知需要在Python中实现它。在这一点上,我不是在寻找有人为我编写Python代码,但我正在尝试理解伪代码,以便我可以用Python编写代码。

statsOut = open('/shared/succedentsupercedent_stats','w')
supercedents = tuple(open('/path', 'r'))
succedents = tuple(open('ABCD_.raw', 'r'))

for ( cols = 1 to succedents.columns , colg = 1 to supercedents.columns )

  /**** load all users for one succedent/supercedent into an array for future stats
  xy_array = new array ( supercedents.rows , 2 )
  for ( user = 1 to supercedents.rows )
    if succedents[user,cols].isValidDouble and supercedents[user,colg].isValidDouble
      then do
        xy_array [ user , 1 ] = succedents[user,cols].toDouble
        xy_array [ user , 2 ] = supercedents[user,colg].toDouble
      enddo
    endif
  endfor

  /*** do some stats with one succedent:supercedent key for all users
    sumx = sum( of xy_array[1:supercedents.rows][1]
    sumy = sum( of xy_array[1:supercedents.rows][2]

  /*** output succedent,supercedent,sumx,sumy
    append(statsOut,succedent,supercedent,sumx,sumy)
endfor        

如果有人可以帮助解释一下这方面做了什么会对我有很大的帮助。

2 个答案:

答案 0 :(得分:1)

这里猜测伪代码需要什么。目前还不完全清楚需要什么,看看正在阅读的实际数据以及要生成的样本将非常有用。如果没有样本输入和输出,很难弄清楚代码应该完成什么。

def main():
    succedents = load_succedents('ABCD_reddob_cases.raw')
    supercedents = load_supercedents('/shared/voom_e_plink_cases')
    with open('/shared/succedentsupercedent_stats', 'w') as stats_out:
        for cols in range(len(succedents[0])):
            for colg in range(len(supercedents[0])):
                xy_array = [[0] * 2 for _ in range(len(supercedents))]
                for user in range(len(supercedents)):
                    s1, s2 = succedents[user][cols], supercedents[user][colg]
                    if is_valid_double(s1) and is_valid_double(s2):
                        xy_array[user] = to_double(s1), to_double(d2)
                sumx = sum(row[0] for row in xy_array)
                sumy = sum(row[1] for row in xy_array)
                append(stats_out, cols, colg, sumx, sumy)

def load_supercedents(path):
    with open(path) as file:
        # perform whatever you need to do for loading the supercedents
        # some sort of data conversion may need to take place here
        supercedents = tuple(file)
    return supercedents

def load_succedents(path):
    with open(path) as file:
        # perform whatever you need to do for loading the succedents
        # some sort of data conversion may need to take place here
        succedents = tuple(file)
    return succedents

def is_valid_double(data):
    # determine if the data is a valid double or not
    return True or False

def to_double(data):
    # convert the data into whatever a double happens to be
    return data

def append(file, s, g, x, y):
    # write the data to your file
    print('cols = {s}, colg = {g}, sumx = {x}, sumy = {y}'.format(**locals()),
          file=file, flush=True)

if __name__ == '__main__':
    main()

答案 1 :(得分:0)

伪代码是任何与代码类似的东西的术语,但不是实际定义的语言。所以用"伪代码"需要猜测具体的意图。一般来说,这应该是显而易见的意思。