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
6f494100
Commit
6f494100
authored
3 months ago
by
Muhamed
Browse files
Options
Downloads
Plain Diff
Merge branch 'Feature-Hatice' into develop
parents
d68e3b06
c383dc7e
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
String calculator #hatice
+136
-0
136 additions, 0 deletions
String calculator #hatice
with
136 additions
and
0 deletions
String calculator #hatice
0 → 100644
+
136
−
0
View file @
6f494100
#vorher im anderen projekt jetzt im neuen projekt
#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
# Delimiters:
# add numbers seperated by a custom delimiter
# Test with - Numbers
#numbers over 1000 should be ignored
#1001 + 2 should return 2
#1001 -2 should return negatives not allowed
#custom delimiters
#input "//[***]\n1***2***3"
#output 6
import unittest
import re
class StringCalculator:
def __init__(self):
self.called_count = 0
def add (self, numbers : str)-> int: #Erwartet Integer
if not numbers:
return 0
if numbers.startswith("//"):
delimiter_end_index = numbers.index("\n")
delimiter_section = numbers[2:delimiter_end_index]
numbers = numbers[delimiter_end_index +1:]
if delimiter_section.startswith("[")and delimiter_section.endswith("]"):
delimiters = re.findall(r'\[([^\]]+)\]',delimiter_section)
else:
delimiters= [delimiter_section]
for delimiters in delimiters:
numbers = numbers.replace(delimiters,",")
numbers = numbers.replace("\n",",")
number_list = numbers.split(',')#aufteilung der strings
negative = [] #array für die negativen nummern
numbers_to_add = []
#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)
elif number > 1000:
continue
else:
numbers_to_add.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))}")
self.called_count += 1
return sum(numbers_to_add)
def get_called_count(self):
return self.called_count
class TestStingCalculator(unittest.TestCase):
def setUp(self):
self.calculator = StringCalculator()
def test_empty_string(self):
self.assertEqual(self.calculator.add(""), 0) #leerer string + 0
def test_single_number(self):
self.assertEqual(self.calculator.add("5"),5) #eingabe von einem String
def test_multiple_numbers(self):
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")
def test_multiple_negative_numbers_exception(self):
with self.assertRaises(ValueError)as e:
self.calculator.add("-1,-2,3")
self.assertEqual(str(e.exception),"negatives not allowed: -1,-2")
with self.assertRaises(ValueError) as e:
self.calculator.add("-1,-3,4")
self.assertEqual(str(e.exception),"negatives not allowed: -1,-3")
with self.assertRaises(ValueError) as e:
self.calculator.add("-1\n-3,4")
self.assertEqual(str(e.exception),"negatives not allowed: -1,-3")
def test_add_numbers_with_custom_delimiter(self):
self.assertEqual(self.calculator.add("//;\n1;2;3"),6)
self.assertEqual(self.calculator.add("//;\n1,2,3"),6)
with self.assertRaises(ValueError) as e:
self.calculator.add("//;\n-3,4")
self.assertEqual(str(e.exception),"negatives not allowed: -3")
def test_add_numbers_greater_than_1000(self):
self.assertEqual(self.calculator.add("1,1001,2,3"),6)
def test_add_numbers_greater_than_1000_1002(self):
self.assertEqual(self.calculator.add("1002,1,2,3"),6)
def test_add_numbers_greater_1000_and_minus(self):
with self.assertRaises(ValueError) as e:
self.calculator.add("//;\n-3,4;1001")
self.assertEqual(str(e.exception),"negatives not allowed: -3")
def test_custom_delimiter(self):
self.assertEqual(self.calculator.add("//[***]\n1***2***3"),6)
def test_custom_del(self):
self.assertEqual(self.calculator.add("//[+++]\n1+++2+++3"),6)
def test_custom_del2(self):
self.assertEqual(self.calculator.add("//[aa]\n1aa2aa3"),6)
if __name__=='__main__':
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