Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
StringCalculator
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Automate
Agent sessions
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Lasse Pikkemaat
StringCalculator
Commits
700de9f9
Commit
700de9f9
authored
6 months ago
by
Lasse Pikkemaat
Browse files
Options
Downloads
Plain Diff
Merge branch 'feature4' into 'develop'
Feature4 See merge request
!4
parents
a22c61d5
dd90e7ac
No related branches found
No related tags found
2 merge requests
!7
Develop into main
,
!4
Feature4
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/stringcalculator.py
+14
-8
14 additions, 8 deletions
src/stringcalculator.py
tests/test_stringclaculator.py
+13
-0
13 additions, 0 deletions
tests/test_stringclaculator.py
with
27 additions
and
8 deletions
src/stringcalculator.py
+
14
−
8
View file @
700de9f9
...
@@ -7,17 +7,23 @@ class StringCalculator(IStringCalculator):
...
@@ -7,17 +7,23 @@ class StringCalculator(IStringCalculator):
if
not
numbers
:
if
not
numbers
:
return
0
return
0
# Prüfe auf das ungültige Format "1,\n"
if
numbers
.
startswith
(
"
//
"
):
if
"
,
\n
"
in
numbers
:
if
"
\n
"
not
in
numbers
:
raise
ValueError
(
"
Ungültiges Zahlenformat:
'
,
\\
n
'
ist nicht erlaubt
"
)
raise
ValueError
(
"
Ungültiges Format: Nicht vollständig
"
)
delimiter_end_index
=
numbers
.
index
(
"
\n
"
)
delimiter
=
numbers
[
2
:
delimiter_end_index
]
numbers
=
numbers
[
delimiter_end_index
+
1
:]
numbers
=
numbers
.
replace
(
delimiter
,
"
,
"
)
# Trenne Zahlen anhand von Komma oder Zeilenumbruch
numbers
=
numbers
.
replace
(
"
\n
"
,
"
,
"
)
tokens
=
re
.
split
(
r
"
,|\n
"
,
numbers
)
# Konvertiere zu Integern und finde negative Zahlen
# Split the string by commas, convert each value to an integer, and sum them up
numbers_list
=
list
(
map
(
int
,
tokens
))
try
:
negative_numbers
=
[
num
for
num
in
numbers_list
if
num
<
0
]
numbers_list
=
list
(
map
(
int
,
numbers
.
split
(
"
,
"
)))
except
ValueError
:
raise
ValueError
(
"
Ungültiges Zahlenformat: Enthält nicht-numerische Werte
"
)
negative_numbers
=
[
num
for
num
in
numbers_list
if
num
<
0
]
if
negative_numbers
:
if
negative_numbers
:
raise
ValueError
(
f
"
Negative nicht erlaubt:
{
negative_numbers
}
"
)
raise
ValueError
(
f
"
Negative nicht erlaubt:
{
negative_numbers
}
"
)
...
...
This diff is collapsed.
Click to expand it.
tests/test_stringclaculator.py
+
13
−
0
View file @
700de9f9
...
@@ -12,6 +12,10 @@
...
@@ -12,6 +12,10 @@
#Feature3 Negative Zahlen ausschließen
#Feature3 Negative Zahlen ausschließen
# Bei Eingabe von 1, -2, 3 soll ein Error erscheinen "Negative nicht erlaubt: [-2]
# Bei Eingabe von 1, -2, 3 soll ein Error erscheinen "Negative nicht erlaubt: [-2]
# Bei Eingabe von -10\n -20, -30 soll ein Error erscheinen "Negative nicht erlaubt: [-10,-20,30]
# Bei Eingabe von -10\n -20, -30 soll ein Error erscheinen "Negative nicht erlaubt: [-10,-20,30]
#Feature4 Eigene Trennzeichen eingeben
# Bei Eingabe eines neuen Trennzeichen ";" //;\n1;2 soll 3 ausgegeben werden
# Bei Eingabe eines neuen Trennzeichen "x" //-\n7x8\n9 soll 24 ausgegeben werden
# Bei Eingabe eines neuen Trennzeichens ";" ohne vollständigen Ausdrück //;1;2 soll Ungültiges Format: Nicht vollständig
import
unittest
import
unittest
from
src.interfaces
import
IStringCalculator
from
src.interfaces
import
IStringCalculator
...
@@ -50,6 +54,15 @@ class TestStringCalculator(unittest.TestCase):
...
@@ -50,6 +54,15 @@ class TestStringCalculator(unittest.TestCase):
print
(
str
(
context
.
exception
))
print
(
str
(
context
.
exception
))
self
.
assertEqual
(
str
(
context
.
exception
),
"
Negative nicht erlaubt: [-10, -20, -30]
"
)
self
.
assertEqual
(
str
(
context
.
exception
),
"
Negative nicht erlaubt: [-10, -20, -30]
"
)
def
test_add_with_custom_delimiter
(
self
):
self
.
assertEqual
(
self
.
calculator
.
add
(
"
//;
\n
1;2
"
),
3
)
self
.
assertEqual
(
self
.
calculator
.
add
(
"
//x
\n
7x8
\n
9
"
),
24
)
def
test_invalid_custom_delimiter_format
(
self
):
with
self
.
assertRaises
(
ValueError
)
as
context
:
self
.
calculator
.
add
(
"
//;1;2
"
)
print
(
str
(
context
.
exception
))
if
__name__
==
"
__main__
"
:
if
__name__
==
"
__main__
"
:
unittest
.
main
()
unittest
.
main
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment