Skip to content
12 changes: 12 additions & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file added __pycache__/object_analisis.cpython-39.pyc
Binary file not shown.
132 changes: 94 additions & 38 deletions object_analisis.py
Original file line number Diff line number Diff line change
@@ -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)))

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()
64 changes: 64 additions & 0 deletions test_object_analisis.py
Original file line number Diff line number Diff line change
@@ -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()