Advertisement
monajuma54

Untitled

Oct 19th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. # a simple parser for python. use get_number() and get_word() to read
  2. def parser():
  3. while 1:
  4. data = list(input().split('\n'))
  5. for number in data:
  6. if len(number) > 0:
  7. yield(number)
  8.  
  9. input_parser = parser()
  10.  
  11. def get_word():
  12. global input_parser
  13. return next(input_parser)
  14.  
  15. def get_number():
  16. data = get_word()
  17. try:
  18. return int(data)
  19. except ValueError:
  20. return float(data)
  21.  
  22. # numpy and scipy are available for use
  23. import numpy
  24. import scipy
  25. import json
  26. import decimal
  27.  
  28. def calc_if(data_17, data_18):
  29. numerator = int(data_17['citations']) + int(data_18['citations'])
  30. denominator = int(data_17['articleCounts']) + int(data_18['articleCounts'])
  31. impact_factor = numerator/denominator
  32. return decimal.Decimal("{0:.2f}".format(impact_factor))
  33.  
  34. def sort_results():
  35. global journal_names, impact_factors
  36.  
  37. n = len(journal_names)
  38. # Traverse through all array elements
  39. for i in range(n):
  40. # Last i elements are already in place
  41. for j in range(0, n-i-1):
  42. # traverse the array from 0 to n-i-1
  43. # Swap if the element found is greater
  44. # than the next element
  45. if impact_factors[j] < impact_factors[j+1] :
  46. impact_factors[j], impact_factors[j+1] = impact_factors[j+1], impact_factors[j]
  47. journal_names[j], journal_names[j+1] = journal_names[j+1], journal_names[j]
  48. elif impact_factors[j] == impact_factors[j+1]:
  49. if journal_names[j] > journal_names[j+1]:
  50. impact_factors[j], impact_factors[j+1] = impact_factors[j+1], impact_factors[j]
  51. journal_names[j], journal_names[j+1] = journal_names[j+1], journal_names[j]
  52.  
  53. # read input data count
  54. record_count = get_number()
  55.  
  56. # read publications
  57. data = json.loads(get_word())
  58.  
  59. # read journal issues
  60. issues = []
  61. for i in range (record_count - 1):
  62. issue = get_word()
  63. issues.append(json.loads(issue))
  64.  
  65. # create dict for specific journal fields, for faster access
  66. journals = {}
  67. journal_ids = []
  68. for publication in data['publications']:
  69. jid = publication['publicationNumber']
  70. journal_ids.append(jid)
  71. journals[jid] = {}
  72. journals[jid]['publicationTitle'] = publication['publicationTitle']
  73. journals[jid]['articleCounts'] = publication['articleCounts']
  74. for article in publication['articleCounts']:
  75. year = article['year']
  76. journals[jid][year] = {}
  77. journals[jid][year]['articleCounts'] = article['articleCount']
  78. journals[jid][year]['citations'] = 0
  79. journals[jid].pop('articleCounts', None)
  80.  
  81. # count the citations for each year, for each journal
  82. for issue in issues:
  83. articles = issue["paperCitations"]["ieee"]
  84. for article in articles:
  85. jid = article['publicationNumber']
  86. year = article['year']
  87. try:
  88. journals[jid][year]['citations'] += 1
  89. except KeyError:
  90. pass
  91.  
  92. # calculate impact factor for each
  93.  
  94. # print(json.dumps(journals, indent=2))
  95.  
  96.  
  97. # publications = data['publications']
  98. journal_names = []
  99. impact_factors = []
  100. for jid in journal_ids:
  101. journal = journals[jid]
  102. # print(journal)
  103. impact_factor = calc_if(journal['2017'], journal['2018'])
  104. journal_names.append(journal['publicationTitle'])
  105. impact_factors.append(impact_factor)
  106.  
  107. sort_results()
  108.  
  109. for i in range (len(journal_ids)):
  110. print(f"{journal_names[i]}: {impact_factors[i]}")
  111.  
  112. # print(journal_ids)
  113. # a = get_number()
  114. # b = get_number()
  115.  
  116. # res = a + b
  117. # print(res)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement