Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions HW3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
Create required phrase.
----------------------
You are given a string of available characters and a string representing a word or a phrase that you need to generate.
Write a function that checks if you cab generate required word/phrase using the characters provided.
If you can, then please return True, otherwise return False.
NOTES:
You can only generate the phrase if the frequency of unique characters in the characters string is equal or greater
than frequency in the document string.
FOR EXAMPLE:
characters = "cbacba"
phrase = "aabbccc"
In this case you CANNOT create required phrase, because you are 1 character short!
IMPORTANT:
The phrase you need to create can contain any characters including special characters, capital letter, numbers
and spaces.
You can always generate an empty string.
"""
from collections import Counter

def generate_phrase(letters: str, phrase: str) -> bool:
phrase_counter = Counter(phrase.lower())
characters_counter = Counter(letters.lower())

if not phrase_counter and not characters_counter:
return True

if len(phrase) > len(letters):
return False

for ch, counter in phrase_counter.items():

if ch in characters_counter and counter <= characters_counter.get(ch):
continue
else:
return False

return True


if __name__ == '__main__':
characters = "cbacba"
phrase = "aabbccc"

print(generate_phrase(characters, phrase))

print(generate_phrase('you name it M1/P9', 'my name 1'))


Binary file not shown.
Binary file added Theory questions assignment .pdf
Binary file not shown.
23 changes: 23 additions & 0 deletions hw4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

def search(matrix, x):

for i in range(len(matrix)):
for j in range(len(matrix)):

if (matrix[i][j] == x):
print("Element found at (", i, ",", j, ")")
return 1

print("[-1, -1]")
return 0

if __name__ == "__main__":
matrix = [
[1,4,7,12,15,1000],
[2,5,19,31,32,1001],
[3,8,24,33,35,1002],
[40,41,42,44,45,1003],
[99,100,103,106,128,1004]
]

search(matrix,44)