-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solution to issue #18 in Python #26
Conversation
@Himanshu1495 good job, it looks like this method would work. Unfortunately, I don’t know any python or how to write python tests, do you have any good suggestions? Are you able to write some tests? |
@Himanshu1495 could you also write this as a function so we can pass in parameters? |
@msach22 , I have wrote the code as function so that you can pass parameters. I am working on creating tests for the same and will be done with it in 2 days.Should I push the code for test of this issue? Do you also need me to push a skeleton code for testing the python files in future too ? |
@Himanshu1495 Yes, please add the test to this issue :) And a skeleton for python testing would be fantastic! I look forward to seeing your other commits :) |
@msach22 , I have added tests for the code. Also I have created and pushed the skeleton code. Will you be able to review it ? I need approved review on my code to be able to merge the pull request. |
@msach22 , Have you checked the code ? Does it meet the requirements needed to contribute to this project ? Please let me know as soon as possible so that I may start working on other issues in this project. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Himanshu1495 sorry for the delay, I'll review it today! |
solutions/18.py
Outdated
''' | ||
|
||
def missing_num(arr): | ||
#get first element of array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't put comments for obvious things. The goal should always to write your code in a way so that it doesn't require comments. For example, firstElement = arr[0]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@songz , Okay , i will modify that in a day.
solutions/18.py
Outdated
#get last element of array | ||
end = arr[-1] | ||
#create new array by iterating from start element to end element | ||
new_arr = [x for x in range(start,end+1)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What you are trying to do here is add up all the numbers from a to b. You don't need an array to do this (for loop? While loop? math formula?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@songz , array has been used with a purpose of using a built-in function sum(name_of_array). Here is the line which uses sum() , return sum(new_arr)-sum(arr)
. We can use a for loop to do the same task but in my opinion, sum() looks cleaner. If you insist on using a loop, I will certainly be able to do that too? Should I use loop or keep it as it is ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Himanshu1495 , try this, it looks much cleaner and doesn't involve creating a new array.
sum = 0;
for x in range(start, end+1)
sum += x;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@songz , Sure, I will do that .
Comments in the code have been removed. for loop has been used as discussed.
Tests have been modified as per the modified code of 18.py . The code has been tested and it passed the test cases.
@songz , I have modified the code and its tests as per the discussion. Please check if it suffices the requirements of the solution. |
solutions/18.py
Outdated
return new_sum - sum(arr) | ||
|
||
#example to run the code | ||
print missing_num([5,6,8,9]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please remove print statements
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@songz , I have removed the print statements
test/test18.py
Outdated
@@ -0,0 +1,19 @@ | |||
import unittest | |||
|
|||
def missing_num(arr): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should be importing from your solutions file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@songz, can you please elaborate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't make sense to write your solution in solutions file, then literally write the same code here. The point of tests is to test your code, so I would import the solutions file. I did a quick google search, perhaps this tutorial would help? https://www.codementor.io/sheena/python-path-virtualenv-import-for-beginners-du107r3o1
By the way, good job. If you pull this off we will officially support python :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@songz Modified the test after almost an hour of searching for a way to do it. Phew! It was done finally. The solution will work on Linux perfectly. For Windows, a bit of modification might be required.
Thanks a lot for appreciating. It always feels good to know that my contribution is helping you :)
solutions/pythontest_skeleton.py
Outdated
@@ -0,0 +1,15 @@ | |||
#Created by Himanshu Nachane (@Himanshu1495) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test should not be in solutions folder.
test/pythontest_skeleton.py
Outdated
@@ -0,0 +1,15 @@ | |||
#Created by Himanshu Nachane (@Himanshu1495) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
y do we need a pythontest_skeleton file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@songz We need pythontest_skeleton so that new Python contributors can easily use it to create their tests. All they need to do is remove the comments in the file and replace it with actual data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but y not just use your test file as a reference?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to use your test file as reference rather than create a new skeleton
…ith new changes. Name of solution changed from 18.py to sol18.py to avoid conflict while importing the file for testing
@songz Here is an explanation of what I did in the latest commit. |
test/pythontest_skeleton.py
Outdated
@@ -0,0 +1,15 @@ | |||
#Created by Himanshu Nachane (@Himanshu1495) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but y not just use your test file as a reference?
test/test18.py
Outdated
@@ -0,0 +1,14 @@ | |||
import sys |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename to test/18.py
. since file is already in the test folder, you don't need to put test in the file name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@songz renaming done :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@songz pythontest_skeleton is deleted.
test/pythontest_skeleton.py
Outdated
@@ -0,0 +1,15 @@ | |||
#Created by Himanshu Nachane (@Himanshu1495) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to use your test file as reference rather than create a new skeleton
@@ -0,0 +1,17 @@ | |||
#Himanshu Nachane (@Himanshu1495) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you rename this file to just 18.py please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@songz, I tried keeping it 18.py but then I found out that Python files should not start with a number for the exact same reason. It would be good to keep the file name as sol18.py or anything which starts with a string.
I have created the solution for issue #18 using Python 2.7 . It works perfectly when tested manually. If it looks good, then i request to merge this solution. Thanks.