Skip to content
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

Merged
merged 13 commits into from
Jul 7, 2017
Merged

Solution to issue #18 in Python #26

merged 13 commits into from
Jul 7, 2017

Conversation

Himanshu1495
Copy link
Contributor

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.

@msach22
Copy link
Collaborator

msach22 commented Apr 25, 2017

@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?

@msach22
Copy link
Collaborator

msach22 commented Apr 26, 2017

@Himanshu1495 could you also write this as a function so we can pass in parameters?

@Himanshu1495
Copy link
Contributor Author

@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 ?

@msach22
Copy link
Collaborator

msach22 commented Apr 26, 2017

@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 :)

@Himanshu1495
Copy link
Contributor Author

@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.

@Himanshu1495
Copy link
Contributor Author

@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.

Copy link
Contributor Author

@Himanshu1495 Himanshu1495 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@msach22 @songz I would like to get my code reviewed by you.

@songz
Copy link
Collaborator

songz commented May 4, 2017

@Himanshu1495 sorry for the delay, I'll review it today!

solutions/18.py Outdated
'''

def missing_num(arr):
#get first element of array
Copy link
Collaborator

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]

Copy link
Contributor Author

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)]
Copy link
Collaborator

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?)

Copy link
Contributor Author

@Himanshu1495 Himanshu1495 May 4, 2017

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 ?

Copy link
Collaborator

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; 

Copy link
Contributor Author

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.
@Himanshu1495
Copy link
Contributor Author

Himanshu1495 commented May 5, 2017

@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])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove print statements

Copy link
Contributor Author

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):
Copy link
Collaborator

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.

Copy link
Contributor Author

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?

Copy link
Collaborator

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 :)

Copy link
Contributor Author

@Himanshu1495 Himanshu1495 Jun 13, 2017

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 :)

@@ -0,0 +1,15 @@
#Created by Himanshu Nachane (@Himanshu1495)
Copy link
Collaborator

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.

@@ -0,0 +1,15 @@
#Created by Himanshu Nachane (@Himanshu1495)
Copy link
Collaborator

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?

Copy link
Contributor Author

@Himanshu1495 Himanshu1495 Jun 13, 2017

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.

Copy link
Collaborator

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?

Copy link
Collaborator

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
@Himanshu1495
Copy link
Contributor Author

Himanshu1495 commented Jun 13, 2017

@songz Here is an explanation of what I did in the latest commit. import sys, this imports system parameters. sys.path.insert(0,'../solutions/'), this navigates to solutions folder and inserts its path at the start of the system parameters. from sol18 import missing_num, this imports the function missing_num from sol18.py. For an obvious reason, I needed to change the file name from 18.py to sol18.py because 18.py was needed to be imported as from 18 import missing_num and the 18 was interpreted as an integer which caused an error.

@@ -0,0 +1,15 @@
#Created by Himanshu Nachane (@Himanshu1495)
Copy link
Collaborator

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
Copy link
Collaborator

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@songz renaming done :)

Copy link
Contributor Author

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.

@@ -0,0 +1,15 @@
#Created by Himanshu Nachane (@Himanshu1495)
Copy link
Collaborator

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)
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

@msach22 msach22 merged commit 100b5c1 into llipio:master Jul 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants