diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml new file mode 100644 index 0000000..d189c7a --- /dev/null +++ b/.github/workflows/actions.yml @@ -0,0 +1,12 @@ +name: learn-github-actions +on: [push] +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 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 diff --git a/__pycache__/object_analisis.cpython-39.pyc b/__pycache__/object_analisis.cpython-39.pyc new file mode 100644 index 0000000..3360b17 Binary files /dev/null and b/__pycache__/object_analisis.cpython-39.pyc differ diff --git a/object_analisis.py b/object_analisis.py index 3fc4c7e..58bc03d 100644 --- a/object_analisis.py +++ b/object_analisis.py @@ -1,60 +1,116 @@ +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): - 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_supernovae += 1 - if sum_stars >= sum_galaxies and sum_stars >= sum_supernovae and sum_stars >= sum_frbs: + sum_frbs += 1 + + # 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 = """ -[ - { - "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))) - +"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +# 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 = None for o in objects: - if highest_redshift is None or 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: return o -print(farthest(json.loads(input))) \ No newline at end of file + +def main(): + # Some example inputs to test funtions + 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 + } + ] + """ + + 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: + main(int(sys.argv[1])) + main() \ No newline at end of file diff --git a/test_object_analisis.py b/test_object_analisis.py new file mode 100644 index 0000000..f7b7db7 --- /dev/null +++ b/test_object_analisis.py @@ -0,0 +1,64 @@ +""" 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 + """ + 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