StefanTodorovski

(Python) AI: Lab 1 - ВИ: Лаб 1

Mar 21st, 2019
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.10 KB | None | 0 0
  1. """
  2. Calculator Problem 1 (0 / 0)
  3. Write a function that contains the functionality of a simple arithmetic calculator.
  4. The interaction with the calculator is performed by reading the parameters from the standard
  5. input with the input() command. Both the operands and the operator are taken from the command line.
  6. The user's request is processed by the function and the result is printed on the screen (standard output).
  7. The commands have the following format:
  8. operand 1
  9. operator
  10. operand2
  11.  
  12. In case of an error, the user should be informed. The calculator has the following operations:
  13. Addition (+)
  14. Substruction (-)
  15. Multiplication (*)
  16. Integer division (//)
  17. Rreal division (/)
  18. Modulo (%)
  19. Power (**)
  20. """
  21.  
  22. # -*- coding: utf-8 -*-
  23. __operators = ('+', '-', '/', '//', '*', '**', '%')
  24.  
  25.  
  26. def calculator():
  27.     x = eval(input())
  28.     operator = eval(input())
  29.     y = eval(input())
  30.  
  31.     if operator not in __operators:
  32.         print("ERROR: the entered operator is incorrect!")
  33.         return 0
  34.  
  35.     result = eval(str(x) + operator + str(y))
  36.  
  37.     print(result)
  38.     return result
  39.  
  40.  
  41. if __name__ == "__main__":
  42.     calculator()
  43.  
  44.  
  45.  
  46. """
  47. Perfect number Problem 2 (0 / 0)
  48. Define a function perfect_number(), which receives a single argument – natural number, and returns True
  49. if the number is perfect, or False if the number is not perfect. The natural number n is called _perfect_ if
  50. it is equal to the sum of its divisors (without considering n as its own divisor).
  51.  
  52. Example. 6 is a perfect number, since its divisors are 1, 2 and 3, and 6 == 1 + 2 + 3
  53.  
  54. Read a single natural number from the standard input and call the previously defined function perfect_number() on this number. Print an appropriate message on the standard output ("The number is perfect" or "The number is not perfect").
  55. """
  56.  
  57. def perfect_number(x):
  58.     sum = 0
  59.     for i in range(1, x//2+1):
  60.         if x%i==0:
  61.             sum += i
  62.     if sum == x:
  63.         return True
  64.     else:
  65.         return False
  66.  
  67.  
  68. if __name__ == "__main__":
  69.     broj = eval(input())
  70.     print(perfect_number(broj))
  71.  
  72.  
  73.  
  74. """
  75. Table with squares, cubes and roots Problem 3 (0 / 0)
  76. Make a table with squares, cubes and roots rounded to 5 decimal spaces from the numbers m and n.
  77. The result should be kept in a dictionary where the key is the number and the value is a tuple
  78. containing the values (square, cube, root (rounded to 5 decimals)). I.e.:
  79.  
  80. {1:(1,1,1), 2:(4,8,1.1412), …}
  81.  
  82. Use the dictionary to print the tuple in the standard output. If the number is out of the interval, print 'no data'.
  83. Print the sorted list of the values in the dictionary based on the input m and n. Use the command sorted(table.items()).
  84. You can use the round function: round(x,5).
  85. """
  86.  
  87. # -*- coding: utf-8 -*-
  88. import math
  89.  
  90. if __name__ == "__main__":
  91.     m = eval(input())
  92.     n = eval(input())
  93.     x = eval(input())
  94.  
  95.     table = {}
  96.     for i in range(m, n+1):
  97.         newTuple = (i**2, i**3, round(math.sqrt(i), 5))
  98.         table[i] = newTuple
  99.     if x not in range(m, n+1):
  100.         print("no data")
  101.     else:
  102.         print(table[x])
  103.     print(sorted(table.items()))
  104.  
  105.  
  106.  
  107. """
  108. Results from partial exams Problem 4 (0 / 0)
  109. A natural number _n_ is read from the standard input, followed by student data for _n_ students - results from
  110. partial exams for the course Artificial intelligence. The data are kept in a list whose elements are dictionaries,
  111. where each dictionary contains the data for a single student: the student's ID number, the subject's name, as well as
  112. the number of points that he/she gained on each of the two partial exams, respectively. The format of each
  113. dictionary is the following:
  114.  
  115. {'ID' : _IDnumber_, 'subject' : _'Artificial Intelligence'_, 'Partial Exam 1' : _points1_, 'Partial Exam 2' : _points2_}
  116. where _IDnumber_, _points1_ and _points2_ are the student's data (read from the standard input).
  117.  
  118. Define a function sum_partials(), which receives a single argument – list of dictionaries containing
  119. student data (as described above), and returns the same list, but modified in such a way that each dictionary
  120. will contain only the total score (i.e. the sum of points) of the partial exams instead of the scores for the two
  121. partial exams. Call this function on the list of dictionaries previously read from the standard input.
  122. Print the function's result on the standard output.
  123. """
  124.  
  125. def sum_partials(dicts):
  126.     for i in dicts:
  127.         p1 = i.pop("Partial Exam 1")
  128.         p2 = i.pop("Partial Exam 2")
  129.         i["Total score"] = p1+p2
  130.     return dicts
  131.  
  132.  
  133. if __name__ == "__main__":
  134.     n = eval(input())
  135.     results = []  # this is the list of dictionaries
  136.     for i in range(0, n):
  137.         r = {}  # a dictionary that will keep the data for a single student
  138.         IDnumber = eval(input())
  139.         points1 = eval(input())
  140.         points2 = eval(input())
  141.         # here, you should add the data to the dictionary. Afterwards, add the dictionary to the results list as well!!
  142.         r = {'ID' : IDnumber, 'subject' : 'Artificial Intelligence', 'Partial Exam 1' : points1, 'Partial Exam 2' : points2}
  143.         results.append(r)
  144.     print(sum_partials(results))
Advertisement
Add Comment
Please, Sign In to add comment