Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
sem3-ivt19-task1-1-mozgovoy
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Мозговой Никита Александрович
sem3-ivt19-task1-1-mozgovoy
Commits
274e30df
Commit
274e30df
authored
Nov 09, 2020
by
Мозговой Никита Александрович
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lr5
parent
97c2b194
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
67 additions
and
54 deletions
+67
-54
calculate.py
calculate.py
+67
-54
No files found.
calculate.py
View file @
274e30df
def
check_types
(
op
,
allowed_types
):
return
type
(
op
)
in
allowed_types
def
calculate
(
op1
,
op2
,
act
,
allowed_types
=
(
int
,
float
,
list
,
tuple
)):
if
not
check_types
(
op1
,
allowed_types
)
and
op1
is
not
None
:
r
=
(
"ошибка в первом операнде"
)
elif
not
check_types
(
op2
,
allowed_types
)
and
op2
is
not
None
:
r
=
(
"ошибка во втором операнде"
)
else
:
if
type
(
op1
)
!=
float
and
type
(
op2
)
!=
float
:
if
type
(
op1
)
in
(
int
,
str
):
op1
=
float
(
op1
)
if
type
(
op2
)
in
(
int
,
str
):
op2
=
float
(
op2
)
if
(
type
(
op1
)
is
list
and
type
(
op2
)
is
list
)
or
(
type
(
op1
)
is
tuple
and
type
(
op2
)
is
tuple
):
if
act
==
"+"
:
r
=
op1
+
op2
else
:
print
(
"Действие невозможно"
)
if
type
(
op1
)
in
(
list
,
tuple
)
and
op2
==
None
:
if
act
==
"+"
:
r
=
0
for
i
in
op1
:
r
+=
i
return
r
else
:
print
(
"Действие невозможно"
)
pass
if
act
==
"+"
:
r
=
op1
+
op2
elif
act
==
"-"
:
r
=
op1
-
op2
elif
act
==
"*"
:
r
=
op1
*
op2
elif
act
==
"/"
:
if
op2
!=
0
:
r
=
op1
/
op2
else
:
r
=
"деление на ноль невозможно"
else
:
r
=
"Операция не распознана"
return
r
def
main
():
operand1
=
float
(
input
(
"Enter 1 value: "
))
operand2
=
float
(
input
(
"Enter 2 value: "
))
action
=
input
(
"Enter type of operation: "
)
result
=
(
calculate
(
operand1
,
operand2
,
action
))
print
(
"Result is: "
+
str
(
result
))
if
__name__
==
"__main__"
:
main
()
print
(
"running by interpreter"
)
\ No newline at end of file
PARAMS
=
{
'precision'
:
0.00001
,
'output_type'
:
float
,
'possible_types'
:
(
int
,
float
)}
def
convert_precision
(
prec
):
''''
Преобразует точность данную в виде числа с плавающей точкой в целое число, возможное для использования с функцией round.
'''
prec
=
str
(
prec
)
for
i
in
prec
:
if
i
==
'e'
:
x
=
prec
.
split
(
'-'
)
return
int
(
x
[
1
])
if
i
==
"."
:
x
=
prec
.
split
(
'.'
)
return
int
(
len
(
x
[
1
]))
def
calculate
(
*
args
,
**
kwargs
):
precision
=
convert_precision
(
kwargs
[
'precision'
])
output_type
=
kwargs
[
'output_type'
]
print
(
precision
)
if
args
[
len
(
args
)
-
1
]
==
'+'
:
result_sum
=
sum
(
args
[
0
:
len
(
args
)
-
1
])
if
type
(
result_sum
)
is
not
output_type
:
result_sum
=
output_type
(
result_sum
)
return
result_sum
if
args
[
len
(
args
)
-
1
]
==
'-'
:
result_diff
=
0
for
n
in
args
[
0
:
len
(
args
)
-
1
]:
result_diff
-=
n
if
type
(
result_diff
)
is
not
output_type
:
result_sum
=
output_type
(
result_diff
)
return
result_diff
if
args
[
len
(
args
)
-
1
]
==
'*'
:
result_mult
=
1
for
n
in
args
[
0
:
len
(
args
)
-
1
]:
result_mult
*=
n
if
type
(
result_mult
)
is
not
output_type
:
result_mult
=
output_type
(
result_mult
)
return
round
(
result_mult
,
precision
)
if
args
[
len
(
args
)
-
1
]
==
'/'
:
result_division
=
args
[
0
]
for
n
in
args
[
1
:
len
(
args
)
-
1
]:
result_division
/=
n
if
type
(
result_division
)
is
not
output_type
:
result_division
=
output_type
(
result_division
)
return
round
(
result_division
,
precision
)
# print(calculate(*list(range(1, 10)), "+", **PARAMS)) # 1 + 2 + 3 + .. + 9
# print(calculate(*list(range(1, 10)), "-", **PARAMS))
print
(
calculate
(
*
list
(
range
(
1
,
5
)),
"/"
,
**
PARAMS
))
# print(calculate(*[1.0001, 2.2345], "*", **PARAMS)) # 2.23472345
def
test_packed_calc_sum
():
inp1
,
action
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
],
"+"
assert
calculate
(
*
inp1
,
action
,
**
PARAMS
)
==
45.0
,
'Sum of list of ints from 1 to 9'
assert
type
(
calculate
(
*
inp1
,
action
,
**
PARAMS
))
is
type
(
45.0
),
'Type of sum is incorrect'
# test_packed_calc_sum()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment