Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
String Calculator
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Muhamed Alici
String Calculator
Commits
eaff9c27
Commit
eaff9c27
authored
2 weeks ago
by
Muhamed
Browse files
Options
Downloads
Patches
Plain Diff
negative strings and added setup method
parent
83e891ad
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
String calculator
+46
-13
46 additions, 13 deletions
String calculator
with
46 additions
and
13 deletions
String calculator
+
46
−
13
View file @
eaff9c27
...
...
@@ -2,6 +2,17 @@
#test no string
#test add 1 string
#test adding 2 strings
#add 3 strings
#add 4 strings
#add 6 strings
#\n statt , bei 2 strings
#\n statt , bei mehreren strings
# eine negative zahl
# mehrere negative zahlen
import unittest
class StringCalculator:
...
...
@@ -9,25 +20,47 @@ class StringCalculator:
if not numbers:
return 0
else:
numbers = numbers.replace('\n',',')
number_list = numbers.split(',')#aufteilung der strings
if len(number_list) ==1:
return int(number_list[0])#bei einem string wird er in int umgewandelt
elif len(number_list)==2:
return int(number_list[0])+ int(number_list[1])#bei 2 strings wird beides in int verwandelt und die summe zurück gegeben(Position 1/0+Position 2/1)
return 0
negative=[] #array für die negativen nummern
total_sum = 0
for number_str in number_list: #jedes element in der liste wird durch genommen
if number_str:
number = int(number_str)# summierung der integer
if number <0:
negative.append(number)
total_sum += number
if negative:
print(f"Negative Zahlen:{negative} ")#ausgabe negative zahlen
raise ValueError(f"negatives not allowed: {','.join(map(str, negative))}")
return total_sum
class TestStingCalculator(unittest.TestCase):
def setUp(self):
self.calculator = StringCalculator()
def test_empty_string(self):
calculator = StringCalculator()
self.assertEqual(calculator.add(""), 0) #leerer string + 0
self.assertEqual(self.calculator.add(""), 0) #leerer string + 0
def test_single_number(self):
calculator = StringCalculator()
self.assertEqual(calculator.add("5"),5) #eingabe von einem String
self.assertEqual(self.calculator.add("5"),5) #eingabe von einem String
def test_multiple_numbers(self):
calculator=StringCalculator()
self.assertEqual(calculator.add("5,5"),10)#eingabe mehrere strings
self.assertEqual(self.calculator.add("5,5"),10)#eingabe mehrere strings
def test_unknowen_amount_of_numbers(self):
self.assertEqual(self.calculator.add("1,2,3"),6)
self.assertEqual(self.calculator.add("10,20,30,40"),100)
self.assertEqual(self.calculator.add("1,2,3,4,5,6"),21)
def test_numbers_seperated_by_newline(self):
self.assertEqual(self.calculator.add("1\n2"),3)
self.assertEqual(self.calculator.add("1\n2\n3"),6)
self.assertEqual(self.calculator.add("10,20\n30"),60)
def test_negative_number_exception(self):
with self.assertRaises(ValueError) as e:
self.calculator.add("-1,2")
self.assertEqual(str(e.exception), "negatives not allowed: -1")
if __name__=='__main__':
unittest.main()
\ No newline at end of file
unittest.main()
\ No newline at end of file
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
register
or
sign in
to comment