Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Calculator Problem 1 (0 / 0)
- Write a function that contains the functionality of a simple arithmetic calculator.
- The interaction with the calculator is performed by reading the parameters from the standard
- input with the input() command. Both the operands and the operator are taken from the command line.
- The user's request is processed by the function and the result is printed on the screen (standard output).
- The commands have the following format:
- operand 1
- operator
- operand2
- In case of an error, the user should be informed. The calculator has the following operations:
- Addition (+)
- Substruction (-)
- Multiplication (*)
- Integer division (//)
- Rreal division (/)
- Modulo (%)
- Power (**)
- """
- # -*- coding: utf-8 -*-
- __operators = ('+', '-', '/', '//', '*', '**', '%')
- def calculator():
- x = eval(input())
- operator = eval(input())
- y = eval(input())
- if operator not in __operators:
- print("ERROR: the entered operator is incorrect!")
- return 0
- result = eval(str(x) + operator + str(y))
- print(result)
- return result
- if __name__ == "__main__":
- calculator()
- """
- Perfect number Problem 2 (0 / 0)
- Define a function perfect_number(), which receives a single argument – natural number, and returns True
- if the number is perfect, or False if the number is not perfect. The natural number n is called _perfect_ if
- it is equal to the sum of its divisors (without considering n as its own divisor).
- Example. 6 is a perfect number, since its divisors are 1, 2 and 3, and 6 == 1 + 2 + 3
- 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").
- """
- def perfect_number(x):
- sum = 0
- for i in range(1, x//2+1):
- if x%i==0:
- sum += i
- if sum == x:
- return True
- else:
- return False
- if __name__ == "__main__":
- broj = eval(input())
- print(perfect_number(broj))
- """
- Table with squares, cubes and roots Problem 3 (0 / 0)
- Make a table with squares, cubes and roots rounded to 5 decimal spaces from the numbers m and n.
- The result should be kept in a dictionary where the key is the number and the value is a tuple
- containing the values (square, cube, root (rounded to 5 decimals)). I.e.:
- {1:(1,1,1), 2:(4,8,1.1412), …}
- Use the dictionary to print the tuple in the standard output. If the number is out of the interval, print 'no data'.
- Print the sorted list of the values in the dictionary based on the input m and n. Use the command sorted(table.items()).
- You can use the round function: round(x,5).
- """
- # -*- coding: utf-8 -*-
- import math
- if __name__ == "__main__":
- m = eval(input())
- n = eval(input())
- x = eval(input())
- table = {}
- for i in range(m, n+1):
- newTuple = (i**2, i**3, round(math.sqrt(i), 5))
- table[i] = newTuple
- if x not in range(m, n+1):
- print("no data")
- else:
- print(table[x])
- print(sorted(table.items()))
- """
- Results from partial exams Problem 4 (0 / 0)
- A natural number _n_ is read from the standard input, followed by student data for _n_ students - results from
- partial exams for the course Artificial intelligence. The data are kept in a list whose elements are dictionaries,
- where each dictionary contains the data for a single student: the student's ID number, the subject's name, as well as
- the number of points that he/she gained on each of the two partial exams, respectively. The format of each
- dictionary is the following:
- {'ID' : _IDnumber_, 'subject' : _'Artificial Intelligence'_, 'Partial Exam 1' : _points1_, 'Partial Exam 2' : _points2_}
- where _IDnumber_, _points1_ and _points2_ are the student's data (read from the standard input).
- Define a function sum_partials(), which receives a single argument – list of dictionaries containing
- student data (as described above), and returns the same list, but modified in such a way that each dictionary
- will contain only the total score (i.e. the sum of points) of the partial exams instead of the scores for the two
- partial exams. Call this function on the list of dictionaries previously read from the standard input.
- Print the function's result on the standard output.
- """
- def sum_partials(dicts):
- for i in dicts:
- p1 = i.pop("Partial Exam 1")
- p2 = i.pop("Partial Exam 2")
- i["Total score"] = p1+p2
- return dicts
- if __name__ == "__main__":
- n = eval(input())
- results = [] # this is the list of dictionaries
- for i in range(0, n):
- r = {} # a dictionary that will keep the data for a single student
- IDnumber = eval(input())
- points1 = eval(input())
- points2 = eval(input())
- # here, you should add the data to the dictionary. Afterwards, add the dictionary to the results list as well!!
- r = {'ID' : IDnumber, 'subject' : 'Artificial Intelligence', 'Partial Exam 1' : points1, 'Partial Exam 2' : points2}
- results.append(r)
- print(sum_partials(results))
Advertisement
Add Comment
Please, Sign In to add comment