Added task 3

This commit is contained in:
2024-12-21 23:50:13 +02:00
parent 94318de22d
commit 82ac1fcf4d
7 changed files with 133 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
def countAlphaBet(txt: str) -> int:
count = 0
for char in txt:
if char.isalpha(): # בודק אם התו הוא אות
count += 1
return count
def main_1ex():
txt1 = "Hello World!"
print(f'in {txt1} there are {countAlphaBet(txt1)} alphabetic characters')
txt2 = "123 test 123 test"
print(f'in {txt2} there are {countAlphaBet(txt2)} alphabetic characters')
main_1ex()

View File

@@ -0,0 +1,23 @@
def sameType(n1: int, n2: int) -> bool:
return (n1 % 2 == n2 % 2) # אם שניהם זוגיים או שניהם אי זוגיים
def main_2ex():
n1, n2 = 2, 4
if sameType(n1, n2):
print(f'{n1} and {n2} are the same type')
else:
print(f'{n1} and {n2} are not the same type')
n1, n2 = 3, 7
if sameType(n1, n2):
print(f'{n1} and {n2} are the same type')
else:
print(f'{n1} and {n2} are not the same type')
n1, n2 = 5, 8
if sameType(n1, n2):
print(f'{n1} and {n2} are the same type')
else:
print(f'{n1} and {n2} are not the same type')
main_2ex()

View File

@@ -0,0 +1,17 @@
def get_middle(n1: float, n2: float, n3: float) -> int:
# בודק את המספר האמצעי באופן ישיר:
if (n1 >= n2 and n1 <= n3) or (n1 <= n2 and n1 >= n3):
return n1
elif (n2 >= n1 and n2 <= n3) or (n2 <= n1 and n2 >= n3):
return n2
else:
return n3
def main_3ex():
n1, n2, n3 = 8, 2, 6
print(f'Amoung {n1} {n2} {n3} the middle is: {get_middle(n1, n2, n3)}')
n1, n2, n3 = 5, 9, 3
print(f'Amoung {n1} {n2} {n3} the middle is: {get_middle(n1, n2, n3)}')
main_3ex()

View File

@@ -0,0 +1,18 @@
def split(txt: str) -> tuple:
space_index = 0
while txt[space_index] != ' ':
space_index += 1
part1 = txt[:space_index]
part2 = txt[space_index+1:]
return part1, part2
def main_4ex():
txt1 = "Hello World"
part1, part2 = split(txt1)
print(f'For input {txt1} we get {part1} and {part2}')
txt2 = "Leo Messi"
part1, part2 = split(txt2)
print(f'For input {txt2} we get {part1} and {part2}')
main_4ex()

View File

@@ -0,0 +1,16 @@
def sequence_has(txt: str) -> bool:
i = 0
while i < len(txt) - 1:
if txt[i] == txt[i + 1]: # אם יש תו שחוזר ברצף
return True
i += 1
return False
def main_5ex():
txt1 = "apple"
print(f'{txt1} contains a sequence' if sequence_has(txt1) else f'{txt1} does not contains a sequence')
txt2 = "banana"
print(f'{txt2} contains a sequence' if sequence_has(txt2) else f'{txt2} does not contains a sequence')
main_5ex()

View File

@@ -0,0 +1,24 @@
def duplicates_has(txt: str) -> str:
seen = ""
for char in txt:
if char in seen:
return char
seen += char
return None
def main_6ex():
txt1 = "banana"
result = duplicates_has(txt1)
if result:
print(f'In {txt1} {result} appears more than once')
else:
print(f'In {txt1} no character appears more than once')
txt2 = "pear"
result = duplicates_has(txt2)
if result:
print(f'In {txt2} {result} appears more than once')
else:
print(f'In {txt2} no character appears more than once')
main_6ex()

View File

@@ -0,0 +1,20 @@
def duplicates_remove(txt: str) -> str:
seen = ""
result = ""
for char in txt:
if char not in seen:
result += char
seen += char
return result
def main_7ex():
txt1 = "banana"
print(f'{txt1} {duplicates_remove(txt1)}')
txt2 = "apple"
print(f'{txt2} {duplicates_remove(txt2)}')
txt3 = "pear"
print(f'{txt3} {duplicates_remove(txt3)}')
main_7ex()