From 89b7b9472cfd5a89bdae5ed4abafdec6154de3d5 Mon Sep 17 00:00:00 2001 From: dorondollev Date: Wed, 16 Mar 2022 20:53:05 +0200 Subject: [PATCH 01/28] 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/28] 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 8059b3ad3f9c983b10c7751c4c5af845566b6f36 Mon Sep 17 00:00:00 2001 From: Ariel Date: Tue, 29 Mar 2022 15:47:04 +0300 Subject: [PATCH 03/28] Just checking everything is alright --- 06_linux_ex2/README | 148 +++++++++++++++++++++++++++++++++++ 06_linux_ex2/tlsHandshake.sh | 43 ++++++++++ 2 files changed, 191 insertions(+) diff --git a/06_linux_ex2/README b/06_linux_ex2/README index 8489ffb1..cddf8209 100644 --- a/06_linux_ex2/README +++ b/06_linux_ex2/README @@ -1,2 +1,150 @@ name@example.com + README_EX2 + Ariel1434@gmail.com + + + + + TLS communication + ----------------- + +In Exercise, We are going to manually implemented a secure communication over HTTP. +The way we are going to perform are by two ways: +1)Follow the instructions in Exercise_2. +2)Learning and implementing the Commands methods, Essential understanding 'How HTTP works' + +---------------------------------------------- + +Learning the fundementals shows here below: +0)Signals +1)TLS communication +2)HTTP +3)JASON +4)curl +5)OpenSSL + +*if SIGSTP doesn't work the next command will be SIGSTOP +*SIGCONT- is used to continue the process after it was stopped. + +----------------------------------------------- + + + Processes handling + -------------------- +Writing the answers here: +~~~~~~~~~~~~~~~~~~~~~~~~~ + +(Q1) A user started a process and logged out from the terminal. Which command he used if the process still running in the background: +(A1) To make the process to still running in the background we are going to use 'nohup' command. + + +(Q2) The kill command always terminates a process. +(A2) The answer is False +explain: You might want to check kill -l command in the terminal, by that you will be able to see that there are a veriry of diffrent signals that you can perform, therefore, the answer is False. + + +(Q3) Which command could be used to know how many processes are running in the background terminal session? +(A3)The answer is: 'jobs' -commands you might want to check the 'man' page of this command. + + +(Q4) Given a terminal session with long process running in it, how will you ask this process to terminate? +(A4) To terminate the process, you should use CTRL+C which is shortkey for 'kill -2' command. + (SIGINT 2 Term Interrupt from keyboard) + + +(Q5) Given a terminal session with long process running in it, how will you ask this process the stop? +(A5) To Stop the process, you should use CTRL+Z which is shortkey for 'kill -20' command. + + +(Q6) How would you run the sleep 10 command as a foreground process? +(A6) To run the sleep 10 command as a foreground process we will write: 'sleep 10' command. + + +(Q7) Which of the following command would deliver a SIGTERM to the xscreensaver process? +(A7) To Deliver a SIGTERM to xscreensaver process, we will use :kill 4846 + explain: As we know,the syntax of exacute the command, require the command kill and process ID. + (This defaults to the SIGTERM signal) + + +(Q8) Which of the following would deliver a SIGKILL to the xscreensaver command? +(A8) To deliver a SIGKILL to the xscreensaver we will use :kill -9 4846 + + +(Q9) Which of the following would send a SIGCHLD (signal number 17) to the ssh-agent process? +(A9) To send SIGCHLD to the ssh-agent process: kill -CHLD 4828 + (explain: The reaon I think is the correct answer, because signal arguments must be process or jons IDs) + + +(Q10) Which key pressed within the top command allows the user to send a signal to a process? +(A10) To send signal to a process in top command use the key :k + (When you will run the top command and try it yourself, you will get :"PID to signal/kill [default pid = xxxx]") + + +(Q11)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? (you can exit this process by typing exit() in the +python console) +(A11) The program implements the kernel default signal handler for the SIGINT signal, which is to stop (suspend) the process. + + + Process states + ----------------- + +1) The main objective here, is to check how two different processes will behave when they have to work +at the 'same time' meaning sequatually. + +2)After following the objective, here are the resualts: + +-------------------------------------------------------------------------------------------------------------- + PS AUX +-------------------------------------------------------------------------------------------------------------- +ariel 30945 9.0 0.0 2616 1768 pts/2 R+ 23:34 0:00 sh multi_process_file_writing.sh +ariel 30947 1.0 0.0 9632 3300 pts/2 D+ 23:34 0:00 /bin/bash ./write_to_file_sequentially.sh + +-------------------------------------------------------------------------------------------------------------- + top command +-------------------------------------------------------------------------------------------------------------- + + top - 23:34:32 up 2:47, 2 users, load average: 1003.55, 751.62, 420.35 + Tasks: 1362 total, 2 running, 1360 sleeping, 0 stopped, 0 zombie + %Cpu(s): 1.6 us, 14.9 sy, 0.0 ni, 74.5 id, 6.9 wa, 0.0 hi, 2.1 si, 0.0 st + MiB Mem : 20947.3 total, 16166.5 free, 2693.0 used, 2087.8 buff/cache + MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 17924.4 avail Mem + + 32948 ariel 20 0 13248 5292 3176 R 10.5 0.0 0:00.06 top + 30954 ariel 20 0 9632 3212 2972 D 5.3 0.0 0:00.01 write_to_file_s + 31002 ariel 20 0 9632 3300 3060 D 5.3 0.0 0:00.01 write_to_file_s + 4475 ariel 20 0 827508 59084 40836 S 5.3 0.3 0:34.54 gnome-terminal- + 16847 root 20 0 0 0 0 I 5.3 0.0 0:05.19 kworker/u256:0-ext4-rsv-conversion + + +-------------------------------------------------------------------------------------------------------------- +PROCESS STATE CODES 'top' + Here are the different values that the s, stat and state output + specifiers (header "STAT" or "S") will display to describe the + state of a process: + + D uninterruptible sleep (usually IO) + I Idle kernel thread + R running or runnable (on run queue) + S interruptible sleep (waiting for an event to + complete) + T stopped by job control signal + t stopped by debugger during the tracing + W paging (not valid since the 2.6.xx kernel) + X dead (should never be seen) + Z defunct ("zombie") process, terminated but not + reaped by its parent + + For BSD formats and when the stat keyword is used, additional + characters may be displayed: + + < high-priority (not nice to other users) + N low-priority (nice to other users) + L has pages locked into memory (for real-time and + custom IO) + s is a session leader + l is multi-threaded (using CLONE_THREAD, like NPTL + pthreads do) + + is in the foreground process group + +-------------------------------------------------------------------------------------------------------------- diff --git a/06_linux_ex2/tlsHandshake.sh b/06_linux_ex2/tlsHandshake.sh index e69de29b..8b56015a 100644 --- a/06_linux_ex2/tlsHandshake.sh +++ b/06_linux_ex2/tlsHandshake.sh @@ -0,0 +1,43 @@ +curl -d '{"clientVersion":"3.2", "message":"Client Hello"}' -H "Content-Type: application/json" -X POST http://devops-jan22-1273001359.eu-north-1.elb.amazonaws.com:8080/clienthello --> secret.txt + +cat secret.txt | jq -r '.serverVersion' > serverVersion.txt + +cat secret.txt | jq -r '.sessionID' > sessionID.txt + +cat secret.txt | jq -r '.serverCert' > cert.pem + +export SESSION_ID=$(cat sessionID.txt) + +wget https://devops-jan22.s3.eu-north-1.amazonaws.com/cert-ca-aws.pem + +openssl verify -CAfile cert-ca-aws.pem cert.pem + +VERIFICATION_RESULT=$( openssl verify -CAfile cert-ca-aws.pem cert.pem ) + +if [ "$VERIFICATION_RESULT" != "cert.pem: OK" ]; then + echo "Server Certificate is invalid."masterKey + exit 1 +fi + +openssl rand -base64 32 > masterKey.txt + +openssl smime -encrypt -aes-256-cbc -in masterKey.txt -outform DER cert.pem | base64 -w 0 > encryptedmasterKey.txt + +export MASTER_KEY=$(cat encryptedmasterKey.txt) + +curl -d '{"sessionID": "'$SESSION_ID'","masterKey": "'$MASTER_KEY'","sampleMessage": "Hi server, please encrypt me and send to client!"}' -H "Content-Type: application/json" -X POST http://devops-jan22-1273001359.eu-north-1.elb.amazonaws.com:8080/keyexchange >secret2.txt + +cat secret2.txt | jq -r '.encryptedSampleMessage' > encSampleMsg.txt + +cat encSampleMsg.txt | base64 -d > encSampleMsgReady.txt + +openssl enc -d -aes-256-cbc -pbkdf2 -kfile masterKey.txt -in encSampleMsgReady.txt -out decryptedSampleMsgReady.txt + +DECRYPTED_SAMPLE_MESSAGE=$( cat decryptedSampleMsgReady.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 From d6711086f81b2aa0f024bf2fa7cda4c53d8838d5 Mon Sep 17 00:00:00 2001 From: alonit Date: Tue, 29 Mar 2022 16:08:00 +0300 Subject: [PATCH 04/28] python katas --- .github/workflows/pre-submit-test.yaml | 16 +-- python_katas/kata_1/questions.py | 191 +++++++++++++++++++++++++ python_katas/kata_1/test.py | 25 ++++ 3 files changed, 222 insertions(+), 10 deletions(-) create mode 100644 python_katas/kata_1/questions.py create mode 100644 python_katas/kata_1/test.py diff --git a/.github/workflows/pre-submit-test.yaml b/.github/workflows/pre-submit-test.yaml index 50c6e633..641e55d3 100644 --- a/.github/workflows/pre-submit-test.yaml +++ b/.github/workflows/pre-submit-test.yaml @@ -20,13 +20,9 @@ jobs: if: ${{ startsWith(github.ref, 'refs/heads/linux_ex2/') }} steps: - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event." - - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!" - - run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}." - - name: Check out repository code - uses: actions/checkout@v2 - - run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner." - - run: echo "🖥️ The workflow is now ready to test your code on the runner." - - name: List files in the repository - run: | - ls ${{ github.workspace }} - - run: echo "🍏 This job's status is ${{ job.status }}." + PythonKata1Test: + runs-on: ubuntu-latest + if: ${{ startsWith(github.ref, 'refs/heads/python_katas_') }} + steps: + - run: | + python -m python_katas.kata_1.test \ No newline at end of file diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py new file mode 100644 index 00000000..4481e6f9 --- /dev/null +++ b/python_katas/kata_1/questions.py @@ -0,0 +1,191 @@ +def sum_of_element(elements): + """ + 1 Kata + + :param elements: list of integers + :return: Return the resulting string. + """ + s = 0 + for num in elements: + s = s + num + + return s + + +def verbing(word): + """ + 1 Kata + + Given a string 'word', if its length is at least 3, add 'ing' to its end. + Unless it already ends in 'ing', in which case add 'ly' instead. + If the string length is less than 3, leave it unchanged. + + :param word: str + :return: Return the resulting string. + """ + return None + + +def words_concatenation(words): + """ + 1 Kata + + Given a list of words, write a program that concatenates the words. + + For example: + >> words_concatenation(['take', 'me', 'home']) + 'take me home' + + :param words: list of str + :return: Return the resulting string. + """ + return None + + +def reverse_words_concatenation(words): + """ + 1 Kata + + Given a list of words, write a program that concatenates the words in a reverse way (both words and each word itself) + + For example: + >> reverse_words_concatenation(['take', 'me', 'home']) + 'home me take' + + :param words: list of str + :return: Return the resulting string. + """ + return None + + +def is_unique_string(some_str): + """ + 1 Kata + + :param some_str: + :return: + """ + return None + + +def list_diff(elements): + """ + 1 Kata + + :param elements: + :return: + """ + return None + + +def prime_number(num): + """ + 1 Kata + + hint: use the built-in function "range" + :param num: + :return: + """ + return None + + +def palindrome_num(num): + """ + 1 Kata + + :param num: + :return: + """ + return None + + +def pair_match(men, women): + """ + 1 Kata + + :param men: + :param women: + :return: + """ + return None + + +def bad_average(): + """ + 1 Kata + :return: + """ + + +def best_student(): + """ + 1 Kata + + + :return: + """ + return None + + +def print_dict_as_table(some_dict): + """ + 1 Kata + + + :return: + """ + return None + + +def merge_dicts(some_dict): + """ + 1 Kata + + + :return: + """ + return None + + +def seven_boom(): + """ + 1 Kata + + :return: + """ + return None + + +def ceaser_cipher(): + """ + 1 Kata + + :return: + """ + return None + + +def sum_of_digits(): + """ + 1 Kata + + :return: + """ + return None + + +if __name__ == '__main__': + + print('\nSum of elements exercise results:\n') + print(sum_of_element([1, 2, 3, 4, 5, 6])) + + print('\nVerbing exercise results:\n') + print(verbing('walk')) + print(verbing('swimming')) + print(verbing('do')) + + + + + + diff --git a/python_katas/kata_1/test.py b/python_katas/kata_1/test.py new file mode 100644 index 00000000..28f60246 --- /dev/null +++ b/python_katas/kata_1/test.py @@ -0,0 +1,25 @@ +import unittest +from python_katas.kata_1 import questions + + +class TestSumOfElements(unittest.TestCase): + + 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) + + def test_negative_numbers(self): + lst = [1, -6, 7, 0, 99] + self.assertEqual(questions.sum_of_element(lst), 101) + + def test_all_zeros(self): + lst = [0] * 50000 + self.assertEqual(questions.sum_of_element(lst), 0) + + +if __name__ == '__main__': + unittest.main() From 33072b95e7fe386fd66ee3773f03ebc9dd8194d4 Mon Sep 17 00:00:00 2001 From: alonit Date: Tue, 29 Mar 2022 16:10:01 +0300 Subject: [PATCH 05/28] python katas --- .github/workflows/pre-submit-test.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/pre-submit-test.yaml b/.github/workflows/pre-submit-test.yaml index 641e55d3..f21e483c 100644 --- a/.github/workflows/pre-submit-test.yaml +++ b/.github/workflows/pre-submit-test.yaml @@ -24,5 +24,10 @@ jobs: runs-on: ubuntu-latest if: ${{ startsWith(github.ref, 'refs/heads/python_katas_') }} steps: + - uses: actions/checkout@v2 + - name: Set up Python 3.8 + uses: actions/setup-python@v2 + with: + python-version: 3.8 - run: | python -m python_katas.kata_1.test \ No newline at end of file From 8fcebcb25fcd09942bfc9f430a4a1bf70c9703f6 Mon Sep 17 00:00:00 2001 From: alonit Date: Tue, 29 Mar 2022 16:13:29 +0300 Subject: [PATCH 06/28] python katas --- .github/workflows/pre-submit-test.yaml | 3 ++- python_katas/kata_1/test.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pre-submit-test.yaml b/.github/workflows/pre-submit-test.yaml index f21e483c..1861b08b 100644 --- a/.github/workflows/pre-submit-test.yaml +++ b/.github/workflows/pre-submit-test.yaml @@ -29,5 +29,6 @@ jobs: uses: actions/setup-python@v2 with: python-version: 3.8 - - run: | + - name: Run Unittests + run: | python -m python_katas.kata_1.test \ No newline at end of file diff --git a/python_katas/kata_1/test.py b/python_katas/kata_1/test.py index 28f60246..92c3ccbe 100644 --- a/python_katas/kata_1/test.py +++ b/python_katas/kata_1/test.py @@ -10,7 +10,7 @@ def test_empty_list(self): def test_integers_list(self): lst = [1, 2, 3, 4, 5] - self.assertEqual(questions.sum_of_element(lst), 15) + self.assertEqual(questions.sum_of_element(lst), 14) def test_negative_numbers(self): lst = [1, -6, 7, 0, 99] From 236e574db2346957befe44ab3a94ea32cd4b035f Mon Sep 17 00:00:00 2001 From: alonit Date: Tue, 29 Mar 2022 16:36:49 +0300 Subject: [PATCH 07/28] python katas --- python_katas/kata_1/test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_katas/kata_1/test.py b/python_katas/kata_1/test.py index 92c3ccbe..0b9e99a2 100644 --- a/python_katas/kata_1/test.py +++ b/python_katas/kata_1/test.py @@ -14,7 +14,7 @@ def test_integers_list(self): def test_negative_numbers(self): lst = [1, -6, 7, 0, 99] - self.assertEqual(questions.sum_of_element(lst), 101) + self.assertEqual(questions.sum_of_element(lst), 100) def test_all_zeros(self): lst = [0] * 50000 From 806fcd6b10f8a566402f41dd8b9448f754aa31a2 Mon Sep 17 00:00:00 2001 From: alonit Date: Tue, 29 Mar 2022 17:46:40 +0300 Subject: [PATCH 08/28] python katas --- .github/workflows/pre-submit-test.yaml | 2 +- python_katas/kata_1/test.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pre-submit-test.yaml b/.github/workflows/pre-submit-test.yaml index 1861b08b..89789fc5 100644 --- a/.github/workflows/pre-submit-test.yaml +++ b/.github/workflows/pre-submit-test.yaml @@ -31,4 +31,4 @@ jobs: python-version: 3.8 - name: Run Unittests run: | - python -m python_katas.kata_1.test \ No newline at end of file + python -m python_katas.kata_1.test > results.txt \ No newline at end of file diff --git a/python_katas/kata_1/test.py b/python_katas/kata_1/test.py index 0b9e99a2..76f059ae 100644 --- a/python_katas/kata_1/test.py +++ b/python_katas/kata_1/test.py @@ -21,5 +21,24 @@ def test_all_zeros(self): self.assertEqual(questions.sum_of_element(lst), 0) +class TestSumOfElements2(unittest.TestCase): + + 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) + + def test_negative_numbers(self): + lst = [1, -6, 7, 0, 99] + self.assertEqual(questions.sum_of_element(lst), 100) + + def test_all_zeros(self): + lst = [0] * 50000 + self.assertEqual(questions.sum_of_element(lst), 0) + + if __name__ == '__main__': unittest.main() From 427cffe669459db51d151c4fe23a7fee4146d120 Mon Sep 17 00:00:00 2001 From: alonit Date: Tue, 29 Mar 2022 18:06:47 +0300 Subject: [PATCH 09/28] python katas --- python_katas/kata_1/test.py | 18 +++++++++++++----- python_katas/utils.py | 38 +++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 python_katas/utils.py diff --git a/python_katas/kata_1/test.py b/python_katas/kata_1/test.py index 76f059ae..699c463f 100644 --- a/python_katas/kata_1/test.py +++ b/python_katas/kata_1/test.py @@ -1,20 +1,23 @@ import unittest from python_katas.kata_1 import questions +from python_katas.utils import unittest_runner class TestSumOfElements(unittest.TestCase): - + """ + 1 Katas + """ 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), 14) + self.assertEqual(questions.sum_of_element(lst), 15) def test_negative_numbers(self): lst = [1, -6, 7, 0, 99] - self.assertEqual(questions.sum_of_element(lst), 100) + self.assertEqual(questions.sum_of_element(lst), 101) def test_all_zeros(self): lst = [0] * 50000 @@ -22,6 +25,9 @@ def test_all_zeros(self): class TestSumOfElements2(unittest.TestCase): + """ + 1 Katas + """ def test_empty_list(self): lst = [] @@ -33,7 +39,7 @@ def test_integers_list(self): def test_negative_numbers(self): lst = [1, -6, 7, 0, 99] - self.assertEqual(questions.sum_of_element(lst), 100) + self.assertEqual(questions.sum_of_element(lst), 101) def test_all_zeros(self): lst = [0] * 50000 @@ -41,4 +47,6 @@ def test_all_zeros(self): if __name__ == '__main__': - unittest.main() + import inspect + import sys + unittest_runner(inspect.getmembers(sys.modules[__name__], inspect.isclass)) diff --git a/python_katas/utils.py b/python_katas/utils.py new file mode 100644 index 00000000..ee9ccb71 --- /dev/null +++ b/python_katas/utils.py @@ -0,0 +1,38 @@ +import contextlib +import io +import sys +import unittest +import re + + +def parse_katas_score(classes, result): + katas = 0 + for name, cls in classes: + regex = r"FAIL: .*" + name + match = re.search(regex, result) + if not match: + katas += int(cls.__doc__.split('Kata')[0].strip()) + print('---------------------------------------') + print(f'Total Katas: {katas}') + print('---------------------------------------') + + +@contextlib.contextmanager +def err_to(file): + old_err = sys.stderr + sys.stderr = file + yield + sys.stderr = old_err + + +def unittest_runner(classes): + result = io.StringIO() + with err_to(result): + unittest.main(exit=False) + result.seek(0) + result = str(result.read()) + parse_katas_score( + classes, + result + ) + print(result) From a6eaba1f0532e7613b1d647755e14cb9c8ccc892 Mon Sep 17 00:00:00 2001 From: alonit Date: Tue, 29 Mar 2022 18:08:16 +0300 Subject: [PATCH 10/28] python katas --- .github/workflows/pre-submit-test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pre-submit-test.yaml b/.github/workflows/pre-submit-test.yaml index 89789fc5..1861b08b 100644 --- a/.github/workflows/pre-submit-test.yaml +++ b/.github/workflows/pre-submit-test.yaml @@ -31,4 +31,4 @@ jobs: python-version: 3.8 - name: Run Unittests run: | - python -m python_katas.kata_1.test > results.txt \ No newline at end of file + python -m python_katas.kata_1.test \ No newline at end of file From 3414d3ab0e9a6475407ab1b46c365b18c84be43a Mon Sep 17 00:00:00 2001 From: alonit Date: Tue, 29 Mar 2022 20:11:55 +0300 Subject: [PATCH 11/28] python katas --- .github/workflows/pre-submit-test.yaml | 2 +- python_katas/utils.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pre-submit-test.yaml b/.github/workflows/pre-submit-test.yaml index 1861b08b..ef869976 100644 --- a/.github/workflows/pre-submit-test.yaml +++ b/.github/workflows/pre-submit-test.yaml @@ -29,6 +29,6 @@ jobs: uses: actions/setup-python@v2 with: python-version: 3.8 - - name: Run Unittests + - name: Katas Tests results run: | python -m python_katas.kata_1.test \ No newline at end of file diff --git a/python_katas/utils.py b/python_katas/utils.py index ee9ccb71..c942289e 100644 --- a/python_katas/utils.py +++ b/python_katas/utils.py @@ -13,7 +13,7 @@ def parse_katas_score(classes, result): if not match: katas += int(cls.__doc__.split('Kata')[0].strip()) print('---------------------------------------') - print(f'Total Katas: {katas}') + print(f'Total Python Katas: {katas}') print('---------------------------------------') From 8c54ee4aa0e372f9c29cc18c406043cd366dab07 Mon Sep 17 00:00:00 2001 From: alonit Date: Tue, 29 Mar 2022 20:18:19 +0300 Subject: [PATCH 12/28] python katas --- python_katas/kata_1/test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_katas/kata_1/test.py b/python_katas/kata_1/test.py index 699c463f..3f1fd96d 100644 --- a/python_katas/kata_1/test.py +++ b/python_katas/kata_1/test.py @@ -13,7 +13,7 @@ def test_empty_list(self): def test_integers_list(self): lst = [1, 2, 3, 4, 5] - self.assertEqual(questions.sum_of_element(lst), 15) + self.assertEqual(questions.sum_of_element(lst), 14) def test_negative_numbers(self): lst = [1, -6, 7, 0, 99] From 93dfd0c816ebec601b9ca734ebbb6728fbbdc311 Mon Sep 17 00:00:00 2001 From: alonit Date: Wed, 30 Mar 2022 18:23:06 +0300 Subject: [PATCH 13/28] update req --- requirements.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index b854bca2..2f72d06b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,4 @@ -mkdocs \ No newline at end of file +mkdocs +pandas +flask +aiohttp \ No newline at end of file From 4d5ad2422de7129b4354034df4edafc2169d346d Mon Sep 17 00:00:00 2001 From: alonit Date: Wed, 30 Mar 2022 18:27:26 +0300 Subject: [PATCH 14/28] python katas --- python_katas/kata_1/test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_katas/kata_1/test.py b/python_katas/kata_1/test.py index 3f1fd96d..699c463f 100644 --- a/python_katas/kata_1/test.py +++ b/python_katas/kata_1/test.py @@ -13,7 +13,7 @@ def test_empty_list(self): def test_integers_list(self): lst = [1, 2, 3, 4, 5] - self.assertEqual(questions.sum_of_element(lst), 14) + self.assertEqual(questions.sum_of_element(lst), 15) def test_negative_numbers(self): lst = [1, -6, 7, 0, 99] From caba58b1cde8f6cb98291fd5c309eb47fa720a84 Mon Sep 17 00:00:00 2001 From: alonit Date: Fri, 1 Apr 2022 13:46:38 +0300 Subject: [PATCH 15/28] python katas --- python_katas/kata_1/questions.py | 245 ++++++++++++++++++++++++++----- python_katas/kata_1/test.py | 163 ++++++++++++++++++-- 2 files changed, 360 insertions(+), 48 deletions(-) diff --git a/python_katas/kata_1/questions.py b/python_katas/kata_1/questions.py index 4481e6f9..95069ee1 100644 --- a/python_katas/kata_1/questions.py +++ b/python_katas/kata_1/questions.py @@ -3,7 +3,7 @@ def sum_of_element(elements): 1 Kata :param elements: list of integers - :return: Return the resulting string. + :return: Return int - the sum of all elements. """ s = 0 for num in elements: @@ -20,6 +20,11 @@ def verbing(word): Unless it already ends in 'ing', in which case add 'ly' instead. If the string length is less than 3, leave it unchanged. + e.g. + teach -> teaching + do -> do + swimming -> swimmingly + :param word: str :return: Return the resulting string. """ @@ -33,8 +38,7 @@ def words_concatenation(words): Given a list of words, write a program that concatenates the words. For example: - >> words_concatenation(['take', 'me', 'home']) - 'take me home' + words_concatenation(['take', 'me', 'home']) returns 'take me home' :param words: list of str :return: Return the resulting string. @@ -49,8 +53,7 @@ def reverse_words_concatenation(words): Given a list of words, write a program that concatenates the words in a reverse way (both words and each word itself) For example: - >> reverse_words_concatenation(['take', 'me', 'home']) - 'home me take' + reverse_words_concatenation(['take', 'me', 'home']) returns 'home me take' :param words: list of str :return: Return the resulting string. @@ -60,10 +63,17 @@ def reverse_words_concatenation(words): def is_unique_string(some_str): """ - 1 Kata + 2 Kata + + Given a string, the function returns True is all characters in the string are unique, False otherwise + + e.g + 'abcd' -> True + 'aaabcd' -> False + '' -> True (empty string) :param some_str: - :return: + :return: bool """ return None @@ -72,8 +82,16 @@ def list_diff(elements): """ 1 Kata - :param elements: - :return: + Given a list of integers as an input, return the "diff" list - each element is + reduces by its previous one. The first element should be None + + e.g. + [1, 2, 3, 4, 7, 11] -> [None, 1, 1, 3, 4] + [] -> [] + [1, 5, 0, 4, 1, 1, 1] -> [None, 4, -5, 4, -3, 0, 0] + + :param elements: list of integers + :return: the diff list """ return None @@ -82,9 +100,11 @@ def prime_number(num): """ 1 Kata + Check if the given number is prime or not. + hint: use the built-in function "range" - :param num: - :return: + :param num: the number to check + :return: bool. True if prime, else False """ return None @@ -93,36 +113,80 @@ def palindrome_num(num): """ 1 Kata - :param num: - :return: + Check whether a number is palindrome or not + + e.g. + 1441 -> True + 123 -> False + + :param num: int + :return: bool. True is palindrome, else False """ return None def pair_match(men, women): """ - 1 Kata + 3 Kata - :param men: - :param women: - :return: + This function gets two dictionaries of the type: + { + "": + } + + Where is a string name, and is an integer representing the age + The function returns a pair of names (tuple), of from men dict, the other from women dict, + where their absolute age differences is the minimal + + e.g. + men = {"John": 20, "Abraham": 45} + women = {"July": 18, "Kim": 26} + + The returned value should be a tuple ("John", "July") since: + + abs(John - Kim) = abs(20 - 26) = abs(-6) = 6 + abs(John - July) = abs(20 - 18) = abs(2) = 2 + abs(Abraham - Kim) = abs(45 - 26) = abs(19) = 19 + abs(Abraham - July) = abs(45 - 18) = abs(27) = 27 + + :param men: dict mapping name -> age + :param women: dict mapping name -> age + :return: tuple (men_name, women_name) such their age absolute difference is the minimal """ return None -def bad_average(): +def bad_average(a, b, c): """ 1 Kata + + 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 -def best_student(): +def best_student(grades): """ 1 Kata + This function gets a dict of students -> grades mapping, and returns the student with the highest grade - :return: + e.g. + { + "Ben": 78, + "Hen": 88, + "Natan": 99, + "Efraim": 65, + "Rachel": 95 + } + + will return "Natan" + + :param grades: dict of name -> grade mapping + :return: str. some key from the dict """ return None @@ -131,61 +195,172 @@ def print_dict_as_table(some_dict): """ 1 Kata - + Prints dictionary keys and values as the following format. For: + { + "Ben": 78, + "Hen": 88, + "Natan": 99, + "Efraim": 65, + "Rachel": 95 + } + + The output will be: + + Key Value + ------------- + Ben 78 + Hen 88 + Natan 99 + Efraim 65 + Rachel 95 + + :param some_dict: :return: """ return None -def merge_dicts(some_dict): +def merge_dicts(dict1, dict2): """ 1 Kata + This functions merges dict2's keys and values into dict1, and returns dict1 + + e.g. + dict1 = {'a': 1} + dict2 = {'b': 2} + The results will by + dict1 = {'a': 1, 'b': 2} + + :param dict1: + :param dict2: :return: """ - return None + return dict1 -def seven_boom(): +def seven_boom(n): """ 1 Kata - :return: + This functions returns a list of all "Booms" for a 7-boom play starting from 1 to n + + e.g. For n = 30 + The return value will be [7, 14, 17, 21, 27, 28] + + :param n: int. The last number for count for a 7-boom play + :return: list of integers """ return None -def ceaser_cipher(): +def caesar_cipher(str_to_encrypt): """ - 1 Kata + 2 Kata + + This function encrypts the given string according to caesar cipher (a - d, b - e, ..., y - b, z - c etc...). + Spaces remain as they are. You can assume the string contain a-z and A-Z chars only. + + e.g. + Fly Me To The Moon -> Iob Ph Wr Wkh Prrq :return: """ return None -def sum_of_digits(): +def sum_of_digits(digits_str): """ 1 Kata - :return: + Calculates the sum of digits in a string (you can assume the input is a string containing numeric digits only) + + e.g. + '2524' -> 13 + '' -> 0 + '00232' -> 7 + + + :param digits_str: str of numerical digits only + :return: int representing the sum of digits """ return None if __name__ == '__main__': - print('\nSum of elements exercise results:\n') + print('\nsum_of_element:\n--------------------') print(sum_of_element([1, 2, 3, 4, 5, 6])) - print('\nVerbing exercise results:\n') + print('\nverbing:\n--------------------') print(verbing('walk')) print(verbing('swimming')) print(verbing('do')) - - - - + print('\nwords_concatenation:\n--------------------') + print(words_concatenation(['take', 'me', 'home'])) + + print('\nreverse_words_concatenation:\n--------------------') + print(reverse_words_concatenation(['take', 'me', 'home'])) + + print('\nis_unique_string:\n--------------------') + print(is_unique_string('aasdssdsederd')) + print(is_unique_string('12345tgbnh')) + + print('\nlist_diff:\n--------------------') + print(list_diff([1, 2, 3, 8, 77, 0])) + + print('\nprime_number:\n--------------------') + print(prime_number(5)) + print(prime_number(22)) + + print('\npalindrome_num:\n--------------------') + print(palindrome_num(12221)) + print(palindrome_num(577)) + + print('\npair_match:\n--------------------') + print(pair_match( + { + "John": 20, + "Abraham": 45 + }, + { + "July": 18, + "Kim": 26 + } + )) + + print('\nbad_average:\n--------------------') + print(bad_average(1, 2, 3)) + + print('\nbest_student:\n--------------------') + print(best_student({ + "Ben": 78, + "Hen": 88, + "Natan": 99, + "Efraim": 65, + "Rachel": 95 + })) + + print('\nprint_dict_as_table:\n--------------------') + print(print_dict_as_table({ + "Ben": 78, + "Hen": 88, + "Natan": 99, + "Efraim": 65, + "Rachel": 95 + })) + + print('\nmerge_dicts:\n--------------------') + print(merge_dicts({'a': 1}, {'b': 2})) + + print('\nseven_boom:\n--------------------') + print(seven_boom(30)) + + print('\ncaesar_cipher:\n--------------------') + print(caesar_cipher('Fly Me To The Moon')) + + print('\nsum_of_digits:\n--------------------') + print(sum_of_digits('1223432')) diff --git a/python_katas/kata_1/test.py b/python_katas/kata_1/test.py index 699c463f..8ee82d09 100644 --- a/python_katas/kata_1/test.py +++ b/python_katas/kata_1/test.py @@ -3,6 +3,15 @@ from python_katas.utils import unittest_runner +testers = ['dorondollev', + 'danielbar0101', + 'DustyMadDude', + 'Amitpoz', + 'netanalm', + 'AlexeyMihaylovDev', + 'xXxARLxXx'] + + class TestSumOfElements(unittest.TestCase): """ 1 Katas @@ -24,26 +33,154 @@ def test_all_zeros(self): self.assertEqual(questions.sum_of_element(lst), 0) -class TestSumOfElements2(unittest.TestCase): +class TestVerbing(unittest.TestCase): """ 1 Katas """ - def test_empty_list(self): - lst = [] - self.assertEqual(questions.sum_of_element(lst), 0) + def test_sample(self): + # your code here + pass - def test_integers_list(self): - lst = [1, 2, 3, 4, 5] - self.assertEqual(questions.sum_of_element(lst), 15) - def test_negative_numbers(self): - lst = [1, -6, 7, 0, 99] - self.assertEqual(questions.sum_of_element(lst), 101) +class TestWordsConcatenation(unittest.TestCase): + """ + 1 Katas + """ - def test_all_zeros(self): - lst = [0] * 50000 - self.assertEqual(questions.sum_of_element(lst), 0) + def test_sample(self): + # your code here + pass + + +class TestReverseWordsConcatenation(unittest.TestCase): + """ + 1 Katas + """ + + def test_sample(self): + # your code here + pass + + +class TestIsUniqueString(unittest.TestCase): + """ + 2 Katas + """ + + def test_sample(self): + # your code here + pass + + +class TestListDiff(unittest.TestCase): + """ + 1 Katas + """ + + def test_sample(self): + # your code here + pass + + +class TestPrimeNumber(unittest.TestCase): + """ + 1 Katas + """ + + def test_sample(self): + # your code here + pass + + +class TestPalindromeNum(unittest.TestCase): + """ + 1 Katas + """ + + def test_sample(self): + # your code here + pass + + +class TestPairMatch(unittest.TestCase): + """ + 3 Katas + """ + + def test_sample(self): + # your code here + pass + + +class TestBadAverage(unittest.TestCase): + """ + 1 Katas + """ + + def test_sample(self): + # your code here + pass + + +class TestBestStudent(unittest.TestCase): + """ + 1 Katas + """ + + def test_sample(self): + # your code here + pass + + +class TestPrintDictAsTable(unittest.TestCase): + """ + 1 Katas + """ + + def test_sample(self): + # your code here + pass + + +class TestMergeDicts(unittest.TestCase): + """ + 1 Katas + """ + + def test_sample(self): + # your code here + pass + + +class TestSevenBoom(unittest.TestCase): + """ + 1 Katas + """ + + def test_sample(self): + # your code here + pass + + +class TestCaesarCipher(unittest.TestCase): + """ + 1 Katas + """ + + def test_sample(self): + # your code here + pass + + +class TestSumOfDigits(unittest.TestCase): + """ + 1 Katas + """ + + def test_sample(self): + # your code here + pass if __name__ == '__main__': From 18c9dc365e1bf2ba9f9dfe10bd9fd2776464af4e Mon Sep 17 00:00:00 2001 From: doron Date: Tue, 5 Apr 2022 08:12:02 +0300 Subject: [PATCH 16/28] 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 17/28] 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 18/28] 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 19/28] 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 20/28] 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 21/28] 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 22/28] 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 23/28] 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 24/28] 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 25/28] 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 26/28] 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 27/28] 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 24c7e6d1045359e738c15a052e0e358c7c14e5af Mon Sep 17 00:00:00 2001 From: Ariel Date: Thu, 14 Apr 2022 01:04:02 +0300 Subject: [PATCH 28/28] Please check 6 - TestPrimeNumber 7 - TestSevenBoom --- python_katas/kata_1/test.py | 43 ++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/python_katas/kata_1/test.py b/python_katas/kata_1/test.py index 1df69ca8..e940a353 100644 --- a/python_katas/kata_1/test.py +++ b/python_katas/kata_1/test.py @@ -95,9 +95,7 @@ class TestListDiff(unittest.TestCase): 1 Katas """ - def test_sample(self): - # your code here - pass + class TestPrimeNumber(unittest.TestCase): @@ -105,9 +103,29 @@ class TestPrimeNumber(unittest.TestCase): 1 Katas """ - def test_sample(self): - # your code here - pass + def test_prime_1(self): + number = 7 + self.assertEqual(questions.prime_number(number), True) + + def test_prime_2(self): + number = 17 + self.assertEqual(questions.prime_number(number), True) + + def test_prime_3(self): + number = 13 + self.assertEqual(questions.prime_number(number), True) + + def test_not_prime_1(self): + number = 1 + self.assertEqual(questions.prime_number(number), False) + + def test_not_prime_2(self): + number = -20 + self.assertEqual(questions.prime_number(number), False) + + def test_not_prime_3(self): + number = 2.4 + self.assertEqual(questions.prime_number(number), False) class TestPalindromeNum(unittest.TestCase): @@ -192,10 +210,6 @@ class TestPrintDictAsTable(unittest.TestCase): 1 Katas """ - def test_sample(self): - # your code here - pass - class TestMergeDicts(unittest.TestCase): """ @@ -212,10 +226,13 @@ class TestSevenBoom(unittest.TestCase): 1 Katas """ - def test_sample(self): - # your code here - pass + def test_if_contains_7(self): + n = 7 + self.assertEqual(questions.seven_boom(n), [7]) + def test_if_modulo_7(self): + n = 30 + self.assertEqual(questions.seven_boom(n), [7, 14, 17, 21, 27, 28]) class TestCaesarCipher(unittest.TestCase): """