From 04b3fba4e458d4073b0d1ff3e20a3f003f6765be Mon Sep 17 00:00:00 2001 From: hudi-hub Date: Sat, 23 Jul 2022 12:54:47 +0800 Subject: [PATCH 1/9] "2. Write a test for each function that demonstrates the problem" --- __pycache__/object_analisis.cpython-39.pyc | Bin 0 -> 1285 bytes object_analisis.py | 3 +- test_object_analisis.py | 65 +++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 __pycache__/object_analisis.cpython-39.pyc create mode 100644 test_object_analisis.py diff --git a/__pycache__/object_analisis.cpython-39.pyc b/__pycache__/object_analisis.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..3360b17d7d3a6837de6ee9940e22041d05b5646b GIT binary patch literal 1285 zcmb7^&2H2%5P)sxXS1QLDzt}|(`s)k(QZ`^l>i|S<%X(Cr9uj-DrfE8O`CPHb^@)) zp5Q@1;=)_>IYMuo`UVx^z}Wj2wp?&DGq%5(N#e|euCDqB#*_Y!aK}aHr(RTt1B>S% zY9ACs41Y!eVkE%KVs=ad!Yz(C`C@bY6@P*YBjzv{yc{6T60wg^yVk{!YBu1W$O3r@ zqJD#7c#J^^hy`L7Xdj(yjGe+^WD_y#oXAFjnZ1X`c+^;;4$$3#7)nOFOVkB=Sio~b ztTj4ZqBWpmPDOzWa_-1)V6CaOwPoHBDoBB0eg#%DZ8f`MbzqiZ*I;$Lwwhft7np6> zEm+O8)$zB?KZ3J`v$=P014n>LJG)GIV5^ME8+}@V+ov(TxD0%ir(BBUJ>`MblVRy- z9~F3pM_dIq?5gjm?UuD9JmcL=1$C7VPbYpWdOG)AGhmU?(`y}H;za5%Z`7|honytN z>J0Kk^jRbZoqev(vn1^tzkShsboiPq!Klde8>$^p2;0-k!z}cCmE;$Ii*^`*6Xbld_M*C^2g}my zQJHDCN3c?BpmZ&GxG}8)ssB=K*L?8OiA0)brG2InQ97xNL{>U+LYXpdmE5L=CpUOH WOjsWCXQtS6Ht&Jm@bG=htN#I5w<|>e literal 0 HcmV?d00001 diff --git a/object_analisis.py b/object_analisis.py index 3fc4c7e..59999c1 100644 --- a/object_analisis.py +++ b/object_analisis.py @@ -57,4 +57,5 @@ def farthest(objects): if o["redshift"] == highest_redshift: return o -print(farthest(json.loads(input))) \ No newline at end of file +print(farthest(json.loads(input))) + diff --git a/test_object_analisis.py b/test_object_analisis.py new file mode 100644 index 0000000..d7cc52b --- /dev/null +++ b/test_object_analisis.py @@ -0,0 +1,65 @@ +""" 2. Write a test for each function that demonstrate the problem """ +import json +import unittest +from object_analisis import aboundant, farthest + +class TestObjectAnalisis(unittest.TestCase): + """ + Our basic test class + Any method which starts with ``test_`` will considered as a test case. + """ + + def test_aboundant(self): + """ + The actual test case for function aboundant. + Actual result is the value returned from executing the function. + Expected result is the correct value. + In order to pass the test, the actual should be equal to expected result + """ + input = """ + [ + { + "type": "frb", + "name": "crab", + "redshift": 0 + } + ] + """ + actual = aboundant(json.loads(input)) + self.assertEqual(actual, 'frbs') # actual should be equal to expected + + + + def test_farthest(seft): + """ + The actual test case for function farthest. + Actual result is the value returned from executing the function. + Expected result is the correct value. + In order to pass the test, the actual should be equal to expected result + """ + print("hello") + input = """ + [ + { + "type": "star", + "name": "alpha-centaurus", + "redshift": 0 + }, + { + "type": "nebula", + "name": "crab", + "redshift": 4 + } + ] + """ + actual = farthest(json.loads(input)) + expected = { + "type": "nebula", + "name": "crab", + "redshift": 4 + } + seft.assertEqual(actual, expected) # actual should be equal to expected + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file From 7d981307341a0d47d33da14f035c042d7c4a40aa Mon Sep 17 00:00:00 2001 From: hudi-hub Date: Sat, 23 Jul 2022 12:58:05 +0800 Subject: [PATCH 2/9] "delete print hello" --- test_object_analisis.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test_object_analisis.py b/test_object_analisis.py index d7cc52b..f7b7db7 100644 --- a/test_object_analisis.py +++ b/test_object_analisis.py @@ -37,7 +37,6 @@ def test_farthest(seft): Expected result is the correct value. In order to pass the test, the actual should be equal to expected result """ - print("hello") input = """ [ { From c9df3ac04c41fe7fd882519eb335f91e40fe200b Mon Sep 17 00:00:00 2001 From: hudi-hub Date: Sat, 23 Jul 2022 13:14:56 +0800 Subject: [PATCH 3/9] " 3. Fix the function and ensure the test passes." --- object_analisis.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/object_analisis.py b/object_analisis.py index 59999c1..7c66ebd 100644 --- a/object_analisis.py +++ b/object_analisis.py @@ -14,7 +14,7 @@ def aboundant(objects): sum_supernovae += 1 for o in objects: if o['type'] == 'frb': - sum_supernovae += 1 + sum_frbs += 1 if sum_stars >= sum_galaxies and sum_stars >= sum_supernovae and sum_stars >= sum_frbs: return 'stars' if sum_galaxies >= sum_stars and sum_galaxies >= sum_supernovae and sum_galaxies >= sum_frbs: @@ -44,6 +44,7 @@ def aboundant(objects): ] """ + import json print(aboundant(json.loads(input))) @@ -51,7 +52,7 @@ def aboundant(objects): def farthest(objects): highest_redshift = None for o in objects: - if highest_redshift is None or o["redshift"] < highest_redshift: + if highest_redshift is None or o["redshift"] > highest_redshift: highest_redshift = o["redshift"] for o in objects: if o["redshift"] == highest_redshift: From a8677b6da9c50ef83a74f9b42d5e611423160cef Mon Sep 17 00:00:00 2001 From: Hudi <82321252+hudi-hub@users.noreply.github.com> Date: Sat, 23 Jul 2022 13:51:59 +0800 Subject: [PATCH 4/9] Functions' performance (analysing and explain how to improve) - Reduce the for loops, logical conditions, using modules and build-in functions. --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 1ab794e..0e61596 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,20 @@ for performance and readability 1. Describe how you would go about increasing the performance. + #### How to increase the performance + - Use Built-In Functions + - Use Module Importing + - Reduce the computation What steps would you take? + #### Steps to increase the performance + - Build in function such as max + - Importing unittest module for test automation + - Reducing calculation and reduce memory usage by using less for loop and logical condition How do you determine what to improve? + #### Determine what to improve + - Tidy up the code + - Save time, labor, cost when doing testing by automation and unittest. + - System perform respond faster because it use less memory and less computation needed 1. Try to increase the performance of both functions. Use only built-in python modules and types, don't bother trying to use external packages From 4bca5f3501325724afee57798275395d9f56ecda Mon Sep 17 00:00:00 2001 From: hudi-hub Date: Sat, 23 Jul 2022 14:10:52 +0800 Subject: [PATCH 5/9] " 6. Increase the performance of both functions" --- README.md | 1 + object_analisis.py | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 1ab794e..a79782b 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ for performance and readability 1. Describe how you would go about increasing the performance. + - Use some build in Python What steps would you take? How do you determine what to improve? 1. Try to increase the performance of both functions. diff --git a/object_analisis.py b/object_analisis.py index 7c66ebd..c057cd1 100644 --- a/object_analisis.py +++ b/object_analisis.py @@ -1,27 +1,26 @@ def aboundant(objects): - sum_stars = 0 - sum_galaxies = 0 - sum_supernovae = 0 - sum_frbs = 0 - for o in objects: + # asign initiate variables in one line + sum_stars = sum_galaxies = sum_supernovae = sum_frbs = 0 + + for o in objects: # number of for loops is reduced if o['type'] == 'star': sum_stars += 1 - for o in objects: if o['type'] == 'galaxy': sum_galaxies += 1 - for o in objects: if o['type'] == 'supernovae': sum_supernovae += 1 - for o in objects: if o['type'] == 'frb': sum_frbs += 1 - if sum_stars >= sum_galaxies and sum_stars >= sum_supernovae and sum_stars >= sum_frbs: + + # using build-in function max and reducing logical conditions + max_count = max(sum_stars,sum_galaxies,sum_stars,sum_supernovae,sum_frbs) + if max_count == sum_stars: return 'stars' - if sum_galaxies >= sum_stars and sum_galaxies >= sum_supernovae and sum_galaxies >= sum_frbs: + if max_count == sum_galaxies: return 'galaxies' - if sum_supernovae >= sum_stars and sum_supernovae >= sum_galaxies and sum_supernovae >= sum_frbs: + if max_count == sum_supernovae: return 'supernovae' - if sum_frbs >= sum_stars and sum_frbs >= sum_galaxies and sum_frbs >= sum_supernovae: + if max_count == sum_frbs: return 'frbs' input = """ @@ -50,9 +49,10 @@ def aboundant(objects): def farthest(objects): - highest_redshift = None + # initiate value is the first object is highest to reduce calculation and logical condition + highest_redshift = objects[0]["redshift"] for o in objects: - if highest_redshift is None or o["redshift"] > highest_redshift: + if o["redshift"] > highest_redshift: highest_redshift = o["redshift"] for o in objects: if o["redshift"] == highest_redshift: From 5e05bd8fff2bebeb516856801860a7def5275306 Mon Sep 17 00:00:00 2001 From: hudi-hub Date: Sat, 23 Jul 2022 14:27:00 +0800 Subject: [PATCH 6/9] "7 & 8. Improve readibility and little examples" --- object_analisis.py | 85 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 60 insertions(+), 25 deletions(-) diff --git a/object_analisis.py b/object_analisis.py index c057cd1..d93f373 100644 --- a/object_analisis.py +++ b/object_analisis.py @@ -1,3 +1,6 @@ +import json +import sys + def aboundant(objects): # asign initiate variables in one line sum_stars = sum_galaxies = sum_supernovae = sum_frbs = 0 @@ -23,30 +26,6 @@ def aboundant(objects): if max_count == sum_frbs: return 'frbs' -input = """ -[ - { - "type": "star", - "name": "alpha-centaurus", - "redshift": 0 - }, - { - "type": "nebula", - "name": "crab", - "redshift": 0 - }, - { - "type": "galaxy", - "name": "sombrero", - "redshift": 0 - } -] -""" - - -import json -print(aboundant(json.loads(input))) - def farthest(objects): # initiate value is the first object is highest to reduce calculation and logical condition @@ -58,5 +37,61 @@ def farthest(objects): if o["redshift"] == highest_redshift: return o -print(farthest(json.loads(input))) +def main(): + input = """ + [ + { + "type": "star", + "name": "alpha-centaurus", + "redshift": 0 + }, + { + "type": "nebula", + "name": "crab", + "redshift": 0 + }, + { + "type": "galaxy", + "name": "sombrero", + "redshift": 0 + } + ] + """ + + input1 = """ + [ + { + "type": "frb", + "name": "crab", + "redshift": 0 + } + ] + """ + + input2 = """ + [ + { + "type": "frb", + "name": "crab", + "redshift": 0 + }, + { + "type": "galaxy", + "name": "sombrero", + "redshift": 4 + } + ] + """ + + print(aboundant(json.loads(input))) + print(farthest(json.loads(input))) + + # little examples + print(aboundant(json.loads(input1))) + print(farthest(json.loads(input2))) + +if __name__ == '__main__': + if len(sys.argv) > 1: + main(int(sys.argv[1])) + main() \ No newline at end of file From f348d1dcd5ec2b97044dd5e67a66037c45ebdcb1 Mon Sep 17 00:00:00 2001 From: hudi-hub Date: Sat, 23 Jul 2022 14:46:35 +0800 Subject: [PATCH 7/9] "9. Add comments and descriptions" --- object_analisis.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/object_analisis.py b/object_analisis.py index d93f373..58bc03d 100644 --- a/object_analisis.py +++ b/object_analisis.py @@ -1,6 +1,12 @@ import json import sys +"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +# Functionn aboundant to find the highest total of objects by type +# Taking an object array with elements are dictionary as input +# Counting the total of stars, galaxies, supernovae, frbs (assumption is fast radio burst - doublecheck) +# Return a string which is the name of the type of the which has the highest count. +"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" def aboundant(objects): # asign initiate variables in one line sum_stars = sum_galaxies = sum_supernovae = sum_frbs = 0 @@ -27,11 +33,17 @@ def aboundant(objects): return 'frbs' +"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +# Function farthest to find the object with highest redshift +# Taking an object array with elements are dictionary as input +# Return an object which has the highest redshift +# If many objects have the same highest redshift. It will return the first one to be found. +"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" def farthest(objects): # initiate value is the first object is highest to reduce calculation and logical condition - highest_redshift = objects[0]["redshift"] + highest_redshift = None for o in objects: - if o["redshift"] > highest_redshift: + if (highest_redshift == None) or (o["redshift"] > highest_redshift): highest_redshift = o["redshift"] for o in objects: if o["redshift"] == highest_redshift: @@ -39,6 +51,7 @@ def farthest(objects): def main(): + # Some example inputs to test funtions input = """ [ { @@ -84,12 +97,18 @@ def main(): ] """ + input3 = """ + [] + """ + print(aboundant(json.loads(input))) print(farthest(json.loads(input))) # little examples print(aboundant(json.loads(input1))) print(farthest(json.loads(input2))) + print(aboundant(json.loads(input3))) + print(farthest(json.loads(input3))) if __name__ == '__main__': if len(sys.argv) > 1: From 345a35b5451a5ea760ae2a41f9fcaa3ba03cd152 Mon Sep 17 00:00:00 2001 From: hudi-hub Date: Sat, 23 Jul 2022 15:22:41 +0800 Subject: [PATCH 8/9] "Auto test after each commit" --- .github/workflows/actions.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 .github/workflows/actions.yml diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml new file mode 100644 index 0000000..f1f644b --- /dev/null +++ b/.github/workflows/actions.yml @@ -0,0 +1,12 @@ +name: learn-github-actions +on: [commit] +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: '14' + - run: npm install -g python3 + - run: python test_object_analisis.py \ No newline at end of file From aa6f474654b91901a0387867c24adee5cb822b72 Mon Sep 17 00:00:00 2001 From: hudi-hub Date: Sat, 23 Jul 2022 15:29:12 +0800 Subject: [PATCH 9/9] "trying keywork push intead of commit" --- .github/workflows/actions.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml index f1f644b..d189c7a 100644 --- a/.github/workflows/actions.yml +++ b/.github/workflows/actions.yml @@ -1,5 +1,5 @@ name: learn-github-actions -on: [commit] +on: [push] jobs: build: runs-on: ubuntu-latest