From 89b7b9472cfd5a89bdae5ed4abafdec6154de3d5 Mon Sep 17 00:00:00 2001 From: dorondollev Date: Wed, 16 Mar 2022 20:53:05 +0200 Subject: [PATCH 01/48] Added the American choise answer --- 06_linux_ex2/README | 58 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/06_linux_ex2/README b/06_linux_ex2/README index 8489ffb1..d220a891 100644 --- a/06_linux_ex2/README +++ b/06_linux_ex2/README @@ -1,2 +1,58 @@ -name@example.com +doron.dolle@gmail.com +### Processes handling + +**(Q1)** A user started a process and logged out from the terminal. Which command he used if the process still running in the background: + +* nohup + + +**(Q2)** The `kill` command always terminates a process. + +* False + + +**(Q3)** Which command could be used to know how many processes are running in the background terminal session? + +* jobs + + +**(Q4)** Given a terminal session with long process running in it, how will you ask this process to terminate? + +* CTRL+c + + +**(Q5)** Given a terminal session with long process running in it, how will you ask this process the stop? + +* CTRL+z + + +**(Q6)** How would you run the `sleep 10` command as a foreground process? + +* sleep 10 + + +**(Q7)** Which of the following command would deliver a SIGTERM to the `xscreensaver` process? + +* None of the above + + +**(Q8)** Which of the following would deliver a SIGKILL to the `xscreensaver` command? + +* kill -9 4846 + + +**(Q9)** Which of the following would send a SIGCHLD (signal number 17) to the `ssh-agent` process? + +* kill -CHLD 4828 + + +**(Q10)** Which key pressed within the `top` command allows the user to send a signal to a process? + +* k + + +**(Q11 - easy 5 points bonus)** Open a new terminal session and type the command `python`. Then send a SIGINT signal using your keyboard. What best describes + how the python process responds to the SIGINT signal? + +* The program has implemented a custom signal handler for the SIGINT signal. From 49b7805c13e8ea927b14aa1702adf6b8fc829356 Mon Sep 17 00:00:00 2001 From: doron Date: Wed, 23 Mar 2022 01:48:08 +0200 Subject: [PATCH 02/48] provided hand shake script over TLS --- 06_linux_ex2/README | 2 +- 06_linux_ex2/tlsHandshake.sh | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/06_linux_ex2/README b/06_linux_ex2/README index d220a891..62f42731 100644 --- a/06_linux_ex2/README +++ b/06_linux_ex2/README @@ -1,4 +1,4 @@ -doron.dolle@gmail.com +doron.dollev@gmail.com ### Processes handling diff --git a/06_linux_ex2/tlsHandshake.sh b/06_linux_ex2/tlsHandshake.sh index e69de29b..49180fe5 100644 --- a/06_linux_ex2/tlsHandshake.sh +++ b/06_linux_ex2/tlsHandshake.sh @@ -0,0 +1,46 @@ +#!/bin/bash +echo "Initializing random number generator..." +openssl rand -out masterKey.txt -base64 32 +if [ $? -gt 0 ] +then + echo "couldn't generate key 32 based" + exit 1 +fi +curl -X POST -H "Content-Type: application/json" -d '{"clientVersion":"3.2","message":"Client Hello"}' http://devops-jan22-1273001359.eu-north-1.elb.amazonaws.com:8080/clienthello | jq -r '.serverCert,.sessionID' >tmp.txt +STATUS=$? +if [ $STATUS -gt 0 ] +then + echo "run \"man curl|jq\" end check for details for error status number $STATUS" + # for example: "error 20 at 0 depth lookup: unable to get local issuer certificate" + # This is Eve that is the reason error 20 is unlisted in curl + exit 1 +fi +# Instead could be used /clienthello -o (>) tmp.txt and use twice jq -r on cert.pem and sessionID +head --lines=-1 tmp.txt>cert.pem +export SESSION_ID=$( tail -1 tmp.txt ) +rm -f tmp.txt +export VERIFICATION_RESULT=$( openssl verify -CAfile cert-ca-aws.pem cert.pem ) + +if [ "$VERIFICATION_RESULT" != "cert.pem: OK" ] +then + echo "Server Certificate is invalid." + exit 1 +fi + +MASTER_KEY=$( openssl smime -encrypt -aes-256-cbc -in masterKey.txt -outform DER cert.pem | base64 -w 0 ) +curl -X POST -H 'Content-Type: application/json' -d '{"sessionID": "'$SESSION_ID'","masterKey": "'$MASTER_KEY'","sampleMessage": "Hi server, please encrypt me and send to client!"}' http://devops-jan22-1273001359.eu-north-1.elb.amazonaws.com:8080/keyexchange | jq -r '.encryptedSampleMessage' > encSampleMsg.txt +STATUS=$? +if [ $STATUS -gt 0 ] +then + echo "run \"man curl|jq\" end check for details for error status number $STATUS" + exit 1 +fi +cat encSampleMsg.txt | base64 -d > encSampleMsgReady.txt +openssl enc -d -aes-256-cbc -pbkdf2 -kfile masterKey.txt -in encSampleMsgReady.txt -out decrypted_secret.txt +export DECRYPTED_SAMPLE_MESSAGE=`cat decrypted_secret.txt` +if [ "$DECRYPTED_SAMPLE_MESSAGE" != "Hi server, please encrypt me and send to client!" ]; then + echo "Server symmetric encryption using the exchanged master-key has failed." + exit 1 +else + echo "Client-Server TLS handshake has been completed successfully" +fi \ No newline at end of file From 18c9dc365e1bf2ba9f9dfe10bd9fd2776464af4e Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 5 Apr 2022 08:12:02 +0300 Subject: [PATCH 03/48] added sum for elements --- python_katas/kata_1/test.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/python_katas/kata_1/test.py b/python_katas/kata_1/test.py index 7335eccb..0849e854 100644 --- a/python_katas/kata_1/test.py +++ b/python_katas/kata_1/test.py @@ -2,7 +2,6 @@ from python_katas.kata_1 import questions from python_katas.utils import unittest_runner - testers = ['dorondollev', 'danielbar0101', 'DustyMadDude', @@ -17,14 +16,13 @@ class TestSumOfElements(unittest.TestCase): 1 Katas """ - def sum_of_element(elements): - return sum(elements) + def sum_of_element(self): + return sum(self) def test_empty_list(self): lst = [] self.assertEqual(questions.sum_of_element(lst), 0) - def test_integers_list(self): lst = [1, 2, 3, 4, 5] self.assertEqual(questions.sum_of_element(lst), 15) @@ -191,4 +189,5 @@ def test_sample(self): if __name__ == '__main__': import inspect import sys + unittest_runner(inspect.getmembers(sys.modules[__name__], inspect.isclass)) From 9f67be3f3cce1d90c36d627fdcfbc69454846071 Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 5 Apr 2022 09:23:27 +0300 Subject: [PATCH 04/48] added verbing --- python_katas/kata_1/questions.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py index 95069ee1..4c4ab10d 100644 --- a/python_katas/kata_1/questions.py +++ b/python_katas/kata_1/questions.py @@ -5,11 +5,7 @@ def sum_of_element(elements): :param elements: list of integers :return: Return int - the sum of all elements. """ - s = 0 - for num in elements: - s = s + num - - return s + return num(elements) def verbing(word): @@ -28,7 +24,13 @@ def verbing(word): :param word: str :return: Return the resulting string. """ - return None + my_str = word + if len(my_str) < 3: + return my_str + elif my_str[-3:] == 'ing': + return my_str += 'ly' + else + return my_str += 'ing' def words_concatenation(words): From 5fa00df93df8a7f621dc80c1a9841f03daa09a83 Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 5 Apr 2022 11:18:26 +0300 Subject: [PATCH 05/48] added words_concatenation --- python_katas/kata_1/questions.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py index 4c4ab10d..352686e5 100644 --- a/python_katas/kata_1/questions.py +++ b/python_katas/kata_1/questions.py @@ -28,9 +28,10 @@ def verbing(word): if len(my_str) < 3: return my_str elif my_str[-3:] == 'ing': - return my_str += 'ly' + my_str += 'ly' else - return my_str += 'ing' + my_str += 'ing' + return my_str def words_concatenation(words): @@ -45,7 +46,8 @@ def words_concatenation(words): :param words: list of str :return: Return the resulting string. """ - return None + my_lst = ' '.join(words) + return my_lst def reverse_words_concatenation(words): From 3e92edfedab50cbcdaf0400e2854449821bdf248 Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 5 Apr 2022 15:01:26 +0300 Subject: [PATCH 06/48] added reverse_words_concatenation --- python_katas/kata_1/questions.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py index 352686e5..765b7790 100644 --- a/python_katas/kata_1/questions.py +++ b/python_katas/kata_1/questions.py @@ -62,7 +62,10 @@ def reverse_words_concatenation(words): :param words: list of str :return: Return the resulting string. """ - return None + strings = words + strings.reverse() + my_lst = words_concatenation(strings) + return my_lst def is_unique_string(some_str): From 8a869c32dea6fac388debfeef3f3b50f5edfbbe3 Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 5 Apr 2022 20:51:34 +0300 Subject: [PATCH 07/48] fixed verbing --- python_katas/kata_1/questions.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py index 765b7790..e43d473a 100644 --- a/python_katas/kata_1/questions.py +++ b/python_katas/kata_1/questions.py @@ -5,7 +5,7 @@ def sum_of_element(elements): :param elements: list of integers :return: Return int - the sum of all elements. """ - return num(elements) + return sum(elements) def verbing(word): @@ -28,8 +28,9 @@ def verbing(word): if len(my_str) < 3: return my_str elif my_str[-3:] == 'ing': + my_str = my_str[:-3] my_str += 'ly' - else + else: my_str += 'ing' return my_str @@ -82,8 +83,10 @@ def is_unique_string(some_str): :param some_str: :return: bool """ - return None - + if len(set(some_str)) == len(some_str): + return True + else: + return False def list_diff(elements): """ From 2cbd23c25c9d60e43ef92ac164733e680e5a9048 Mon Sep 17 00:00:00 2001 From: doron Date: Thu, 7 Apr 2022 12:36:09 +0300 Subject: [PATCH 08/48] created prime_number --- python_katas/kata_1/questions.py | 33 ++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py index 17897a68..b90bb77e 100644 --- a/python_katas/kata_1/questions.py +++ b/python_katas/kata_1/questions.py @@ -88,7 +88,9 @@ def is_unique_string(some_str): else: return False + def list_diff(elements): + """ 1 Kata @@ -103,20 +105,31 @@ def list_diff(elements): :param elements: list of integers :return: the diff list """ - return None + if len(elements) > 1: + diff_list = [elements[i] - elements[i - 1] for i in range(1, len(elements))] + diff_list.insert(0, None) + return diff_list + elif len(elements) > 0: + return [None] + else: + return [] def prime_number(num): """ - 1 Kata + 1 Kata - Check if the given number is prime or not. + Check if the given number is prime or not. - hint: use the built-in function "range" - :param num: the number to check - :return: bool. True if prime, else False - """ - return None + hint: use the built-in function "range" + :param num: the number to check + :return: bool. True if prime, else False + """ + if num > 1: + for i in range(2, num): + if (num % i) == 0: + return False + return True def palindrome_num(num): @@ -168,14 +181,14 @@ def pair_match(men, women): def bad_average(a, b, c): """ - 1 Kata + 1 Kata: fixed This function gets 3 numbers and calculates the average. There is a mistake in the following implementation, you are required to fix it :return: """ - return a + b + c / 3 + return (a + b + c) / 3 def best_student(grades): From cfa098b7d4afc35623ec4bfaf192561e9bd2d67f Mon Sep 17 00:00:00 2001 From: doron Date: Thu, 7 Apr 2022 13:58:29 +0300 Subject: [PATCH 09/48] finished my tests: 3, 10 --- python_katas/kata_1/questions.py | 7 +++++- python_katas/kata_1/test.py | 42 +++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py index b90bb77e..f6ceae6f 100644 --- a/python_katas/kata_1/questions.py +++ b/python_katas/kata_1/questions.py @@ -145,7 +145,12 @@ def palindrome_num(num): :param num: int :return: bool. True is palindrome, else False """ - return None + my_num = str(num) + my_rnum = my_num[::-1] + if my_num == my_rnum: + return True + else: + return False def pair_match(men, women): diff --git a/python_katas/kata_1/test.py b/python_katas/kata_1/test.py index 0849e854..d3502df3 100644 --- a/python_katas/kata_1/test.py +++ b/python_katas/kata_1/test.py @@ -16,8 +16,8 @@ class TestSumOfElements(unittest.TestCase): 1 Katas """ - def sum_of_element(self): - return sum(self) +# def sum_of_element(self): +# return sum(self) def test_empty_list(self): lst = [] @@ -40,7 +40,6 @@ class TestVerbing(unittest.TestCase): """ 1 Katas """ - def test_sample(self): # your code here pass @@ -51,12 +50,25 @@ class TestWordsConcatenation(unittest.TestCase): 1 Katas """ - def test_sample(self): - # your code here - pass + def concat_empty_list(self): + lst = [] + self.assertEqual(questions.words_concatenation(lst), 0) + + def concat_int_list(self): + lst = [1, 100, 111, 11] + self.assertEqual(questions.words_concatenation(lst), 0) + + def concat_nested_lists(self): + lst = [[1], [3], [2, 0], [4, 1]] + self.assertEqual(questions.words_concatenation(lst), 0) + + def concat_mixed_elements(self): + lst = [1, 'foo', 'bar', 'baz', 2] + self.assertEqual(questions.words_concatenation(lst), 0) class TestReverseWordsConcatenation(unittest.TestCase): + """ 1 Katas """ @@ -121,9 +133,21 @@ class TestBadAverage(unittest.TestCase): 1 Katas """ - def test_sample(self): - # your code here - pass + def BadAverage_int_elements(self): + lst = [1, 2, 3] + self.assertEqual(questions.bad_average(lst), 0) + + def BadAverage_big_negative_element(self): + lst = [-500000000000000000000000000, 50000000000000000000000001, 1] + self.assertEqual(questions.bad_average(lst), 0) + + def BadAverage_negative_elements(self): + lst = [-1, -2, -3] + self.assertEqual(questions.bad_average(lst), 0) + + def BadAverage_float_elements(self): + lst = [1.1111111111111, 2.2, 3.5] + self.assertEqual(questions.bad_average(lst), 0) class TestBestStudent(unittest.TestCase): From 868b90830a8437264ad910b0dae967f91bb8fb1b Mon Sep 17 00:00:00 2001 From: doron Date: Fri, 8 Apr 2022 14:14:15 +0300 Subject: [PATCH 10/48] finished my tests: 4, 11 --- python_katas/kata_1/test.py | 67 +++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/python_katas/kata_1/test.py b/python_katas/kata_1/test.py index d3502df3..38f34601 100644 --- a/python_katas/kata_1/test.py +++ b/python_katas/kata_1/test.py @@ -50,32 +50,27 @@ class TestWordsConcatenation(unittest.TestCase): 1 Katas """ - def concat_empty_list(self): - lst = [] - self.assertEqual(questions.words_concatenation(lst), 0) - - def concat_int_list(self): - lst = [1, 100, 111, 11] - self.assertEqual(questions.words_concatenation(lst), 0) - - def concat_nested_lists(self): - lst = [[1], [3], [2, 0], [4, 1]] - self.assertEqual(questions.words_concatenation(lst), 0) - - def concat_mixed_elements(self): - lst = [1, 'foo', 'bar', 'baz', 2] - self.assertEqual(questions.words_concatenation(lst), 0) - class TestReverseWordsConcatenation(unittest.TestCase): """ 1 Katas """ + def reverse_empty_string(self): + lst = [] + self.assertEqual(questions.reverse_words_concatenation(lst), 0) - def test_sample(self): - # your code here - pass + def reverse_long_strings(self): + lst = ["runasdfasdffwerfzvxzcvzxcvfassdafasfeqewr", "jvxcvzxcvzxvcxzcvxzvcxvcohnny", "hzxvczxvcxzcvzxvcxzcvome"] + self.assertEqual(questions.reverse_words_concatenation(lst), 0) + + def reverse_integer_string(self): + lst = ['1', '3', '2', '0'] + self.assertEqual(questions.reverse_words_concatenation(lst), 0) + + def reverse_mixed_elements(self): + lst = ["1", 'foo', 'bar', 'baz', "2"] + self.assertEqual(questions.reverse_words_concatenation(lst), 0) class TestIsUniqueString(unittest.TestCase): @@ -155,9 +150,37 @@ class TestBestStudent(unittest.TestCase): 1 Katas """ - def test_sample(self): - # your code here - pass + def BestGrade_example_elements(self): + dict1 = { + "Ben": 78, + "Hen": 88, + "Natan": 99, + "Efraim": 65, + "Rachel": 95 + } + self.assertEqual(questions.best_student(dict1), 0) + + + def bestgrade_overgrade_elements(self): + dict1 = { + "Ben": 101, + "Hen": 112, + "Natan": 110, + "Efraim": 103, + "Rachel": 30 + } + self.assertEqual(questions.best_student(dict1), 0) + + +def bestgrade_samegrade_elements(self): + dict1 = { + "Ben": 88, + "Hen": 88, + "Natan": 88, + "Efraim": 88, + "Rachel": 88 + } + self.assertEqual(questions.best_student(dict1), 0) class TestPrintDictAsTable(unittest.TestCase): From ffac30242ea30dc8812a4deba842391b9c050b77 Mon Sep 17 00:00:00 2001 From: dorondollev Date: Sun, 10 Apr 2022 23:11:31 +0300 Subject: [PATCH 11/48] Update test.py --- python_katas/kata_1/test.py | 55 +++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/python_katas/kata_1/test.py b/python_katas/kata_1/test.py index fff04d66..cae9243f 100644 --- a/python_katas/kata_1/test.py +++ b/python_katas/kata_1/test.py @@ -88,11 +88,23 @@ class TestReverseWordsConcatenation(unittest.TestCase): """ 1 Katas """ + def reverse_empty_string(self): + lst = [] + self.assertEqual(questions.reverse_words_concatenation(lst), 0) - def test_sample(self): - # your code here - pass + def reverse_long_strings(self): + lst = ["runasdfasdffwerfzvxzcvzxcvfassdafasfeqewr", "jvxcvzxcvzxvcxzcvxzvcxvcohnny", "hzxvczxvcxzcvzxvcxzcvome"] + self.assertEqual(questions.reverse_words_concatenation(lst), 0) + + def reverse_integer_string(self): + lst = ['1', '3', '2', '0'] + self.assertEqual(questions.reverse_words_concatenation(lst), 0) + def reverse_mixed_elements(self): + lst = ["1", 'foo', 'bar', 'baz', "2"] + self.assertEqual(questions.reverse_words_concatenation(lst), 0) + + class TestIsUniqueString(unittest.TestCase): """ 2 Katas @@ -187,12 +199,37 @@ class TestBestStudent(unittest.TestCase): """ 1 Katas """ - - def test_sample(self): - # your code here - pass - - + def BestGrade_example_elements(self): + dict1 = { + "Ben": 78, + "Hen": 88, + "Natan": 99, + "Efraim": 65, + "Rachel": 95 + } + self.assertEqual(questions.best_student(dict1), 0) + + def bestgrade_overgrade_elements(self): + dict1 = { + "Ben": 101, + "Hen": 112, + "Natan": 110, + "Efraim": 103, + "Rachel": 30 + } + self.assertEqual(questions.best_student(dict1), 0) + + def bestgrade_samegrade_elements(self): + dict1 = { + "Ben": 88, + "Hen": 88, + "Natan": 88, + "Efraim": 88, + "Rachel": 88 + } + self.assertEqual(questions.best_student(dict1), 0) + + class TestPrintDictAsTable(unittest.TestCase): """ 1 Katas From 315b745e1a567e3ea44489d492fb8563564b63e1 Mon Sep 17 00:00:00 2001 From: doron Date: Mon, 11 Apr 2022 14:32:13 +0300 Subject: [PATCH 12/48] finished my tests: 4, 11 --- python_katas/kata_1/test.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/python_katas/kata_1/test.py b/python_katas/kata_1/test.py index 38f34601..b981f783 100644 --- a/python_katas/kata_1/test.py +++ b/python_katas/kata_1/test.py @@ -172,15 +172,15 @@ def bestgrade_overgrade_elements(self): self.assertEqual(questions.best_student(dict1), 0) -def bestgrade_samegrade_elements(self): - dict1 = { - "Ben": 88, - "Hen": 88, - "Natan": 88, - "Efraim": 88, - "Rachel": 88 - } - self.assertEqual(questions.best_student(dict1), 0) + def bestgrade_samegrade_elements(self): + dict1 = { + "Ben": 88, + "Hen": 88, + "Natan": 88, + "Efraim": 88, + "Rachel": 88 + } + self.assertEqual(questions.best_student(dict1), 0) class TestPrintDictAsTable(unittest.TestCase): From 43d15d2f0191849e37f81cc3b21768ac690c365e Mon Sep 17 00:00:00 2001 From: doron Date: Mon, 11 Apr 2022 16:36:15 +0300 Subject: [PATCH 13/48] finished test: 4 --- python_katas/kata_1/test.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/python_katas/kata_1/test.py b/python_katas/kata_1/test.py index 7335eccb..5a23bfbf 100644 --- a/python_katas/kata_1/test.py +++ b/python_katas/kata_1/test.py @@ -63,9 +63,21 @@ class TestReverseWordsConcatenation(unittest.TestCase): 1 Katas """ - def test_sample(self): - # your code here - pass + def reverse_original_example(self): + lst = ['take', 'me', 'home'] + self.assertEqual(questions.reverse_words_concatenation(lst), 0) + + def reverse_empty_list(self): + lst = [] + self.assertEqual(questions.reverse_words_concatenation(lst), 0) + + def reverse_one_string(self): + lst = ['me'] + self.assertEqual(questions.reverse_words_concatenation(lst), 0) + + def reverse_same_strings(self): + lst = ['me', 'me', 'me'] + self.assertEqual(questions.reverse_words_concatenation(lst), 0) class TestIsUniqueString(unittest.TestCase): @@ -138,6 +150,7 @@ def test_sample(self): pass + class TestPrintDictAsTable(unittest.TestCase): """ 1 Katas From 8fb48d30ef256e95b92659e0e28f19ef09ef240b Mon Sep 17 00:00:00 2001 From: doron Date: Mon, 11 Apr 2022 16:47:49 +0300 Subject: [PATCH 14/48] finished test: 11 --- python_katas/kata_1/test.py | 44 +++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/python_katas/kata_1/test.py b/python_katas/kata_1/test.py index 5a23bfbf..1df69ca8 100644 --- a/python_katas/kata_1/test.py +++ b/python_katas/kata_1/test.py @@ -145,10 +145,46 @@ class TestBestStudent(unittest.TestCase): 1 Katas """ - def test_sample(self): - # your code here - pass - + def best_student_original_example(self): + + dict1 = { + "Ben": 78, + "Hen": 88, + "Natan": 99, + "Efraim": 65, + "Rachel": 95 + } + self.assertEqual(questions.best_student(lst), 0) + + def best_student_over_grades(self): + dict1 = { + "Ben": 178, + "Hen": 188, + "Natan": 299, + "Efraim": 365, + "Rachel": -95 + } + self.assertEqual(questions.best_student(lst), 0) + + def best_student_same_gardes(self): + dict1 = { + "Ben": 88, + "Hen": 88, + "Natan": 88, + "Efraim": 88, + "Rachel": 88 + } + self.assertEqual(questions.best_student(lst), 0) + + def best_student_float_grades(self): + dict1 = { + "Ben": 7.5, + "Hen": 8, + "Natan": 9, + "Efraim": 5.5, + "Rachel": 9.1 + } + self.assertEqual(questions.best_student(lst), 0) class TestPrintDictAsTable(unittest.TestCase): From c28a6c2170f7e0838fa51ecf2db87c590172c156 Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 12 Apr 2022 13:47:04 +0300 Subject: [PATCH 15/48] added best student --- python_katas/kata_1/questions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py index a7e7fea4..bbf77f75 100644 --- a/python_katas/kata_1/questions.py +++ b/python_katas/kata_1/questions.py @@ -216,7 +216,8 @@ def best_student(grades): :param grades: dict of name -> grade mapping :return: str. some key from the dict """ - return None + sorted_values = sorted(grades.values()) + return list(grades.keys())[list(grades.values()).index(-1)] def print_dict_as_table(some_dict): From 9b93e6457de9edeb66dbe2ef8b1677135ced2320 Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 12 Apr 2022 21:02:59 +0300 Subject: [PATCH 16/48] added print_dict_as_table --- python_katas/kata_1/questions.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py index bbf77f75..0e30bfe0 100644 --- a/python_katas/kata_1/questions.py +++ b/python_katas/kata_1/questions.py @@ -1,3 +1,4 @@ + def sum_of_element(elements): """ 1 Kata @@ -246,7 +247,10 @@ def print_dict_as_table(some_dict): :param some_dict: :return: """ - return None + print("{:<7} {}".format('Key', 'Value')) + print("{}".format('-' * 16)) + for key, value in dict1.items(): + print("{:<7} {}".format(key, value)) def merge_dicts(dict1, dict2): From bbbf6b033a736af8bd0b5adda15e9d60727757cb Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 12 Apr 2022 22:23:21 +0300 Subject: [PATCH 17/48] fixed best_student --- python_katas/kata_1/questions.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py index 0e30bfe0..aac7e7d1 100644 --- a/python_katas/kata_1/questions.py +++ b/python_katas/kata_1/questions.py @@ -218,7 +218,10 @@ def best_student(grades): :return: str. some key from the dict """ sorted_values = sorted(grades.values()) - return list(grades.keys())[list(grades.values()).index(-1)] + for key, value in grades.items(): + if sorted_values[-1] == value: + return key + #return list(grades.keys())[list(grades.values()).index(len(grades)-1)] def print_dict_as_table(some_dict): @@ -270,7 +273,7 @@ def merge_dicts(dict1, dict2): :param dict2: :return: """ - return dict1 + return dict1.update(dict2) def seven_boom(n): From da52e0e756b34feaaf5b527ded9e79e8ab2c7bf1 Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 12 Apr 2022 22:31:08 +0300 Subject: [PATCH 18/48] fixed merge_dicts --- python_katas/kata_1/questions.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py index aac7e7d1..ce5b3c2c 100644 --- a/python_katas/kata_1/questions.py +++ b/python_katas/kata_1/questions.py @@ -221,7 +221,6 @@ def best_student(grades): for key, value in grades.items(): if sorted_values[-1] == value: return key - #return list(grades.keys())[list(grades.values()).index(len(grades)-1)] def print_dict_as_table(some_dict): @@ -273,7 +272,9 @@ def merge_dicts(dict1, dict2): :param dict2: :return: """ - return dict1.update(dict2) + dict3 = dict1.copy() + dict3.update(dict2) + return dict3 def seven_boom(n): From 53446608fa1d663f9e404781c216195daf49f568 Mon Sep 17 00:00:00 2001 From: doron Date: Wed, 13 Apr 2022 12:17:27 +0300 Subject: [PATCH 19/48] added 7 boom --- python_katas/kata_1/questions.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py index ce5b3c2c..d5469fd9 100644 --- a/python_katas/kata_1/questions.py +++ b/python_katas/kata_1/questions.py @@ -289,7 +289,9 @@ def seven_boom(n): :param n: int. The last number for count for a 7-boom play :return: list of integers """ - return None + boom = [] + [boom.append(x) if '7' in str(x) or x % 7 == 0 else x for x in range(0, n - 1)] + return boom def caesar_cipher(str_to_encrypt): From 1380cef4d38eb36fa94e6dfca4050c60244554e4 Mon Sep 17 00:00:00 2001 From: doron Date: Wed, 13 Apr 2022 13:02:33 +0300 Subject: [PATCH 20/48] added 7 boom --- python_katas/kata_1/questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py index d5469fd9..a8156cd8 100644 --- a/python_katas/kata_1/questions.py +++ b/python_katas/kata_1/questions.py @@ -250,7 +250,7 @@ def print_dict_as_table(some_dict): :return: """ print("{:<7} {}".format('Key', 'Value')) - print("{}".format('-' * 16)) + print("{}".format('-' * 13)) for key, value in dict1.items(): print("{:<7} {}".format(key, value)) From 2f2f8a5db3ede5ea43a441bdb725b6eda8627cb2 Mon Sep 17 00:00:00 2001 From: doron Date: Thu, 14 Apr 2022 00:16:45 +0300 Subject: [PATCH 21/48] added caesar_cipher --- 02_linux_ex1/README | 2 +- python_katas/kata_1/questions.py | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/02_linux_ex1/README b/02_linux_ex1/README index 2fab31e8..65522175 100644 --- a/02_linux_ex1/README +++ b/02_linux_ex1/README @@ -9,4 +9,4 @@ Kernel System Calls Binary Numbers -------------- -< Your Answer > \ No newline at end of file +< Your Answer > \ No newline at end of file diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py index a8156cd8..90f984dc 100644 --- a/python_katas/kata_1/questions.py +++ b/python_katas/kata_1/questions.py @@ -306,7 +306,27 @@ def caesar_cipher(str_to_encrypt): :return: """ - return None + key = 3 + encryption = "" + + for c in str_to_encrypt: + if c.isspace(): + encryption += c + elif c.isupper(): + c_unicode = ord(c) + c_index = c_unicode - ord("A") + new_index = (c_index + key) % 26 + new_unicode = new_index + ord("A") + new_c = chr(new_unicode) + encryption += new_c + elif c.islower: + c_unicode = ord(c) + c_index = ord(c) - ord("a") + new_index = (c_index + key) % 26 + new_unicode = new_index + ord("a") + new_c = chr(new_unicode) + encryption += new_c + return encryption def sum_of_digits(digits_str): From fd54ee89b0ef8767df0fcf373ad53fbf4acec45f Mon Sep 17 00:00:00 2001 From: doron Date: Thu, 14 Apr 2022 14:23:21 +0300 Subject: [PATCH 22/48] added sum_of_digits string --- python_katas/kata_1/questions.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py index 90f984dc..900a7826 100644 --- a/python_katas/kata_1/questions.py +++ b/python_katas/kata_1/questions.py @@ -344,7 +344,9 @@ def sum_of_digits(digits_str): :param digits_str: str of numerical digits only :return: int representing the sum of digits """ - return None + chars_array = [char for char in digits_str] + int_array = [int(c) for c in chars_array] + return (sum(int_array)) if __name__ == '__main__': From c8bd91f8528ce97f260da91cd380ee2ef145dd34 Mon Sep 17 00:00:00 2001 From: doron Date: Thu, 14 Apr 2022 14:25:56 +0300 Subject: [PATCH 23/48] added sum_of_digits string --- python_katas/kata_1/questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py index 900a7826..a5a2ee4e 100644 --- a/python_katas/kata_1/questions.py +++ b/python_katas/kata_1/questions.py @@ -321,7 +321,7 @@ def caesar_cipher(str_to_encrypt): encryption += new_c elif c.islower: c_unicode = ord(c) - c_index = ord(c) - ord("a") + c_index = c_unicode - ord("a") new_index = (c_index + key) % 26 new_unicode = new_index + ord("a") new_c = chr(new_unicode) From 9e314d3932051359a5cbee125c5f5e29e72cb73b Mon Sep 17 00:00:00 2001 From: doron Date: Sun, 17 Apr 2022 21:27:34 +0300 Subject: [PATCH 24/48] added pair_match --- python_katas/kata_1/questions.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py index a5a2ee4e..c15678b6 100644 --- a/python_katas/kata_1/questions.py +++ b/python_katas/kata_1/questions.py @@ -182,7 +182,15 @@ def pair_match(men, women): :param women: dict mapping name -> age :return: tuple (men_name, women_name) such their age absolute difference is the minimal """ - return None + minimum = 1000 + # dict_diff = {} # empty dictionary + for m in men.keys(): # run over all men + for w in women.keys(): # run over all women + diff_age = abs(men[m] - women[w]) + if diff_age < minimum: + minimum = diff_age + couple = (m, w) + return couple def bad_average(a, b, c): From f5d36f42ae1593da9e47fa57d88881860dbc5782 Mon Sep 17 00:00:00 2001 From: doron Date: Mon, 18 Apr 2022 12:09:16 +0300 Subject: [PATCH 25/48] fixed print_dict_as_table --- python_katas/kata_1/questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py index c15678b6..615a1b7c 100644 --- a/python_katas/kata_1/questions.py +++ b/python_katas/kata_1/questions.py @@ -259,7 +259,7 @@ def print_dict_as_table(some_dict): """ print("{:<7} {}".format('Key', 'Value')) print("{}".format('-' * 13)) - for key, value in dict1.items(): + for key, value in some_dict.items(): print("{:<7} {}".format(key, value)) From 86f537ed89f2389efbc76855a216f16055a4a1e4 Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 26 Apr 2022 00:04:15 +0300 Subject: [PATCH 26/48] finished valid_parentheses --- python_katas/kata_2/questions.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index 907808d4..d23e4a1d 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -13,7 +13,18 @@ def valid_parentheses(s): s = '[[{()}](){}]' -> True s = '[{]}' -> False """ - return None + stack = [] + parentheses_pairs = {")": "(", "]": "[", "}": "{"} + + for i in s: + if i in parentheses_pairs: + if stack and stack[-1] == parentheses_pairs[-1]: + stack.pop() + else: + return False + else: + stack.append(i) + return True if not stack else False def fibonacci_fixme(n): From 935434383a4495ed4c85aeebd2c5acd8a695df0f Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 26 Apr 2022 00:29:04 +0300 Subject: [PATCH 27/48] finished fibonacci_fixme --- python_katas/kata_2/questions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index d23e4a1d..d2d6aec3 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -47,10 +47,10 @@ def fibonacci_fixme(n): """ a = 0 b = 1 - for i in range(1, n): + for i in range(0, n): a = b - tmp = a + b b = tmp + tmp = a + b return a From a7b4be8a84da6c57efdb4875221b00cdfa3eed70 Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 26 Apr 2022 19:39:28 +0300 Subject: [PATCH 28/48] fixed valid_parentheses --- python_katas/kata_2/questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index d2d6aec3..d03ffd00 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -18,7 +18,7 @@ def valid_parentheses(s): for i in s: if i in parentheses_pairs: - if stack and stack[-1] == parentheses_pairs[-1]: + if stack and stack[-1] == parentheses_pairs[i]: stack.pop() else: return False From 63d3500b85bc92832c75108d076b5e63c64abf40 Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 26 Apr 2022 23:07:59 +0300 Subject: [PATCH 29/48] fixed fibonacci_fixme --- python_katas/kata_2/questions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index d03ffd00..9af052ce 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -47,12 +47,13 @@ def fibonacci_fixme(n): """ a = 0 b = 1 + tmp = a for i in range(0, n): a = b b = tmp tmp = a + b - return a + return tmp def most_frequent_name(file_path): From afb0719119294980a5adc95a9130c23dc5f7140e Mon Sep 17 00:00:00 2001 From: doron Date: Wed, 27 Apr 2022 13:42:05 +0300 Subject: [PATCH 30/48] added most_frequent_name --- python_katas/kata_2/questions.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index 9af052ce..38711913 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -68,7 +68,18 @@ def most_frequent_name(file_path): :param file_path: str - absolute or relative file to read names from :return: str - the mose frequent name. If there are many, return one of them """ - return None + my_file = open(file_path, "r") + content = my_file.read() + content_list = content.split(" ") + my_file.close() + element = content_list[0] + counter = 0 + for i in content_list: + cur = content_list.count(i) + if cur > counter: + counter = cur + element = i + return element def files_backup(dir_path): From ec47cc11e71dfd193b99a281cbaabf947b188818 Mon Sep 17 00:00:00 2001 From: doron Date: Fri, 29 Apr 2022 01:01:54 +0300 Subject: [PATCH 31/48] added files_backup --- python_katas/kata_2/questions.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index 38711913..9484c213 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -99,7 +99,11 @@ def files_backup(dir_path): :param dir_path: string - path to a directory :return: str - the backup file name """ - return None + path_to_dir = dir_path.split("/") + source_dir = (path_to_dir[-1]) + command = "/usr/bin/tar zcf backup_{0}_`date +%Y-%m-%d`.tar.gz {1}".format(source_dir, dir_path) + status = os.system(command) + return source_dir def replace_in_file(file_path, text, replace_text): From abab3282d1d10d21452f3062e27aaf00dd089b32 Mon Sep 17 00:00:00 2001 From: doron Date: Fri, 29 Apr 2022 01:44:18 +0300 Subject: [PATCH 32/48] added replace_in_file --- python_katas/kata_2/questions.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index 9484c213..416a622c 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -1,3 +1,6 @@ +import os + + def valid_parentheses(s): """ 3 Kata @@ -120,6 +123,13 @@ def replace_in_file(file_path, text, replace_text): :param replace_text: text to replace with :return: None """ + if os.path.exists(file_path): + file = open(file_path, "r") + data = file.read() + data = data.replace(text, replace_text) + file.close() + file = open(file_path, "w") + file.write(data) return None From a205a771901a339dc144b198747a57be7998bd9c Mon Sep 17 00:00:00 2001 From: doron Date: Mon, 9 May 2022 08:41:07 +0300 Subject: [PATCH 33/48] added merge json files --- python_katas/kata_2/questions.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index 416a622c..8365416c 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -1,4 +1,6 @@ import os +import json +import glob def valid_parentheses(s): @@ -144,7 +146,11 @@ def json_configs_merge(*json_paths): :param json_paths: :return: dict - the merges json files """ - return None + my_dict = {} + for f in glob.glob(*json_paths): + with open(f, "rb") as data: + my_dict.update(data) + return my_dict def monotonic_array(lst): From 19576b8a30af036d3c97db4e7179faaf83231cdd Mon Sep 17 00:00:00 2001 From: doron Date: Mon, 9 May 2022 13:06:01 +0300 Subject: [PATCH 34/48] added merge json files --- python_katas/kata_2/questions.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index 8365416c..b9250fe8 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -146,11 +146,10 @@ def json_configs_merge(*json_paths): :param json_paths: :return: dict - the merges json files """ - my_dict = {} - for f in glob.glob(*json_paths): - with open(f, "rb") as data: - my_dict.update(data) - return my_dict + for file in json_paths: + with open(file) as data: + my_dict = json.load(data) + return my_dict def monotonic_array(lst): From cb68ff4a06edf64033bee1fbff9d0840e2c3b3cd Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 10 May 2022 15:26:58 +0300 Subject: [PATCH 35/48] created monotonic_array --- python_katas/kata_2/questions.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index b9250fe8..2dc86414 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -146,10 +146,10 @@ def json_configs_merge(*json_paths): :param json_paths: :return: dict - the merges json files """ - for file in json_paths: - with open(file) as data: - my_dict = json.load(data) - return my_dict + for file in json_paths: + with open(file) as data: + my_dict = json.load(data) + return my_dict def monotonic_array(lst): @@ -161,7 +161,14 @@ def monotonic_array(lst): :param lst: list of numbers (int, floats) :return: bool: indicating for monotonicity """ - return None + n = len(lst) # size of array + if n == 1: + return True + else: + if all(lst[i] >= lst[i + 1] for i in range(0, n - 1) or lst[i] <= lst[i + 1] for i in range(0, n - 1)): + return True + else: + return False def matrix_avg(mat, rows=None): From 6870a4ff28145ef86fab6eeedbfe1a6a94cf5a7a Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 17 May 2022 18:00:09 +0300 Subject: [PATCH 36/48] created strong password --- python_katas/kata_2/questions.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index 2dc86414..3362de1e 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -1,6 +1,6 @@ import os import json -import glob +import re def valid_parentheses(s): @@ -371,7 +371,18 @@ def strong_pass(password): This function returns True if the given password is strong enough """ - return None + if len(password) < 6: + return False + if not re.search("[0-9]", password): + return False + if not re.search("[a-z]", password): + return False + if not re.search("[A-Z]", password): + return False + if not re.search("[!@#$%^&*()-+]", password): + return False + return True + if __name__ == '__main__': From d06de488d290999d8b51d38329dd8d7bae715dfd Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 17 May 2022 18:02:19 +0300 Subject: [PATCH 37/48] created strong password --- python_katas/kata_2/questions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index 3362de1e..db3ffd13 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -384,7 +384,6 @@ def strong_pass(password): return True - if __name__ == '__main__': print('\nvalid_parentheses:\n--------------------') print(valid_parentheses('[[{()}](){}]')) From 3b28e83c82d1911bf7dc7ea55d850f9a7bf744a3 Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 17 May 2022 22:47:26 +0300 Subject: [PATCH 38/48] created str_compression --- python_katas/kata_2/questions.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index db3ffd13..8f69e503 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -355,7 +355,20 @@ def str_compression(text): :param text: str :return: list representing the compressed form of the string """ - return None + lst = [] + cnt = 1 + for i in range(1, len(text)): + if text[i] == text[i-1]: + cnt += 1 + else: + lst.append(text[i-1]) + if cnt > 1: + lst.append(str(cnt)) + cnt = 1 + lst.append(text[-1]) + if cnt > 1: + lst.append(str(cnt)) + return lst def strong_pass(password): From f982814485114efe41491e672ea0d4ec1d058bd9 Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 24 May 2022 21:53:40 +0300 Subject: [PATCH 39/48] removed redundant parenthesis in sum(int_array) --- python_katas/kata_1/questions.py | 2 +- python_katas/kata_2/questions.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py index 615a1b7c..00942287 100644 --- a/python_katas/kata_1/questions.py +++ b/python_katas/kata_1/questions.py @@ -354,7 +354,7 @@ def sum_of_digits(digits_str): """ chars_array = [char for char in digits_str] int_array = [int(c) for c in chars_array] - return (sum(int_array)) + return sum(int_array) if __name__ == '__main__': diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index 8f69e503..d78346bf 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -182,6 +182,10 @@ def matrix_avg(mat, rows=None): :param rows: list of unique integers in the range [0, 2] and length of maximum 3 :return: int - the average values """ + mylist = [] + if rows is not None: + for i in rows: + mylist.append(zip(mat[i])) return None From b973fb977afedf125e4994231e02d1aea49c9570 Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 24 May 2022 23:33:04 +0300 Subject: [PATCH 40/48] commit matrix_avg --- python_katas/kata_2/questions.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index d78346bf..eff3dc4a 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -182,11 +182,17 @@ def matrix_avg(mat, rows=None): :param rows: list of unique integers in the range [0, 2] and length of maximum 3 :return: int - the average values """ - mylist = [] + mylist = 0 + rng = 0 if rows is not None: for i in rows: - mylist.append(zip(mat[i])) - return None + mylist += sum(mat[i]) + rng = len(mat[i]) + else: + for j in range(len(mat)): + mylist += sum(mat[j]) + rng = len(mat) + return mylist / rng def merge_sorted_lists(l1, l2): From 4245de0af78b264b281909fd26868f924c76b456 Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 24 May 2022 23:33:04 +0300 Subject: [PATCH 41/48] commit matrix_avg + fix rng += len(mat[i]) --- python_katas/kata_2/questions.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index d78346bf..eff3dc4a 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -182,11 +182,17 @@ def matrix_avg(mat, rows=None): :param rows: list of unique integers in the range [0, 2] and length of maximum 3 :return: int - the average values """ - mylist = [] + mylist = 0 + rng = 0 if rows is not None: for i in rows: - mylist.append(zip(mat[i])) - return None + mylist += sum(mat[i]) + rng = len(mat[i]) + else: + for j in range(len(mat)): + mylist += sum(mat[j]) + rng = len(mat) + return mylist / rng def merge_sorted_lists(l1, l2): From 535692fbbf4040fdd9f614201ba0c1a1d6988485 Mon Sep 17 00:00:00 2001 From: doron Date: Wed, 25 May 2022 11:39:33 +0300 Subject: [PATCH 42/48] commit matrix_avg + fix rng += len(mat[j]) --- python_katas/kata_2/questions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index eff3dc4a..efa20dca 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -187,11 +187,11 @@ def matrix_avg(mat, rows=None): if rows is not None: for i in rows: mylist += sum(mat[i]) - rng = len(mat[i]) + rng += len(mat[i]) else: for j in range(len(mat)): mylist += sum(mat[j]) - rng = len(mat) + rng += len(mat[j]) return mylist / rng From 38ff9bd0fefd267006378807aced6851418cbc92 Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 31 May 2022 23:12:02 +0300 Subject: [PATCH 43/48] fixed main run --- python_katas/kata_2/mnist-predictor.yaml | 2 +- python_katas/kata_2/questions.py | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/python_katas/kata_2/mnist-predictor.yaml b/python_katas/kata_2/mnist-predictor.yaml index 989de028..2cc31bc3 100644 --- a/python_katas/kata_2/mnist-predictor.yaml +++ b/python_katas/kata_2/mnist-predictor.yaml @@ -18,7 +18,7 @@ spec: terminationGracePeriodSeconds: 30 containers: - name: mnist-predictor - image: {{REGISTRY_URL}}/{{IMG_NAME}} + image: {{REGISTRY_URL}}/mnist-pred:0.0.1 --- apiVersion: v1 kind: Service diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index efa20dca..69a0d0b9 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -106,7 +106,7 @@ def files_backup(dir_path): """ path_to_dir = dir_path.split("/") source_dir = (path_to_dir[-1]) - command = "/usr/bin/tar zcf backup_{0}_`date +%Y-%m-%d`.tar.gz {1}".format(source_dir, dir_path) + command = "/usr/bin/tar zcvf backup_{0}_`date +%Y-%m-%d`.tar.gz {1}".format(source_dir, dir_path) status = os.system(command) return source_dir @@ -208,7 +208,22 @@ def merge_sorted_lists(l1, l2): :param l2: list of integers :return: list: sorted list combining l1 and l2 """ - return None + return sorted(l1 + l2) + size_1 = len(l1) + size_2 = len(l2) + + res = [] + i, j = 0, 0 + + while i < size_1 and j < size_2: + if l1[i] < l2[j]: + res.append(l1[i]) + i += 1 + else: + res.append(l2[j]) + j += 1 + res = res + l1[i:] + l2[j:] + return res def longest_common_substring(str1, str2): @@ -418,7 +433,7 @@ def strong_pass(password): print(most_frequent_name('names.txt')) print('\nfiles_backup:\n--------------------') - print(files_backup('python_katas/kata_2')) + print(files_backup('/home/doron')) print('\nreplace_in_file:\n--------------------') print(replace_in_file('mnist-predictor.yaml', '{{IMG_NAME}}', 'mnist-pred:0.0.1')) From 099059f0242231aa20ca1b3856a44b5a2c9ab4ca Mon Sep 17 00:00:00 2001 From: doron Date: Wed, 1 Jun 2022 09:57:52 +0300 Subject: [PATCH 44/48] committing the longest common string --- python_katas/kata_2/questions.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index 47919134..c81dc778 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -57,7 +57,6 @@ def fibonacci_fixme(n): a = b b = tmp tmp = a + b - return tmp @@ -233,7 +232,24 @@ def longest_common_substring(str1, str2): :param str2: str :return: str - the longest common substring """ - return None + m = len(str1) + n = len(str2) + counter = [[0] * (n + 1) for x in range(m + 1)] + longest = 0 + lcs = set() + for i in range(m): + for j in range(n): + if str1[i] == str2[j]: + c = counter[i][j] + 1 + counter[i + 1][j + 1] = c + if c > longest: + lcs = set() + longest = c + lcs.add(str1[i - c + 1:i + 1]) + elif c == longest: + lcs.add(str1[i - c + 1:i + 1]) + + return lcs def longest_common_prefix(str1, str2): From 74f85bcfa79e3bdba400c8dd4e3e538d853a0b7d Mon Sep 17 00:00:00 2001 From: doron Date: Wed, 1 Jun 2022 11:41:07 +0300 Subject: [PATCH 45/48] committing longest_common_prefix --- python_katas/kata_2/questions.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index c81dc778..85b767f5 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -268,7 +268,13 @@ def longest_common_prefix(str1, str2): :param str2: str :return: str - the longest common prefix """ - return None + i = j = 0 + while i < len(str1) and j < len(str2): + if str1[i] != str2[j]: + break + i = i + 1 + j = j + 1 + return str1[:i] def rotate_matrix(mat): From 5ad6498e2d3725ceb890e3e355e7248d8921b267 Mon Sep 17 00:00:00 2001 From: doron Date: Thu, 2 Jun 2022 01:17:21 +0300 Subject: [PATCH 46/48] committing list_flatten --- python_katas/kata_2/questions.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index 85b767f5..612d4e64 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -372,7 +372,10 @@ def list_flatten(lst): :param lst: list of integers of another list :return: flatten list """ - return None + for i, x in enumerate(lst): + while i < len(lst) and isinstance(lst[i], list): + lst[i:i + 1] = lst[i] + return lst def str_compression(text): From a270b411e1612bde267442260f56afba863a3498 Mon Sep 17 00:00:00 2001 From: doron Date: Thu, 2 Jun 2022 16:30:19 +0300 Subject: [PATCH 47/48] committing pascal_triangle --- python_katas/kata_2/questions.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index 612d4e64..d08d577c 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -355,7 +355,19 @@ def pascal_triangle(lines): :param lines: int :return: None """ - return None + lst = [] # an empty list + for n in range(lines): + lst.append([]) + lst[n].append(1) + for m in range(1, n): + lst[n].append(lst[n - 1][m - 1] + lst[n - 1][m]) + if lines != 0: + lst[n].append(1) + for n in range(lines): + print(" " * (lines - n), end=" ", sep=" ") + for m in range(0, n + 1): + print('{0:5}'.format(lst[n][m]), end=" ", sep=" ") + print() def list_flatten(lst): From fbf7cb81a17c1c09fb2a359fbf821ac07ba00fdd Mon Sep 17 00:00:00 2001 From: doron Date: Wed, 22 Jun 2022 11:57:14 +0300 Subject: [PATCH 48/48] finished rotate_matrix --- python_katas/kata_2/questions.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/python_katas/kata_2/questions.py b/python_katas/kata_2/questions.py index d08d577c..3e24bb9a 100644 --- a/python_katas/kata_2/questions.py +++ b/python_katas/kata_2/questions.py @@ -300,7 +300,15 @@ def rotate_matrix(mat): :param mat: :return: list of lists - rotate matrix """ - return None + rows = len(mat) + cols = len(mat[0]) + + mat2 = [[""] * rows for _ in range(cols)] + + for i in range(rows): + for j in range(cols): + mat2[j][rows - i - 1] = box[i][j] + return mat2 def is_valid_email(mail_str):